Commit | Line | Data |
---|---|---|
32d2d479 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 | ||
f6a5e476 | 19 | import unittest |
f6a5e476 PP |
20 | import bt2 |
21 | ||
22 | ||
23 | class UserComponentClassTestCase(unittest.TestCase): | |
24 | def _test_no_init(self, cls): | |
3b2be708 | 25 | with self.assertRaises(RuntimeError): |
f6a5e476 PP |
26 | cls() |
27 | ||
28 | def test_no_init_source(self): | |
fa4c33e3 | 29 | class MyIter(bt2._UserMessageIterator): |
f6a5e476 PP |
30 | def __next__(self): |
31 | raise bt2.Stop | |
32 | ||
61d96b89 | 33 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
f6a5e476 PP |
34 | pass |
35 | ||
36 | self._test_no_init(MySource) | |
37 | ||
38 | def test_no_init_filter(self): | |
fa4c33e3 | 39 | class MyIter(bt2._UserMessageIterator): |
f6a5e476 PP |
40 | def __next__(self): |
41 | raise bt2.Stop | |
42 | ||
61d96b89 | 43 | class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyIter): |
f6a5e476 PP |
44 | pass |
45 | ||
46 | self._test_no_init(MyFilter) | |
47 | ||
48 | def test_no_init_sink(self): | |
49 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 50 | def _user_consume(self): |
8a08af82 SM |
51 | pass |
52 | ||
f6a5e476 PP |
53 | self._test_no_init(MySink) |
54 | ||
fa4c33e3 SM |
55 | def test_incomplete_source_no_msg_iter_cls(self): |
56 | class MyIter(bt2._UserMessageIterator): | |
f6a5e476 PP |
57 | pass |
58 | ||
64e96b5d | 59 | with self.assertRaises(bt2._IncompleteUserClass): |
61d96b89 | 60 | |
f6a5e476 PP |
61 | class MySource(bt2._UserSourceComponent): |
62 | pass | |
63 | ||
fa4c33e3 SM |
64 | def test_incomplete_source_wrong_msg_iter_cls_type(self): |
65 | class MyIter(bt2._UserMessageIterator): | |
f6a5e476 PP |
66 | pass |
67 | ||
64e96b5d | 68 | with self.assertRaises(bt2._IncompleteUserClass): |
61d96b89 FD |
69 | |
70 | class MySource(bt2._UserSourceComponent, message_iterator_class=int): | |
f6a5e476 PP |
71 | pass |
72 | ||
fa4c33e3 SM |
73 | def test_incomplete_filter_no_msg_iter_cls(self): |
74 | class MyIter(bt2._UserMessageIterator): | |
f6a5e476 PP |
75 | pass |
76 | ||
64e96b5d | 77 | with self.assertRaises(bt2._IncompleteUserClass): |
61d96b89 | 78 | |
f6a5e476 PP |
79 | class MyFilter(bt2._UserFilterComponent): |
80 | pass | |
81 | ||
82 | def test_incomplete_sink_no_consume_method(self): | |
fa4c33e3 | 83 | class MyIter(bt2._UserMessageIterator): |
f6a5e476 PP |
84 | pass |
85 | ||
64e96b5d | 86 | with self.assertRaises(bt2._IncompleteUserClass): |
61d96b89 | 87 | |
f6a5e476 PP |
88 | class MySink(bt2._UserSinkComponent): |
89 | pass | |
90 | ||
91 | def test_minimal_source(self): | |
fa4c33e3 | 92 | class MyIter(bt2._UserMessageIterator): |
f6a5e476 PP |
93 | pass |
94 | ||
61d96b89 | 95 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
f6a5e476 PP |
96 | pass |
97 | ||
98 | def test_minimal_filter(self): | |
fa4c33e3 | 99 | class MyIter(bt2._UserMessageIterator): |
f6a5e476 PP |
100 | pass |
101 | ||
61d96b89 | 102 | class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyIter): |
f6a5e476 PP |
103 | pass |
104 | ||
105 | def test_minimal_sink(self): | |
106 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 107 | def _user_consume(self): |
8a08af82 SM |
108 | pass |
109 | ||
f6a5e476 PP |
110 | def test_default_name(self): |
111 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 112 | def _user_consume(self): |
8a08af82 SM |
113 | pass |
114 | ||
f6a5e476 PP |
115 | self.assertEqual(MySink.name, 'MySink') |
116 | ||
117 | def test_custom_name(self): | |
118 | class MySink(bt2._UserSinkComponent, name='salut'): | |
819d0ae7 | 119 | def _user_consume(self): |
8a08af82 SM |
120 | pass |
121 | ||
f6a5e476 PP |
122 | self.assertEqual(MySink.name, 'salut') |
123 | ||
124 | def test_invalid_custom_name(self): | |
125 | with self.assertRaises(TypeError): | |
61d96b89 | 126 | |
f6a5e476 | 127 | class MySink(bt2._UserSinkComponent, name=23): |
819d0ae7 | 128 | def _user_consume(self): |
8a08af82 SM |
129 | pass |
130 | ||
f6a5e476 PP |
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 | ||
819d0ae7 | 143 | def _user_consume(self): |
8a08af82 SM |
144 | pass |
145 | ||
f6a5e476 PP |
146 | self.assertEqual(MySink.description, 'The description.') |
147 | ||
148 | def test_empty_description(self): | |
149 | class MySink(bt2._UserSinkComponent): | |
150 | """ | |
151 | """ | |
152 | ||
819d0ae7 | 153 | def _user_consume(self): |
8a08af82 SM |
154 | pass |
155 | ||
f6a5e476 PP |
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 | ||
819d0ae7 | 168 | def _user_consume(self): |
8a08af82 SM |
169 | pass |
170 | ||
f6a5e476 PP |
171 | self.assertEqual(MySink.help, 'The help\ntext is\nhere.') |
172 | ||
173 | def test_addr(self): | |
174 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 175 | def _user_consume(self): |
8a08af82 SM |
176 | pass |
177 | ||
f6a5e476 PP |
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): | |
819d0ae7 | 183 | def _user_consume(self): |
8a08af82 SM |
184 | pass |
185 | ||
7dd3e712 | 186 | with self.assertRaises(bt2.UnknownObject): |
bf403eb2 | 187 | bt2.QueryExecutor(MySink, 'obj', 23).query() |
f6a5e476 PP |
188 | |
189 | def test_query_raises(self): | |
190 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 191 | def _user_consume(self): |
8a08af82 SM |
192 | pass |
193 | ||
f6a5e476 | 194 | @classmethod |
bf403eb2 | 195 | def _user_query(cls, priv_query_exec, obj, params): |
f6a5e476 PP |
196 | raise ValueError |
197 | ||
614743a5 | 198 | with self.assertRaises(bt2._Error): |
bf403eb2 | 199 | bt2.QueryExecutor(MySink, 'obj', 23).query() |
f6a5e476 PP |
200 | |
201 | def test_query_wrong_return_type(self): | |
202 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 203 | def _user_consume(self): |
8a08af82 SM |
204 | pass |
205 | ||
f6a5e476 | 206 | @classmethod |
f6e305ce | 207 | def _user_query(cls, priv_query_exec, obj, params, method_obj): |
f6a5e476 PP |
208 | return ... |
209 | ||
614743a5 | 210 | with self.assertRaises(bt2._Error): |
bf403eb2 | 211 | bt2.QueryExecutor(MySink, 'obj', 23).query() |
f6a5e476 PP |
212 | |
213 | def test_query_params_none(self): | |
214 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 215 | def _user_consume(self): |
8a08af82 SM |
216 | pass |
217 | ||
f6a5e476 | 218 | @classmethod |
f6e305ce | 219 | def _user_query(cls, priv_query_exec, obj, params, method_obj): |
f6a5e476 PP |
220 | nonlocal query_params |
221 | query_params = params | |
222 | return None | |
223 | ||
224 | query_params = None | |
225 | params = None | |
bf403eb2 | 226 | res = bt2.QueryExecutor(MySink, 'obj', params).query() |
f6a5e476 PP |
227 | self.assertEqual(query_params, params) |
228 | self.assertIsNone(res) | |
229 | del query_params | |
230 | ||
999e7442 PP |
231 | def test_query_logging_level(self): |
232 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 233 | def _user_consume(self): |
8a08af82 SM |
234 | pass |
235 | ||
999e7442 | 236 | @classmethod |
f6e305ce | 237 | def _user_query(cls, priv_query_exec, obj, params, method_obj): |
999e7442 | 238 | nonlocal query_log_level |
bf403eb2 | 239 | query_log_level = priv_query_exec.logging_level |
999e7442 PP |
240 | |
241 | query_log_level = None | |
bf403eb2 PP |
242 | query_exec = bt2.QueryExecutor(MySink, 'obj', None) |
243 | query_exec.logging_level = bt2.LoggingLevel.WARNING | |
244 | query_exec.query() | |
e9d0e821 | 245 | self.assertEqual(query_log_level, bt2.LoggingLevel.WARNING) |
999e7442 PP |
246 | del query_log_level |
247 | ||
70ad0c69 SM |
248 | def test_query_returns_none(self): |
249 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 250 | def _user_consume(self): |
8a08af82 SM |
251 | pass |
252 | ||
70ad0c69 | 253 | @staticmethod |
f6e305ce | 254 | def _user_query(priv_query_exec, obj, params, method_obj): |
70ad0c69 SM |
255 | return |
256 | ||
bf403eb2 | 257 | res = bt2.QueryExecutor(MySink, 'obj', None).query() |
70ad0c69 SM |
258 | self.assertIsNone(res) |
259 | ||
f6a5e476 PP |
260 | def test_query_simple(self): |
261 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 262 | def _user_consume(self): |
8a08af82 SM |
263 | pass |
264 | ||
f6a5e476 | 265 | @classmethod |
f6e305ce | 266 | def _user_query(cls, priv_query_exec, obj, params, method_obj): |
f6a5e476 PP |
267 | nonlocal query_params |
268 | query_params = params | |
269 | return 17.5 | |
270 | ||
271 | query_params = None | |
272 | params = ['coucou', 23, None] | |
bf403eb2 | 273 | res = bt2.QueryExecutor(MySink, 'obj', params).query() |
f6a5e476 PP |
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): | |
819d0ae7 | 280 | def _user_consume(self): |
8a08af82 SM |
281 | pass |
282 | ||
f6a5e476 | 283 | @classmethod |
f6e305ce | 284 | def _user_query(cls, priv_query_exec, obj, params, method_obj): |
f6a5e476 PP |
285 | nonlocal query_params |
286 | query_params = params | |
61d96b89 | 287 | return {'null': None, 'bt2': 'BT2'} |
f6a5e476 PP |
288 | |
289 | query_params = None | |
290 | params = { | |
291 | 'array': ['coucou', 23, None], | |
61d96b89 | 292 | 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5}, |
f6a5e476 PP |
293 | 'null': None, |
294 | } | |
295 | ||
bf403eb2 | 296 | res = bt2.QueryExecutor(MySink, 'obj', params).query() |
f6a5e476 | 297 | self.assertEqual(query_params, params) |
61d96b89 | 298 | self.assertEqual(res, {'null': None, 'bt2': 'BT2'}) |
f6a5e476 PP |
299 | del query_params |
300 | ||
301 | def test_eq(self): | |
302 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 303 | def _user_consume(self): |
8a08af82 SM |
304 | pass |
305 | ||
f6a5e476 PP |
306 | self.assertEqual(MySink, MySink) |
307 | ||
308 | ||
ede525fd | 309 | class ComponentClassTestCase(unittest.TestCase): |
f6a5e476 PP |
310 | def setUp(self): |
311 | class MySink(bt2._UserSinkComponent): | |
312 | ''' | |
313 | The description. | |
314 | ||
315 | The help. | |
316 | ''' | |
61d96b89 | 317 | |
819d0ae7 | 318 | def _user_consume(self): |
8a08af82 SM |
319 | pass |
320 | ||
f6a5e476 | 321 | @classmethod |
f6e305ce | 322 | def _user_query(cls, priv_query_exec, obj, params, method_obj): |
f6a5e476 PP |
323 | return [obj, params, 23] |
324 | ||
325 | self._py_comp_cls = MySink | |
326 | graph = bt2.Graph() | |
bc5c9924 | 327 | comp = graph.add_component(MySink, 'salut') |
c88be1c8 | 328 | self._comp_cls = comp.cls |
c946c9de | 329 | self.assertTrue(issubclass(type(self._comp_cls), bt2._SinkComponentClass)) |
f6a5e476 PP |
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): | |
bf403eb2 | 356 | res = bt2.QueryExecutor( |
61d96b89 | 357 | self._comp_cls, 'an object', {'yes': 'no', 'book': -17} |
bf403eb2 | 358 | ).query() |
f6a5e476 PP |
359 | expected = ['an object', {'yes': 'no', 'book': -17}, 23] |
360 | self.assertEqual(res, expected) |