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