|
| 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 | +# pylint: disable=C0330, g-bad-import-order, g-multiple-import, missing-class-docstring, missing-module-docstring, missing-function-docstring |
| 16 | + |
| 17 | +import json |
| 18 | +import pathlib |
| 19 | +import subprocess |
| 20 | + |
| 21 | +_SCRIPT_PATH = pathlib.Path(__file__).parent |
| 22 | + |
| 23 | + |
| 24 | +class TestApiQueryExecutor: |
| 25 | + def test_fake_source(self): |
| 26 | + fake_data = _SCRIPT_PATH / 'test.json' |
| 27 | + query = ( |
| 28 | + 'SELECT resource, dimensions.name AS name, metrics.clicks AS clicks ' |
| 29 | + 'FROM resource' |
| 30 | + ) |
| 31 | + command = ( |
| 32 | + f'garf "{query}" --input console --source fake ' |
| 33 | + f'--source.data_location={fake_data} ' |
| 34 | + '--output console --console.format=json ' |
| 35 | + '--loglevel ERROR' |
| 36 | + ) |
| 37 | + result = subprocess.run( |
| 38 | + command, |
| 39 | + shell=True, |
| 40 | + check=False, |
| 41 | + capture_output=True, |
| 42 | + text=True, |
| 43 | + ) |
| 44 | + |
| 45 | + expected_output = [ |
| 46 | + { |
| 47 | + 'resource': 'Campaign A', |
| 48 | + 'name': 'Ad Group 1', |
| 49 | + 'clicks': 1500, |
| 50 | + }, |
| 51 | + { |
| 52 | + 'resource': 'Campaign B', |
| 53 | + 'name': 'Ad Group 2', |
| 54 | + 'clicks': 2300, |
| 55 | + }, |
| 56 | + { |
| 57 | + 'resource': 'Campaign C', |
| 58 | + 'name': 'Ad Group 3', |
| 59 | + 'clicks': 800, |
| 60 | + }, |
| 61 | + { |
| 62 | + 'resource': 'Campaign A', |
| 63 | + 'name': 'Ad Group 4', |
| 64 | + 'clicks': 3200, |
| 65 | + }, |
| 66 | + ] |
| 67 | + |
| 68 | + assert result.returncode == 0 |
| 69 | + assert json.loads(result.stdout) == expected_output |
0 commit comments