Fix: log.h: missing defines of `_ERRNO()` macros to `_UNUSED()`
[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 import unittest
20 import bt2
21
22
23 class UserComponentClassTestCase(unittest.TestCase):
24 def _test_no_init(self, cls):
25 with self.assertRaises(RuntimeError):
26 cls()
27
28 def test_no_init_source(self):
29 class MyIter(bt2._UserMessageIterator):
30 def __next__(self):
31 raise bt2.Stop
32
33 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
34 pass
35
36 self._test_no_init(MySource)
37
38 def test_no_init_filter(self):
39 class MyIter(bt2._UserMessageIterator):
40 def __next__(self):
41 raise bt2.Stop
42
43 class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyIter):
44 pass
45
46 self._test_no_init(MyFilter)
47
48 def test_no_init_sink(self):
49 class MySink(bt2._UserSinkComponent):
50 def _user_consume(self):
51 pass
52
53 self._test_no_init(MySink)
54
55 def test_incomplete_source_no_msg_iter_cls(self):
56 class MyIter(bt2._UserMessageIterator):
57 pass
58
59 with self.assertRaises(bt2._IncompleteUserClass):
60
61 class MySource(bt2._UserSourceComponent):
62 pass
63
64 def test_incomplete_source_wrong_msg_iter_cls_type(self):
65 class MyIter(bt2._UserMessageIterator):
66 pass
67
68 with self.assertRaises(bt2._IncompleteUserClass):
69
70 class MySource(bt2._UserSourceComponent, message_iterator_class=int):
71 pass
72
73 def test_incomplete_filter_no_msg_iter_cls(self):
74 class MyIter(bt2._UserMessageIterator):
75 pass
76
77 with self.assertRaises(bt2._IncompleteUserClass):
78
79 class MyFilter(bt2._UserFilterComponent):
80 pass
81
82 def test_incomplete_sink_no_consume_method(self):
83 class MyIter(bt2._UserMessageIterator):
84 pass
85
86 with self.assertRaises(bt2._IncompleteUserClass):
87
88 class MySink(bt2._UserSinkComponent):
89 pass
90
91 def test_minimal_source(self):
92 class MyIter(bt2._UserMessageIterator):
93 pass
94
95 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
96 pass
97
98 def test_minimal_filter(self):
99 class MyIter(bt2._UserMessageIterator):
100 pass
101
102 class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyIter):
103 pass
104
105 def test_minimal_sink(self):
106 class MySink(bt2._UserSinkComponent):
107 def _user_consume(self):
108 pass
109
110 def test_default_name(self):
111 class MySink(bt2._UserSinkComponent):
112 def _user_consume(self):
113 pass
114
115 self.assertEqual(MySink.name, 'MySink')
116
117 def test_custom_name(self):
118 class MySink(bt2._UserSinkComponent, name='salut'):
119 def _user_consume(self):
120 pass
121
122 self.assertEqual(MySink.name, 'salut')
123
124 def test_invalid_custom_name(self):
125 with self.assertRaises(TypeError):
126
127 class MySink(bt2._UserSinkComponent, name=23):
128 def _user_consume(self):
129 pass
130
131 def test_description(self):
132 class MySink(bt2._UserSinkComponent):
133 """
134 The description.
135
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.
141 """
142
143 def _user_consume(self):
144 pass
145
146 self.assertEqual(MySink.description, 'The description.')
147
148 def test_empty_description(self):
149 class MySink(bt2._UserSinkComponent):
150 """
151 """
152
153 def _user_consume(self):
154 pass
155
156 self.assertIsNone(MySink.description)
157
158 def test_help(self):
159 class MySink(bt2._UserSinkComponent):
160 """
161 The description.
162
163 The help
164 text is
165 here.
166 """
167
168 def _user_consume(self):
169 pass
170
171 self.assertEqual(MySink.help, 'The help\ntext is\nhere.')
172
173 def test_addr(self):
174 class MySink(bt2._UserSinkComponent):
175 def _user_consume(self):
176 pass
177
178 self.assertIsInstance(MySink.addr, int)
179 self.assertNotEqual(MySink.addr, 0)
180
181 def test_query_not_implemented(self):
182 class MySink(bt2._UserSinkComponent):
183 def _user_consume(self):
184 pass
185
186 with self.assertRaises(bt2.UnknownObject):
187 bt2.QueryExecutor(MySink, 'obj', 23).query()
188
189 def test_query_raises(self):
190 class MySink(bt2._UserSinkComponent):
191 def _user_consume(self):
192 pass
193
194 @classmethod
195 def _user_query(cls, priv_query_exec, obj, params):
196 raise ValueError
197
198 with self.assertRaises(bt2._Error):
199 bt2.QueryExecutor(MySink, 'obj', 23).query()
200
201 def test_query_wrong_return_type(self):
202 class MySink(bt2._UserSinkComponent):
203 def _user_consume(self):
204 pass
205
206 @classmethod
207 def _user_query(cls, priv_query_exec, obj, params, method_obj):
208 return ...
209
210 with self.assertRaises(bt2._Error):
211 bt2.QueryExecutor(MySink, 'obj', 23).query()
212
213 def test_query_params_none(self):
214 class MySink(bt2._UserSinkComponent):
215 def _user_consume(self):
216 pass
217
218 @classmethod
219 def _user_query(cls, priv_query_exec, obj, params, method_obj):
220 nonlocal query_params
221 query_params = params
222 return None
223
224 query_params = None
225 params = None
226 res = bt2.QueryExecutor(MySink, 'obj', params).query()
227 self.assertEqual(query_params, params)
228 self.assertIsNone(res)
229 del query_params
230
231 def test_query_logging_level(self):
232 class MySink(bt2._UserSinkComponent):
233 def _user_consume(self):
234 pass
235
236 @classmethod
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
240
241 query_log_level = None
242 query_exec = bt2.QueryExecutor(MySink, 'obj', None)
243 query_exec.logging_level = bt2.LoggingLevel.WARNING
244 query_exec.query()
245 self.assertEqual(query_log_level, bt2.LoggingLevel.WARNING)
246 del query_log_level
247
248 def test_query_returns_none(self):
249 class MySink(bt2._UserSinkComponent):
250 def _user_consume(self):
251 pass
252
253 @staticmethod
254 def _user_query(priv_query_exec, obj, params, method_obj):
255 return
256
257 res = bt2.QueryExecutor(MySink, 'obj', None).query()
258 self.assertIsNone(res)
259
260 def test_query_simple(self):
261 class MySink(bt2._UserSinkComponent):
262 def _user_consume(self):
263 pass
264
265 @classmethod
266 def _user_query(cls, priv_query_exec, obj, params, method_obj):
267 nonlocal query_params
268 query_params = params
269 return 17.5
270
271 query_params = None
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)
276 del query_params
277
278 def test_query_complex(self):
279 class MySink(bt2._UserSinkComponent):
280 def _user_consume(self):
281 pass
282
283 @classmethod
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'}
288
289 query_params = None
290 params = {
291 'array': ['coucou', 23, None],
292 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
293 'null': None,
294 }
295
296 res = bt2.QueryExecutor(MySink, 'obj', params).query()
297 self.assertEqual(query_params, params)
298 self.assertEqual(res, {'null': None, 'bt2': 'BT2'})
299 del query_params
300
301 def test_eq(self):
302 class MySink(bt2._UserSinkComponent):
303 def _user_consume(self):
304 pass
305
306 self.assertEqual(MySink, MySink)
307
308
309 class ComponentClassTestCase(unittest.TestCase):
310 def setUp(self):
311 class MySink(bt2._UserSinkComponent):
312 '''
313 The description.
314
315 The help.
316 '''
317
318 def _user_consume(self):
319 pass
320
321 @classmethod
322 def _user_query(cls, priv_query_exec, obj, params, method_obj):
323 return [obj, params, 23]
324
325 self._py_comp_cls = MySink
326 graph = bt2.Graph()
327 comp = graph.add_component(MySink, 'salut')
328 self._comp_cls = comp.cls
329 self.assertIs(type(self._comp_cls), bt2._SinkComponentClassConst)
330
331 def tearDown(self):
332 del self._py_comp_cls
333 del self._comp_cls
334
335 def test_description(self):
336 self.assertEqual(self._comp_cls.description, 'The description.')
337
338 def test_help(self):
339 self.assertEqual(self._comp_cls.help, 'The help.')
340
341 def test_name(self):
342 self.assertEqual(self._comp_cls.name, 'MySink')
343
344 def test_addr(self):
345 self.assertIsInstance(self._comp_cls.addr, int)
346 self.assertNotEqual(self._comp_cls.addr, 0)
347
348 def test_eq_invalid(self):
349 self.assertFalse(self._comp_cls == 23)
350
351 def test_eq(self):
352 self.assertEqual(self._comp_cls, self._comp_cls)
353 self.assertEqual(self._py_comp_cls, self._comp_cls)
354
355 def test_query(self):
356 res = bt2.QueryExecutor(
357 self._comp_cls, 'an object', {'yes': 'no', 'book': -17}
358 ).query()
359 expected = ['an object', {'yes': 'no', 'book': -17}, 23]
360 self.assertEqual(res, expected)
361
362
363 if __name__ == '__main__':
364 unittest.main()
This page took 0.037683 seconds and 4 git commands to generate.