2 # Copyright (C) 2019 EfficiOS Inc.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; only version 2
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 class UserComponentClassTestCase(unittest
.TestCase
):
25 def _test_no_init(self
, cls
):
26 with self
.assertRaises(bt2
.Error
):
29 def test_no_init_source(self
):
30 class MyIter(bt2
._UserMessageIterator
):
34 class MySource(bt2
._UserSourceComponent
,
35 message_iterator_class
=MyIter
):
38 self
._test
_no
_init
(MySource
)
40 def test_no_init_filter(self
):
41 class MyIter(bt2
._UserMessageIterator
):
45 class MyFilter(bt2
._UserFilterComponent
,
46 message_iterator_class
=MyIter
):
49 self
._test
_no
_init
(MyFilter
)
51 def test_no_init_sink(self
):
52 class MySink(bt2
._UserSinkComponent
):
56 self
._test
_no
_init
(MySink
)
58 def test_incomplete_source_no_msg_iter_cls(self
):
59 class MyIter(bt2
._UserMessageIterator
):
62 with self
.assertRaises(bt2
.IncompleteUserClass
):
63 class MySource(bt2
._UserSourceComponent
):
66 def test_incomplete_source_wrong_msg_iter_cls_type(self
):
67 class MyIter(bt2
._UserMessageIterator
):
70 with self
.assertRaises(bt2
.IncompleteUserClass
):
71 class MySource(bt2
._UserSourceComponent
,
72 message_iterator_class
=int):
75 def test_incomplete_filter_no_msg_iter_cls(self
):
76 class MyIter(bt2
._UserMessageIterator
):
79 with self
.assertRaises(bt2
.IncompleteUserClass
):
80 class MyFilter(bt2
._UserFilterComponent
):
83 def test_incomplete_sink_no_consume_method(self
):
84 class MyIter(bt2
._UserMessageIterator
):
87 with self
.assertRaises(bt2
.IncompleteUserClass
):
88 class MySink(bt2
._UserSinkComponent
):
91 def test_minimal_source(self
):
92 class MyIter(bt2
._UserMessageIterator
):
95 class MySource(bt2
._UserSourceComponent
,
96 message_iterator_class
=MyIter
):
99 def test_minimal_filter(self
):
100 class MyIter(bt2
._UserMessageIterator
):
103 class MyFilter(bt2
._UserFilterComponent
,
104 message_iterator_class
=MyIter
):
107 def test_minimal_sink(self
):
108 class MySink(bt2
._UserSinkComponent
):
112 def test_default_name(self
):
113 class MySink(bt2
._UserSinkComponent
):
117 self
.assertEqual(MySink
.name
, 'MySink')
119 def test_custom_name(self
):
120 class MySink(bt2
._UserSinkComponent
, name
='salut'):
124 self
.assertEqual(MySink
.name
, 'salut')
126 def test_invalid_custom_name(self
):
127 with self
.assertRaises(TypeError):
128 class MySink(bt2
._UserSinkComponent
, name
=23):
132 def test_description(self
):
133 class MySink(bt2
._UserSinkComponent
):
137 Bacon ipsum dolor amet ribeye t-bone corned beef, beef jerky
138 porchetta burgdoggen prosciutto chicken frankfurter boudin
139 hamburger doner bacon turducken. Sirloin shank sausage,
140 boudin meatloaf alcatra meatball t-bone tongue pastrami
141 cupim flank tenderloin.
147 self
.assertEqual(MySink
.description
, 'The description.')
149 def test_empty_description(self
):
150 class MySink(bt2
._UserSinkComponent
):
157 self
.assertIsNone(MySink
.description
)
160 class MySink(bt2
._UserSinkComponent
):
172 self
.assertEqual(MySink
.help, 'The help\ntext is\nhere.')
175 class MySink(bt2
._UserSinkComponent
):
179 self
.assertIsInstance(MySink
.addr
, int)
180 self
.assertNotEqual(MySink
.addr
, 0)
182 def test_query_not_implemented(self
):
183 class MySink(bt2
._UserSinkComponent
):
187 with self
.assertRaises(bt2
.Error
):
188 bt2
.QueryExecutor().query(MySink
, 'obj', 23)
190 def test_query_raises(self
):
191 class MySink(bt2
._UserSinkComponent
):
196 def _query(cls
, query_exec
, obj
, params
, log_level
):
199 with self
.assertRaises(bt2
.Error
):
200 bt2
.QueryExecutor().query(MySink
, 'obj', 23)
202 def test_query_wrong_return_type(self
):
203 class MySink(bt2
._UserSinkComponent
):
208 def _query(cls
, query_exec
, obj
, params
, log_level
):
211 with self
.assertRaises(bt2
.Error
):
212 bt2
.QueryExecutor().query(MySink
, 'obj', 23)
214 def test_query_params_none(self
):
215 class MySink(bt2
._UserSinkComponent
):
220 def _query(cls
, query_exec
, obj
, params
, log_level
):
221 nonlocal query_params
222 query_params
= params
227 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
228 self
.assertEqual(query_params
, params
)
229 self
.assertIsNone(res
)
232 def test_query_logging_level(self
):
233 class MySink(bt2
._UserSinkComponent
):
238 def _query(cls
, query_exec
, obj
, params
, log_level
):
239 nonlocal query_log_level
240 query_log_level
= log_level
242 query_log_level
= None
243 res
= bt2
.QueryExecutor().query(MySink
, 'obj', None,
244 bt2
.LoggingLevel
.WARN
)
245 self
.assertEqual(query_log_level
, bt2
.LoggingLevel
.WARN
)
248 def test_query_simple(self
):
249 class MySink(bt2
._UserSinkComponent
):
254 def _query(cls
, query_exec
, obj
, params
, log_level
):
255 nonlocal query_params
256 query_params
= params
260 params
= ['coucou', 23, None]
261 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
262 self
.assertEqual(query_params
, params
)
263 self
.assertEqual(res
, 17.5)
266 def test_query_complex(self
):
267 class MySink(bt2
._UserSinkComponent
):
272 def _query(cls
, query_exec
, obj
, params
, log_level
):
273 nonlocal query_params
274 query_params
= params
282 'array': ['coucou', 23, None],
291 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
292 self
.assertEqual(query_params
, params
)
293 self
.assertEqual(res
, {
300 class MySink(bt2
._UserSinkComponent
):
304 self
.assertEqual(MySink
, MySink
)
307 class GenericComponentClassTestCase(unittest
.TestCase
):
309 class MySink(bt2
._UserSinkComponent
):
319 def _query(cls
, query_exec
, obj
, params
, log_level
):
320 return [obj
, params
, 23]
322 self
._py
_comp
_cls
= MySink
324 comp
= graph
.add_component(MySink
, 'salut')
325 self
._comp
_cls
= comp
.cls
326 self
.assertTrue(issubclass(type(self
._comp
_cls
),
327 bt2
.component
._GenericComponentClass
))
330 del self
._py
_comp
_cls
333 def test_description(self
):
334 self
.assertEqual(self
._comp
_cls
.description
, 'The description.')
337 self
.assertEqual(self
._comp
_cls
.help, 'The help.')
340 self
.assertEqual(self
._comp
_cls
.name
, 'MySink')
343 self
.assertIsInstance(self
._comp
_cls
.addr
, int)
344 self
.assertNotEqual(self
._comp
_cls
.addr
, 0)
346 def test_eq_invalid(self
):
347 self
.assertFalse(self
._comp
_cls
== 23)
350 self
.assertEqual(self
._comp
_cls
, self
._comp
_cls
)
351 self
.assertEqual(self
._py
_comp
_cls
, self
._comp
_cls
)
353 def test_query(self
):
354 res
= bt2
.QueryExecutor().query(self
._comp
_cls
, 'an object',
355 {'yes': 'no', 'book': -17})
356 expected
= ['an object', {'yes': 'no', 'book': -17}, 23]
357 self
.assertEqual(res
, expected
)
This page took 0.035762 seconds and 4 git commands to generate.