| 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 _consume(self): |
| 29 | pass |
| 30 | |
| 31 | def _graph_is_configured(self): |
| 32 | pass |
| 33 | |
| 34 | @classmethod |
| 35 | def _query(cls, query_exec, obj, params, log_level): |
| 36 | nonlocal query_params |
| 37 | query_params = params |
| 38 | return {'null': None, 'bt2': 'BT2'} |
| 39 | |
| 40 | query_params = None |
| 41 | params = { |
| 42 | 'array': ['coucou', 23, None], |
| 43 | 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5}, |
| 44 | 'null': None, |
| 45 | } |
| 46 | |
| 47 | res = bt2.QueryExecutor().query(MySink, 'obj', params) |
| 48 | self.assertEqual(query_params, params) |
| 49 | self.assertEqual(res, {'null': None, 'bt2': 'BT2'}) |
| 50 | del query_params |
| 51 | |
| 52 | def test_query_params_none(self): |
| 53 | class MySink(bt2._UserSinkComponent): |
| 54 | def _consume(self): |
| 55 | pass |
| 56 | |
| 57 | def _graph_is_configured(self): |
| 58 | pass |
| 59 | |
| 60 | @classmethod |
| 61 | def _query(cls, query_exec, obj, params, log_level): |
| 62 | nonlocal query_params |
| 63 | query_params = params |
| 64 | |
| 65 | query_params = 23 |
| 66 | res = bt2.QueryExecutor().query(MySink, 'obj', None) |
| 67 | self.assertEqual(query_params, None) |
| 68 | del query_params |
| 69 | |
| 70 | def test_query_logging_level(self): |
| 71 | class MySink(bt2._UserSinkComponent): |
| 72 | def _consume(self): |
| 73 | pass |
| 74 | |
| 75 | def _graph_is_configured(self): |
| 76 | pass |
| 77 | |
| 78 | @classmethod |
| 79 | def _query(cls, query_exec, obj, params, log_level): |
| 80 | nonlocal query_log_level |
| 81 | query_log_level = log_level |
| 82 | |
| 83 | query_log_level = None |
| 84 | res = bt2.QueryExecutor().query(MySink, 'obj', None, bt2.LoggingLevel.INFO) |
| 85 | self.assertEqual(query_log_level, bt2.LoggingLevel.INFO) |
| 86 | del query_log_level |
| 87 | |
| 88 | def test_query_gen_error(self): |
| 89 | class MySink(bt2._UserSinkComponent): |
| 90 | def _consume(self): |
| 91 | pass |
| 92 | |
| 93 | def _graph_is_configured(self): |
| 94 | pass |
| 95 | |
| 96 | @classmethod |
| 97 | def _query(cls, query_exec, obj, params, log_level): |
| 98 | raise ValueError |
| 99 | |
| 100 | with self.assertRaises(bt2.Error): |
| 101 | res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23]) |
| 102 | |
| 103 | def test_query_invalid_object(self): |
| 104 | class MySink(bt2._UserSinkComponent): |
| 105 | def _consume(self): |
| 106 | pass |
| 107 | |
| 108 | def _graph_is_configured(self): |
| 109 | pass |
| 110 | |
| 111 | @classmethod |
| 112 | def _query(cls, query_exec, obj, params, log_level): |
| 113 | raise bt2.InvalidObject |
| 114 | |
| 115 | with self.assertRaises(bt2.InvalidObject): |
| 116 | res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23]) |
| 117 | |
| 118 | def test_query_logging_level_invalid_type(self): |
| 119 | class MySink(bt2._UserSinkComponent): |
| 120 | def _consume(self): |
| 121 | pass |
| 122 | |
| 123 | def _graph_is_configured(self): |
| 124 | pass |
| 125 | |
| 126 | @classmethod |
| 127 | def _query(cls, query_exec, obj, params, log_level): |
| 128 | pass |
| 129 | |
| 130 | with self.assertRaises(TypeError): |
| 131 | res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23], 'yeah') |
| 132 | |
| 133 | def test_query_logging_level_invalid_value(self): |
| 134 | class MySink(bt2._UserSinkComponent): |
| 135 | def _consume(self): |
| 136 | pass |
| 137 | |
| 138 | def _graph_is_configured(self): |
| 139 | pass |
| 140 | |
| 141 | @classmethod |
| 142 | def _query(cls, query_exec, obj, params, log_level): |
| 143 | pass |
| 144 | |
| 145 | with self.assertRaises(ValueError): |
| 146 | res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23], 12345) |
| 147 | |
| 148 | def test_query_invalid_params(self): |
| 149 | class MySink(bt2._UserSinkComponent): |
| 150 | def _consume(self): |
| 151 | pass |
| 152 | |
| 153 | def _graph_is_configured(self): |
| 154 | pass |
| 155 | |
| 156 | @classmethod |
| 157 | def _query(cls, query_exec, obj, params, log_level): |
| 158 | raise bt2.InvalidParams |
| 159 | |
| 160 | with self.assertRaises(bt2.InvalidParams): |
| 161 | res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23]) |
| 162 | |
| 163 | def test_query_try_again(self): |
| 164 | class MySink(bt2._UserSinkComponent): |
| 165 | def _consume(self): |
| 166 | pass |
| 167 | |
| 168 | def _graph_is_configured(self): |
| 169 | pass |
| 170 | |
| 171 | @classmethod |
| 172 | def _query(cls, query_exec, obj, params, log_level): |
| 173 | raise bt2.TryAgain |
| 174 | |
| 175 | with self.assertRaises(bt2.TryAgain): |
| 176 | res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23]) |
| 177 | |
| 178 | def test_cancel_no_query(self): |
| 179 | query_exec = bt2.QueryExecutor() |
| 180 | self.assertFalse(query_exec.is_canceled) |
| 181 | query_exec.cancel() |
| 182 | self.assertTrue(query_exec.is_canceled) |
| 183 | |
| 184 | def test_query_canceled(self): |
| 185 | class MySink(bt2._UserSinkComponent): |
| 186 | def _consume(self): |
| 187 | pass |
| 188 | |
| 189 | def _graph_is_configured(self): |
| 190 | pass |
| 191 | |
| 192 | @classmethod |
| 193 | def _query(cls, query_exec, obj, params, log_level): |
| 194 | raise bt2.TryAgain |
| 195 | |
| 196 | query_exec = bt2.QueryExecutor() |
| 197 | query_exec.cancel() |
| 198 | |
| 199 | with self.assertRaises(bt2.Canceled): |
| 200 | res = query_exec.query(MySink, 'obj', [17, 23]) |