Commit | Line | Data |
---|---|---|
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 | 19 | from bt2 import value |
c7eee084 PP |
20 | import unittest |
21 | import copy | |
22 | import bt2 | |
23 | ||
24 | ||
25 | class QueryExecutorTestCase(unittest.TestCase): | |
26 | def test_query(self): | |
27 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 28 | def _user_consume(self): |
a01b452b SM |
29 | pass |
30 | ||
c7eee084 | 31 | @classmethod |
3c729b9a | 32 | def _user_query(cls, priv_query_exec, obj, params): |
c7eee084 PP |
33 | nonlocal query_params |
34 | query_params = params | |
cfbd7cf3 | 35 | return {'null': None, 'bt2': 'BT2'} |
c7eee084 PP |
36 | |
37 | query_params = None | |
38 | params = { | |
39 | 'array': ['coucou', 23, None], | |
cfbd7cf3 | 40 | 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5}, |
c7eee084 PP |
41 | 'null': None, |
42 | } | |
43 | ||
3c729b9a | 44 | res = bt2.QueryExecutor(MySink, 'obj', params).query() |
c7eee084 | 45 | self.assertEqual(query_params, params) |
cfbd7cf3 | 46 | self.assertEqual(res, {'null': None, 'bt2': 'BT2'}) |
c7eee084 PP |
47 | del query_params |
48 | ||
49 | def test_query_params_none(self): | |
50 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 51 | def _user_consume(self): |
a01b452b SM |
52 | pass |
53 | ||
c7eee084 | 54 | @classmethod |
3c729b9a | 55 | def _user_query(cls, priv_query_exec, obj, params): |
c7eee084 PP |
56 | nonlocal query_params |
57 | query_params = params | |
58 | ||
59 | query_params = 23 | |
3c729b9a PP |
60 | res = bt2.QueryExecutor(MySink, 'obj', None).query() |
61 | self.assertIs(query_params, None) | |
62 | del query_params | |
63 | ||
64 | def test_query_no_params(self): | |
65 | class MySink(bt2._UserSinkComponent): | |
66 | def _user_consume(self): | |
67 | pass | |
68 | ||
69 | @classmethod | |
70 | def _user_query(cls, priv_query_exec, obj, params): | |
71 | nonlocal query_params | |
72 | query_params = params | |
73 | ||
74 | query_params = 23 | |
75 | res = bt2.QueryExecutor(MySink, 'obj').query() | |
76 | self.assertIs(query_params, None) | |
c7eee084 PP |
77 | del query_params |
78 | ||
761e1890 PP |
79 | def test_query_logging_level(self): |
80 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 81 | def _user_consume(self): |
a01b452b SM |
82 | pass |
83 | ||
761e1890 | 84 | @classmethod |
3c729b9a | 85 | def _user_query(cls, priv_query_exec, obj, params): |
761e1890 | 86 | nonlocal query_log_level |
3c729b9a | 87 | query_log_level = priv_query_exec.logging_level |
761e1890 PP |
88 | |
89 | query_log_level = None | |
3c729b9a PP |
90 | query_exec = bt2.QueryExecutor(MySink, 'obj', None) |
91 | query_exec.logging_level = bt2.LoggingLevel.INFO | |
92 | query_exec.query() | |
761e1890 PP |
93 | self.assertEqual(query_log_level, bt2.LoggingLevel.INFO) |
94 | del query_log_level | |
95 | ||
c7eee084 PP |
96 | def test_query_gen_error(self): |
97 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 98 | def _user_consume(self): |
a01b452b SM |
99 | pass |
100 | ||
c7eee084 | 101 | @classmethod |
3c729b9a | 102 | def _user_query(cls, priv_query_exec, obj, params): |
c7eee084 PP |
103 | raise ValueError |
104 | ||
694c792b | 105 | with self.assertRaises(bt2._Error) as ctx: |
3c729b9a | 106 | res = bt2.QueryExecutor(MySink, 'obj', [17, 23]).query() |
c7eee084 | 107 | |
ce4923b0 | 108 | exc = ctx.exception |
a635e507 | 109 | self.assertEqual(len(exc), 2) |
ce4923b0 | 110 | cause = exc[0] |
3fb99a22 | 111 | self.assertIsInstance(cause, bt2._ComponentClassErrorCause) |
ce4923b0 SM |
112 | self.assertIn('raise ValueError', cause.message) |
113 | self.assertEqual(cause.component_class_type, bt2.ComponentClassType.SINK) | |
114 | self.assertEqual(cause.component_class_name, 'MySink') | |
115 | ||
76b6c2f7 | 116 | def test_query_unknown_object(self): |
c7eee084 | 117 | class MySink(bt2._UserSinkComponent): |
6a91742b | 118 | def _user_consume(self): |
a01b452b SM |
119 | pass |
120 | ||
c7eee084 | 121 | @classmethod |
3c729b9a | 122 | def _user_query(cls, priv_query_exec, obj, params): |
76b6c2f7 | 123 | raise bt2.UnknownObject |
c7eee084 | 124 | |
76b6c2f7 | 125 | with self.assertRaises(bt2.UnknownObject): |
3c729b9a | 126 | res = bt2.QueryExecutor(MySink, 'obj', [17, 23]).query() |
c7eee084 | 127 | |
761e1890 PP |
128 | def test_query_logging_level_invalid_type(self): |
129 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 130 | def _user_consume(self): |
a01b452b SM |
131 | pass |
132 | ||
761e1890 | 133 | @classmethod |
3c729b9a | 134 | def _user_query(cls, priv_query_exec, obj, params): |
761e1890 PP |
135 | pass |
136 | ||
3c729b9a PP |
137 | query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23]) |
138 | ||
761e1890 | 139 | with self.assertRaises(TypeError): |
3c729b9a | 140 | query_exec.logging_level = 'yeah' |
761e1890 PP |
141 | |
142 | def test_query_logging_level_invalid_value(self): | |
143 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 144 | def _user_consume(self): |
a01b452b SM |
145 | pass |
146 | ||
761e1890 | 147 | @classmethod |
3c729b9a | 148 | def _user_query(cls, priv_query_exec, obj, params): |
761e1890 PP |
149 | pass |
150 | ||
3c729b9a PP |
151 | query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23]) |
152 | ||
761e1890 | 153 | with self.assertRaises(ValueError): |
3c729b9a | 154 | query_exec.logging_level = 12345 |
761e1890 | 155 | |
c7eee084 PP |
156 | def test_query_try_again(self): |
157 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 158 | def _user_consume(self): |
a01b452b SM |
159 | pass |
160 | ||
c7eee084 | 161 | @classmethod |
3c729b9a | 162 | def _user_query(cls, priv_query_exec, obj, params): |
c7eee084 PP |
163 | raise bt2.TryAgain |
164 | ||
165 | with self.assertRaises(bt2.TryAgain): | |
3c729b9a | 166 | res = bt2.QueryExecutor(MySink, 'obj', [17, 23]).query() |
c7eee084 | 167 | |
9b4f9b42 PP |
168 | def test_query_add_interrupter(self): |
169 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 170 | def _user_consume(self): |
9b4f9b42 PP |
171 | pass |
172 | ||
173 | @classmethod | |
3c729b9a | 174 | def _user_query(cls, priv_query_exec, obj, params): |
9b4f9b42 PP |
175 | nonlocal interrupter2 |
176 | test_self.assertFalse(query_exec.is_interrupted) | |
177 | interrupter2.set() | |
178 | test_self.assertTrue(query_exec.is_interrupted) | |
179 | interrupter2.reset() | |
180 | test_self.assertFalse(query_exec.is_interrupted) | |
181 | ||
182 | interrupter1 = bt2.Interrupter() | |
183 | interrupter2 = bt2.Interrupter() | |
184 | test_self = self | |
3c729b9a | 185 | query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23]) |
9b4f9b42 PP |
186 | query_exec.add_interrupter(interrupter1) |
187 | query_exec.add_interrupter(interrupter2) | |
3c729b9a | 188 | query_exec.query() |
c7eee084 | 189 | |
9b4f9b42 | 190 | def test_query_interrupt(self): |
c7eee084 | 191 | class MySink(bt2._UserSinkComponent): |
6a91742b | 192 | def _user_consume(self): |
a01b452b SM |
193 | pass |
194 | ||
c7eee084 | 195 | @classmethod |
3c729b9a | 196 | def _user_query(cls, priv_query_exec, obj, params): |
9b4f9b42 PP |
197 | test_self.assertFalse(query_exec.is_interrupted) |
198 | query_exec.interrupt() | |
199 | test_self.assertTrue(query_exec.is_interrupted) | |
c7eee084 | 200 | |
9b4f9b42 | 201 | test_self = self |
3c729b9a PP |
202 | query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23]) |
203 | query_exec.query() | |
204 | ||
205 | def test_query_priv_executor_invalid_after(self): | |
206 | class MySink(bt2._UserSinkComponent): | |
207 | def _user_consume(self): | |
208 | pass | |
209 | ||
210 | @classmethod | |
211 | def _user_query(cls, priv_query_exec, obj, params): | |
212 | nonlocal test_priv_query_exec | |
213 | test_priv_query_exec = priv_query_exec | |
214 | ||
215 | test_priv_query_exec = None | |
216 | query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23]) | |
217 | query_exec.query() | |
218 | assert test_priv_query_exec is not None | |
219 | ||
220 | with self.assertRaises(RuntimeError): | |
221 | test_priv_query_exec.logging_level | |
222 | ||
223 | del test_priv_query_exec |