From 5aa15c10f7857128d39a3167a642341103f514b0 Mon Sep 17 00:00:00 2001 From: Sam Debruyn Date: Sun, 21 May 2023 12:56:01 +0200 Subject: [PATCH 1/3] Include null checks in utils test base --- .../Under the Hood-20230521-125720.yaml | 6 ++++++ .../dbt/tests/adapter/utils/base_utils.py | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 .changes/unreleased/Under the Hood-20230521-125720.yaml diff --git a/.changes/unreleased/Under the Hood-20230521-125720.yaml b/.changes/unreleased/Under the Hood-20230521-125720.yaml new file mode 100644 index 00000000000..06707aacbb9 --- /dev/null +++ b/.changes/unreleased/Under the Hood-20230521-125720.yaml @@ -0,0 +1,6 @@ +kind: Under the Hood +body: Include null checks in utils test base +time: 2023-05-21T12:57:20.659726+02:00 +custom: + Author: sdebruyn + Issue: "7670" diff --git a/tests/adapter/dbt/tests/adapter/utils/base_utils.py b/tests/adapter/dbt/tests/adapter/utils/base_utils.py index 9e0df09aa39..75d621c2bf9 100644 --- a/tests/adapter/dbt/tests/adapter/utils/base_utils.py +++ b/tests/adapter/dbt/tests/adapter/utils/base_utils.py @@ -1,10 +1,17 @@ import pytest from dbt.tests.util import run_dbt +macros__equals_sql = """ +{% macro equals(actual, expected) %} +{# -- actual is not distinct from expected #} +(({{ actual }} = {{ expected }}) or ({{ actual }} is null and {{ expected }} is null)) +{% endmacro %} +""" + macros__test_assert_equal_sql = """ {% test assert_equal(model, actual, expected) %} -select * from {{ model }} where {{ actual }} != {{ expected }} - +select * from {{ model }} +where not {{ equals(actual, expected) }} {% endtest %} """ @@ -13,7 +20,10 @@ class BaseUtils: # setup @pytest.fixture(scope="class") def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} + return { + "equals.sql": macros__equals_sql, + "test_assert_equal.sql": macros__test_assert_equal_sql, + } # make it possible to dynamically update the macro call with a namespace # (e.g.) 'dateadd', 'dbt.dateadd', 'dbt_utils.dateadd' From a13ba87e7dbff5d8cb4b68dfaf4b4314a6bf7fad Mon Sep 17 00:00:00 2001 From: Mila Page Date: Tue, 23 May 2023 18:56:50 -0700 Subject: [PATCH 2/3] Add tests for the schema test --- .../adapter/utils/fixture_null_compare.py | 16 +++++++++++++++ .../tests/adapter/utils/test_null_compare.py | 20 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/adapter/dbt/tests/adapter/utils/fixture_null_compare.py create mode 100644 tests/adapter/dbt/tests/adapter/utils/test_null_compare.py diff --git a/tests/adapter/dbt/tests/adapter/utils/fixture_null_compare.py b/tests/adapter/dbt/tests/adapter/utils/fixture_null_compare.py new file mode 100644 index 00000000000..573144c6dfd --- /dev/null +++ b/tests/adapter/dbt/tests/adapter/utils/fixture_null_compare.py @@ -0,0 +1,16 @@ +models__test_null_compare_sql = """ +select + null as actual, + null as expected +""" + + +models__test_null_compare_yml = """ +version: 2 +models: + - name: test_null_compare + tests: + - assert_equal: + actual: actual + expected: expected +""" diff --git a/tests/adapter/dbt/tests/adapter/utils/test_null_compare.py b/tests/adapter/dbt/tests/adapter/utils/test_null_compare.py new file mode 100644 index 00000000000..af76c88c652 --- /dev/null +++ b/tests/adapter/dbt/tests/adapter/utils/test_null_compare.py @@ -0,0 +1,20 @@ +import pytest + +from dbt.tests.adapter.utils.base_utils import BaseUtils +from dbt.tests.adapter.utils.fixture_null_compare import ( + models__test_null_compare_sql, + models__test_null_compare_yml, +) + + +class BaseNullCompare(BaseUtils): + @pytest.fixture(scope="class") + def models(self): + return { + "test_null_compare.yml": models__test_null_compare_yml, + "test_null_compare.sql": models__test_null_compare_sql, + } + + +class TestNullCompare(BaseNullCompare): + pass From 1c3f5b575d9c520ef7e84fa41d5bd2196000d454 Mon Sep 17 00:00:00 2001 From: Mila Page Date: Tue, 23 May 2023 19:11:27 -0700 Subject: [PATCH 3/3] Add tests for this macro --- .../adapter/utils/fixture_null_compare.py | 22 +++++++++++++-- .../tests/adapter/utils/test_null_compare.py | 28 ++++++++++++++++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/tests/adapter/dbt/tests/adapter/utils/fixture_null_compare.py b/tests/adapter/dbt/tests/adapter/utils/fixture_null_compare.py index 573144c6dfd..8a524010082 100644 --- a/tests/adapter/dbt/tests/adapter/utils/fixture_null_compare.py +++ b/tests/adapter/dbt/tests/adapter/utils/fixture_null_compare.py @@ -1,11 +1,29 @@ -models__test_null_compare_sql = """ +MODELS__TEST_MIXED_NULL_COMPARE_SQL = """ +select + 1 as actual, + null as expected +""" + + +MODELS__TEST_MIXED_NULL_COMPARE_YML = """ +version: 2 +models: + - name: test_null_compare + tests: + - assert_equal: + actual: actual + expected: expected +""" + + +MODELS__TEST_NULL_COMPARE_SQL = """ select null as actual, null as expected """ -models__test_null_compare_yml = """ +MODELS__TEST_NULL_COMPARE_YML = """ version: 2 models: - name: test_null_compare diff --git a/tests/adapter/dbt/tests/adapter/utils/test_null_compare.py b/tests/adapter/dbt/tests/adapter/utils/test_null_compare.py index af76c88c652..58a1c9daaf9 100644 --- a/tests/adapter/dbt/tests/adapter/utils/test_null_compare.py +++ b/tests/adapter/dbt/tests/adapter/utils/test_null_compare.py @@ -2,19 +2,39 @@ from dbt.tests.adapter.utils.base_utils import BaseUtils from dbt.tests.adapter.utils.fixture_null_compare import ( - models__test_null_compare_sql, - models__test_null_compare_yml, + MODELS__TEST_MIXED_NULL_COMPARE_SQL, + MODELS__TEST_MIXED_NULL_COMPARE_YML, + MODELS__TEST_NULL_COMPARE_SQL, + MODELS__TEST_NULL_COMPARE_YML, ) +from dbt.tests.util import run_dbt + + +class BaseMixedNullCompare(BaseUtils): + @pytest.fixture(scope="class") + def models(self): + return { + "test_mixed_null_compare.yml": MODELS__TEST_MIXED_NULL_COMPARE_SQL, + "test_mixed_null_compare.sql": MODELS__TEST_MIXED_NULL_COMPARE_YML, + } + + def test_build_assert_equal(self, project): + run_dbt() + run_dbt(["test"], expect_pass=False) class BaseNullCompare(BaseUtils): @pytest.fixture(scope="class") def models(self): return { - "test_null_compare.yml": models__test_null_compare_yml, - "test_null_compare.sql": models__test_null_compare_sql, + "test_null_compare.yml": MODELS__TEST_NULL_COMPARE_YML, + "test_null_compare.sql": MODELS__TEST_NULL_COMPARE_SQL, } +class TestMixedNullCompare(BaseNullCompare): + pass + + class TestNullCompare(BaseNullCompare): pass