test_error.py: remove dangling print()
[babeltrace.git] / tests / bindings / python / bt2 / test_component_class.py
1 #
2 # Copyright (C) 2019 EfficiOS Inc.
3 #
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
7 # of the License.
8 #
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.
13 #
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.
17 #
18
19 from bt2 import value
20 import unittest
21 import bt2
22
23
24 class UserComponentClassTestCase(unittest.TestCase):
25 def _test_no_init(self, cls):
26 with self.assertRaises(RuntimeError):
27 cls()
28
29 def test_no_init_source(self):
30 class MyIter(bt2._UserMessageIterator):
31 def __next__(self):
32 raise bt2.Stop
33
34 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
35 pass
36
37 self._test_no_init(MySource)
38
39 def test_no_init_filter(self):
40 class MyIter(bt2._UserMessageIterator):
41 def __next__(self):
42 raise bt2.Stop
43
44 class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyIter):
45 pass
46
47 self._test_no_init(MyFilter)
48
49 def test_no_init_sink(self):
50 class MySink(bt2._UserSinkComponent):
51 def _consume(self):
52 pass
53
54 def _graph_is_configured(self):
55 pass
56
57 self._test_no_init(MySink)
58
59 def test_incomplete_source_no_msg_iter_cls(self):
60 class MyIter(bt2._UserMessageIterator):
61 pass
62
63 with self.assertRaises(bt2._IncompleteUserClass):
64
65 class MySource(bt2._UserSourceComponent):
66 pass
67
68 def test_incomplete_source_wrong_msg_iter_cls_type(self):
69 class MyIter(bt2._UserMessageIterator):
70 pass
71
72 with self.assertRaises(bt2._IncompleteUserClass):
73
74 class MySource(bt2._UserSourceComponent, message_iterator_class=int):
75 pass
76
77 def test_incomplete_filter_no_msg_iter_cls(self):
78 class MyIter(bt2._UserMessageIterator):
79 pass
80
81 with self.assertRaises(bt2._IncompleteUserClass):
82
83 class MyFilter(bt2._UserFilterComponent):
84 pass
85
86 def test_incomplete_sink_no_consume_method(self):
87 class MyIter(bt2._UserMessageIterator):
88 pass
89
90 with self.assertRaises(bt2._IncompleteUserClass):
91
92 class MySink(bt2._UserSinkComponent):
93 pass
94
95 def test_minimal_source(self):
96 class MyIter(bt2._UserMessageIterator):
97 pass
98
99 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
100 pass
101
102 def test_minimal_filter(self):
103 class MyIter(bt2._UserMessageIterator):
104 pass
105
106 class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyIter):
107 pass
108
109 def test_minimal_sink(self):
110 class MySink(bt2._UserSinkComponent):
111 def _consume(self):
112 pass
113
114 def _graph_is_configured(self):
115 pass
116
117 def test_default_name(self):
118 class MySink(bt2._UserSinkComponent):
119 def _consume(self):
120 pass
121
122 def _graph_is_configured(self):
123 pass
124
125 self.assertEqual(MySink.name, 'MySink')
126
127 def test_custom_name(self):
128 class MySink(bt2._UserSinkComponent, name='salut'):
129 def _consume(self):
130 pass
131
132 def _graph_is_configured(self):
133 pass
134
135 self.assertEqual(MySink.name, 'salut')
136
137 def test_invalid_custom_name(self):
138 with self.assertRaises(TypeError):
139
140 class MySink(bt2._UserSinkComponent, name=23):
141 def _consume(self):
142 pass
143
144 def _graph_is_configured(self):
145 pass
146
147 def test_description(self):
148 class MySink(bt2._UserSinkComponent):
149 """
150 The description.
151
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.
157 """
158
159 def _consume(self):
160 pass
161
162 def _graph_is_configured(self):
163 pass
164
165 self.assertEqual(MySink.description, 'The description.')
166
167 def test_empty_description(self):
168 class MySink(bt2._UserSinkComponent):
169 """
170 """
171
172 def _consume(self):
173 pass
174
175 def _graph_is_configured(self):
176 pass
177
178 self.assertIsNone(MySink.description)
179
180 def test_help(self):
181 class MySink(bt2._UserSinkComponent):
182 """
183 The description.
184
185 The help
186 text is
187 here.
188 """
189
190 def _consume(self):
191 pass
192
193 def _graph_is_configured(self):
194 pass
195
196 self.assertEqual(MySink.help, 'The help\ntext is\nhere.')
197
198 def test_addr(self):
199 class MySink(bt2._UserSinkComponent):
200 def _consume(self):
201 pass
202
203 def _graph_is_configured(self):
204 pass
205
206 self.assertIsInstance(MySink.addr, int)
207 self.assertNotEqual(MySink.addr, 0)
208
209 def test_query_not_implemented(self):
210 class MySink(bt2._UserSinkComponent):
211 def _consume(self):
212 pass
213
214 def _graph_is_configured(self):
215 pass
216
217 with self.assertRaises(bt2._Error):
218 bt2.QueryExecutor().query(MySink, 'obj', 23)
219
220 def test_query_raises(self):
221 class MySink(bt2._UserSinkComponent):
222 def _consume(self):
223 pass
224
225 def _graph_is_configured(self):
226 pass
227
228 @classmethod
229 def _query(cls, query_exec, obj, params, log_level):
230 raise ValueError
231
232 with self.assertRaises(bt2._Error):
233 bt2.QueryExecutor().query(MySink, 'obj', 23)
234
235 def test_query_wrong_return_type(self):
236 class MySink(bt2._UserSinkComponent):
237 def _consume(self):
238 pass
239
240 def _graph_is_configured(self):
241 pass
242
243 @classmethod
244 def _query(cls, query_exec, obj, params, log_level):
245 return ...
246
247 with self.assertRaises(bt2._Error):
248 bt2.QueryExecutor().query(MySink, 'obj', 23)
249
250 def test_query_params_none(self):
251 class MySink(bt2._UserSinkComponent):
252 def _consume(self):
253 pass
254
255 def _graph_is_configured(self):
256 pass
257
258 @classmethod
259 def _query(cls, query_exec, obj, params, log_level):
260 nonlocal query_params
261 query_params = params
262 return None
263
264 query_params = None
265 params = None
266 res = bt2.QueryExecutor().query(MySink, 'obj', params)
267 self.assertEqual(query_params, params)
268 self.assertIsNone(res)
269 del query_params
270
271 def test_query_logging_level(self):
272 class MySink(bt2._UserSinkComponent):
273 def _consume(self):
274 pass
275
276 def _graph_is_configured(self):
277 pass
278
279 @classmethod
280 def _query(cls, query_exec, obj, params, log_level):
281 nonlocal query_log_level
282 query_log_level = log_level
283
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)
287 del query_log_level
288
289 def test_query_returns_none(self):
290 class MySink(bt2._UserSinkComponent):
291 def _consume(self):
292 pass
293
294 def _graph_is_configured(self):
295 pass
296
297 @staticmethod
298 def _query(query_exec, obj, params, log_level):
299 return
300
301 res = bt2.QueryExecutor().query(MySink, 'obj', None)
302 self.assertIsNone(res)
303
304 def test_query_simple(self):
305 class MySink(bt2._UserSinkComponent):
306 def _consume(self):
307 pass
308
309 def _graph_is_configured(self):
310 pass
311
312 @classmethod
313 def _query(cls, query_exec, obj, params, log_level):
314 nonlocal query_params
315 query_params = params
316 return 17.5
317
318 query_params = None
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)
323 del query_params
324
325 def test_query_complex(self):
326 class MySink(bt2._UserSinkComponent):
327 def _consume(self):
328 pass
329
330 def _graph_is_configured(self):
331 pass
332
333 @classmethod
334 def _query(cls, query_exec, obj, params, log_level):
335 nonlocal query_params
336 query_params = params
337 return {'null': None, 'bt2': 'BT2'}
338
339 query_params = None
340 params = {
341 'array': ['coucou', 23, None],
342 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
343 'null': None,
344 }
345
346 res = bt2.QueryExecutor().query(MySink, 'obj', params)
347 self.assertEqual(query_params, params)
348 self.assertEqual(res, {'null': None, 'bt2': 'BT2'})
349 del query_params
350
351 def test_eq(self):
352 class MySink(bt2._UserSinkComponent):
353 def _consume(self):
354 pass
355
356 def _graph_is_configured(self):
357 pass
358
359 self.assertEqual(MySink, MySink)
360
361
362 class ComponentClassTestCase(unittest.TestCase):
363 def setUp(self):
364 class MySink(bt2._UserSinkComponent):
365 '''
366 The description.
367
368 The help.
369 '''
370
371 def _consume(self):
372 pass
373
374 def _graph_is_configured(self):
375 pass
376
377 @classmethod
378 def _query(cls, query_exec, obj, params, log_level):
379 return [obj, params, 23]
380
381 self._py_comp_cls = MySink
382 graph = bt2.Graph()
383 comp = graph.add_component(MySink, 'salut')
384 self._comp_cls = comp.cls
385 self.assertTrue(issubclass(type(self._comp_cls), bt2.component._ComponentClass))
386
387 def tearDown(self):
388 del self._py_comp_cls
389 del self._comp_cls
390
391 def test_description(self):
392 self.assertEqual(self._comp_cls.description, 'The description.')
393
394 def test_help(self):
395 self.assertEqual(self._comp_cls.help, 'The help.')
396
397 def test_name(self):
398 self.assertEqual(self._comp_cls.name, 'MySink')
399
400 def test_addr(self):
401 self.assertIsInstance(self._comp_cls.addr, int)
402 self.assertNotEqual(self._comp_cls.addr, 0)
403
404 def test_eq_invalid(self):
405 self.assertFalse(self._comp_cls == 23)
406
407 def test_eq(self):
408 self.assertEqual(self._comp_cls, self._comp_cls)
409 self.assertEqual(self._py_comp_cls, self._comp_cls)
410
411 def test_query(self):
412 res = bt2.QueryExecutor().query(
413 self._comp_cls, 'an object', {'yes': 'no', 'book': -17}
414 )
415 expected = ['an object', {'yes': 'no', 'book': -17}, 23]
416 self.assertEqual(res, expected)
This page took 0.039741 seconds and 4 git commands to generate.