@@ -25,6 +25,18 @@ class NestedResource(pydantic.BaseModel):
2525 nested_element : int
2626
2727
28+ class ArrayElement (pydantic .BaseModel ):
29+ element : int
30+
31+
32+ class FakeProtoMessage (pydantic .BaseModel ):
33+ resource : str
34+ resource_id : int
35+ resource_name : str
36+ resource_data : NestedResource
37+ array_data : list [ArrayElement ]
38+
39+
2840class TestDictParser :
2941 @pytest .fixture
3042 def test_parser (self ):
@@ -174,38 +186,6 @@ def test_parse_response_raises_customizer_error_on_invalid_resource(self):
174186 ):
175187 test_parser .parse_response (test_response )
176188
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-
209189 def test_parse_response_skips_omitted_columns (self ):
210190 test_specification = query_editor .QuerySpecification (
211191 'SELECT test_column_1 AS _, test_column_2 FROM test'
@@ -237,3 +217,58 @@ def test_parse_row_returns_converted_numeric_values(self, test_parser):
237217 expected_row = [1 ]
238218
239219 assert parsed_row == expected_row
220+
221+
222+ class TestProtoParser :
223+ @pytest .fixture
224+ def test_parser (self ):
225+ spec = query_editor .QuerySpecification (
226+ """
227+ SELECT
228+ resource_id,
229+ resource_id + 1 AS next_resource_id,
230+ resource_name,
231+ resource_data.nested_element,
232+ array_data[0].element AS slice_element
233+ FROM test
234+ """
235+ ).generate ()
236+ return parsers .ProtoParser (spec )
237+
238+ def test_parse_row_returns_converted_numeric_values (self , test_parser ):
239+ test_row = FakeProtoMessage (
240+ resource = 'resources/1/resource/99' ,
241+ resource_id = 1 ,
242+ resource_name = 'test' ,
243+ resource_data = NestedResource (nested_element = 10 ),
244+ array_data = [ArrayElement (element = 100 )],
245+ )
246+
247+ parsed_row = test_parser .parse_row (test_row )
248+ expected_row = [1 , 2 , 'test' , 10 , [100 ]]
249+
250+ assert parsed_row == expected_row
251+
252+ def test_parse_response_raises_customizer_error_on_missing_nested_attribute (
253+ self ,
254+ ):
255+ spec = query_editor .QuerySpecification (
256+ text = 'SELECT resource_data:missing_element AS column FROM test'
257+ ).generate ()
258+ test_parser = parsers .ProtoParser (spec )
259+ test_response = api_clients .GarfApiResponse (
260+ results = [
261+ FakeProtoMessage (
262+ resource = 'resources/1/resource/99' ,
263+ resource_id = 1 ,
264+ resource_name = 'test' ,
265+ resource_data = NestedResource (nested_element = 10 ),
266+ array_data = [ArrayElement (element = 100 )],
267+ )
268+ ]
269+ )
270+ with pytest .raises (
271+ query_parser .GarfFieldError ,
272+ match = 'field missing_element is missing in row resource_data' ,
273+ ):
274+ test_parser .parse_response (test_response )
0 commit comments