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
):
51 def _user_consume(self
):
54 self
._test
_no
_init
(MySink
)
56 def test_incomplete_source_no_msg_iter_cls(self
):
57 class MyIter(bt2
._UserMessageIterator
):
60 with self
.assertRaises(bt2
._IncompleteUserClass
):
62 class MySource(bt2
._UserSourceComponent
):
65 def test_incomplete_source_wrong_msg_iter_cls_type(self
):
66 class MyIter(bt2
._UserMessageIterator
):
69 with self
.assertRaises(bt2
._IncompleteUserClass
):
71 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=int):
74 def test_incomplete_filter_no_msg_iter_cls(self
):
75 class MyIter(bt2
._UserMessageIterator
):
78 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
):
89 class MySink(bt2
._UserSinkComponent
):
92 def test_minimal_source(self
):
93 class MyIter(bt2
._UserMessageIterator
):
96 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
99 def test_minimal_filter(self
):
100 class MyIter(bt2
._UserMessageIterator
):
103 class MyFilter(bt2
._UserFilterComponent
, message_iterator_class
=MyIter
):
106 def test_minimal_sink(self
):
107 class MySink(bt2
._UserSinkComponent
):
108 def _user_consume(self
):
111 def test_default_name(self
):
112 class MySink(bt2
._UserSinkComponent
):
113 def _user_consume(self
):
116 self
.assertEqual(MySink
.name
, 'MySink')
118 def test_custom_name(self
):
119 class MySink(bt2
._UserSinkComponent
, name
='salut'):
120 def _user_consume(self
):
123 self
.assertEqual(MySink
.name
, 'salut')
125 def test_invalid_custom_name(self
):
126 with self
.assertRaises(TypeError):
128 class MySink(bt2
._UserSinkComponent
, name
=23):
129 def _user_consume(self
):
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.
144 def _user_consume(self
):
147 self
.assertEqual(MySink
.description
, 'The description.')
149 def test_empty_description(self
):
150 class MySink(bt2
._UserSinkComponent
):
154 def _user_consume(self
):
157 self
.assertIsNone(MySink
.description
)
160 class MySink(bt2
._UserSinkComponent
):
169 def _user_consume(self
):
172 self
.assertEqual(MySink
.help, 'The help\ntext is\nhere.')
175 class MySink(bt2
._UserSinkComponent
):
176 def _user_consume(self
):
179 self
.assertIsInstance(MySink
.addr
, int)
180 self
.assertNotEqual(MySink
.addr
, 0)
182 def test_query_not_implemented(self
):
183 class MySink(bt2
._UserSinkComponent
):
184 def _user_consume(self
):
187 with self
.assertRaises(bt2
.UnknownObject
):
188 bt2
.QueryExecutor(MySink
, 'obj', 23).query()
190 def test_query_raises(self
):
191 class MySink(bt2
._UserSinkComponent
):
192 def _user_consume(self
):
196 def _user_query(cls
, priv_query_exec
, obj
, params
):
199 with self
.assertRaises(bt2
._Error
):
200 bt2
.QueryExecutor(MySink
, 'obj', 23).query()
202 def test_query_wrong_return_type(self
):
203 class MySink(bt2
._UserSinkComponent
):
204 def _user_consume(self
):
208 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
211 with self
.assertRaises(bt2
._Error
):
212 bt2
.QueryExecutor(MySink
, 'obj', 23).query()
214 def test_query_params_none(self
):
215 class MySink(bt2
._UserSinkComponent
):
216 def _user_consume(self
):
220 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
221 nonlocal query_params
222 query_params
= params
227 res
= bt2
.QueryExecutor(MySink
, 'obj', params
).query()
228 self
.assertEqual(query_params
, params
)
229 self
.assertIsNone(res
)
232 def test_query_logging_level(self
):
233 class MySink(bt2
._UserSinkComponent
):
234 def _user_consume(self
):
238 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
239 nonlocal query_log_level
240 query_log_level
= priv_query_exec
.logging_level
242 query_log_level
= None
243 query_exec
= bt2
.QueryExecutor(MySink
, 'obj', None)
244 query_exec
.logging_level
= bt2
.LoggingLevel
.WARNING
246 self
.assertEqual(query_log_level
, bt2
.LoggingLevel
.WARNING
)
249 def test_query_returns_none(self
):
250 class MySink(bt2
._UserSinkComponent
):
251 def _user_consume(self
):
255 def _user_query(priv_query_exec
, obj
, params
, method_obj
):
258 res
= bt2
.QueryExecutor(MySink
, 'obj', None).query()
259 self
.assertIsNone(res
)
261 def test_query_simple(self
):
262 class MySink(bt2
._UserSinkComponent
):
263 def _user_consume(self
):
267 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
268 nonlocal query_params
269 query_params
= params
273 params
= ['coucou', 23, None]
274 res
= bt2
.QueryExecutor(MySink
, 'obj', params
).query()
275 self
.assertEqual(query_params
, params
)
276 self
.assertEqual(res
, 17.5)
279 def test_query_complex(self
):
280 class MySink(bt2
._UserSinkComponent
):
281 def _user_consume(self
):
285 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
286 nonlocal query_params
287 query_params
= params
288 return {'null': None, 'bt2': 'BT2'}
292 'array': ['coucou', 23, None],
293 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
297 res
= bt2
.QueryExecutor(MySink
, 'obj', params
).query()
298 self
.assertEqual(query_params
, params
)
299 self
.assertEqual(res
, {'null': None, 'bt2': 'BT2'})
303 class MySink(bt2
._UserSinkComponent
):
304 def _user_consume(self
):
307 self
.assertEqual(MySink
, MySink
)
310 class ComponentClassTestCase(unittest
.TestCase
):
312 class MySink(bt2
._UserSinkComponent
):
319 def _user_consume(self
):
323 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
324 return [obj
, params
, 23]
326 self
._py
_comp
_cls
= MySink
328 comp
= graph
.add_component(MySink
, 'salut')
329 self
._comp
_cls
= comp
.cls
330 self
.assertTrue(issubclass(type(self
._comp
_cls
), bt2
._SinkComponentClass
))
333 del self
._py
_comp
_cls
336 def test_description(self
):
337 self
.assertEqual(self
._comp
_cls
.description
, 'The description.')
340 self
.assertEqual(self
._comp
_cls
.help, 'The help.')
343 self
.assertEqual(self
._comp
_cls
.name
, 'MySink')
346 self
.assertIsInstance(self
._comp
_cls
.addr
, int)
347 self
.assertNotEqual(self
._comp
_cls
.addr
, 0)
349 def test_eq_invalid(self
):
350 self
.assertFalse(self
._comp
_cls
== 23)
353 self
.assertEqual(self
._comp
_cls
, self
._comp
_cls
)
354 self
.assertEqual(self
._py
_comp
_cls
, self
._comp
_cls
)
356 def test_query(self
):
357 res
= bt2
.QueryExecutor(
358 self
._comp
_cls
, 'an object', {'yes': 'no', 'book': -17}
360 expected
= ['an object', {'yes': 'no', 'book': -17}, 23]
361 self
.assertEqual(res
, expected
)
This page took 0.036893 seconds and 4 git commands to generate.