1414from __future__ import annotations
1515
1616import json
17- import os
17+ import pathlib
1818
1919import pytest
2020
2121from garf_core import api_clients , parsers , report_fetcher
22+ from garf_core .fetchers import fake as fake_fetcher
2223from garf_executors import api_executor
2324from garf_io .writers import json_writer
2425
26+ _TEST_DATA = [
27+ {'customer_id' : 1 },
28+ {'customer_id' : 2 },
29+ {'customer_id' : 3 },
30+ ]
31+
32+ _TEST_QUERY = 'SELECT customer.id FROM customer'
33+
2534
2635class TestApiQueryExecutor :
2736 @pytest .fixture
@@ -43,27 +52,37 @@ def test_json_writer(self, tmp_path):
4352 return json_writer .JsonWriter (destination_folder = tmp_path )
4453
4554 def test_execute_returns_success (self , executor , tmp_path ):
46- query_text = 'SELECT customer.id FROM customer'
47- expected_result = [
48- {'customer_id' : 1 },
49- {'customer_id' : 2 },
50- {'customer_id' : 3 },
51- ]
52-
5355 context = api_executor .ApiExecutionContext (
5456 writer = 'json' ,
5557 writer_parameters = {'destination_folder' : str (tmp_path )},
5658 )
5759 executor .execute (
58- query = query_text ,
60+ query = _TEST_QUERY ,
5961 title = 'test' ,
6062 context = context ,
6163 )
62- with open (
63- os . path . join (context .writer_client .destination_folder , 'test.json' ) ,
64+ with pathlib . Path . open (
65+ pathlib . Path (context .writer_client .destination_folder ) / 'test.json' ,
6466 'r' ,
6567 encoding = 'utf-8' ,
6668 ) as f :
6769 result = json .load (f )
6870
69- assert result == expected_result
71+ assert result == _TEST_DATA
72+
73+ def test_from_fetcher_alias_returns_initialized_executor (self , tmp_path ):
74+ tmp_file = tmp_path / 'test.json'
75+ with pathlib .Path .open (
76+ tmp_file ,
77+ 'w' ,
78+ encoding = 'utf-8' ,
79+ ) as f :
80+ json .dump (_TEST_DATA , f )
81+
82+ executor = api_executor .ApiQueryExecutor .from_fetcher_alias (
83+ source = 'fake' ,
84+ fetcher_parameters = {
85+ 'json_location' : tmp_file ,
86+ },
87+ )
88+ assert isinstance (executor .fetcher , fake_fetcher .FakeApiReportFetcher )
0 commit comments