Skip to content

Commit

Permalink
add adapter-zone test, expand data type coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Nov 21, 2023
1 parent 827e35e commit 8197fa7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
11 changes: 8 additions & 3 deletions plugins/postgres/dbt/adapters/postgres/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@


class PostgresColumn(Column):
_DTYPE_ARRAY_TO_DATA_TYPE = {"stringarray": "text[]"}
_DTYPE_TO_DATA_TYPE = {
"stringarray": "text[]",
"integerarray": "int[]",
"datetime": "timestamp",
"datetimetz": "timestamptz",
}

@property
def data_type(self):
Expand All @@ -12,7 +17,7 @@ def data_type(self):
):
return self.dtype

if self.dtype.lower() in self._DTYPE_ARRAY_TO_DATA_TYPE:
return self._DTYPE_ARRAY_TO_DATA_TYPE[self.dtype.lower()]
if self.dtype.lower() in self._DTYPE_TO_DATA_TYPE:
return self._DTYPE_TO_DATA_TYPE[self.dtype.lower()]

return super().data_type
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

0 comments on commit 8197fa7

Please sign in to comment.