From aadab69d2483e1804a25c1fc6a895145e836a146 Mon Sep 17 00:00:00 2001 From: Yu Ishikawa Date: Thu, 28 Nov 2024 00:21:50 +0900 Subject: [PATCH] Add checking macros (#92) --- .../tests/comparisons/test_dict_equals.sql | 86 +++++++++++++++++++ .../macros/tests/comparisons/test_equals.sql | 25 ++++++ .../macros/tests/comparisons/test_in.sql | 25 ++++++ .../tests/comparisons/test_is_false.sql | 26 ++++++ .../macros/tests/comparisons/test_is_none.sql | 28 ++++++ .../macros/tests/comparisons/test_is_true.sql | 26 ++++++ .../tests/comparisons/test_not_equals.sql | 25 ++++++ .../macros/tests/comparisons/test_not_in.sql | 25 ++++++ .../macros/tests/test_macros.sql | 18 ++-- macros/assert_dict_equals.sql | 39 +-------- macros/assert_equals.sql | 8 +- macros/assert_false.sql | 12 +-- macros/assert_in.sql | 6 +- macros/assert_is_none.sql | 6 +- macros/assert_is_not_none.sql | 6 +- macros/assert_list_equals.sql | 24 +----- macros/assert_not_equals.sql | 8 +- macros/assert_not_in.sql | 8 +- macros/assert_true.sql | 12 +-- macros/comparisons/dict_equals.sql | 40 +++++++++ macros/comparisons/equals.sql | 3 + macros/comparisons/in.sql | 16 ++++ macros/comparisons/is_false.sql | 3 + macros/comparisons/is_none.sql | 3 + macros/comparisons/is_not_none.sql | 3 + macros/comparisons/is_true.sql | 3 + macros/comparisons/not_equals.sql | 3 + macros/comparisons/not_in.sql | 16 ++++ 28 files changed, 392 insertions(+), 111 deletions(-) create mode 100644 integration_tests/macros/tests/comparisons/test_dict_equals.sql create mode 100644 integration_tests/macros/tests/comparisons/test_equals.sql create mode 100644 integration_tests/macros/tests/comparisons/test_in.sql create mode 100644 integration_tests/macros/tests/comparisons/test_is_false.sql create mode 100644 integration_tests/macros/tests/comparisons/test_is_none.sql create mode 100644 integration_tests/macros/tests/comparisons/test_is_true.sql create mode 100644 integration_tests/macros/tests/comparisons/test_not_equals.sql create mode 100644 integration_tests/macros/tests/comparisons/test_not_in.sql create mode 100644 macros/comparisons/dict_equals.sql create mode 100644 macros/comparisons/equals.sql create mode 100644 macros/comparisons/in.sql create mode 100644 macros/comparisons/is_false.sql create mode 100644 macros/comparisons/is_none.sql create mode 100644 macros/comparisons/is_not_none.sql create mode 100644 macros/comparisons/is_true.sql create mode 100644 macros/comparisons/not_equals.sql create mode 100644 macros/comparisons/not_in.sql diff --git a/integration_tests/macros/tests/comparisons/test_dict_equals.sql b/integration_tests/macros/tests/comparisons/test_dict_equals.sql new file mode 100644 index 0000000..5689059 --- /dev/null +++ b/integration_tests/macros/tests/comparisons/test_dict_equals.sql @@ -0,0 +1,86 @@ +{% macro test_dict_equals() %} + {# Test successful dictionary equality #} + {% set dict1 = {'a': 1, 'b': 2, 'c': 3} %} + {% set dict2 = {'a': 1, 'b': 2, 'c': 3} %} + {% set result1 = dbt_unittest.dict_equals(dict1, dict2) %} + {% if not result1 %} + {{ exceptions.raise_compiler_error("Failed: Identical dictionaries should be equal") }} + {% endif %} + + {# Test failed dictionary equality - different values #} + {% set dict3 = {'a': 1, 'b': 2, 'c': 4} %} + {% set result2 = dbt_unittest.dict_equals(dict1, dict3) %} + {% if result2 %} + {{ exceptions.raise_compiler_error("Failed: Dictionaries with different values should not be equal") }} + {% endif %} + + {# Test failed dictionary equality - different keys #} + {% set dict4 = {'a': 1, 'b': 2, 'd': 3} %} + {% set result3 = dbt_unittest.dict_equals(dict1, dict4) %} + {% if result3 %} + {{ exceptions.raise_compiler_error("Failed: Dictionaries with different keys should not be equal") }} + {% endif %} + + {# Test failed dictionary equality - different lengths #} + {% set dict5 = {'a': 1, 'b': 2} %} + {% set result4 = dbt_unittest.dict_equals(dict1, dict5) %} + {% if result4 %} + {{ exceptions.raise_compiler_error("Failed: Dictionaries with different lengths should not be equal") }} + {% endif %} + + {# Test failed dictionary equality - non-mapping types #} + {% set result5 = dbt_unittest.dict_equals(dict1, [1, 2, 3]) %} + {% if result5 %} + {{ exceptions.raise_compiler_error("Failed: Comparing dictionary with non-mapping type should fail") }} + {% endif %} + + {# Test failed dictionary equality - None values #} + {% set result6 = dbt_unittest.dict_equals(dict1, none) %} + {% if result6 %} + {{ exceptions.raise_compiler_error("Failed: Comparing dictionary with None should fail") }} + {% endif %} + + {# Test None equality #} + {% set result7 = dbt_unittest.dict_equals(none, none) %} + {% if not result7 %} + {{ exceptions.raise_compiler_error("Failed: None should be equal to None") }} + {% endif %} + + {# Test nested dictionary equality #} + {% set nested_dict1 = {'a': 1, 'b': {'c': 2, 'd': 3}} %} + {% set nested_dict2 = {'a': 1, 'b': {'c': 2, 'd': 3}} %} + {% set result8 = dbt_unittest.dict_equals(nested_dict1, nested_dict2) %} + {% if not result8 %} + {{ exceptions.raise_compiler_error("Failed: Identical nested dictionaries should be equal") }} + {% endif %} + + {# Test nested dictionary inequality #} + {% set nested_dict3 = {'a': 1, 'b': {'c': 2, 'd': 4}} %} + {% set result9 = dbt_unittest.dict_equals(nested_dict1, nested_dict3) %} + {% if result9 %} + {{ exceptions.raise_compiler_error("Failed: Nested dictionaries with different values should not be equal") }} + {% endif %} + + {# Test empty dictionary equality #} + {% set empty_dict1 = {} %} + {% set empty_dict2 = {} %} + {% set result10 = dbt_unittest.dict_equals(empty_dict1, empty_dict2) %} + {% if not result10 %} + {{ exceptions.raise_compiler_error("Failed: Empty dictionaries should be equal") }} + {% endif %} + + {# Test dictionary with different value types #} + {% set dict_mixed1 = {'a': 1, 'b': 'string', 'c': true} %} + {% set dict_mixed2 = {'a': 1, 'b': 'string', 'c': true} %} + {% set result11 = dbt_unittest.dict_equals(dict_mixed1, dict_mixed2) %} + {% if not result11 %} + {{ exceptions.raise_compiler_error("Failed: Dictionaries with mixed value types should be equal") }} + {% endif %} + + {# Test dictionary with different value types - inequality #} + {% set dict_mixed3 = {'a': 1, 'b': 'string', 'c': false} %} + {% set result12 = dbt_unittest.dict_equals(dict_mixed1, dict_mixed3) %} + {% if result12 %} + {{ exceptions.raise_compiler_error("Failed: Dictionaries with different boolean values should not be equal") }} + {% endif %} +{% endmacro %} diff --git a/integration_tests/macros/tests/comparisons/test_equals.sql b/integration_tests/macros/tests/comparisons/test_equals.sql new file mode 100644 index 0000000..a98c083 --- /dev/null +++ b/integration_tests/macros/tests/comparisons/test_equals.sql @@ -0,0 +1,25 @@ +{% macro test_equals() %} + {# Test successful equality #} + {% set result1 = dbt_unittest.equals(5, 5) %} + {% if not result1 %} + {{ exceptions.raise_compiler_error("Failed: 5 should equal 5") }} + {% endif %} + + {# Test failed equality #} + {% set result2 = dbt_unittest.equals(5, 6) %} + {% if result2 %} + {{ exceptions.raise_compiler_error("Failed: 5 should not equal 6") }} + {% endif %} + + {# Test string equality #} + {% set result3 = dbt_unittest.equals('hello', 'hello') %} + {% if not result3 %} + {{ exceptions.raise_compiler_error("Failed: 'hello' should equal 'hello'") }} + {% endif %} + + {# Test string inequality #} + {% set result4 = dbt_unittest.equals('hello', 'world') %} + {% if result4 %} + {{ exceptions.raise_compiler_error("Failed: 'hello' should not equal 'world'") }} + {% endif %} +{% endmacro %} diff --git a/integration_tests/macros/tests/comparisons/test_in.sql b/integration_tests/macros/tests/comparisons/test_in.sql new file mode 100644 index 0000000..80fd9e1 --- /dev/null +++ b/integration_tests/macros/tests/comparisons/test_in.sql @@ -0,0 +1,25 @@ +{% macro test_in() %} + {# Test successful in condition with list #} + {% set result1 = dbt_unittest.in(5, [1, 2, 3, 4, 5]) %} + {% if not result1 %} + {{ exceptions.raise_compiler_error("Failed: 5 should be in [1, 2, 3, 4, 5]") }} + {% endif %} + + {# Test failed in condition with list #} + {% set result2 = dbt_unittest.in(6, [1, 2, 3, 4, 5]) %} + {% if result2 %} + {{ exceptions.raise_compiler_error("Failed: 6 should not be in [1, 2, 3, 4, 5]") }} + {% endif %} + + {# Test successful in condition with string #} + {% set result3 = dbt_unittest.in('a', 'abcde') %} + {% if not result3 %} + {{ exceptions.raise_compiler_error("Failed: 'a' should be in 'abcde'") }} + {% endif %} + + {# Test failed in condition with string #} + {% set result4 = dbt_unittest.in('f', 'abcde') %} + {% if result4 %} + {{ exceptions.raise_compiler_error("Failed: 'f' should not be in 'abcde'") }} + {% endif %} +{% endmacro %} diff --git a/integration_tests/macros/tests/comparisons/test_is_false.sql b/integration_tests/macros/tests/comparisons/test_is_false.sql new file mode 100644 index 0000000..dcd71c9 --- /dev/null +++ b/integration_tests/macros/tests/comparisons/test_is_false.sql @@ -0,0 +1,26 @@ +{% macro test_is_false() %} + {# Test successful false condition #} + {% set result1 = dbt_unittest.is_false(false) %} + {% if not result1 %} + {{ exceptions.raise_compiler_error("Failed: false should be false") }} + {% endif %} + + {# Test failed false condition #} + {% set result2 = dbt_unittest.is_false(true) %} + {% if result2 %} + {{ exceptions.raise_compiler_error("Failed: true should not be false") }} + {% endif %} + + {# Test false with boolean expression #} + {% set x = 5 %} + {% set result3 = dbt_unittest.is_false(x != 5) %} + {% if not result3 %} + {{ exceptions.raise_compiler_error("Failed: 5 != 5 should be false") }} + {% endif %} + + {# Test true with boolean expression #} + {% set result4 = dbt_unittest.is_false(x == 5) %} + {% if result4 %} + {{ exceptions.raise_compiler_error("Failed: 5 == 5 should not be false") }} + {% endif %} +{% endmacro %} diff --git a/integration_tests/macros/tests/comparisons/test_is_none.sql b/integration_tests/macros/tests/comparisons/test_is_none.sql new file mode 100644 index 0000000..224c39d --- /dev/null +++ b/integration_tests/macros/tests/comparisons/test_is_none.sql @@ -0,0 +1,28 @@ +{% macro test_is_none() %} + {# Test successful is_none condition #} + {% set result1 = dbt_unittest.is_none(none) %} + {% if not result1 %} + {{ exceptions.raise_compiler_error("Failed: None should be None") }} + {% endif %} + + {# Test failed is_none conditions #} + {% set result2 = dbt_unittest.is_none(true) %} + {% if result2 %} + {{ exceptions.raise_compiler_error("Failed: true should not be None") }} + {% endif %} + + {% set result3 = dbt_unittest.is_none(1) %} + {% if result3 %} + {{ exceptions.raise_compiler_error("Failed: 1 should not be None") }} + {% endif %} + + {% set result4 = dbt_unittest.is_none([1, 2, 3]) %} + {% if result4 %} + {{ exceptions.raise_compiler_error("Failed: list should not be None") }} + {% endif %} + + {% set result5 = dbt_unittest.is_none({"1": "2"}) %} + {% if result5 %} + {{ exceptions.raise_compiler_error("Failed: dictionary should not be None") }} + {% endif %} +{% endmacro %} diff --git a/integration_tests/macros/tests/comparisons/test_is_true.sql b/integration_tests/macros/tests/comparisons/test_is_true.sql new file mode 100644 index 0000000..04c9c09 --- /dev/null +++ b/integration_tests/macros/tests/comparisons/test_is_true.sql @@ -0,0 +1,26 @@ +{% macro test_is_true() %} + {# Test successful true condition #} + {% set result1 = dbt_unittest.is_true(true) %} + {% if not result1 %} + {{ exceptions.raise_compiler_error("Failed: true should be true") }} + {% endif %} + + {# Test failed true condition #} + {% set result2 = dbt_unittest.is_true(false) %} + {% if result2 %} + {{ exceptions.raise_compiler_error("Failed: false should not be true") }} + {% endif %} + + {# Test true with boolean expression #} + {% set x = 5 %} + {% set result3 = dbt_unittest.is_true(x == 5) %} + {% if not result3 %} + {{ exceptions.raise_compiler_error("Failed: 5 == 5 should be true") }} + {% endif %} + + {# Test false with boolean expression #} + {% set result4 = dbt_unittest.is_true(x != 5) %} + {% if result4 %} + {{ exceptions.raise_compiler_error("Failed: 5 != 5 should not be true") }} + {% endif %} +{% endmacro %} diff --git a/integration_tests/macros/tests/comparisons/test_not_equals.sql b/integration_tests/macros/tests/comparisons/test_not_equals.sql new file mode 100644 index 0000000..bd8ba0d --- /dev/null +++ b/integration_tests/macros/tests/comparisons/test_not_equals.sql @@ -0,0 +1,25 @@ +{% macro test_not_equals() %} + {# Test successful not equals #} + {% set result1 = dbt_unittest.not_equals(5, 6) %} + {% if not result1 %} + {{ exceptions.raise_compiler_error("Failed: 5 should not equal 6") }} + {% endif %} + + {# Test failed not equals #} + {% set result2 = dbt_unittest.not_equals(5, 5) %} + {% if result2 %} + {{ exceptions.raise_compiler_error("Failed: 5 should equal 5") }} + {% endif %} + + {# Test string not equals #} + {% set result3 = dbt_unittest.not_equals('hello', 'world') %} + {% if not result3 %} + {{ exceptions.raise_compiler_error("Failed: 'hello' should not equal 'world'") }} + {% endif %} + + {# Test string equals #} + {% set result4 = dbt_unittest.not_equals('hello', 'hello') %} + {% if result4 %} + {{ exceptions.raise_compiler_error("Failed: 'hello' should equal 'hello'") }} + {% endif %} +{% endmacro %} diff --git a/integration_tests/macros/tests/comparisons/test_not_in.sql b/integration_tests/macros/tests/comparisons/test_not_in.sql new file mode 100644 index 0000000..1acf9f4 --- /dev/null +++ b/integration_tests/macros/tests/comparisons/test_not_in.sql @@ -0,0 +1,25 @@ +{% macro test_not_in() %} + {# Test successful not in condition with list #} + {% set result1 = dbt_unittest.not_in(6, [1, 2, 3, 4, 5]) %} + {% if not result1 %} + {{ exceptions.raise_compiler_error("Failed: 6 should not be in [1, 2, 3, 4, 5]") }} + {% endif %} + + {# Test failed not in condition with list #} + {% set result2 = dbt_unittest.not_in(5, [1, 2, 3, 4, 5]) %} + {% if result2 %} + {{ exceptions.raise_compiler_error("Failed: 5 should be in [1, 2, 3, 4, 5]") }} + {% endif %} + + {# Test successful not in condition with string #} + {% set result3 = dbt_unittest.not_in('f', 'abcde') %} + {% if not result3 %} + {{ exceptions.raise_compiler_error("Failed: 'f' should not be in 'abcde'") }} + {% endif %} + + {# Test failed not in condition with string #} + {% set result4 = dbt_unittest.not_in('a', 'abcde') %} + {% if result4 %} + {{ exceptions.raise_compiler_error("Failed: 'a' should be in 'abcde'") }} + {% endif %} +{% endmacro %} diff --git a/integration_tests/macros/tests/test_macros.sql b/integration_tests/macros/tests/test_macros.sql index 686645e..fa1ecd6 100644 --- a/integration_tests/macros/tests/test_macros.sql +++ b/integration_tests/macros/tests/test_macros.sql @@ -1,21 +1,21 @@ {% macro test_macros() %} {% do test_assert_dict_equals() %} - {% do test_assert_equals() %} - {% do test_assert_false() %} - {% do test_assert_in() %} - {% do test_assert_not_in() %} - {% do test_assert_is_none() %} - {% do test_assert_is_not_none() %} - {% do test_assert_list_equals() %} - {% do test_assert_not_equals() %} - {% do test_assert_true() %} + + {# New comparison macro unit tests #} + {% do test_equals() %} + {% do test_not_equals() %} + {% do test_is_true() %} + {% do test_is_false() %} + {% do test_in() %} + {% do test_not_in() %} + {% do test_dict_equals() %} {% endmacro %} diff --git a/macros/assert_dict_equals.sql b/macros/assert_dict_equals.sql index e2bd87b..4b3aba6 100644 --- a/macros/assert_dict_equals.sql +++ b/macros/assert_dict_equals.sql @@ -1,38 +1,5 @@ -{% macro assert_dict_equals(value, expected) %} - {% if value is not mapping %} - {% do exceptions.raise_compiler_error("FAILED: 1st argument " ~ value ~ " is not a mapping.") %} +{% macro assert_dict_equals(actual, expected) %} + {% if not dbt_unittest.dict_equals(actual, expected) %} + {% do exceptions.raise_compiler_error("FAILED: Dictionaries are not equal.") %} {% endif %} - {% if expected is not mapping %} - {% do exceptions.raise_compiler_error("FAILED: 2nd argument " ~ expected ~ " is not a mapping.") %} - {% endif %} - - {% for k, v in value.items() %} - {% if k not in expected %} - {% do exceptions.raise_compiler_error("FAILED: key " ~ k ~ " of 1st argument is not in " ~ expected ~ ".") %} - {% endif %} - - {% if v is none and expected[k] is not none %} - {% do exceptions.raise_compiler_error("FAILED: values on the key " ~ k ~ " are not same.") %} - {% endif %} - - {% if v != expected[k] %} - {% do exceptions.raise_compiler_error("FAILED: values on the key " ~ k ~ " are not same.") %} - {% endif %} - {% endfor %} - - {% for k, v in expected.items() %} - {% if k not in value %} - {% do exceptions.raise_compiler_error("FAILED: key " ~ k ~ " of 2nd argument is not in " ~ value ~ ".") %} - {% endif %} - - {% if v is none and value[k] is not none %} - {% do exceptions.raise_compiler_error("FAILED: values on the key " ~ k ~ " are not same.") %} - {% endif %} - - {% if v != value[k] %} - {% do exceptions.raise_compiler_error("FAILED: values on the key " ~ k ~ " are not same.") %} - {% endif %} - {% endfor %} - - {% do log("SUCCESS") %} {% endmacro %} diff --git a/macros/assert_equals.sql b/macros/assert_equals.sql index 8dace03..b148c96 100644 --- a/macros/assert_equals.sql +++ b/macros/assert_equals.sql @@ -1,7 +1,5 @@ -{% macro assert_equals(value, expected) %} - {% if value == expected %} - {% do log("SUCCESS") %} - {% else %} - {% do exceptions.raise_compiler_error("FAILED: " ~ value ~ " is not equal to " ~ expected ~ ".") %} +{% macro assert_equals(actual, expected) %} + {% if not dbt_unittest.equals(actual, expected) %} + {% do exceptions.raise_compiler_error("FAILED: " ~ actual ~ " does not equal " ~ expected ~ ".") %} {% endif %} {% endmacro %} diff --git a/macros/assert_false.sql b/macros/assert_false.sql index 34d8f9a..0948136 100644 --- a/macros/assert_false.sql +++ b/macros/assert_false.sql @@ -1,11 +1,5 @@ -{% macro assert_false(value) %} - {% if value is not boolean %} - {% do exceptions.raise_compiler_error("FAILED: " ~ value ~ " is not boolean.") %} - {% endif %} - - {% if value is false %} - {% do log("SUCCESS") %} - {% else %} - {% do exceptions.raise_compiler_error("FAILED: value " ~ value ~ " is not false.") %} +{% macro assert_false(condition) %} + {% if not dbt_unittest.is_false(condition) %} + {% do exceptions.raise_compiler_error("FAILED: Condition is not false.") %} {% endif %} {% endmacro %} diff --git a/macros/assert_in.sql b/macros/assert_in.sql index fbf9713..9a6f45c 100644 --- a/macros/assert_in.sql +++ b/macros/assert_in.sql @@ -1,7 +1,5 @@ {% macro assert_in(value, expected) %} - {% if value in expected %} - {% do log("SUCCESS") %} - {% else %} - {% do exceptions.raise_compiler_error("FAILED: value " ~ value ~ " is not in " ~ expected ~ ".") %} + {% if not dbt_unittest.in(value, expected) %} + {% do exceptions.raise_compiler_error("FAILED: " ~ value ~ " is not in " ~ expected ~ ".") %} {% endif %} {% endmacro %} diff --git a/macros/assert_is_none.sql b/macros/assert_is_none.sql index bca3252..537a1cf 100644 --- a/macros/assert_is_none.sql +++ b/macros/assert_is_none.sql @@ -1,7 +1,5 @@ {% macro assert_is_none(value) %} - {% if value is none %} - {% do log("SUCCESS") %} - {% else %} - {% do exceptions.raise_compiler_error("FAILED: value " ~ value ~ " is not none.") %} + {% if not dbt_unittest.is_none(value) %} + {% do exceptions.raise_compiler_error("FAILED: Value is not None.") %} {% endif %} {% endmacro %} diff --git a/macros/assert_is_not_none.sql b/macros/assert_is_not_none.sql index 1867084..87d770e 100644 --- a/macros/assert_is_not_none.sql +++ b/macros/assert_is_not_none.sql @@ -1,7 +1,5 @@ {% macro assert_is_not_none(value) %} - {% if value is not none %} - {% do log("SUCCESS") %} - {% else %} - {% do exceptions.raise_compiler_error("FAILED: value " ~ value ~ " is none.") %} + {% if not dbt_unittest.is_not_none(value) %} + {% do exceptions.raise_compiler_error("FAILED: Value is None.") %} {% endif %} {% endmacro %} diff --git a/macros/assert_list_equals.sql b/macros/assert_list_equals.sql index 7632eef..3719d0b 100644 --- a/macros/assert_list_equals.sql +++ b/macros/assert_list_equals.sql @@ -1,23 +1,5 @@ -{% macro assert_list_equals(value, expected) %} - {% if value is not iterable %} - {% do exceptions.raise_compiler_error("FAILED: 1st argument " ~ value ~ " is not iterable.") %} +{% macro assert_list_equals(actual, expected) %} + {% if not dbt_unittest.list_equals(actual, expected) %} + {% do exceptions.raise_compiler_error("FAILED: Lists are not equal.") %} {% endif %} - {% if expected is not iterable %} - {% do exceptions.raise_compiler_error("FAILED: 2nd argument " ~ expected ~ " is not iterable.") %} - {% endif %} - - {% set length_of_1st_arg = value | length %} - {% set length_of_2nd_arg = expected | length %} - - {% if length_of_1st_arg != length_of_2nd_arg %} - {% do exceptions.raise_compiler_error("FAILED: lengths of the two are not equal.") %} - {% endif %} - - {% for i in range(length_of_1st_arg) %} - {% if value[i] != expected[i] %} - {% do exceptions.raise_compiler_error("FAILED: values at the index {{ i }} is not equal.") %} - {% endif %} - {% endfor %} - - {% do log("SUCCESS") %} {% endmacro %} diff --git a/macros/assert_not_equals.sql b/macros/assert_not_equals.sql index e041ee6..20682d4 100644 --- a/macros/assert_not_equals.sql +++ b/macros/assert_not_equals.sql @@ -1,7 +1,5 @@ -{% macro assert_not_equals(value, expected) %} - {% if value != expected %} - {% do log("SUCCESS") %} - {% else %} - {% do exceptions.raise_compiler_error("FAILED: " ~ value ~ " is equal to " ~ expected ~ ".") %} +{% macro assert_not_equals(actual, unexpected) %} + {% if not dbt_unittest.not_equals(actual, unexpected) %} + {% do exceptions.raise_compiler_error("FAILED: " ~ actual ~ " should not equal " ~ unexpected ~ ".") %} {% endif %} {% endmacro %} diff --git a/macros/assert_not_in.sql b/macros/assert_not_in.sql index 514f480..d99122a 100644 --- a/macros/assert_not_in.sql +++ b/macros/assert_not_in.sql @@ -1,7 +1,5 @@ -{% macro assert_not_in(value, expected) %} - {% if value not in expected %} - {% do log("SUCCESS") %} - {% else %} - {% do exceptions.raise_compiler_error("FAILED: value " ~ value ~ " is in " ~ expected ~ ".") %} +{% macro assert_not_in(value, unexpected) %} + {% if not dbt_unittest.not_in(value, unexpected) %} + {% do exceptions.raise_compiler_error("FAILED: " ~ value ~ " is in " ~ unexpected ~ ".") %} {% endif %} {% endmacro %} diff --git a/macros/assert_true.sql b/macros/assert_true.sql index 636f0c6..b4b0c05 100644 --- a/macros/assert_true.sql +++ b/macros/assert_true.sql @@ -1,11 +1,5 @@ -{% macro assert_true(value) %} - {% if value is not boolean %} - {% do exceptions.raise_compiler_error("FAILED: " ~ value ~ " is not boolean.") %} - {% endif %} - - {% if value is true %} - {% do log("SUCCESS") %} - {% else %} - {% do exceptions.raise_compiler_error("FAILED: value " ~ value ~ " is not true.") %} +{% macro assert_true(condition) %} + {% if not dbt_unittest.is_true(condition) %} + {% do exceptions.raise_compiler_error("FAILED: Condition is not true.") %} {% endif %} {% endmacro %} diff --git a/macros/comparisons/dict_equals.sql b/macros/comparisons/dict_equals.sql new file mode 100644 index 0000000..1b1a249 --- /dev/null +++ b/macros/comparisons/dict_equals.sql @@ -0,0 +1,40 @@ +{% macro dict_equals(actual, expected) %} + {# Handle None cases #} + {% if actual is none and expected is none %} + {{ return(true) }} + {% endif %} + + {# Check for None values #} + {% if actual is none or expected is none %} + {{ return(false) }} + {% endif %} + + {# Check for mapping types #} + {% if actual is not mapping or expected is not mapping %} + {{ return(false) }} + {% endif %} + + {# Check key lengths #} + {% set actual_keys = actual.keys() | list | sort %} + {% set expected_keys = expected.keys() | list | sort %} + {% if actual_keys | length != expected_keys | length %} + {{ return(false) }} + {% endif %} + + {# Check keys exist and match #} + {% for key in actual_keys %} + {% if key not in expected_keys %} + {{ return(false) }} + {% endif %} + {% endfor %} + + {# Check values #} + {% for key in actual_keys %} + {% if actual[key] != expected[key] %} + {{ return(false) }} + {% endif %} + {% endfor %} + + {# If we've made it this far, dictionaries are equal #} + {{ return(true) }} +{% endmacro %} diff --git a/macros/comparisons/equals.sql b/macros/comparisons/equals.sql new file mode 100644 index 0000000..eeba055 --- /dev/null +++ b/macros/comparisons/equals.sql @@ -0,0 +1,3 @@ +{% macro equals(actual, expected) %} + {{ return(actual == expected) }} +{% endmacro %} diff --git a/macros/comparisons/in.sql b/macros/comparisons/in.sql new file mode 100644 index 0000000..0915a12 --- /dev/null +++ b/macros/comparisons/in.sql @@ -0,0 +1,16 @@ +{% macro in(value, expected) %} + {% set is_in = false %} + + {# Handle different types of collections #} + {% if expected is mapping %} + {% set is_in = value in expected.keys() %} + {% elif expected is iterable and expected is not string %} + {% set is_in = value in expected %} + {% elif expected is string %} + {% set is_in = value in expected %} + {% else %} + {% set is_in = value == expected %} + {% endif %} + + {{ return(is_in) }} +{% endmacro %} diff --git a/macros/comparisons/is_false.sql b/macros/comparisons/is_false.sql new file mode 100644 index 0000000..5afc8b1 --- /dev/null +++ b/macros/comparisons/is_false.sql @@ -0,0 +1,3 @@ +{% macro is_false(condition) %} + {{ return(condition == false or condition is false) }} +{% endmacro %} diff --git a/macros/comparisons/is_none.sql b/macros/comparisons/is_none.sql new file mode 100644 index 0000000..299efcd --- /dev/null +++ b/macros/comparisons/is_none.sql @@ -0,0 +1,3 @@ +{% macro is_none(value) %} + {{ return(value is none) }} +{% endmacro %} diff --git a/macros/comparisons/is_not_none.sql b/macros/comparisons/is_not_none.sql new file mode 100644 index 0000000..304dbea --- /dev/null +++ b/macros/comparisons/is_not_none.sql @@ -0,0 +1,3 @@ +{% macro is_not_none(value) %} + {{ return(value is not none) }} +{% endmacro %} diff --git a/macros/comparisons/is_true.sql b/macros/comparisons/is_true.sql new file mode 100644 index 0000000..3f4c9d0 --- /dev/null +++ b/macros/comparisons/is_true.sql @@ -0,0 +1,3 @@ +{% macro is_true(condition) %} + {{ return(condition == true or condition is true) }} +{% endmacro %} diff --git a/macros/comparisons/not_equals.sql b/macros/comparisons/not_equals.sql new file mode 100644 index 0000000..ad5706d --- /dev/null +++ b/macros/comparisons/not_equals.sql @@ -0,0 +1,3 @@ +{% macro not_equals(actual, unexpected) %} + {{ return(actual != unexpected) }} +{% endmacro %} diff --git a/macros/comparisons/not_in.sql b/macros/comparisons/not_in.sql new file mode 100644 index 0000000..e442611 --- /dev/null +++ b/macros/comparisons/not_in.sql @@ -0,0 +1,16 @@ +{% macro not_in(value, expected) %} + {% set is_not_in = true %} + + {# Handle different types of collections #} + {% if expected is mapping %} + {% set is_not_in = value not in expected.keys() %} + {% elif expected is iterable and expected is not string %} + {% set is_not_in = value not in expected %} + {% elif expected is string %} + {% set is_not_in = value not in expected %} + {% else %} + {% set is_not_in = value != expected %} + {% endif %} + + {{ return(is_not_in) }} +{% endmacro %}