lib: rename INVALID_OBJECT status to UNKNOWN_OBJECT
[babeltrace.git] / tests / bindings / python / bt2 / test_query_executor.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 from bt2 import value
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):
28 def _user_consume(self):
29 pass
30
31 @classmethod
32 def _user_query(cls, query_exec, obj, params, log_level):
33 nonlocal query_params
34 query_params = params
35 return {'null': None, 'bt2': 'BT2'}
36
37 query_params = None
38 params = {
39 'array': ['coucou', 23, None],
40 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
41 'null': None,
42 }
43
44 res = bt2.QueryExecutor().query(MySink, 'obj', params)
45 self.assertEqual(query_params, params)
46 self.assertEqual(res, {'null': None, 'bt2': 'BT2'})
47 del query_params
48
49 def test_query_params_none(self):
50 class MySink(bt2._UserSinkComponent):
51 def _user_consume(self):
52 pass
53
54 @classmethod
55 def _user_query(cls, query_exec, obj, params, log_level):
56 nonlocal query_params
57 query_params = params
58
59 query_params = 23
60 res = bt2.QueryExecutor().query(MySink, 'obj', None)
61 self.assertEqual(query_params, None)
62 del query_params
63
64 def test_query_logging_level(self):
65 class MySink(bt2._UserSinkComponent):
66 def _user_consume(self):
67 pass
68
69 @classmethod
70 def _user_query(cls, query_exec, obj, params, log_level):
71 nonlocal query_log_level
72 query_log_level = log_level
73
74 query_log_level = None
75 res = bt2.QueryExecutor().query(MySink, 'obj', None, bt2.LoggingLevel.INFO)
76 self.assertEqual(query_log_level, bt2.LoggingLevel.INFO)
77 del query_log_level
78
79 def test_query_gen_error(self):
80 class MySink(bt2._UserSinkComponent):
81 def _user_consume(self):
82 pass
83
84 @classmethod
85 def _user_query(cls, query_exec, obj, params, log_level):
86 raise ValueError
87
88 with self.assertRaises(bt2._Error) as ctx:
89 res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23])
90
91 exc = ctx.exception
92 self.assertEqual(len(exc), 2)
93 cause = exc[0]
94 self.assertIsInstance(cause, bt2._ComponentClassErrorCause)
95 self.assertIn('raise ValueError', cause.message)
96 self.assertEqual(cause.component_class_type, bt2.ComponentClassType.SINK)
97 self.assertEqual(cause.component_class_name, 'MySink')
98
99 def test_query_unknown_object(self):
100 class MySink(bt2._UserSinkComponent):
101 def _user_consume(self):
102 pass
103
104 @classmethod
105 def _user_query(cls, query_exec, obj, params, log_level):
106 raise bt2.UnknownObject
107
108 with self.assertRaises(bt2.UnknownObject):
109 res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23])
110
111 def test_query_logging_level_invalid_type(self):
112 class MySink(bt2._UserSinkComponent):
113 def _user_consume(self):
114 pass
115
116 @classmethod
117 def _user_query(cls, query_exec, obj, params, log_level):
118 pass
119
120 with self.assertRaises(TypeError):
121 res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23], 'yeah')
122
123 def test_query_logging_level_invalid_value(self):
124 class MySink(bt2._UserSinkComponent):
125 def _user_consume(self):
126 pass
127
128 @classmethod
129 def _user_query(cls, query_exec, obj, params, log_level):
130 pass
131
132 with self.assertRaises(ValueError):
133 res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23], 12345)
134
135 def test_query_try_again(self):
136 class MySink(bt2._UserSinkComponent):
137 def _user_consume(self):
138 pass
139
140 @classmethod
141 def _user_query(cls, query_exec, obj, params, log_level):
142 raise bt2.TryAgain
143
144 with self.assertRaises(bt2.TryAgain):
145 res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23])
146
147 def test_query_add_interrupter(self):
148 class MySink(bt2._UserSinkComponent):
149 def _user_consume(self):
150 pass
151
152 @classmethod
153 def _user_query(cls, query_exec, obj, params, log_level):
154 nonlocal interrupter2
155 test_self.assertFalse(query_exec.is_interrupted)
156 interrupter2.set()
157 test_self.assertTrue(query_exec.is_interrupted)
158 interrupter2.reset()
159 test_self.assertFalse(query_exec.is_interrupted)
160
161 interrupter1 = bt2.Interrupter()
162 interrupter2 = bt2.Interrupter()
163 test_self = self
164 query_exec = bt2.QueryExecutor()
165 query_exec.add_interrupter(interrupter1)
166 query_exec.add_interrupter(interrupter2)
167 query_exec.query(MySink, 'obj', [17, 23])
168
169 def test_query_interrupt(self):
170 class MySink(bt2._UserSinkComponent):
171 def _user_consume(self):
172 pass
173
174 @classmethod
175 def _user_query(cls, query_exec, obj, params, log_level):
176 test_self.assertFalse(query_exec.is_interrupted)
177 query_exec.interrupt()
178 test_self.assertTrue(query_exec.is_interrupted)
179
180 test_self = self
181 query_exec = bt2.QueryExecutor()
182 query_exec.query(MySink, 'obj', [17, 23])
This page took 0.032849 seconds and 4 git commands to generate.