From f16cd66d07e502c7a5ea324614d82a3a7f42b233 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Sun, 28 Jan 2024 21:52:59 -0500 Subject: [PATCH 1/9] case-insensitive comparisons in unit testing, base unit testing test --- dbt/include/__init__.py | 2 +- .../macros/materializations/tests/helpers.sql | 8 +- .../macros/materializations/tests/unit.sql | 2 +- .../macros/unit_test_sql/get_fixture_sql.sql | 11 +-- .../adapter/unit_testing/test_unit_testing.py | 84 +++++++++++++++++++ 5 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 dbt/tests/adapter/unit_testing/test_unit_testing.py diff --git a/dbt/include/__init__.py b/dbt/include/__init__.py index 9088ea6a..b36383a6 100644 --- a/dbt/include/__init__.py +++ b/dbt/include/__init__.py @@ -1,3 +1,3 @@ from pkgutil import extend_path -__path__ = extend_path(__path__, __name__) \ No newline at end of file +__path__ = extend_path(__path__, __name__) diff --git a/dbt/include/global_project/macros/materializations/tests/helpers.sql b/dbt/include/global_project/macros/materializations/tests/helpers.sql index 13e640c2..ead727d9 100644 --- a/dbt/include/global_project/macros/materializations/tests/helpers.sql +++ b/dbt/include/global_project/macros/materializations/tests/helpers.sql @@ -22,17 +22,17 @@ {% macro default__get_unit_test_sql(main_sql, expected_fixture_sql, expected_column_names) -%} -- Build actual result given inputs -with dbt_internal_unit_test_actual AS ( +with dbt_internal_unit_test_actual as ( select - {% for expected_column_name in expected_column_names %}{{expected_column_name}}{% if not loop.last -%},{% endif %}{%- endfor -%}, {{ dbt.string_literal("actual") }} as actual_or_expected + {% for expected_column_name in expected_column_names %}{{expected_column_name}}{% if not loop.last -%},{% endif %}{%- endfor -%}, {{ dbt.string_literal("actual") }} as {{ adapter.quote("actual_or_expected") }} from ( {{ main_sql }} ) _dbt_internal_unit_test_actual ), -- Build expected result -dbt_internal_unit_test_expected AS ( +dbt_internal_unit_test_expected as ( select - {% for expected_column_name in expected_column_names %}{{expected_column_name}}{% if not loop.last -%}, {% endif %}{%- endfor -%}, {{ dbt.string_literal("expected") }} as actual_or_expected + {% for expected_column_name in expected_column_names %}{{expected_column_name}}{% if not loop.last -%}, {% endif %}{%- endfor -%}, {{ dbt.string_literal("expected") }} as {{ adapter.quote("actual_or_expected") }} from ( {{ expected_fixture_sql }} ) _dbt_internal_unit_test_expected diff --git a/dbt/include/global_project/macros/materializations/tests/unit.sql b/dbt/include/global_project/macros/materializations/tests/unit.sql index 79d5631b..6d7b632c 100644 --- a/dbt/include/global_project/macros/materializations/tests/unit.sql +++ b/dbt/include/global_project/macros/materializations/tests/unit.sql @@ -11,7 +11,7 @@ {%- set columns_in_relation = adapter.get_columns_in_relation(temp_relation) -%} {%- set column_name_to_data_types = {} -%} {%- for column in columns_in_relation -%} - {%- do column_name_to_data_types.update({column.name: column.dtype}) -%} + {%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%} {%- endfor -%} {% set unit_test_sql = get_unit_test_sql(sql, get_expected_sql(expected_rows, column_name_to_data_types), tested_expected_column_names) %} diff --git a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql index 2f90a561..48c37106 100644 --- a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql +++ b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql @@ -6,7 +6,8 @@ {%- set columns_in_relation = adapter.get_columns_in_relation(this) -%} {%- set column_name_to_data_types = {} -%} {%- for column in columns_in_relation -%} -{%- do column_name_to_data_types.update({column.name: column.dtype}) -%} +{#-- This needs to be a case-insensitive comparison --#} +{%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%} {%- endfor -%} {%- endif -%} @@ -23,7 +24,7 @@ {%- set default_row_copy = default_row.copy() -%} {%- do default_row_copy.update(row) -%} select -{%- for column_name, column_value in default_row_copy.items() %} {{ column_value }} AS {{ column_name }}{% if not loop.last -%}, {%- endif %} +{%- for column_name, column_value in default_row_copy.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%}, {%- endif %} {%- endfor %} {%- if not loop.last %} union all @@ -32,7 +33,7 @@ union all {%- if (rows | length) == 0 -%} select - {%- for column_name, column_value in default_row.items() %} {{ column_value }} AS {{ column_name }}{% if not loop.last -%},{%- endif %} + {%- for column_name, column_value in default_row.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%},{%- endif %} {%- endfor %} limit 0 {%- endif -%} @@ -48,7 +49,7 @@ union all {%- for row in rows -%} {%- do format_row(row, column_name_to_data_types) -%} select -{%- for column_name, column_value in row.items() %} {{ column_value }} AS {{ column_name }}{% if not loop.last -%}, {%- endif %} +{%- for column_name, column_value in row.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%}, {%- endif %} {%- endfor %} {%- if not loop.last %} union all @@ -64,7 +65,7 @@ union all {%- for column_name, column_value in row.items() -%} {% set row_update = {column_name: column_value} %} {%- if column_value is string -%} -{%- set row_update = {column_name: safe_cast(dbt.string_literal(column_value), column_name_to_data_types[column_name]) } -%} +{%- set row_update = {column_name: safe_cast(dbt.string_literal(dbt.escape_single_quotes(column_value)), column_name_to_data_types[column_name]) } -%} {%- elif column_value is none -%} {%- set row_update = {column_name: safe_cast('null', column_name_to_data_types[column_name]) } -%} {%- else -%} diff --git a/dbt/tests/adapter/unit_testing/test_unit_testing.py b/dbt/tests/adapter/unit_testing/test_unit_testing.py new file mode 100644 index 00000000..644398d6 --- /dev/null +++ b/dbt/tests/adapter/unit_testing/test_unit_testing.py @@ -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 From 632dec736fe6a1af6b3df0b599761d19b592980f Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Wed, 31 Jan 2024 18:58:01 -0500 Subject: [PATCH 2/9] get test working in dbt-adapters repo --- dbt/tests/adapter/unit_testing/test_unit_testing.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/dbt/tests/adapter/unit_testing/test_unit_testing.py b/dbt/tests/adapter/unit_testing/test_unit_testing.py index 644398d6..4d13c0bf 100644 --- a/dbt/tests/adapter/unit_testing/test_unit_testing.py +++ b/dbt/tests/adapter/unit_testing/test_unit_testing.py @@ -1,7 +1,7 @@ import pytest from dbt.tests.util import write_file, run_dbt - +from dbt.tests.fixtures.project import * my_model_sql = """ select @@ -38,13 +38,14 @@ def data_types(self): ["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}'""", ], + # TODO: support complex types + # ["ARRAY['a','b','c']", """'{"a", "b", "c"}'"""], + # ["ARRAY[1,2,3]", """'{1, 2, 3}'"""], ] @pytest.fixture(scope="class") @@ -75,7 +76,7 @@ def test_unit_test_data_type(self, project, data_types): assert len(results) == 1 try: - run_dbt(["unit-test", "--select", "my_model"]) + run_dbt(["test", "--select", "my_model"]) except Exception: raise AssertionError(f"unit test failed when testing model with {sql_value}") From 5a50be73163514dd7055af046c5223b092ee9b61 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Thu, 1 Feb 2024 16:32:56 -0500 Subject: [PATCH 3/9] add utils.cast --- dbt/include/global_project/macros/utils/cast.sql | 7 +++++++ dbt/tests/adapter/unit_testing/test_unit_testing.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 dbt/include/global_project/macros/utils/cast.sql diff --git a/dbt/include/global_project/macros/utils/cast.sql b/dbt/include/global_project/macros/utils/cast.sql new file mode 100644 index 00000000..ea5b1aac --- /dev/null +++ b/dbt/include/global_project/macros/utils/cast.sql @@ -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 %} diff --git a/dbt/tests/adapter/unit_testing/test_unit_testing.py b/dbt/tests/adapter/unit_testing/test_unit_testing.py index 4d13c0bf..5dc9838c 100644 --- a/dbt/tests/adapter/unit_testing/test_unit_testing.py +++ b/dbt/tests/adapter/unit_testing/test_unit_testing.py @@ -81,5 +81,5 @@ def test_unit_test_data_type(self, project, data_types): raise AssertionError(f"unit test failed when testing model with {sql_value}") -class TestUnitTestingTypes(BaseUnitTestingTypes): +class TestPostgresUnitTestingTypes(BaseUnitTestingTypes): pass From 0fd8709728052e28b800c1e4ed2fee35563ed9ad Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Mon, 5 Feb 2024 17:09:20 -0500 Subject: [PATCH 4/9] BaseUnitTestCaseInsensivity --- .../macros/unit_test_sql/get_fixture_sql.sql | 38 +++++++------- .../unit_testing/test_case_insensitivity.py | 50 +++++++++++++++++++ 2 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 dbt/tests/adapter/unit_testing/test_case_insensitivity.py diff --git a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql index f1e668c3..23aa35e6 100644 --- a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql +++ b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql @@ -3,7 +3,7 @@ {% set default_row = {} %} {%- if not column_name_to_data_types -%} -{%- set columns_in_relation = adapter.get_columns_in_relation(defer_relation or this) -%} +{%- set columns_in_relation = adapter.get_columns_in_relation(this) -%} {%- set column_name_to_data_types = {} -%} {%- for column in columns_in_relation -%} {#-- This needs to be a case-insensitive comparison --#} @@ -19,10 +19,11 @@ {%- do default_row.update({column_name: (safe_cast("null", column_type) | trim )}) -%} {%- endfor -%} + {%- for row in rows -%} -{%- do format_row(row, column_name_to_data_types) -%} +{%- set formatted_row = format_row(row, column_name_to_data_types) -%} {%- set default_row_copy = default_row.copy() -%} -{%- do default_row_copy.update(row) -%} +{%- do default_row_copy.update(formatted_row) -%} select {%- for column_name, column_value in default_row_copy.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%}, {%- endif %} {%- endfor %} @@ -60,18 +61,21 @@ union all {% endmacro %} {%- macro format_row(row, column_name_to_data_types) -%} - -{#-- wrap yaml strings in quotes, apply cast --#} -{%- for column_name, column_value in row.items() -%} -{% set row_update = {column_name: column_value} %} -{%- if column_value is string -%} -{%- set row_update = {column_name: safe_cast(dbt.string_literal(dbt.escape_single_quotes(column_value)), column_name_to_data_types[column_name]) } -%} -{%- elif column_value is none -%} -{%- set row_update = {column_name: safe_cast('null', column_name_to_data_types[column_name]) } -%} -{%- else -%} -{%- set row_update = {column_name: safe_cast(column_value, column_name_to_data_types[column_name]) } -%} -{%- endif -%} -{%- do row.update(row_update) -%} -{%- endfor -%} - + {% set formatted_row = {} %} + {%- for column_name, column_value in row.items() -%} + {#-- generate case-insensitive formatted row --#} + {% set column_name = column_name|lower %} + {% set row_update = {column_name: column_value} %} + + {#-- wrap yaml strings in quotes, apply cast --#} + {%- if column_value is string -%} + {%- set row_update = {column_name: safe_cast(dbt.string_literal(dbt.escape_single_quotes(column_value)), column_name_to_data_types[column_name]) } -%} + {%- elif column_value is none -%} + {%- set row_update = {column_name: safe_cast('null', column_name_to_data_types[column_name]) } -%} + {%- else -%} + {%- set row_update = {column_name: safe_cast(column_value, column_name_to_data_types[column_name]) } -%} + {%- endif -%} + {%- do formatted_row.update(row_update) -%} + {%- endfor -%} + {{ return(formatted_row) }} {%- endmacro -%} diff --git a/dbt/tests/adapter/unit_testing/test_case_insensitivity.py b/dbt/tests/adapter/unit_testing/test_case_insensitivity.py new file mode 100644 index 00000000..ca96911c --- /dev/null +++ b/dbt/tests/adapter/unit_testing/test_case_insensitivity.py @@ -0,0 +1,50 @@ +import pytest +from dbt.tests.util import run_dbt +from dbt.tests.fixtures.project import * + + +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_dim_wizards(self, project): + results = run_dbt(["run"]) + assert len(results) == 2 + + results = run_dbt(["test"]) + + +class TestPosgresUnitTestCaseInsensitivity(BaseUnitTestCaseInsensivity): + pass \ No newline at end of file From 6a5dccd4a3a155fff908d1c93cca12b13a430300 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Mon, 5 Feb 2024 17:32:48 -0500 Subject: [PATCH 5/9] use formatted_row in get_expected_sql --- .../global_project/macros/unit_test_sql/get_fixture_sql.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql index 23aa35e6..dff550f3 100644 --- a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql +++ b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql @@ -48,9 +48,9 @@ union all limit 0 {%- else -%} {%- for row in rows -%} -{%- do format_row(row, column_name_to_data_types) -%} +{%- set formatted_row = format_row(row, column_name_to_data_types) -%} select -{%- for column_name, column_value in row.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%}, {%- endif %} +{%- for column_name, column_value in formatted_row.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%}, {%- endif %} {%- endfor %} {%- if not loop.last %} union all From 6beb1ec2395502a2bc7b0d0e04fac0140ac3f8b4 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Tue, 6 Feb 2024 16:32:45 -0500 Subject: [PATCH 6/9] add check + test for invalid fixture input --- .../macros/unit_test_sql/get_fixture_sql.sql | 25 +++++--- .../unit_testing/test_invalid_input.py | 63 +++++++++++++++++++ .../{test_unit_testing.py => test_types.py} | 0 3 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 dbt/tests/adapter/unit_testing/test_invalid_input.py rename dbt/tests/adapter/unit_testing/{test_unit_testing.py => test_types.py} (100%) diff --git a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql index dff550f3..385fd73f 100644 --- a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql +++ b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql @@ -61,21 +61,32 @@ union all {% endmacro %} {%- macro format_row(row, column_name_to_data_types) -%} + {#-- generate case-insensitive formatted row --#} {% set formatted_row = {} %} {%- for column_name, column_value in row.items() -%} - {#-- generate case-insensitive formatted row --#} {% set column_name = column_name|lower %} - {% set row_update = {column_name: column_value} %} + + {%- if column_name not in column_name_to_data_types %} + {#-- if user-provided row contains column name that relation does not contain, raise an error --#} + {% set fixture_name = "expected output" if model.resource_type == 'unit_test' else ("'" ~ model.name ~ "'") %} + {{ exceptions.raise_compiler_error( + "Invalid column name: '" ~ column_name ~ "' in unit test fixture for " ~ fixture_name ~ "." + "\nAccepted columns for " ~ fixture_name ~ " are: " ~ (column_name_to_data_types.keys()|list) + ) }} + {%- endif -%} + + {%- set column_type = column_name_to_data_types[column_name] %} {#-- wrap yaml strings in quotes, apply cast --#} {%- if column_value is string -%} - {%- set row_update = {column_name: safe_cast(dbt.string_literal(dbt.escape_single_quotes(column_value)), column_name_to_data_types[column_name]) } -%} + {%- set row_update = {column_name: safe_cast(dbt.string_literal(dbt.escape_single_quotes(column_value)), column_type) } -%} {%- elif column_value is none -%} - {%- set row_update = {column_name: safe_cast('null', column_name_to_data_types[column_name]) } -%} + {%- set row_update = {column_name: safe_cast('null', column_type) } -%} {%- else -%} - {%- set row_update = {column_name: safe_cast(column_value, column_name_to_data_types[column_name]) } -%} - {%- endif -%} - {%- do formatted_row.update(row_update) -%} + {%- set row_update = {column_name: safe_cast(column_value, column_type) } -%} + {%- endif -%} + + {%- do formatted_row.update(row_update) -%} {%- endfor -%} {{ return(formatted_row) }} {%- endmacro -%} diff --git a/dbt/tests/adapter/unit_testing/test_invalid_input.py b/dbt/tests/adapter/unit_testing/test_invalid_input.py new file mode 100644 index 00000000..2989534c --- /dev/null +++ b/dbt/tests/adapter/unit_testing/test_invalid_input.py @@ -0,0 +1,63 @@ +import pytest +from dbt.tests.util import run_dbt, run_dbt_and_capture +from dbt.tests.fixtures.project import * + + +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_dim_wizards(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 diff --git a/dbt/tests/adapter/unit_testing/test_unit_testing.py b/dbt/tests/adapter/unit_testing/test_types.py similarity index 100% rename from dbt/tests/adapter/unit_testing/test_unit_testing.py rename to dbt/tests/adapter/unit_testing/test_types.py From f1bc15d43ee6f8cba030ae5b7e016b336994ff32 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Wed, 7 Feb 2024 14:59:31 -0500 Subject: [PATCH 7/9] improve test names --- dbt/tests/adapter/unit_testing/test_case_insensitivity.py | 2 +- dbt/tests/adapter/unit_testing/test_invalid_input.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dbt/tests/adapter/unit_testing/test_case_insensitivity.py b/dbt/tests/adapter/unit_testing/test_case_insensitivity.py index ca96911c..13d3b362 100644 --- a/dbt/tests/adapter/unit_testing/test_case_insensitivity.py +++ b/dbt/tests/adapter/unit_testing/test_case_insensitivity.py @@ -39,7 +39,7 @@ def models(self): "unit_tests.yml": test_my_model_yml, } - def test_dim_wizards(self, project): + def test_case_insensitivity(self, project): results = run_dbt(["run"]) assert len(results) == 2 diff --git a/dbt/tests/adapter/unit_testing/test_invalid_input.py b/dbt/tests/adapter/unit_testing/test_invalid_input.py index 2989534c..a4d5aa6e 100644 --- a/dbt/tests/adapter/unit_testing/test_invalid_input.py +++ b/dbt/tests/adapter/unit_testing/test_invalid_input.py @@ -44,7 +44,7 @@ def models(self): "unit_tests.yml": test_my_model_yml, } - def test_dim_wizards(self, project): + def test_invalid_input(self, project): results = run_dbt(["run"]) assert len(results) == 2 From 425fa1f8bc9d82eddbdc9e7685722d62ec94c8b7 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Thu, 8 Feb 2024 10:28:14 -0500 Subject: [PATCH 8/9] lint --- dbt/tests/adapter/unit_testing/test_case_insensitivity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbt/tests/adapter/unit_testing/test_case_insensitivity.py b/dbt/tests/adapter/unit_testing/test_case_insensitivity.py index 13d3b362..84a87c5c 100644 --- a/dbt/tests/adapter/unit_testing/test_case_insensitivity.py +++ b/dbt/tests/adapter/unit_testing/test_case_insensitivity.py @@ -47,4 +47,4 @@ def test_case_insensitivity(self, project): class TestPosgresUnitTestCaseInsensitivity(BaseUnitTestCaseInsensivity): - pass \ No newline at end of file + pass From a138e28d4481df35bac2db6f50047348f1cd9e14 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Thu, 8 Feb 2024 19:42:47 -0500 Subject: [PATCH 9/9] refactor get_fixture_sql --- .../macros/unit_test_sql/get_fixture_sql.sql | 10 +++++----- .../adapter/unit_testing/test_case_insensitivity.py | 1 - dbt/tests/adapter/unit_testing/test_invalid_input.py | 1 - dbt/tests/adapter/unit_testing/test_types.py | 1 - 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql index 385fd73f..ba35fab2 100644 --- a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql +++ b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql @@ -77,15 +77,15 @@ union all {%- set column_type = column_name_to_data_types[column_name] %} - {#-- wrap yaml strings in quotes, apply cast --#} + {#-- sanitize column_value: wrap yaml strings in quotes, apply cast --#} + {%- set column_value_clean = column_value -%} {%- if column_value is string -%} - {%- set row_update = {column_name: safe_cast(dbt.string_literal(dbt.escape_single_quotes(column_value)), column_type) } -%} + {%- set column_value_clean = dbt.string_literal(dbt.escape_single_quotes(column_value)) -%} {%- elif column_value is none -%} - {%- set row_update = {column_name: safe_cast('null', column_type) } -%} - {%- else -%} - {%- set row_update = {column_name: safe_cast(column_value, column_type) } -%} + {%- set column_value_clean = 'null' -%} {%- endif -%} + {%- set row_update = {column_name: safe_cast(column_value_clean, column_type) } -%} {%- do formatted_row.update(row_update) -%} {%- endfor -%} {{ return(formatted_row) }} diff --git a/dbt/tests/adapter/unit_testing/test_case_insensitivity.py b/dbt/tests/adapter/unit_testing/test_case_insensitivity.py index 84a87c5c..f6f89766 100644 --- a/dbt/tests/adapter/unit_testing/test_case_insensitivity.py +++ b/dbt/tests/adapter/unit_testing/test_case_insensitivity.py @@ -1,6 +1,5 @@ import pytest from dbt.tests.util import run_dbt -from dbt.tests.fixtures.project import * my_model_sql = """ diff --git a/dbt/tests/adapter/unit_testing/test_invalid_input.py b/dbt/tests/adapter/unit_testing/test_invalid_input.py index a4d5aa6e..6c41ceb9 100644 --- a/dbt/tests/adapter/unit_testing/test_invalid_input.py +++ b/dbt/tests/adapter/unit_testing/test_invalid_input.py @@ -1,6 +1,5 @@ import pytest from dbt.tests.util import run_dbt, run_dbt_and_capture -from dbt.tests.fixtures.project import * my_model_sql = """ diff --git a/dbt/tests/adapter/unit_testing/test_types.py b/dbt/tests/adapter/unit_testing/test_types.py index 5dc9838c..1d19aafb 100644 --- a/dbt/tests/adapter/unit_testing/test_types.py +++ b/dbt/tests/adapter/unit_testing/test_types.py @@ -1,7 +1,6 @@ import pytest from dbt.tests.util import write_file, run_dbt -from dbt.tests.fixtures.project import * my_model_sql = """ select