|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | +from garf_core.query_parser import ( |
| 17 | + Customizer, |
| 18 | + ExtractedLineElements, |
| 19 | + GarfVirtualColumnError, |
| 20 | + SliceField, |
| 21 | + VirtualColumn, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +class TestExtractedLineElemens: |
| 26 | + @pytest.mark.parametrize( |
| 27 | + ('line', 'expected'), |
| 28 | + [ |
| 29 | + ( |
| 30 | + 'field AS column', |
| 31 | + ExtractedLineElements(field='field', alias='column'), |
| 32 | + ), |
| 33 | + ( |
| 34 | + 'field', |
| 35 | + ExtractedLineElements(field='field', alias='field'), |
| 36 | + ), |
| 37 | + ( |
| 38 | + '1 + 1 AS column', |
| 39 | + ExtractedLineElements( |
| 40 | + field=None, |
| 41 | + alias='column', |
| 42 | + virtual_column=VirtualColumn( |
| 43 | + type='expression', |
| 44 | + value='1 + 1', |
| 45 | + fields=[], |
| 46 | + substitute_expression='1 + 1', |
| 47 | + ), |
| 48 | + ), |
| 49 | + ), |
| 50 | + ( |
| 51 | + 'field + 1 AS column', |
| 52 | + ExtractedLineElements( |
| 53 | + field=None, |
| 54 | + alias='column', |
| 55 | + virtual_column=VirtualColumn( |
| 56 | + type='expression', |
| 57 | + value='field + 1', |
| 58 | + fields=['field'], |
| 59 | + substitute_expression='{field} + 1', |
| 60 | + ), |
| 61 | + ), |
| 62 | + ), |
| 63 | + ( |
| 64 | + 'field[0].value AS column', |
| 65 | + ExtractedLineElements( |
| 66 | + field='field', |
| 67 | + alias='column', |
| 68 | + customizer=Customizer( |
| 69 | + type='slice', |
| 70 | + value=SliceField(slice_literal=slice(0, 1, None), value='value'), |
| 71 | + ), |
| 72 | + ), |
| 73 | + ), |
| 74 | + ], |
| 75 | + ) |
| 76 | + def test_from_query_line_returns_correct_elements(self, line, expected): |
| 77 | + elements = ExtractedLineElements.from_query_line(line) |
| 78 | + assert elements == expected |
| 79 | + |
| 80 | + def test_from_query_line_raises_error_on_unaliased_virtual_column(self): |
| 81 | + with pytest.raises( |
| 82 | + GarfVirtualColumnError, |
| 83 | + match='Virtual attributes should be aliased: 1', |
| 84 | + ): |
| 85 | + ExtractedLineElements.from_query_line('1') |
0 commit comments