|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import pydantic |
15 | 16 | import pytest |
16 | | -from garf_core import api_clients, parsers, query_editor |
| 17 | +from garf_core import api_clients, parsers, query_editor, query_parser |
17 | 18 |
|
18 | 19 | test_specification = query_editor.QuerySpecification( |
19 | 20 | 'SELECT test_column_1 FROM test' |
20 | 21 | ).generate() |
21 | 22 |
|
22 | 23 |
|
| 24 | +class NestedResource(pydantic.BaseModel): |
| 25 | + nested_element: int |
| 26 | + |
| 27 | + |
23 | 28 | class TestDictParser: |
24 | 29 | @pytest.fixture |
25 | 30 | def test_parser(self): |
@@ -117,6 +122,90 @@ def test_parse_response_returns_correct_result_for_arrays( |
117 | 122 |
|
118 | 123 | assert parsed_row == expected_row |
119 | 124 |
|
| 125 | + @pytest.mark.parametrize( |
| 126 | + ('index', 'expected'), |
| 127 | + [ |
| 128 | + ('0', [[0], [1]]), |
| 129 | + ('1', [['TEXT'], ['IMAGE']]), |
| 130 | + ('2', [[54321], [12345]]), |
| 131 | + ], |
| 132 | + ) |
| 133 | + def test_parse_response_returns_correct_resource_index(self, index, expected): |
| 134 | + spec = query_editor.QuerySpecification( |
| 135 | + text=f'SELECT resource~{index} AS column FROM test' |
| 136 | + ).generate() |
| 137 | + test_parser = parsers.DictParser(spec) |
| 138 | + test_response = api_clients.GarfApiResponse( |
| 139 | + results=[ |
| 140 | + {'resource': 'resource/1/test/0~TEXT~54321'}, |
| 141 | + {'resource': 'resource/1/test/1~IMAGE~12345'}, |
| 142 | + ] |
| 143 | + ) |
| 144 | + parsed_row = test_parser.parse_response(test_response) |
| 145 | + assert parsed_row == expected |
| 146 | + |
| 147 | + def test_parse_response_raises_customizer_error_on_invalid_position(self): |
| 148 | + spec = query_editor.QuerySpecification( |
| 149 | + text='SELECT resource~4 AS column FROM test' |
| 150 | + ).generate() |
| 151 | + test_parser = parsers.DictParser(spec) |
| 152 | + test_response = api_clients.GarfApiResponse( |
| 153 | + results=[ |
| 154 | + {'resource': 'resource/1/test/0~TEXT~54321'}, |
| 155 | + ] |
| 156 | + ) |
| 157 | + with pytest.raises( |
| 158 | + query_parser.GarfCustomizerError, match='Not a valid position in resource' |
| 159 | + ): |
| 160 | + test_parser.parse_response(test_response) |
| 161 | + |
| 162 | + def test_parse_response_raises_customizer_error_on_invalid_resource(self): |
| 163 | + spec = query_editor.QuerySpecification( |
| 164 | + text='SELECT resource~0 AS column FROM test' |
| 165 | + ).generate() |
| 166 | + test_parser = parsers.DictParser(spec) |
| 167 | + test_response = api_clients.GarfApiResponse( |
| 168 | + results=[ |
| 169 | + {'resource': 'resource'}, |
| 170 | + ] |
| 171 | + ) |
| 172 | + with pytest.raises( |
| 173 | + query_parser.GarfCustomizerError, match='Not a valid resource' |
| 174 | + ): |
| 175 | + test_parser.parse_response(test_response) |
| 176 | + |
| 177 | + def test_parse_response_returns_correct_nested_attribute(self): |
| 178 | + spec = query_editor.QuerySpecification( |
| 179 | + text='SELECT resource:nested_element AS column FROM test' |
| 180 | + ).generate() |
| 181 | + test_parser = parsers.DictParser(spec) |
| 182 | + test_response = api_clients.GarfApiResponse( |
| 183 | + results=[ |
| 184 | + {'resource': NestedResource(nested_element=1)}, |
| 185 | + {'resource': NestedResource(nested_element=2)}, |
| 186 | + ] |
| 187 | + ) |
| 188 | + parsed_row = test_parser.parse_response(test_response) |
| 189 | + assert parsed_row == [[1], [2]] |
| 190 | + |
| 191 | + def test_parse_response_raises_customizer_error_on_missing_nested_attribute( |
| 192 | + self, |
| 193 | + ): |
| 194 | + spec = query_editor.QuerySpecification( |
| 195 | + text='SELECT resource:missing_element AS column FROM test' |
| 196 | + ).generate() |
| 197 | + test_parser = parsers.DictParser(spec) |
| 198 | + test_response = api_clients.GarfApiResponse( |
| 199 | + results=[ |
| 200 | + {'resource': NestedResource(nested_element=1)}, |
| 201 | + ] |
| 202 | + ) |
| 203 | + with pytest.raises( |
| 204 | + query_parser.GarfCustomizerError, |
| 205 | + match='nested field missing_element is missing in row', |
| 206 | + ): |
| 207 | + test_parser.parse_response(test_response) |
| 208 | + |
120 | 209 | def test_parse_response_skips_omitted_columns(self): |
121 | 210 | test_specification = query_editor.QuerySpecification( |
122 | 211 | 'SELECT test_column_1 AS _, test_column_2 FROM test' |
|
0 commit comments