7 class UserComponentClassTestCase(unittest
.TestCase
):
8 def _test_no_init(self
, cls
):
9 with self
.assertRaises(bt2
.Error
):
12 def test_no_init_source(self
):
13 class MyIter(bt2
._UserNotificationIterator
):
17 class MySource(bt2
._UserSourceComponent
,
18 notification_iterator_class
=MyIter
):
21 self
._test
_no
_init
(MySource
)
23 def test_no_init_filter(self
):
24 class MyIter(bt2
._UserNotificationIterator
):
28 class MyFilter(bt2
._UserFilterComponent
,
29 notification_iterator_class
=MyIter
):
32 self
._test
_no
_init
(MyFilter
)
34 def test_no_init_sink(self
):
35 class MySink(bt2
._UserSinkComponent
):
39 self
._test
_no
_init
(MySink
)
41 def test_incomplete_source_no_notif_iter_cls(self
):
42 class MyIter(bt2
._UserNotificationIterator
):
45 with self
.assertRaises(bt2
.IncompleteUserClass
):
46 class MySource(bt2
._UserSourceComponent
):
49 def test_incomplete_source_wrong_notif_iter_cls_type(self
):
50 class MyIter(bt2
._UserNotificationIterator
):
53 with self
.assertRaises(bt2
.IncompleteUserClass
):
54 class MySource(bt2
._UserSourceComponent
,
55 notification_iterator_class
=int):
58 def test_incomplete_filter_no_notif_iter_cls(self
):
59 class MyIter(bt2
._UserNotificationIterator
):
62 with self
.assertRaises(bt2
.IncompleteUserClass
):
63 class MyFilter(bt2
._UserFilterComponent
):
66 def test_incomplete_sink_no_consume_method(self
):
67 class MyIter(bt2
._UserNotificationIterator
):
70 with self
.assertRaises(bt2
.IncompleteUserClass
):
71 class MySink(bt2
._UserSinkComponent
):
74 def test_minimal_source(self
):
75 class MyIter(bt2
._UserNotificationIterator
):
78 class MySource(bt2
._UserSourceComponent
,
79 notification_iterator_class
=MyIter
):
82 def test_minimal_filter(self
):
83 class MyIter(bt2
._UserNotificationIterator
):
86 class MyFilter(bt2
._UserFilterComponent
,
87 notification_iterator_class
=MyIter
):
90 def test_minimal_sink(self
):
91 class MySink(bt2
._UserSinkComponent
):
95 def test_default_name(self
):
96 class MySink(bt2
._UserSinkComponent
):
100 self
.assertEqual(MySink
.name
, 'MySink')
102 def test_custom_name(self
):
103 class MySink(bt2
._UserSinkComponent
, name
='salut'):
107 self
.assertEqual(MySink
.name
, 'salut')
109 def test_invalid_custom_name(self
):
110 with self
.assertRaises(TypeError):
111 class MySink(bt2
._UserSinkComponent
, name
=23):
115 def test_description(self
):
116 class MySink(bt2
._UserSinkComponent
):
120 Bacon ipsum dolor amet ribeye t-bone corned beef, beef jerky
121 porchetta burgdoggen prosciutto chicken frankfurter boudin
122 hamburger doner bacon turducken. Sirloin shank sausage,
123 boudin meatloaf alcatra meatball t-bone tongue pastrami
124 cupim flank tenderloin.
130 self
.assertEqual(MySink
.description
, 'The description.')
132 def test_empty_description(self
):
133 class MySink(bt2
._UserSinkComponent
):
140 self
.assertIsNone(MySink
.description
)
143 class MySink(bt2
._UserSinkComponent
):
155 self
.assertEqual(MySink
.help, 'The help\ntext is\nhere.')
158 class MySink(bt2
._UserSinkComponent
):
162 self
.assertIsInstance(MySink
.addr
, int)
163 self
.assertNotEqual(MySink
.addr
, 0)
165 def test_query_not_implemented(self
):
166 class MySink(bt2
._UserSinkComponent
):
170 with self
.assertRaises(bt2
.Error
):
171 bt2
.QueryExecutor().query(MySink
, 'obj', 23)
173 def test_query_raises(self
):
174 class MySink(bt2
._UserSinkComponent
):
179 def _query(cls
, query_exec
, obj
, params
):
182 with self
.assertRaises(bt2
.Error
):
183 bt2
.QueryExecutor().query(MySink
, 'obj', 23)
185 def test_query_wrong_return_type(self
):
186 class MySink(bt2
._UserSinkComponent
):
191 def _query(cls
, query_exec
, obj
, params
):
194 with self
.assertRaises(bt2
.Error
):
195 bt2
.QueryExecutor().query(MySink
, 'obj', 23)
197 def test_query_params_none(self
):
198 class MySink(bt2
._UserSinkComponent
):
203 def _query(cls
, query_exec
, obj
, params
):
204 nonlocal query_params
205 query_params
= params
210 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
211 self
.assertEqual(query_params
, params
)
212 self
.assertIsNone(res
)
215 def test_query_simple(self
):
216 class MySink(bt2
._UserSinkComponent
):
221 def _query(cls
, query_exec
, obj
, params
):
222 nonlocal query_params
223 query_params
= params
227 params
= ['coucou', 23, None]
228 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
229 self
.assertEqual(query_params
, params
)
230 self
.assertEqual(res
, 17.5)
233 def test_query_complex(self
):
234 class MySink(bt2
._UserSinkComponent
):
239 def _query(cls
, query_exec
, obj
, params
):
240 nonlocal query_params
241 query_params
= params
249 'array': ['coucou', 23, None],
258 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
259 self
.assertEqual(query_params
, params
)
260 self
.assertEqual(res
, {
267 class MySink(bt2
._UserSinkComponent
):
271 self
.assertEqual(MySink
, MySink
)
274 class GenericComponentClassTestCase(unittest
.TestCase
):
276 class MySink(bt2
._UserSinkComponent
):
286 def _query(cls
, query_exec
, obj
, params
):
287 return [obj
, params
, 23]
289 self
._py
_comp
_cls
= MySink
291 comp
= graph
.add_component(MySink
, 'salut')
292 self
._comp
_cls
= comp
.component_class
293 self
.assertTrue(issubclass(type(self
._comp
_cls
),
294 bt2
.component
._GenericComponentClass
))
297 del self
._py
_comp
_cls
300 def test_description(self
):
301 self
.assertEqual(self
._comp
_cls
.description
, 'The description.')
304 self
.assertEqual(self
._comp
_cls
.help, 'The help.')
307 self
.assertEqual(self
._comp
_cls
.name
, 'MySink')
310 self
.assertIsInstance(self
._comp
_cls
.addr
, int)
311 self
.assertNotEqual(self
._comp
_cls
.addr
, 0)
313 def test_eq_invalid(self
):
314 self
.assertFalse(self
._comp
_cls
== 23)
317 self
.assertEqual(self
._comp
_cls
, self
._comp
_cls
)
318 self
.assertEqual(self
._py
_comp
_cls
, self
._comp
_cls
)
320 def test_query(self
):
321 res
= bt2
.QueryExecutor().query(self
._comp
_cls
, 'an object',
322 {'yes': 'no', 'book': -17})
323 expected
= ['an object', {'yes': 'no', 'book': -17}, 23]
324 self
.assertEqual(res
, expected
)