1 # SPDX-License-Identifier: GPL-2.0-only
3 # Copyright (C) 2019 EfficiOS Inc.
10 class UserComponentClassTestCase(unittest
.TestCase
):
11 def _test_no_init(self
, cls
):
12 with self
.assertRaises(RuntimeError):
15 def test_no_init_source(self
):
17 bt2
._UserSourceComponent
, message_iterator_class
=bt2
._UserMessageIterator
21 self
._test
_no
_init
(MySource
)
23 def test_no_init_filter(self
):
25 bt2
._UserFilterComponent
, message_iterator_class
=bt2
._UserMessageIterator
29 self
._test
_no
_init
(MyFilter
)
31 def test_no_init_sink(self
):
32 class MySink(bt2
._UserSinkComponent
):
33 def _user_consume(self
):
36 self
._test
_no
_init
(MySink
)
38 def test_incomplete_source_no_msg_iter_cls(self
):
39 with self
.assertRaises(bt2
._IncompleteUserClass
):
41 class MySource(bt2
._UserSourceComponent
):
44 def test_incomplete_source_wrong_msg_iter_cls_type(self
):
45 with self
.assertRaises(bt2
._IncompleteUserClass
):
47 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=int):
50 def test_incomplete_filter_no_msg_iter_cls(self
):
51 with self
.assertRaises(bt2
._IncompleteUserClass
):
53 class MyFilter(bt2
._UserFilterComponent
):
56 def test_incomplete_sink_no_consume_method(self
):
57 with self
.assertRaises(bt2
._IncompleteUserClass
):
59 class MySink(bt2
._UserSinkComponent
):
62 def test_minimal_source(self
):
64 bt2
._UserSourceComponent
, message_iterator_class
=bt2
._UserMessageIterator
68 def test_minimal_filter(self
):
70 bt2
._UserFilterComponent
, message_iterator_class
=bt2
._UserMessageIterator
74 def test_minimal_sink(self
):
75 class MySink(bt2
._UserSinkComponent
):
76 def _user_consume(self
):
79 def test_default_name(self
):
80 class MySink(bt2
._UserSinkComponent
):
81 def _user_consume(self
):
84 self
.assertEqual(MySink
.name
, 'MySink')
86 def test_custom_name(self
):
87 class MySink(bt2
._UserSinkComponent
, name
='salut'):
88 def _user_consume(self
):
91 self
.assertEqual(MySink
.name
, 'salut')
93 def test_invalid_custom_name(self
):
94 with self
.assertRaises(TypeError):
96 class MySink(bt2
._UserSinkComponent
, name
=23):
97 def _user_consume(self
):
100 def test_description(self
):
101 class MySink(bt2
._UserSinkComponent
):
105 Bacon ipsum dolor amet ribeye t-bone corned beef, beef jerky
106 porchetta burgdoggen prosciutto chicken frankfurter boudin
107 hamburger doner bacon turducken. Sirloin shank sausage,
108 boudin meatloaf alcatra meatball t-bone tongue pastrami
109 cupim flank tenderloin.
112 def _user_consume(self
):
115 self
.assertEqual(MySink
.description
, 'The description.')
117 def test_empty_description(self
):
118 class MySink(bt2
._UserSinkComponent
):
122 def _user_consume(self
):
125 self
.assertIsNone(MySink
.description
)
128 class MySink(bt2
._UserSinkComponent
):
137 def _user_consume(self
):
140 self
.assertEqual(MySink
.help, 'The help\ntext is\nhere.')
143 class MySink(bt2
._UserSinkComponent
):
144 def _user_consume(self
):
147 self
.assertIsInstance(MySink
.addr
, int)
148 self
.assertNotEqual(MySink
.addr
, 0)
150 def test_query_not_implemented(self
):
151 class MySink(bt2
._UserSinkComponent
):
152 def _user_consume(self
):
155 with self
.assertRaises(bt2
.UnknownObject
):
156 bt2
.QueryExecutor(MySink
, 'obj', 23).query()
158 def test_query_raises(self
):
159 class MySink(bt2
._UserSinkComponent
):
160 def _user_consume(self
):
164 def _user_query(cls
, priv_query_exec
, obj
, params
):
167 with self
.assertRaises(bt2
._Error
):
168 bt2
.QueryExecutor(MySink
, 'obj', 23).query()
170 def test_query_wrong_return_type(self
):
171 class MySink(bt2
._UserSinkComponent
):
172 def _user_consume(self
):
176 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
179 with self
.assertRaises(bt2
._Error
):
180 bt2
.QueryExecutor(MySink
, 'obj', 23).query()
182 def test_query_params_none(self
):
183 class MySink(bt2
._UserSinkComponent
):
184 def _user_consume(self
):
188 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
189 nonlocal query_params
190 query_params
= params
195 res
= bt2
.QueryExecutor(MySink
, 'obj', params
).query()
196 self
.assertEqual(query_params
, params
)
197 self
.assertIsNone(res
)
200 def test_query_logging_level(self
):
201 class MySink(bt2
._UserSinkComponent
):
202 def _user_consume(self
):
206 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
207 nonlocal query_log_level
208 query_log_level
= priv_query_exec
.logging_level
210 query_log_level
= None
211 query_exec
= bt2
.QueryExecutor(MySink
, 'obj', None)
212 query_exec
.logging_level
= bt2
.LoggingLevel
.WARNING
214 self
.assertEqual(query_log_level
, bt2
.LoggingLevel
.WARNING
)
217 def test_query_returns_none(self
):
218 class MySink(bt2
._UserSinkComponent
):
219 def _user_consume(self
):
223 def _user_query(priv_query_exec
, obj
, params
, method_obj
):
226 res
= bt2
.QueryExecutor(MySink
, 'obj', None).query()
227 self
.assertIsNone(res
)
229 def test_query_simple(self
):
230 class MySink(bt2
._UserSinkComponent
):
231 def _user_consume(self
):
235 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
236 nonlocal query_params
237 query_params
= params
241 params
= ['coucou', 23, None]
242 res
= bt2
.QueryExecutor(MySink
, 'obj', params
).query()
243 self
.assertEqual(query_params
, params
)
244 self
.assertEqual(res
, 17.5)
247 def test_query_complex(self
):
248 class MySink(bt2
._UserSinkComponent
):
249 def _user_consume(self
):
253 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
254 nonlocal query_params
255 query_params
= params
256 return {'null': None, 'bt2': 'BT2'}
260 'array': ['coucou', 23, None],
261 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
265 res
= bt2
.QueryExecutor(MySink
, 'obj', params
).query()
266 self
.assertEqual(query_params
, params
)
267 self
.assertEqual(res
, {'null': None, 'bt2': 'BT2'})
271 class MySink(bt2
._UserSinkComponent
):
272 def _user_consume(self
):
275 self
.assertEqual(MySink
, MySink
)
278 class ComponentClassTestCase(unittest
.TestCase
):
280 class MySink(bt2
._UserSinkComponent
):
287 def _user_consume(self
):
291 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
292 return [obj
, params
, 23]
294 self
._py
_comp
_cls
= MySink
296 comp
= graph
.add_component(MySink
, 'salut')
297 self
._comp
_cls
= comp
.cls
298 self
.assertIs(type(self
._comp
_cls
), bt2
._SinkComponentClassConst
)
301 del self
._py
_comp
_cls
304 def test_description(self
):
305 self
.assertEqual(self
._comp
_cls
.description
, 'The description.')
308 self
.assertEqual(self
._comp
_cls
.help, 'The help.')
311 self
.assertEqual(self
._comp
_cls
.name
, 'MySink')
314 self
.assertIsInstance(self
._comp
_cls
.addr
, int)
315 self
.assertNotEqual(self
._comp
_cls
.addr
, 0)
317 def test_eq_invalid(self
):
318 self
.assertFalse(self
._comp
_cls
== 23)
321 self
.assertEqual(self
._comp
_cls
, self
._comp
_cls
)
322 self
.assertEqual(self
._py
_comp
_cls
, self
._comp
_cls
)
324 def test_query(self
):
325 res
= bt2
.QueryExecutor(
326 self
._comp
_cls
, 'an object', {'yes': 'no', 'book': -17}
328 expected
= ['an object', {'yes': 'no', 'book': -17}, 23]
329 self
.assertEqual(res
, expected
)
332 if __name__
== '__main__':
This page took 0.036685 seconds and 4 git commands to generate.