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 def _graph_is_configured(self
):
59 self
._test
_no
_init
(MySink
)
61 def test_incomplete_source_no_msg_iter_cls(self
):
62 class MyIter(bt2
._UserMessageIterator
):
65 with self
.assertRaises(bt2
.IncompleteUserClass
):
66 class MySource(bt2
._UserSourceComponent
):
69 def test_incomplete_source_wrong_msg_iter_cls_type(self
):
70 class MyIter(bt2
._UserMessageIterator
):
73 with self
.assertRaises(bt2
.IncompleteUserClass
):
74 class MySource(bt2
._UserSourceComponent
,
75 message_iterator_class
=int):
78 def test_incomplete_filter_no_msg_iter_cls(self
):
79 class MyIter(bt2
._UserMessageIterator
):
82 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
):
91 class MySink(bt2
._UserSinkComponent
):
94 def test_minimal_source(self
):
95 class MyIter(bt2
._UserMessageIterator
):
98 class MySource(bt2
._UserSourceComponent
,
99 message_iterator_class
=MyIter
):
102 def test_minimal_filter(self
):
103 class MyIter(bt2
._UserMessageIterator
):
106 class MyFilter(bt2
._UserFilterComponent
,
107 message_iterator_class
=MyIter
):
110 def test_minimal_sink(self
):
111 class MySink(bt2
._UserSinkComponent
):
115 def _graph_is_configured(self
):
118 def test_default_name(self
):
119 class MySink(bt2
._UserSinkComponent
):
123 def _graph_is_configured(self
):
126 self
.assertEqual(MySink
.name
, 'MySink')
128 def test_custom_name(self
):
129 class MySink(bt2
._UserSinkComponent
, name
='salut'):
133 def _graph_is_configured(self
):
136 self
.assertEqual(MySink
.name
, 'salut')
138 def test_invalid_custom_name(self
):
139 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,
286 bt2
.LoggingLevel
.WARNING
)
287 self
.assertEqual(query_log_level
, bt2
.LoggingLevel
.WARNING
)
290 def test_query_returns_none(self
):
291 class MySink(bt2
._UserSinkComponent
):
295 def _graph_is_configured(self
):
299 def _query(query_exec
, obj
, params
, log_level
):
302 res
= bt2
.QueryExecutor().query(MySink
, 'obj', None)
303 self
.assertIsNone(res
)
305 def test_query_simple(self
):
306 class MySink(bt2
._UserSinkComponent
):
310 def _graph_is_configured(self
):
314 def _query(cls
, query_exec
, obj
, params
, log_level
):
315 nonlocal query_params
316 query_params
= params
320 params
= ['coucou', 23, None]
321 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
322 self
.assertEqual(query_params
, params
)
323 self
.assertEqual(res
, 17.5)
326 def test_query_complex(self
):
327 class MySink(bt2
._UserSinkComponent
):
331 def _graph_is_configured(self
):
335 def _query(cls
, query_exec
, obj
, params
, log_level
):
336 nonlocal query_params
337 query_params
= params
345 'array': ['coucou', 23, None],
354 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
355 self
.assertEqual(query_params
, params
)
356 self
.assertEqual(res
, {
363 class MySink(bt2
._UserSinkComponent
):
367 def _graph_is_configured(self
):
370 self
.assertEqual(MySink
, MySink
)
373 class GenericComponentClassTestCase(unittest
.TestCase
):
375 class MySink(bt2
._UserSinkComponent
):
384 def _graph_is_configured(self
):
388 def _query(cls
, query_exec
, obj
, params
, log_level
):
389 return [obj
, params
, 23]
391 self
._py
_comp
_cls
= MySink
393 comp
= graph
.add_component(MySink
, 'salut')
394 self
._comp
_cls
= comp
.cls
395 self
.assertTrue(issubclass(type(self
._comp
_cls
),
396 bt2
.component
._GenericComponentClass
))
399 del self
._py
_comp
_cls
402 def test_description(self
):
403 self
.assertEqual(self
._comp
_cls
.description
, 'The description.')
406 self
.assertEqual(self
._comp
_cls
.help, 'The help.')
409 self
.assertEqual(self
._comp
_cls
.name
, 'MySink')
412 self
.assertIsInstance(self
._comp
_cls
.addr
, int)
413 self
.assertNotEqual(self
._comp
_cls
.addr
, 0)
415 def test_eq_invalid(self
):
416 self
.assertFalse(self
._comp
_cls
== 23)
419 self
.assertEqual(self
._comp
_cls
, self
._comp
_cls
)
420 self
.assertEqual(self
._py
_comp
_cls
, self
._comp
_cls
)
422 def test_query(self
):
423 res
= bt2
.QueryExecutor().query(self
._comp
_cls
, 'an object',
424 {'yes': 'no', 'book': -17})
425 expected
= ['an object', {'yes': 'no', 'book': -17}, 23]
426 self
.assertEqual(res
, expected
)
This page took 0.03807 seconds and 5 git commands to generate.