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(RuntimeError):
29 def test_no_init_source(self
):
30 class MyIter(bt2
._UserMessageIterator
):
34 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
37 self
._test
_no
_init
(MySource
)
39 def test_no_init_filter(self
):
40 class MyIter(bt2
._UserMessageIterator
):
44 class MyFilter(bt2
._UserFilterComponent
, message_iterator_class
=MyIter
):
47 self
._test
_no
_init
(MyFilter
)
49 def test_no_init_sink(self
):
50 class MySink(bt2
._UserSinkComponent
):
54 def _graph_is_configured(self
):
57 self
._test
_no
_init
(MySink
)
59 def test_incomplete_source_no_msg_iter_cls(self
):
60 class MyIter(bt2
._UserMessageIterator
):
63 with self
.assertRaises(bt2
.IncompleteUserClass
):
65 class MySource(bt2
._UserSourceComponent
):
68 def test_incomplete_source_wrong_msg_iter_cls_type(self
):
69 class MyIter(bt2
._UserMessageIterator
):
72 with self
.assertRaises(bt2
.IncompleteUserClass
):
74 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=int):
77 def test_incomplete_filter_no_msg_iter_cls(self
):
78 class MyIter(bt2
._UserMessageIterator
):
81 with self
.assertRaises(bt2
.IncompleteUserClass
):
83 class MyFilter(bt2
._UserFilterComponent
):
86 def test_incomplete_sink_no_consume_method(self
):
87 class MyIter(bt2
._UserMessageIterator
):
90 with self
.assertRaises(bt2
.IncompleteUserClass
):
92 class MySink(bt2
._UserSinkComponent
):
95 def test_minimal_source(self
):
96 class MyIter(bt2
._UserMessageIterator
):
99 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
102 def test_minimal_filter(self
):
103 class MyIter(bt2
._UserMessageIterator
):
106 class MyFilter(bt2
._UserFilterComponent
, message_iterator_class
=MyIter
):
109 def test_minimal_sink(self
):
110 class MySink(bt2
._UserSinkComponent
):
114 def _graph_is_configured(self
):
117 def test_default_name(self
):
118 class MySink(bt2
._UserSinkComponent
):
122 def _graph_is_configured(self
):
125 self
.assertEqual(MySink
.name
, 'MySink')
127 def test_custom_name(self
):
128 class MySink(bt2
._UserSinkComponent
, name
='salut'):
132 def _graph_is_configured(self
):
135 self
.assertEqual(MySink
.name
, 'salut')
137 def test_invalid_custom_name(self
):
138 with self
.assertRaises(TypeError):
140 class MySink(bt2
._UserSinkComponent
, name
=23):
144 def _graph_is_configured(self
):
147 def test_description(self
):
148 class MySink(bt2
._UserSinkComponent
):
152 Bacon ipsum dolor amet ribeye t-bone corned beef, beef jerky
153 porchetta burgdoggen prosciutto chicken frankfurter boudin
154 hamburger doner bacon turducken. Sirloin shank sausage,
155 boudin meatloaf alcatra meatball t-bone tongue pastrami
156 cupim flank tenderloin.
162 def _graph_is_configured(self
):
165 self
.assertEqual(MySink
.description
, 'The description.')
167 def test_empty_description(self
):
168 class MySink(bt2
._UserSinkComponent
):
175 def _graph_is_configured(self
):
178 self
.assertIsNone(MySink
.description
)
181 class MySink(bt2
._UserSinkComponent
):
193 def _graph_is_configured(self
):
196 self
.assertEqual(MySink
.help, 'The help\ntext is\nhere.')
199 class MySink(bt2
._UserSinkComponent
):
203 def _graph_is_configured(self
):
206 self
.assertIsInstance(MySink
.addr
, int)
207 self
.assertNotEqual(MySink
.addr
, 0)
209 def test_query_not_implemented(self
):
210 class MySink(bt2
._UserSinkComponent
):
214 def _graph_is_configured(self
):
217 with self
.assertRaises(bt2
._Error
):
218 bt2
.QueryExecutor().query(MySink
, 'obj', 23)
220 def test_query_raises(self
):
221 class MySink(bt2
._UserSinkComponent
):
225 def _graph_is_configured(self
):
229 def _query(cls
, query_exec
, obj
, params
, log_level
):
232 with self
.assertRaises(bt2
._Error
):
233 bt2
.QueryExecutor().query(MySink
, 'obj', 23)
235 def test_query_wrong_return_type(self
):
236 class MySink(bt2
._UserSinkComponent
):
240 def _graph_is_configured(self
):
244 def _query(cls
, query_exec
, obj
, params
, log_level
):
247 with self
.assertRaises(bt2
._Error
):
248 bt2
.QueryExecutor().query(MySink
, 'obj', 23)
250 def test_query_params_none(self
):
251 class MySink(bt2
._UserSinkComponent
):
255 def _graph_is_configured(self
):
259 def _query(cls
, query_exec
, obj
, params
, log_level
):
260 nonlocal query_params
261 query_params
= params
266 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
267 self
.assertEqual(query_params
, params
)
268 self
.assertIsNone(res
)
271 def test_query_logging_level(self
):
272 class MySink(bt2
._UserSinkComponent
):
276 def _graph_is_configured(self
):
280 def _query(cls
, query_exec
, obj
, params
, log_level
):
281 nonlocal query_log_level
282 query_log_level
= log_level
284 query_log_level
= None
285 res
= bt2
.QueryExecutor().query(MySink
, 'obj', None, bt2
.LoggingLevel
.WARNING
)
286 self
.assertEqual(query_log_level
, bt2
.LoggingLevel
.WARNING
)
289 def test_query_returns_none(self
):
290 class MySink(bt2
._UserSinkComponent
):
294 def _graph_is_configured(self
):
298 def _query(query_exec
, obj
, params
, log_level
):
301 res
= bt2
.QueryExecutor().query(MySink
, 'obj', None)
302 self
.assertIsNone(res
)
304 def test_query_simple(self
):
305 class MySink(bt2
._UserSinkComponent
):
309 def _graph_is_configured(self
):
313 def _query(cls
, query_exec
, obj
, params
, log_level
):
314 nonlocal query_params
315 query_params
= params
319 params
= ['coucou', 23, None]
320 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
321 self
.assertEqual(query_params
, params
)
322 self
.assertEqual(res
, 17.5)
325 def test_query_complex(self
):
326 class MySink(bt2
._UserSinkComponent
):
330 def _graph_is_configured(self
):
334 def _query(cls
, query_exec
, obj
, params
, log_level
):
335 nonlocal query_params
336 query_params
= params
337 return {'null': None, 'bt2': 'BT2'}
341 'array': ['coucou', 23, None],
342 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
346 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
347 self
.assertEqual(query_params
, params
)
348 self
.assertEqual(res
, {'null': None, 'bt2': 'BT2'})
352 class MySink(bt2
._UserSinkComponent
):
356 def _graph_is_configured(self
):
359 self
.assertEqual(MySink
, MySink
)
362 class ComponentClassTestCase(unittest
.TestCase
):
364 class MySink(bt2
._UserSinkComponent
):
374 def _graph_is_configured(self
):
378 def _query(cls
, query_exec
, obj
, params
, log_level
):
379 return [obj
, params
, 23]
381 self
._py
_comp
_cls
= MySink
383 comp
= graph
.add_component(MySink
, 'salut')
384 self
._comp
_cls
= comp
.cls
385 self
.assertTrue(issubclass(type(self
._comp
_cls
), bt2
.component
._ComponentClass
))
388 del self
._py
_comp
_cls
391 def test_description(self
):
392 self
.assertEqual(self
._comp
_cls
.description
, 'The description.')
395 self
.assertEqual(self
._comp
_cls
.help, 'The help.')
398 self
.assertEqual(self
._comp
_cls
.name
, 'MySink')
401 self
.assertIsInstance(self
._comp
_cls
.addr
, int)
402 self
.assertNotEqual(self
._comp
_cls
.addr
, 0)
404 def test_eq_invalid(self
):
405 self
.assertFalse(self
._comp
_cls
== 23)
408 self
.assertEqual(self
._comp
_cls
, self
._comp
_cls
)
409 self
.assertEqual(self
._py
_comp
_cls
, self
._comp
_cls
)
411 def test_query(self
):
412 res
= bt2
.QueryExecutor().query(
413 self
._comp
_cls
, 'an object', {'yes': 'no', 'book': -17}
415 expected
= ['an object', {'yes': 'no', 'book': -17}, 23]
416 self
.assertEqual(res
, expected
)
This page took 0.037469 seconds and 5 git commands to generate.