-
Notifications
You must be signed in to change notification settings - Fork 36
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
- Loading branch information
1 parent
60005a0
commit f16cd66
Showing
5 changed files
with
96 additions
and
11 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,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"], | ||
["ARRAY['a','b','c']", """'{"a", "b", "c"}'"""], | ||
["ARRAY[1,2,3]", """'{1, 2, 3}'"""], | ||
["'1'::numeric", "1"], | ||
[ | ||
"""'{"bar": "baz", "balance": 7.77, "active": false}'::json""", | ||
"""'{"bar": "baz", "balance": 7.77, "active": false}'""", | ||
], | ||
] | ||
|
||
@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(["unit-test", "--select", "my_model"]) | ||
except Exception: | ||
raise AssertionError(f"unit test failed when testing model with {sql_value}") | ||
|
||
|
||
class TestUnitTestingTypes(BaseUnitTestingTypes): | ||
pass |