From fface1d70c1443c855ccac241ec9101d0f5bd839 Mon Sep 17 00:00:00 2001 From: Doug Beatty Date: Fri, 12 Apr 2024 19:22:01 -0600 Subject: [PATCH 1/3] Cross-database `cast` macro --- .../unreleased/Features-20240412-192040.yaml | 6 ++++ .../dbt/tests/adapter/utils/fixture_cast.py | 33 +++++++++++++++++++ .../dbt/tests/adapter/utils/test_cast.py | 25 ++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 .changes/unreleased/Features-20240412-192040.yaml create mode 100644 dbt-tests-adapter/dbt/tests/adapter/utils/fixture_cast.py create mode 100644 dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py diff --git a/.changes/unreleased/Features-20240412-192040.yaml b/.changes/unreleased/Features-20240412-192040.yaml new file mode 100644 index 00000000..dc3f6799 --- /dev/null +++ b/.changes/unreleased/Features-20240412-192040.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Cross-database `cast` macro +time: 2024-04-12T19:20:40.904842-06:00 +custom: + Author: MichelleArk dbeatty10 + Issue: "84" diff --git a/dbt-tests-adapter/dbt/tests/adapter/utils/fixture_cast.py b/dbt-tests-adapter/dbt/tests/adapter/utils/fixture_cast.py new file mode 100644 index 00000000..fe29c706 --- /dev/null +++ b/dbt-tests-adapter/dbt/tests/adapter/utils/fixture_cast.py @@ -0,0 +1,33 @@ +# cast + +seeds__data_cast_csv = """field,output +abc,abc +123,123 +, +""" + + +models__test_cast_sql = """ +with data as ( + + select * from {{ ref('data_cast') }} + +) + +select + {{ cast('field', api.Column.translate_type('string')) }} as actual, + output as expected + +from data +""" + + +models__test_cast_yml = """ +version: 2 +models: + - name: test_cast + data_tests: + - assert_equal: + actual: actual + expected: expected +""" diff --git a/dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py b/dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py new file mode 100644 index 00000000..0d91d7d6 --- /dev/null +++ b/dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py @@ -0,0 +1,25 @@ +import pytest + +from dbt.tests.adapter.utils import base_utils, fixture_cast + + +class BaseSafeCast(base_utils.BaseUtils): + @pytest.fixture(scope="class") + def seeds(self): + return {"data_cast.csv": fixture_cast.seeds__data_cast_csv} + + @pytest.fixture(scope="class") + def models(self): + return { + "test_cast.yml": fixture_cast.models__test_cast_yml, + "test_cast.sql": self.interpolate_macro_namespace( + self.interpolate_macro_namespace( + fixture_cast.models__test_cast_sql, "cast" + ), + "type_string", + ), + } + + +class TestSafeCast(BaseSafeCast): + pass From f624cc1c7fb5b1dbdcb854c358b4e02882f3de2a Mon Sep 17 00:00:00 2001 From: Doug Beatty <44704949+dbeatty10@users.noreply.github.com> Date: Thu, 18 Apr 2024 08:11:01 -0600 Subject: [PATCH 2/3] Remove accidental inclusion of "Safe" in the pytest names --- dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py b/dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py index 0d91d7d6..bc209464 100644 --- a/dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py +++ b/dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py @@ -3,7 +3,7 @@ from dbt.tests.adapter.utils import base_utils, fixture_cast -class BaseSafeCast(base_utils.BaseUtils): +class BaseCast(base_utils.BaseUtils): @pytest.fixture(scope="class") def seeds(self): return {"data_cast.csv": fixture_cast.seeds__data_cast_csv} @@ -21,5 +21,5 @@ def models(self): } -class TestSafeCast(BaseSafeCast): +class TestCast(BaseCast): pass From fbc6254a89a1e1a095580caff672b6ef57e90e88 Mon Sep 17 00:00:00 2001 From: Doug Beatty Date: Fri, 19 Apr 2024 12:02:44 -0600 Subject: [PATCH 3/3] Fix code quality --- dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py b/dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py index bc209464..686b7697 100644 --- a/dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py +++ b/dbt-tests-adapter/dbt/tests/adapter/utils/test_cast.py @@ -13,9 +13,7 @@ def models(self): return { "test_cast.yml": fixture_cast.models__test_cast_yml, "test_cast.sql": self.interpolate_macro_namespace( - self.interpolate_macro_namespace( - fixture_cast.models__test_cast_sql, "cast" - ), + self.interpolate_macro_namespace(fixture_cast.models__test_cast_sql, "cast"), "type_string", ), }