2 # Copyright (C) 2019 EfficiOS Inc.
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
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.
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.
25 class QueryExecutorTestCase(unittest
.TestCase
):
27 class MySink(bt2
._UserSinkComponent
):
28 def _user_consume(self
):
32 def _user_query(cls
, query_exec
, obj
, params
, log_level
):
35 return {'null': None, 'bt2': 'BT2'}
39 'array': ['coucou', 23, None],
40 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
44 res
= bt2
.QueryExecutor().query(MySink
, 'obj', params
)
45 self
.assertEqual(query_params
, params
)
46 self
.assertEqual(res
, {'null': None, 'bt2': 'BT2'})
49 def test_query_params_none(self
):
50 class MySink(bt2
._UserSinkComponent
):
51 def _user_consume(self
):
55 def _user_query(cls
, query_exec
, obj
, params
, log_level
):
60 res
= bt2
.QueryExecutor().query(MySink
, 'obj', None)
61 self
.assertEqual(query_params
, None)
64 def test_query_logging_level(self
):
65 class MySink(bt2
._UserSinkComponent
):
66 def _user_consume(self
):
70 def _user_query(cls
, query_exec
, obj
, params
, log_level
):
71 nonlocal query_log_level
72 query_log_level
= log_level
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
)
79 def test_query_gen_error(self
):
80 class MySink(bt2
._UserSinkComponent
):
81 def _user_consume(self
):
85 def _user_query(cls
, query_exec
, obj
, params
, log_level
):
88 with self
.assertRaises(bt2
._Error
) as ctx
:
89 res
= bt2
.QueryExecutor().query(MySink
, 'obj', [17, 23])
92 self
.assertEqual(len(exc
), 2)
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')
99 def test_query_unknown_object(self
):
100 class MySink(bt2
._UserSinkComponent
):
101 def _user_consume(self
):
105 def _user_query(cls
, query_exec
, obj
, params
, log_level
):
106 raise bt2
.UnknownObject
108 with self
.assertRaises(bt2
.UnknownObject
):
109 res
= bt2
.QueryExecutor().query(MySink
, 'obj', [17, 23])
111 def test_query_logging_level_invalid_type(self
):
112 class MySink(bt2
._UserSinkComponent
):
113 def _user_consume(self
):
117 def _user_query(cls
, query_exec
, obj
, params
, log_level
):
120 with self
.assertRaises(TypeError):
121 res
= bt2
.QueryExecutor().query(MySink
, 'obj', [17, 23], 'yeah')
123 def test_query_logging_level_invalid_value(self
):
124 class MySink(bt2
._UserSinkComponent
):
125 def _user_consume(self
):
129 def _user_query(cls
, query_exec
, obj
, params
, log_level
):
132 with self
.assertRaises(ValueError):
133 res
= bt2
.QueryExecutor().query(MySink
, 'obj', [17, 23], 12345)
135 def test_query_try_again(self
):
136 class MySink(bt2
._UserSinkComponent
):
137 def _user_consume(self
):
141 def _user_query(cls
, query_exec
, obj
, params
, log_level
):
144 with self
.assertRaises(bt2
.TryAgain
):
145 res
= bt2
.QueryExecutor().query(MySink
, 'obj', [17, 23])
147 def test_query_add_interrupter(self
):
148 class MySink(bt2
._UserSinkComponent
):
149 def _user_consume(self
):
153 def _user_query(cls
, query_exec
, obj
, params
, log_level
):
154 nonlocal interrupter2
155 test_self
.assertFalse(query_exec
.is_interrupted
)
157 test_self
.assertTrue(query_exec
.is_interrupted
)
159 test_self
.assertFalse(query_exec
.is_interrupted
)
161 interrupter1
= bt2
.Interrupter()
162 interrupter2
= bt2
.Interrupter()
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])
169 def test_query_interrupt(self
):
170 class MySink(bt2
._UserSinkComponent
):
171 def _user_consume(self
):
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
)
181 query_exec
= bt2
.QueryExecutor()
182 query_exec
.query(MySink
, 'obj', [17, 23])