Skip to content

Commit

Permalink
Include null checks in utils test base (#7672)
Browse files Browse the repository at this point in the history
* Include null checks in utils test base

* Add tests for the schema test

* Add tests for this macro

---------

Co-authored-by: Mila Page <[email protected]>
  • Loading branch information
sdebruyn and VersusFacit authored May 24, 2023
1 parent 0f22366 commit 0d71a32
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20230521-125720.yaml
Original file line number Diff line number Diff line change
@@ -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"
16 changes: 13 additions & 3 deletions tests/adapter/dbt/tests/adapter/utils/base_utils.py
Original file line number Diff line number Diff line change
@@ -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 %}
"""

Expand All @@ -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'
Expand Down
34 changes: 34 additions & 0 deletions tests/adapter/dbt/tests/adapter/utils/fixture_null_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 = """
version: 2
models:
- name: test_null_compare
tests:
- assert_equal:
actual: actual
expected: expected
"""
40 changes: 40 additions & 0 deletions tests/adapter/dbt/tests/adapter/utils/test_null_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest

from dbt.tests.adapter.utils.base_utils import BaseUtils
from dbt.tests.adapter.utils.fixture_null_compare import (
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,
}


class TestMixedNullCompare(BaseNullCompare):
pass


class TestNullCompare(BaseNullCompare):
pass

0 comments on commit 0d71a32

Please sign in to comment.