test_error.py: remove dangling print()
[babeltrace.git] / tests / bindings / python / bt2 / test_component_class.py
CommitLineData
d2d857a8
MJ
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
c4239792 19from bt2 import value
811644b8 20import unittest
811644b8
PP
21import bt2
22
23
24class UserComponentClassTestCase(unittest.TestCase):
25 def _test_no_init(self, cls):
ce4923b0 26 with self.assertRaises(RuntimeError):
811644b8
PP
27 cls()
28
29 def test_no_init_source(self):
5602ef81 30 class MyIter(bt2._UserMessageIterator):
811644b8
PP
31 def __next__(self):
32 raise bt2.Stop
33
cfbd7cf3 34 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
811644b8
PP
35 pass
36
37 self._test_no_init(MySource)
38
39 def test_no_init_filter(self):
5602ef81 40 class MyIter(bt2._UserMessageIterator):
811644b8
PP
41 def __next__(self):
42 raise bt2.Stop
43
cfbd7cf3 44 class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyIter):
811644b8
PP
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
a01b452b
SM
54 def _graph_is_configured(self):
55 pass
56
811644b8
PP
57 self._test_no_init(MySink)
58
5602ef81
SM
59 def test_incomplete_source_no_msg_iter_cls(self):
60 class MyIter(bt2._UserMessageIterator):
811644b8
PP
61 pass
62
cb06aa27 63 with self.assertRaises(bt2._IncompleteUserClass):
cfbd7cf3 64
811644b8
PP
65 class MySource(bt2._UserSourceComponent):
66 pass
67
5602ef81
SM
68 def test_incomplete_source_wrong_msg_iter_cls_type(self):
69 class MyIter(bt2._UserMessageIterator):
811644b8
PP
70 pass
71
cb06aa27 72 with self.assertRaises(bt2._IncompleteUserClass):
cfbd7cf3
FD
73
74 class MySource(bt2._UserSourceComponent, message_iterator_class=int):
811644b8
PP
75 pass
76
5602ef81
SM
77 def test_incomplete_filter_no_msg_iter_cls(self):
78 class MyIter(bt2._UserMessageIterator):
811644b8
PP
79 pass
80
cb06aa27 81 with self.assertRaises(bt2._IncompleteUserClass):
cfbd7cf3 82
811644b8
PP
83 class MyFilter(bt2._UserFilterComponent):
84 pass
85
86 def test_incomplete_sink_no_consume_method(self):
5602ef81 87 class MyIter(bt2._UserMessageIterator):
811644b8
PP
88 pass
89
cb06aa27 90 with self.assertRaises(bt2._IncompleteUserClass):
cfbd7cf3 91
811644b8
PP
92 class MySink(bt2._UserSinkComponent):
93 pass
94
95 def test_minimal_source(self):
5602ef81 96 class MyIter(bt2._UserMessageIterator):
811644b8
PP
97 pass
98
cfbd7cf3 99 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
811644b8
PP
100 pass
101
102 def test_minimal_filter(self):
5602ef81 103 class MyIter(bt2._UserMessageIterator):
811644b8
PP
104 pass
105
cfbd7cf3 106 class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyIter):
811644b8
PP
107 pass
108
109 def test_minimal_sink(self):
110 class MySink(bt2._UserSinkComponent):
111 def _consume(self):
112 pass
113
a01b452b
SM
114 def _graph_is_configured(self):
115 pass
116
811644b8
PP
117 def test_default_name(self):
118 class MySink(bt2._UserSinkComponent):
119 def _consume(self):
120 pass
121
a01b452b
SM
122 def _graph_is_configured(self):
123 pass
124
811644b8
PP
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
a01b452b
SM
132 def _graph_is_configured(self):
133 pass
134
811644b8
PP
135 self.assertEqual(MySink.name, 'salut')
136
137 def test_invalid_custom_name(self):
138 with self.assertRaises(TypeError):
cfbd7cf3 139
811644b8
PP
140 class MySink(bt2._UserSinkComponent, name=23):
141 def _consume(self):
142 pass
143
a01b452b
SM
144 def _graph_is_configured(self):
145 pass
146
811644b8
PP
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
a01b452b
SM
162 def _graph_is_configured(self):
163 pass
164
811644b8
PP
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
a01b452b
SM
175 def _graph_is_configured(self):
176 pass
177
811644b8
PP
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
a01b452b
SM
193 def _graph_is_configured(self):
194 pass
195
811644b8
PP
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
a01b452b
SM
203 def _graph_is_configured(self):
204 pass
205
811644b8
PP
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
a01b452b
SM
214 def _graph_is_configured(self):
215 pass
216
694c792b 217 with self.assertRaises(bt2._Error):
c7eee084 218 bt2.QueryExecutor().query(MySink, 'obj', 23)
811644b8
PP
219
220 def test_query_raises(self):
221 class MySink(bt2._UserSinkComponent):
222 def _consume(self):
223 pass
224
a01b452b
SM
225 def _graph_is_configured(self):
226 pass
227
811644b8 228 @classmethod
f4e38e70 229 def _query(cls, query_exec, obj, params, log_level):
811644b8
PP
230 raise ValueError
231
694c792b 232 with self.assertRaises(bt2._Error):
c7eee084 233 bt2.QueryExecutor().query(MySink, 'obj', 23)
811644b8
PP
234
235 def test_query_wrong_return_type(self):
236 class MySink(bt2._UserSinkComponent):
237 def _consume(self):
238 pass
239
a01b452b
SM
240 def _graph_is_configured(self):
241 pass
242
811644b8 243 @classmethod
f4e38e70 244 def _query(cls, query_exec, obj, params, log_level):
811644b8
PP
245 return ...
246
694c792b 247 with self.assertRaises(bt2._Error):
c7eee084 248 bt2.QueryExecutor().query(MySink, 'obj', 23)
811644b8
PP
249
250 def test_query_params_none(self):
251 class MySink(bt2._UserSinkComponent):
252 def _consume(self):
253 pass
254
a01b452b
SM
255 def _graph_is_configured(self):
256 pass
257
811644b8 258 @classmethod
f4e38e70 259 def _query(cls, query_exec, obj, params, log_level):
811644b8
PP
260 nonlocal query_params
261 query_params = params
262 return None
263
264 query_params = None
265 params = None
c7eee084 266 res = bt2.QueryExecutor().query(MySink, 'obj', params)
811644b8
PP
267 self.assertEqual(query_params, params)
268 self.assertIsNone(res)
269 del query_params
270
761e1890
PP
271 def test_query_logging_level(self):
272 class MySink(bt2._UserSinkComponent):
273 def _consume(self):
274 pass
275
a01b452b
SM
276 def _graph_is_configured(self):
277 pass
278
761e1890
PP
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
cfbd7cf3 285 res = bt2.QueryExecutor().query(MySink, 'obj', None, bt2.LoggingLevel.WARNING)
770538dd 286 self.assertEqual(query_log_level, bt2.LoggingLevel.WARNING)
761e1890
PP
287 del query_log_level
288
b5947615
SM
289 def test_query_returns_none(self):
290 class MySink(bt2._UserSinkComponent):
291 def _consume(self):
292 pass
293
a01b452b
SM
294 def _graph_is_configured(self):
295 pass
296
b5947615
SM
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
811644b8
PP
304 def test_query_simple(self):
305 class MySink(bt2._UserSinkComponent):
306 def _consume(self):
307 pass
308
a01b452b
SM
309 def _graph_is_configured(self):
310 pass
311
811644b8 312 @classmethod
f4e38e70 313 def _query(cls, query_exec, obj, params, log_level):
811644b8
PP
314 nonlocal query_params
315 query_params = params
316 return 17.5
317
318 query_params = None
319 params = ['coucou', 23, None]
c7eee084 320 res = bt2.QueryExecutor().query(MySink, 'obj', params)
811644b8
PP
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
a01b452b
SM
330 def _graph_is_configured(self):
331 pass
332
811644b8 333 @classmethod
f4e38e70 334 def _query(cls, query_exec, obj, params, log_level):
811644b8
PP
335 nonlocal query_params
336 query_params = params
cfbd7cf3 337 return {'null': None, 'bt2': 'BT2'}
811644b8
PP
338
339 query_params = None
340 params = {
341 'array': ['coucou', 23, None],
cfbd7cf3 342 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
811644b8
PP
343 'null': None,
344 }
345
c7eee084 346 res = bt2.QueryExecutor().query(MySink, 'obj', params)
811644b8 347 self.assertEqual(query_params, params)
cfbd7cf3 348 self.assertEqual(res, {'null': None, 'bt2': 'BT2'})
811644b8
PP
349 del query_params
350
351 def test_eq(self):
352 class MySink(bt2._UserSinkComponent):
353 def _consume(self):
354 pass
355
a01b452b
SM
356 def _graph_is_configured(self):
357 pass
358
811644b8
PP
359 self.assertEqual(MySink, MySink)
360
361
68e606ef 362class ComponentClassTestCase(unittest.TestCase):
811644b8
PP
363 def setUp(self):
364 class MySink(bt2._UserSinkComponent):
365 '''
366 The description.
367
368 The help.
369 '''
cfbd7cf3 370
811644b8
PP
371 def _consume(self):
372 pass
373
a01b452b
SM
374 def _graph_is_configured(self):
375 pass
376
811644b8 377 @classmethod
f4e38e70 378 def _query(cls, query_exec, obj, params, log_level):
811644b8
PP
379 return [obj, params, 23]
380
381 self._py_comp_cls = MySink
382 graph = bt2.Graph()
894a8df5 383 comp = graph.add_component(MySink, 'salut')
e8ac1aae 384 self._comp_cls = comp.cls
68e606ef 385 self.assertTrue(issubclass(type(self._comp_cls), bt2.component._ComponentClass))
811644b8
PP
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):
cfbd7cf3
FD
412 res = bt2.QueryExecutor().query(
413 self._comp_cls, 'an object', {'yes': 'no', 'book': -17}
414 )
811644b8
PP
415 expected = ['an object', {'yes': 'no', 'book': -17}, 23]
416 self.assertEqual(res, expected)
This page took 0.060379 seconds and 4 git commands to generate.