From 522c0c6c698de2996badefdd37c3d78f8fe793ae Mon Sep 17 00:00:00 2001 From: Sam Debruyn Date: Sun, 21 May 2023 12:48:11 +0200 Subject: [PATCH] fix hashing function --- dbt/include/sqlserver/macros/utils/hash.sql | 2 +- tests/functional/adapter/test_utils.py | 168 ++++++++------------ 2 files changed, 66 insertions(+), 104 deletions(-) diff --git a/dbt/include/sqlserver/macros/utils/hash.sql b/dbt/include/sqlserver/macros/utils/hash.sql index d3d2360d..e170631e 100644 --- a/dbt/include/sqlserver/macros/utils/hash.sql +++ b/dbt/include/sqlserver/macros/utils/hash.sql @@ -1,3 +1,3 @@ {% macro sqlserver__hash(field) %} - convert(varchar(50), hashbytes('md5', {{field}}), 2) + lower(convert(varchar(50), hashbytes('md5', coalesce(convert(varchar(max), {{field}}), '')), 2)) {% endmacro %} diff --git a/tests/functional/adapter/test_utils.py b/tests/functional/adapter/test_utils.py index bc338187..1686d0da 100644 --- a/tests/functional/adapter/test_utils.py +++ b/tests/functional/adapter/test_utils.py @@ -29,34 +29,32 @@ from dbt.tests.adapter.utils.test_split_part import BaseSplitPart from dbt.tests.adapter.utils.test_string_literal import BaseStringLiteral -macros__test_assert_equal_sql = """ -{% test assert_equal(model, actual, expected) %} -select * from {{ model }} -where {{ actual }} != {{ expected }} -or ({{ actual }} is null and {{ expected }} is not null) -or ({{ expected }} is null and {{ actual }} is not null) -{% endtest %} -""" - -class TestAnyValueSQLServer(BaseAnyValue): +class BaseFixedMacro: @pytest.fixture(scope="class") def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} + return { + "test_assert_equal.sql": """ + {% test assert_equal(model, actual, expected) %} + select * from {{ model }} + where {{ actual }} != {{ expected }} + or ({{ actual }} is null and {{ expected }} is not null) + or ({{ expected }} is null and {{ actual }} is not null) + {% endtest %} + """ + } -@pytest.mark.skip("bool_or not supported in this adapter") -class TestBoolOrSQLServer(BaseBoolOr): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestAnyValueSQLServer(BaseFixedMacro, BaseAnyValue): + pass -class TestCastBoolToTextSQLServer(BaseCastBoolToText): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +@pytest.mark.skip("bool_or not supported in this adapter") +class TestBoolOrSQLServer(BaseFixedMacro, BaseBoolOr): + pass + +class TestCastBoolToTextSQLServer(BaseFixedMacro, BaseCastBoolToText): @pytest.fixture(scope="class") def models(self): models__test_cast_bool_to_text_sql = """ @@ -84,75 +82,59 @@ def models(self): } -class TestConcatSQLServer(BaseConcat): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestConcatSQLServer(BaseFixedMacro, BaseConcat): + pass -class TestDateTruncSQLServer(BaseDateTrunc): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestDateTruncSQLServer(BaseFixedMacro, BaseDateTrunc): + pass -class TestHashSQLServer(BaseHash): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +seeds__data_hash_csv = """input_1,output +ab,187ef4436122d1cc2f40dc2b92f0eba0 +a,0cc175b9c0f1b6a831c399e269772661 +1,c4ca4238a0b923820dcc509a6f75849b +,d41d8cd98f00b204e9800998ecf8427e""" -class TestStringLiteralSQLServer(BaseStringLiteral): +class TestHashSQLServer(BaseFixedMacro, BaseHash): @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} + def seeds(self): + return {"data_hash.csv": seeds__data_hash_csv} -class TestSplitPartSQLServer(BaseSplitPart): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestStringLiteralSQLServer(BaseFixedMacro, BaseStringLiteral): + pass -class TestDateDiffSQLServer(BaseDateDiff): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestSplitPartSQLServer(BaseFixedMacro, BaseSplitPart): + pass -class TestEscapeSingleQuotesSQLServer(BaseEscapeSingleQuotesQuote): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestDateDiffSQLServer(BaseFixedMacro, BaseDateDiff): + pass -class TestIntersectSQLServer(BaseIntersect): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestEscapeSingleQuotesSQLServer(BaseFixedMacro, BaseEscapeSingleQuotesQuote): + pass -class TestLastDaySQLServer(BaseLastDay): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestIntersectSQLServer(BaseFixedMacro, BaseIntersect): + pass -class TestLengthSQLServer(BaseLength): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestLastDaySQLServer(BaseFixedMacro, BaseLastDay): + pass + + +class TestLengthSQLServer(BaseFixedMacro, BaseLength): + pass -class TestListaggSQLServer(BaseListagg): +class TestListaggSQLServer(BaseFixedMacro, BaseListagg): # Only supported in SQL Server 2017 and later or cloud versions # DISTINCT not supported # limit not supported - - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} - @pytest.fixture(scope="class") def seeds(self): seeds__data_listagg_output_csv = """group_col,expected,version @@ -231,23 +213,15 @@ def models(self): } -class TestRightSQLServer(BaseRight): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestRightSQLServer(BaseFixedMacro, BaseRight): + pass -class TestSafeCastSQLServer(BaseSafeCast): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} - +class TestSafeCastSQLServer(BaseFixedMacro, BaseSafeCast): + pass -class TestDateAddSQLServer(BaseDateAdd): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestDateAddSQLServer(BaseFixedMacro, BaseDateAdd): @pytest.fixture(scope="class") def project_config_update(self): return { @@ -265,22 +239,16 @@ def project_config_update(self): } -class TestExceptSQLServer(BaseExcept): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestExceptSQLServer(BaseFixedMacro, BaseExcept): + pass -class TestPositionSQLServer(BasePosition): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestPositionSQLServer(BaseFixedMacro, BasePosition): + pass -class TestReplaceSQLServer(BaseReplace): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestReplaceSQLServer(BaseFixedMacro, BaseReplace): + pass class TestCurrentTimestampSQLServer(BaseCurrentTimestampNaive): @@ -288,21 +256,15 @@ class TestCurrentTimestampSQLServer(BaseCurrentTimestampNaive): @pytest.mark.skip(reason="arrays not supported") -class TestArrayAppendSQLServer(BaseArrayAppend): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestArrayAppendSQLServer(BaseFixedMacro, BaseArrayAppend): + pass -@pytest.mark.skip(reason="arrays not supported") -class TestArrayConcatSQLServer(BaseArrayConcat): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +@pytest.mark.skip(reason="arrays not supporteTd") +class TestArrayConcatSQLServer(BaseFixedMacro, BaseArrayConcat): + pass @pytest.mark.skip(reason="arrays not supported") -class TestArrayConstructSQLServer(BaseArrayConstruct): - @pytest.fixture(scope="class") - def macros(self): - return {"test_assert_equal.sql": macros__test_assert_equal_sql} +class TestArrayConstructSQLServer(BaseFixedMacro, BaseArrayConstruct): + pass