-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
case-insensitive comparisons in unit testing, base unit testing test (#…
…55) Co-authored-by: Mike Alfare <[email protected]>
- Loading branch information
1 parent
cb08aae
commit c30497e
Showing
8 changed files
with
244 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from pkgutil import extend_path | ||
|
||
__path__ = extend_path(__path__, __name__) | ||
__path__ = extend_path(__path__, __name__) |
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,7 @@ | ||
{% macro cast(field, type) %} | ||
{{ return(adapter.dispatch('cast', 'dbt') (field, type)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__cast(field, type) %} | ||
cast({{field}} as {{type}}) | ||
{% endmacro %} |
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,49 @@ | ||
import pytest | ||
from dbt.tests.util import run_dbt | ||
|
||
|
||
my_model_sql = """ | ||
select | ||
tested_column from {{ ref('my_upstream_model')}} | ||
""" | ||
|
||
my_upstream_model_sql = """ | ||
select 1 as tested_column | ||
""" | ||
|
||
test_my_model_yml = """ | ||
unit_tests: | ||
- name: test_my_model | ||
model: my_model | ||
given: | ||
- input: ref('my_upstream_model') | ||
rows: | ||
- {tested_column: 1} | ||
- {TESTED_COLUMN: 2} | ||
- {tested_colUmn: 3} | ||
expect: | ||
rows: | ||
- {tested_column: 1} | ||
- {TESTED_COLUMN: 2} | ||
- {tested_colUmn: 3} | ||
""" | ||
|
||
|
||
class BaseUnitTestCaseInsensivity: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_model.sql": my_model_sql, | ||
"my_upstream_model.sql": my_upstream_model_sql, | ||
"unit_tests.yml": test_my_model_yml, | ||
} | ||
|
||
def test_case_insensitivity(self, project): | ||
results = run_dbt(["run"]) | ||
assert len(results) == 2 | ||
|
||
results = run_dbt(["test"]) | ||
|
||
|
||
class TestPosgresUnitTestCaseInsensitivity(BaseUnitTestCaseInsensivity): | ||
pass |
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,62 @@ | ||
import pytest | ||
from dbt.tests.util import run_dbt, run_dbt_and_capture | ||
|
||
|
||
my_model_sql = """ | ||
select | ||
tested_column from {{ ref('my_upstream_model')}} | ||
""" | ||
|
||
my_upstream_model_sql = """ | ||
select 1 as tested_column | ||
""" | ||
|
||
test_my_model_yml = """ | ||
unit_tests: | ||
- name: test_invalid_input_column_name | ||
model: my_model | ||
given: | ||
- input: ref('my_upstream_model') | ||
rows: | ||
- {invalid_column_name: 1} | ||
expect: | ||
rows: | ||
- {tested_column: 1} | ||
- name: test_invalid_expect_column_name | ||
model: my_model | ||
given: | ||
- input: ref('my_upstream_model') | ||
rows: | ||
- {tested_column: 1} | ||
expect: | ||
rows: | ||
- {invalid_column_name: 1} | ||
""" | ||
|
||
|
||
class BaseUnitTestInvalidInput: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_model.sql": my_model_sql, | ||
"my_upstream_model.sql": my_upstream_model_sql, | ||
"unit_tests.yml": test_my_model_yml, | ||
} | ||
|
||
def test_invalid_input(self, project): | ||
results = run_dbt(["run"]) | ||
assert len(results) == 2 | ||
|
||
_, out = run_dbt_and_capture( | ||
["test", "--select", "test_name:test_invalid_input_column_name"], expect_pass=False | ||
) | ||
assert "Invalid column name: 'invalid_column_name' in unit test fixture for 'my_upstream_model'." in out | ||
|
||
_, out = run_dbt_and_capture( | ||
["test", "--select", "test_name:test_invalid_expect_column_name"], expect_pass=False | ||
) | ||
assert "Invalid column name: 'invalid_column_name' in unit test fixture for expected output." in out | ||
|
||
|
||
class TestPostgresUnitTestInvalidInput(BaseUnitTestInvalidInput): | ||
pass |
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,84 @@ | ||
import pytest | ||
|
||
from dbt.tests.util import write_file, run_dbt | ||
|
||
my_model_sql = """ | ||
select | ||
tested_column from {{ ref('my_upstream_model')}} | ||
""" | ||
|
||
my_upstream_model_sql = """ | ||
select | ||
{sql_value} as tested_column | ||
""" | ||
|
||
test_my_model_yml = """ | ||
unit_tests: | ||
- name: test_my_model | ||
model: my_model | ||
given: | ||
- input: ref('my_upstream_model') | ||
rows: | ||
- {{ tested_column: {yaml_value} }} | ||
expect: | ||
rows: | ||
- {{ tested_column: {yaml_value} }} | ||
""" | ||
|
||
|
||
class BaseUnitTestingTypes: | ||
@pytest.fixture | ||
def data_types(self): | ||
# sql_value, yaml_value | ||
return [ | ||
["1", "1"], | ||
["'1'", "1"], | ||
["true", "true"], | ||
["DATE '2020-01-02'", "2020-01-02"], | ||
["TIMESTAMP '2013-11-03 00:00:00-0'", "2013-11-03 00:00:00-0"], | ||
["TIMESTAMPTZ '2013-11-03 00:00:00-0'", "2013-11-03 00:00:00-0"], | ||
["'1'::numeric", "1"], | ||
[ | ||
"""'{"bar": "baz", "balance": 7.77, "active": false}'::json""", | ||
"""'{"bar": "baz", "balance": 7.77, "active": false}'""", | ||
], | ||
# TODO: support complex types | ||
# ["ARRAY['a','b','c']", """'{"a", "b", "c"}'"""], | ||
# ["ARRAY[1,2,3]", """'{1, 2, 3}'"""], | ||
] | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_model.sql": my_model_sql, | ||
"my_upstream_model.sql": my_upstream_model_sql, | ||
"schema.yml": test_my_model_yml, | ||
} | ||
|
||
def test_unit_test_data_type(self, project, data_types): | ||
for sql_value, yaml_value in data_types: | ||
# Write parametrized type value to sql files | ||
write_file( | ||
my_upstream_model_sql.format(sql_value=sql_value), | ||
"models", | ||
"my_upstream_model.sql", | ||
) | ||
|
||
# Write parametrized type value to unit test yaml definition | ||
write_file( | ||
test_my_model_yml.format(yaml_value=yaml_value), | ||
"models", | ||
"schema.yml", | ||
) | ||
|
||
results = run_dbt(["run", "--select", "my_upstream_model"]) | ||
assert len(results) == 1 | ||
|
||
try: | ||
run_dbt(["test", "--select", "my_model"]) | ||
except Exception: | ||
raise AssertionError(f"unit test failed when testing model with {sql_value}") | ||
|
||
|
||
class TestPostgresUnitTestingTypes(BaseUnitTestingTypes): | ||
pass |