Reformat Python files with Black v20.8b1
[babeltrace.git] / tests / bindings / python / bt2 / test_component_class.py
CommitLineData
0235b0db 1# SPDX-License-Identifier: GPL-2.0-only
d2d857a8
MJ
2#
3# Copyright (C) 2019 EfficiOS Inc.
4#
d2d857a8 5
811644b8 6import unittest
811644b8
PP
7import bt2
8
9
10class UserComponentClassTestCase(unittest.TestCase):
11 def _test_no_init(self, cls):
ce4923b0 12 with self.assertRaises(RuntimeError):
811644b8
PP
13 cls()
14
15 def test_no_init_source(self):
5c61fb9d
SM
16 class MySource(
17 bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator
18 ):
811644b8
PP
19 pass
20
21 self._test_no_init(MySource)
22
23 def test_no_init_filter(self):
5c61fb9d
SM
24 class MyFilter(
25 bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator
26 ):
811644b8
PP
27 pass
28
29 self._test_no_init(MyFilter)
30
31 def test_no_init_sink(self):
32 class MySink(bt2._UserSinkComponent):
6a91742b 33 def _user_consume(self):
a01b452b
SM
34 pass
35
811644b8
PP
36 self._test_no_init(MySink)
37
5602ef81 38 def test_incomplete_source_no_msg_iter_cls(self):
cb06aa27 39 with self.assertRaises(bt2._IncompleteUserClass):
cfbd7cf3 40
811644b8
PP
41 class MySource(bt2._UserSourceComponent):
42 pass
43
5602ef81 44 def test_incomplete_source_wrong_msg_iter_cls_type(self):
cb06aa27 45 with self.assertRaises(bt2._IncompleteUserClass):
cfbd7cf3
FD
46
47 class MySource(bt2._UserSourceComponent, message_iterator_class=int):
811644b8
PP
48 pass
49
5602ef81 50 def test_incomplete_filter_no_msg_iter_cls(self):
cb06aa27 51 with self.assertRaises(bt2._IncompleteUserClass):
cfbd7cf3 52
811644b8
PP
53 class MyFilter(bt2._UserFilterComponent):
54 pass
55
56 def test_incomplete_sink_no_consume_method(self):
cb06aa27 57 with self.assertRaises(bt2._IncompleteUserClass):
cfbd7cf3 58
811644b8
PP
59 class MySink(bt2._UserSinkComponent):
60 pass
61
62 def test_minimal_source(self):
5c61fb9d
SM
63 class MySource(
64 bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator
65 ):
811644b8
PP
66 pass
67
68 def test_minimal_filter(self):
5c61fb9d
SM
69 class MyFilter(
70 bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator
71 ):
811644b8
PP
72 pass
73
74 def test_minimal_sink(self):
75 class MySink(bt2._UserSinkComponent):
6a91742b 76 def _user_consume(self):
a01b452b
SM
77 pass
78
811644b8
PP
79 def test_default_name(self):
80 class MySink(bt2._UserSinkComponent):
6a91742b 81 def _user_consume(self):
a01b452b
SM
82 pass
83
811644b8
PP
84 self.assertEqual(MySink.name, 'MySink')
85
86 def test_custom_name(self):
87 class MySink(bt2._UserSinkComponent, name='salut'):
6a91742b 88 def _user_consume(self):
a01b452b
SM
89 pass
90
811644b8
PP
91 self.assertEqual(MySink.name, 'salut')
92
93 def test_invalid_custom_name(self):
94 with self.assertRaises(TypeError):
cfbd7cf3 95
811644b8 96 class MySink(bt2._UserSinkComponent, name=23):
6a91742b 97 def _user_consume(self):
a01b452b
SM
98 pass
99
811644b8
PP
100 def test_description(self):
101 class MySink(bt2._UserSinkComponent):
102 """
103 The description.
104
105 Bacon ipsum dolor amet ribeye t-bone corned beef, beef jerky
106 porchetta burgdoggen prosciutto chicken frankfurter boudin
107 hamburger doner bacon turducken. Sirloin shank sausage,
108 boudin meatloaf alcatra meatball t-bone tongue pastrami
109 cupim flank tenderloin.
110 """
111
6a91742b 112 def _user_consume(self):
a01b452b
SM
113 pass
114
811644b8
PP
115 self.assertEqual(MySink.description, 'The description.')
116
ade5c95e 117 def test_empty_description_no_lines(self):
811644b8 118 class MySink(bt2._UserSinkComponent):
ade5c95e
PP
119 # fmt: off
120 """"""
121 # fmt: on
122
123 def _user_consume(self):
124 pass
125
126 self.assertIsNone(MySink.description)
127
128 def test_empty_description_no_contents(self):
129 class MySink(bt2._UserSinkComponent):
130 # fmt: off
811644b8
PP
131 """
132 """
ade5c95e 133 # fmt: on
811644b8 134
6a91742b 135 def _user_consume(self):
a01b452b
SM
136 pass
137
811644b8
PP
138 self.assertIsNone(MySink.description)
139
ade5c95e
PP
140 def test_empty_description_single_line(self):
141 class MySink(bt2._UserSinkComponent):
142 """my description"""
143
144 def _user_consume(self):
145 pass
146
147 self.assertEqual(MySink.description, "my description")
148
811644b8
PP
149 def test_help(self):
150 class MySink(bt2._UserSinkComponent):
151 """
152 The description.
153
154 The help
155 text is
156 here.
157 """
158
6a91742b 159 def _user_consume(self):
a01b452b
SM
160 pass
161
811644b8
PP
162 self.assertEqual(MySink.help, 'The help\ntext is\nhere.')
163
164 def test_addr(self):
165 class MySink(bt2._UserSinkComponent):
6a91742b 166 def _user_consume(self):
a01b452b
SM
167 pass
168
811644b8
PP
169 self.assertIsInstance(MySink.addr, int)
170 self.assertNotEqual(MySink.addr, 0)
171
172 def test_query_not_implemented(self):
173 class MySink(bt2._UserSinkComponent):
6a91742b 174 def _user_consume(self):
a01b452b
SM
175 pass
176
76b6c2f7 177 with self.assertRaises(bt2.UnknownObject):
3c729b9a 178 bt2.QueryExecutor(MySink, 'obj', 23).query()
811644b8
PP
179
180 def test_query_raises(self):
181 class MySink(bt2._UserSinkComponent):
6a91742b 182 def _user_consume(self):
a01b452b
SM
183 pass
184
811644b8 185 @classmethod
3c729b9a 186 def _user_query(cls, priv_query_exec, obj, params):
811644b8
PP
187 raise ValueError
188
694c792b 189 with self.assertRaises(bt2._Error):
3c729b9a 190 bt2.QueryExecutor(MySink, 'obj', 23).query()
811644b8
PP
191
192 def test_query_wrong_return_type(self):
193 class MySink(bt2._UserSinkComponent):
6a91742b 194 def _user_consume(self):
a01b452b
SM
195 pass
196
811644b8 197 @classmethod
7c14d641 198 def _user_query(cls, priv_query_exec, obj, params, method_obj):
811644b8
PP
199 return ...
200
694c792b 201 with self.assertRaises(bt2._Error):
3c729b9a 202 bt2.QueryExecutor(MySink, 'obj', 23).query()
811644b8
PP
203
204 def test_query_params_none(self):
205 class MySink(bt2._UserSinkComponent):
6a91742b 206 def _user_consume(self):
a01b452b
SM
207 pass
208
811644b8 209 @classmethod
7c14d641 210 def _user_query(cls, priv_query_exec, obj, params, method_obj):
811644b8
PP
211 nonlocal query_params
212 query_params = params
213 return None
214
215 query_params = None
216 params = None
3c729b9a 217 res = bt2.QueryExecutor(MySink, 'obj', params).query()
811644b8
PP
218 self.assertEqual(query_params, params)
219 self.assertIsNone(res)
220 del query_params
221
761e1890
PP
222 def test_query_logging_level(self):
223 class MySink(bt2._UserSinkComponent):
6a91742b 224 def _user_consume(self):
a01b452b
SM
225 pass
226
761e1890 227 @classmethod
7c14d641 228 def _user_query(cls, priv_query_exec, obj, params, method_obj):
761e1890 229 nonlocal query_log_level
3c729b9a 230 query_log_level = priv_query_exec.logging_level
761e1890
PP
231
232 query_log_level = None
3c729b9a
PP
233 query_exec = bt2.QueryExecutor(MySink, 'obj', None)
234 query_exec.logging_level = bt2.LoggingLevel.WARNING
235 query_exec.query()
770538dd 236 self.assertEqual(query_log_level, bt2.LoggingLevel.WARNING)
761e1890
PP
237 del query_log_level
238
b5947615
SM
239 def test_query_returns_none(self):
240 class MySink(bt2._UserSinkComponent):
6a91742b 241 def _user_consume(self):
a01b452b
SM
242 pass
243
b5947615 244 @staticmethod
7c14d641 245 def _user_query(priv_query_exec, obj, params, method_obj):
b5947615
SM
246 return
247
3c729b9a 248 res = bt2.QueryExecutor(MySink, 'obj', None).query()
b5947615
SM
249 self.assertIsNone(res)
250
811644b8
PP
251 def test_query_simple(self):
252 class MySink(bt2._UserSinkComponent):
6a91742b 253 def _user_consume(self):
a01b452b
SM
254 pass
255
811644b8 256 @classmethod
7c14d641 257 def _user_query(cls, priv_query_exec, obj, params, method_obj):
811644b8
PP
258 nonlocal query_params
259 query_params = params
260 return 17.5
261
262 query_params = None
263 params = ['coucou', 23, None]
3c729b9a 264 res = bt2.QueryExecutor(MySink, 'obj', params).query()
811644b8
PP
265 self.assertEqual(query_params, params)
266 self.assertEqual(res, 17.5)
267 del query_params
268
269 def test_query_complex(self):
270 class MySink(bt2._UserSinkComponent):
6a91742b 271 def _user_consume(self):
a01b452b
SM
272 pass
273
811644b8 274 @classmethod
7c14d641 275 def _user_query(cls, priv_query_exec, obj, params, method_obj):
811644b8
PP
276 nonlocal query_params
277 query_params = params
cfbd7cf3 278 return {'null': None, 'bt2': 'BT2'}
811644b8
PP
279
280 query_params = None
281 params = {
282 'array': ['coucou', 23, None],
cfbd7cf3 283 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
811644b8
PP
284 'null': None,
285 }
286
3c729b9a 287 res = bt2.QueryExecutor(MySink, 'obj', params).query()
811644b8 288 self.assertEqual(query_params, params)
cfbd7cf3 289 self.assertEqual(res, {'null': None, 'bt2': 'BT2'})
811644b8
PP
290 del query_params
291
292 def test_eq(self):
293 class MySink(bt2._UserSinkComponent):
6a91742b 294 def _user_consume(self):
a01b452b
SM
295 pass
296
811644b8
PP
297 self.assertEqual(MySink, MySink)
298
299
68e606ef 300class ComponentClassTestCase(unittest.TestCase):
811644b8
PP
301 def setUp(self):
302 class MySink(bt2._UserSinkComponent):
776a2a25 303 """
811644b8
PP
304 The description.
305
306 The help.
776a2a25 307 """
cfbd7cf3 308
6a91742b 309 def _user_consume(self):
a01b452b
SM
310 pass
311
811644b8 312 @classmethod
7c14d641 313 def _user_query(cls, priv_query_exec, obj, params, method_obj):
811644b8
PP
314 return [obj, params, 23]
315
316 self._py_comp_cls = MySink
317 graph = bt2.Graph()
894a8df5 318 comp = graph.add_component(MySink, 'salut')
e8ac1aae 319 self._comp_cls = comp.cls
242b863f 320 self.assertIs(type(self._comp_cls), bt2._SinkComponentClassConst)
811644b8
PP
321
322 def tearDown(self):
323 del self._py_comp_cls
324 del self._comp_cls
325
326 def test_description(self):
327 self.assertEqual(self._comp_cls.description, 'The description.')
328
329 def test_help(self):
330 self.assertEqual(self._comp_cls.help, 'The help.')
331
332 def test_name(self):
333 self.assertEqual(self._comp_cls.name, 'MySink')
334
335 def test_addr(self):
336 self.assertIsInstance(self._comp_cls.addr, int)
337 self.assertNotEqual(self._comp_cls.addr, 0)
338
339 def test_eq_invalid(self):
340 self.assertFalse(self._comp_cls == 23)
341
342 def test_eq(self):
343 self.assertEqual(self._comp_cls, self._comp_cls)
344 self.assertEqual(self._py_comp_cls, self._comp_cls)
345
346 def test_query(self):
3c729b9a 347 res = bt2.QueryExecutor(
cfbd7cf3 348 self._comp_cls, 'an object', {'yes': 'no', 'book': -17}
3c729b9a 349 ).query()
811644b8
PP
350 expected = ['an object', {'yes': 'no', 'book': -17}, 23]
351 self.assertEqual(res, expected)
d14ddbba
SM
352
353
354if __name__ == '__main__':
355 unittest.main()
This page took 0.077223 seconds and 4 git commands to generate.