Skip to content

Commit

Permalink
Support nested dictionaries (#93)
Browse files Browse the repository at this point in the history
Signed-off-by: Yu Ishikawa <yu-iskw@users.noreply.github.com>
  • Loading branch information
yu-iskw authored Nov 27, 2024
1 parent aadab69 commit f5b5525
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ jobs:
python-version: "3.9"
- pip-requirements: requirements-1.4.txt
python-version: "3.10"
- pip-requirements: requirements-1.4.txt
python-version: "3.11"
- pip-requirements: requirements-1.5.txt
python-version: "3.9"
- pip-requirements: requirements-1.5.txt
Expand Down
30 changes: 30 additions & 0 deletions integration_tests/macros/tests/comparisons/test_dict_equals.sql
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@
{{ exceptions.raise_compiler_error("Failed: Nested dictionaries with different values should not be equal") }}
{% endif %}

{# Test nested dictionaries with additional nested levels #}
{% set nested_dict4 = {'a': 1, 'b': {'c': {'x': 10}, 'd': 3}} %}
{% set nested_dict5 = {'a': 1, 'b': {'c': {'x': 10}, 'd': 3}} %}
{% set result13 = dbt_unittest.dict_equals(nested_dict4, nested_dict5) %}
{% if not result13 %}
{{ exceptions.raise_compiler_error("Failed: Identical deeply nested dictionaries should be equal") }}
{% endif %}

{# Test nested dictionaries with differing nested levels #}
{% set nested_dict6 = {'a': 1, 'b': {'c': {'x': 11}, 'd': 3}} %}
{% set result14 = dbt_unittest.dict_equals(nested_dict4, nested_dict6) %}
{% if result14 %}
{{ exceptions.raise_compiler_error("Failed: Deeply nested dictionaries with different values should not be equal") }}
{% endif %}

{# Test nested dictionaries with lists as values #}
{% set nested_dict7 = {'a': 1, 'b': {'c': [1, 2, 3], 'd': 3}} %}
{% set nested_dict8 = {'a': 1, 'b': {'c': [1, 2, 3], 'd': 3}} %}
{% set result15 = dbt_unittest.dict_equals(nested_dict7, nested_dict8) %}
{% if not result15 %}
{{ exceptions.raise_compiler_error("Failed: Identical nested dictionaries with list values should be equal") }}
{% endif %}

{# Test nested dictionaries with lists containing different elements #}
{% set nested_dict9 = {'a': 1, 'b': {'c': [1, 2, 4], 'd': 3}} %}
{% set result16 = dbt_unittest.dict_equals(nested_dict7, nested_dict9) %}
{% if result16 %}
{{ exceptions.raise_compiler_error("Failed: Nested dictionaries with lists containing different elements should not be equal") }}
{% endif %}

{# Test empty dictionary equality #}
{% set empty_dict1 = {} %}
{% set empty_dict2 = {} %}
Expand Down
8 changes: 6 additions & 2 deletions macros/comparisons/dict_equals.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
{% endif %}
{% endfor %}

{# Check values #}
{# Check values, recursively for nested dicts #}
{% for key in actual_keys %}
{% if actual[key] != expected[key] %}
{% if actual[key] is mapping and expected[key] is mapping %}
{% if not dbt_unittest.dict_equals(actual[key], expected[key]) %}
{{ return(false) }}
{% endif %}
{% elif actual[key] != expected[key] %}
{{ return(false) }}
{% endif %}
{% endfor %}
Expand Down

0 comments on commit f5b5525

Please sign in to comment.