-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update tests and docs regarding availability of the argument query_mo…
…difier (#1914) Although we `transform` and `transform_file` supported the `query_modifier` argument, we didn't have tests nor documentation describing this. This PR adds tests and docs for these use cases.
- Loading branch information
Showing
4 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import tempfile | ||
from unittest import mock | ||
|
||
from astro import sql as aql | ||
from tests.sql.operators import utils as test_utils | ||
|
||
|
||
class MockReturn: | ||
_scalar = [] | ||
|
||
def scalar(self): | ||
return self._scalar | ||
|
||
|
||
@mock.patch("astro.databases.base.BaseDatabase.connection") | ||
def test_transform_calls_with_query_tag(run_sql, sample_dag): | ||
from astro.query_modifier import QueryModifier | ||
|
||
run_sql.execute.return_value = MockReturn() | ||
|
||
with sample_dag: | ||
|
||
@aql.transform( | ||
conn_id="sqlite_default", | ||
query_modifier=QueryModifier(pre_queries=["ALTER team_1", "ALTER team_2"]), | ||
) | ||
def dummy_method(): | ||
return "SELECT 1+1" | ||
|
||
dummy_method() | ||
|
||
test_utils.run_dag(sample_dag) | ||
enriched_query = run_sql.method_calls[1].args[0].text | ||
assert enriched_query.startswith("ALTER team_1;ALTER team_2;CREATE TABLE IF NOT EXISTS ") | ||
assert enriched_query.endswith("AS SELECT 1+1") | ||
|
||
|
||
@mock.patch("astro.databases.base.BaseDatabase.connection") | ||
def test_transform_file_calls_with_query_tag(run_sql, sample_dag): | ||
from astro.query_modifier import QueryModifier | ||
|
||
run_sql.execute.return_value = MockReturn() | ||
|
||
with tempfile.NamedTemporaryFile(suffix=".sql") as tmp_file: | ||
tmp_file.write(b"SELECT 1+1") | ||
tmp_file.flush() | ||
|
||
with sample_dag: | ||
aql.transform_file( | ||
file_path=tmp_file.name, | ||
conn_id="sqlite_default", | ||
query_modifier=QueryModifier(pre_queries=["ALTER team_1", "ALTER team_2"]), | ||
) | ||
test_utils.run_dag(sample_dag) | ||
|
||
enriched_query = run_sql.method_calls[1].args[0].text | ||
assert enriched_query.startswith("ALTER team_1;ALTER team_2;CREATE TABLE IF NOT EXISTS ") | ||
assert enriched_query.endswith("AS SELECT 1+1") |