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.
23 class UserComponentClassTestCase(unittest
.TestCase
):
24 def _test_no_init(self
, cls
):
25 with self
.assertRaises(RuntimeError):
28 def test_no_init_source(self
):
29 class MyIter(bt2
._UserMessageIterator
):
33 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
36 self
._test
_no
_init
(MySource
)
38 def test_no_init_filter(self
):
39 class MyIter(bt2
._UserMessageIterator
):
43 class MyFilter(bt2
._UserFilterComponent
, message_iterator_class
=MyIter
):
46 self
._test
_no
_init
(MyFilter
)
48 def test_no_init_sink(self
):
49 class MySink(bt2
._UserSinkComponent
):
50 def _user_consume(self
):
53 self
._test
_no
_init
(MySink
)
55 def test_incomplete_source_no_msg_iter_cls(self
):
56 class MyIter(bt2
._UserMessageIterator
):
59 with self
.assertRaises(bt2
._IncompleteUserClass
):
61 class MySource(bt2
._UserSourceComponent
):
64 def test_incomplete_source_wrong_msg_iter_cls_type(self
):
65 class MyIter(bt2
._UserMessageIterator
):
68 with self
.assertRaises(bt2
._IncompleteUserClass
):
70 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=int):
73 def test_incomplete_filter_no_msg_iter_cls(self
):
74 class MyIter(bt2
._UserMessageIterator
):
77 with self
.assertRaises(bt2
._IncompleteUserClass
):
79 class MyFilter(bt2
._UserFilterComponent
):
82 def test_incomplete_sink_no_consume_method(self
):
83 class MyIter(bt2
._UserMessageIterator
):
86 with self
.assertRaises(bt2
._IncompleteUserClass
):
88 class MySink(bt2
._UserSinkComponent
):
91 def test_minimal_source(self
):
92 class MyIter(bt2
._UserMessageIterator
):
95 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
98 def test_minimal_filter(self
):
99 class MyIter(bt2
._UserMessageIterator
):
102 class MyFilter(bt2
._UserFilterComponent
, message_iterator_class
=MyIter
):
105 def test_minimal_sink(self
):
106 class MySink(bt2
._UserSinkComponent
):
107 def _user_consume(self
):
110 def test_default_name(self
):
111 class MySink(bt2
._UserSinkComponent
):
112 def _user_consume(self
):
115 self
.assertEqual(MySink
.name
, 'MySink')
117 def test_custom_name(self
):
118 class MySink(bt2
._UserSinkComponent
, name
='salut'):
119 def _user_consume(self
):
122 self
.assertEqual(MySink
.name
, 'salut')
124 def test_invalid_custom_name(self
):
125 with self
.assertRaises(TypeError):
127 class MySink(bt2
._UserSinkComponent
, name
=23):
128 def _user_consume(self
):
131 def test_description(self
):
132 class MySink(bt2
._UserSinkComponent
):
136 Bacon ipsum dolor amet ribeye t-bone corned beef, beef jerky
137 porchetta burgdoggen prosciutto chicken frankfurter boudin
138 hamburger doner bacon turducken. Sirloin shank sausage,
139 boudin meatloaf alcatra meatball t-bone tongue pastrami
140 cupim flank tenderloin.
143 def _user_consume(self
):
146 self
.assertEqual(MySink
.description
, 'The description.')
148 def test_empty_description(self
):
149 class MySink(bt2
._UserSinkComponent
):
153 def _user_consume(self
):
156 self
.assertIsNone(MySink
.description
)
159 class MySink(bt2
._UserSinkComponent
):
168 def _user_consume(self
):
171 self
.assertEqual(MySink
.help, 'The help\ntext is\nhere.')
174 class MySink(bt2
._UserSinkComponent
):
175 def _user_consume(self
):
178 self
.assertIsInstance(MySink
.addr
, int)
179 self
.assertNotEqual(MySink
.addr
, 0)
181 def test_query_not_implemented(self
):
182 class MySink(bt2
._UserSinkComponent
):
183 def _user_consume(self
):
186 with self
.assertRaises(bt2
.UnknownObject
):
187 bt2
.QueryExecutor(MySink
, 'obj', 23).query()
189 def test_query_raises(self
):
190 class MySink(bt2
._UserSinkComponent
):
191 def _user_consume(self
):
195 def _user_query(cls
, priv_query_exec
, obj
, params
):
198 with self
.assertRaises(bt2
._Error
):
199 bt2
.QueryExecutor(MySink
, 'obj', 23).query()
201 def test_query_wrong_return_type(self
):
202 class MySink(bt2
._UserSinkComponent
):
203 def _user_consume(self
):
207 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
210 with self
.assertRaises(bt2
._Error
):
211 bt2
.QueryExecutor(MySink
, 'obj', 23).query()
213 def test_query_params_none(self
):
214 class MySink(bt2
._UserSinkComponent
):
215 def _user_consume(self
):
219 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
220 nonlocal query_params
221 query_params
= params
226 res
= bt2
.QueryExecutor(MySink
, 'obj', params
).query()
227 self
.assertEqual(query_params
, params
)
228 self
.assertIsNone(res
)
231 def test_query_logging_level(self
):
232 class MySink(bt2
._UserSinkComponent
):
233 def _user_consume(self
):
237 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
238 nonlocal query_log_level
239 query_log_level
= priv_query_exec
.logging_level
241 query_log_level
= None
242 query_exec
= bt2
.QueryExecutor(MySink
, 'obj', None)
243 query_exec
.logging_level
= bt2
.LoggingLevel
.WARNING
245 self
.assertEqual(query_log_level
, bt2
.LoggingLevel
.WARNING
)
248 def test_query_returns_none(self
):
249 class MySink(bt2
._UserSinkComponent
):
250 def _user_consume(self
):
254 def _user_query(priv_query_exec
, obj
, params
, method_obj
):
257 res
= bt2
.QueryExecutor(MySink
, 'obj', None).query()
258 self
.assertIsNone(res
)
260 def test_query_simple(self
):
261 class MySink(bt2
._UserSinkComponent
):
262 def _user_consume(self
):
266 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
267 nonlocal query_params
268 query_params
= params
272 params
= ['coucou', 23, None]
273 res
= bt2
.QueryExecutor(MySink
, 'obj', params
).query()
274 self
.assertEqual(query_params
, params
)
275 self
.assertEqual(res
, 17.5)
278 def test_query_complex(self
):
279 class MySink(bt2
._UserSinkComponent
):
280 def _user_consume(self
):
284 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
285 nonlocal query_params
286 query_params
= params
287 return {'null': None, 'bt2': 'BT2'}
291 'array': ['coucou', 23, None],
292 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
296 res
= bt2
.QueryExecutor(MySink
, 'obj', params
).query()
297 self
.assertEqual(query_params
, params
)
298 self
.assertEqual(res
, {'null': None, 'bt2': 'BT2'})
302 class MySink(bt2
._UserSinkComponent
):
303 def _user_consume(self
):
306 self
.assertEqual(MySink
, MySink
)
309 class ComponentClassTestCase(unittest
.TestCase
):
311 class MySink(bt2
._UserSinkComponent
):
318 def _user_consume(self
):
322 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
323 return [obj
, params
, 23]
325 self
._py
_comp
_cls
= MySink
327 comp
= graph
.add_component(MySink
, 'salut')
328 self
._comp
_cls
= comp
.cls
329 self
.assertIs(type(self
._comp
_cls
), bt2
._SinkComponentClassConst
)
332 del self
._py
_comp
_cls
335 def test_description(self
):
336 self
.assertEqual(self
._comp
_cls
.description
, 'The description.')
339 self
.assertEqual(self
._comp
_cls
.help, 'The help.')
342 self
.assertEqual(self
._comp
_cls
.name
, 'MySink')
345 self
.assertIsInstance(self
._comp
_cls
.addr
, int)
346 self
.assertNotEqual(self
._comp
_cls
.addr
, 0)
348 def test_eq_invalid(self
):
349 self
.assertFalse(self
._comp
_cls
== 23)
352 self
.assertEqual(self
._comp
_cls
, self
._comp
_cls
)
353 self
.assertEqual(self
._py
_comp
_cls
, self
._comp
_cls
)
355 def test_query(self
):
356 res
= bt2
.QueryExecutor(
357 self
._comp
_cls
, 'an object', {'yes': 'no', 'book': -17}
359 expected
= ['an object', {'yes': 'no', 'book': -17}, 23]
360 self
.assertEqual(res
, expected
)
363 if __name__
== '__main__':
This page took 0.037683 seconds and 4 git commands to generate.