Skip to content

Commit 367f3c2

Browse files
[executors] test: add e2e test for reading query from a file
1 parent d3a8b8d commit 367f3c2

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

libs/garf_executors/tests/end-to-end/test_cli.py

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,37 @@
2222

2323

2424
class TestApiQueryExecutor:
25-
def test_fake_source(self):
25+
query = (
26+
'SELECT resource, dimensions.name AS name, metrics.clicks AS clicks '
27+
'FROM resource'
28+
)
29+
expected_output = [
30+
{
31+
'resource': 'Campaign A',
32+
'name': 'Ad Group 1',
33+
'clicks': 1500,
34+
},
35+
{
36+
'resource': 'Campaign B',
37+
'name': 'Ad Group 2',
38+
'clicks': 2300,
39+
},
40+
{
41+
'resource': 'Campaign C',
42+
'name': 'Ad Group 3',
43+
'clicks': 800,
44+
},
45+
{
46+
'resource': 'Campaign A',
47+
'name': 'Ad Group 4',
48+
'clicks': 3200,
49+
},
50+
]
51+
52+
def test_fake_source_from_console(self):
2653
fake_data = _SCRIPT_PATH / 'test.json'
27-
query = (
28-
'SELECT resource, dimensions.name AS name, metrics.clicks AS clicks '
29-
'FROM resource'
30-
)
3154
command = (
32-
f'garf "{query}" --input console --source fake '
55+
f'garf "{self.query}" --input console --source fake '
3356
f'--source.data_location={fake_data} '
3457
'--output console --console.format=json '
3558
'--loglevel ERROR'
@@ -42,28 +65,27 @@ def test_fake_source(self):
4265
text=True,
4366
)
4467

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-
]
68+
assert result.returncode == 0
69+
assert json.loads(result.stdout) == self.expected_output
70+
71+
def test_fake_source_from_file(self, tmp_path):
72+
query_path = tmp_path / 'query.sql'
73+
with pathlib.Path.open(query_path, 'w', encoding='utf-8') as f:
74+
f.write(self.query)
75+
fake_data = _SCRIPT_PATH / 'test.json'
76+
command = (
77+
f'garf {str(query_path)} --source fake '
78+
f'--source.data_location={fake_data} '
79+
'--output console --console.format=json '
80+
'--loglevel ERROR'
81+
)
82+
result = subprocess.run(
83+
command,
84+
shell=True,
85+
check=False,
86+
capture_output=True,
87+
text=True,
88+
)
6789

6890
assert result.returncode == 0
69-
assert json.loads(result.stdout) == expected_output
91+
assert json.loads(result.stdout) == self.expected_output

0 commit comments

Comments
 (0)