|
3 | 3 | {% set expected_dictionary={
|
4 | 4 | 'col_1': [1, 2, 3],
|
5 | 5 | 'col_2': ['a', 'b', 'c'],
|
6 |
| - 'col_3': [4.1, 5.2, none] |
| 6 | + 'col_3': [True, False, none] |
7 | 7 | } %}
|
8 | 8 |
|
| 9 | +{#- Handle snowflake casing silliness -#} |
| 10 | +{% if target.type == 'snowflake' %} |
| 11 | +{% set expected_dictionary={ |
| 12 | + 'COL_1': [1, 2, 3], |
| 13 | + 'COL_2': ['a', 'b', 'c'], |
| 14 | + 'COL_3': [True, False, none] |
| 15 | +} %} |
| 16 | +{% endif %} |
| 17 | + |
9 | 18 |
|
10 | 19 | {% set actual_dictionary=dbt_utils.get_sql_results_as_dict(
|
11 | 20 | "select * from " ~ ref('data_get_sql_results_as_dict')
|
12 | 21 | ) %}
|
13 |
| - |
14 | 22 | {#-
|
15 |
| -Cast the keys to lower case to handle snowflake silliness |
| 23 | +For reasons that remain unclear, Jinja won't return True for actual_dictionary == expected_dictionary. |
| 24 | +Instead, we'll manually check that the values of these dictionaries are equivalent. |
16 | 25 | -#}
|
17 | 26 |
|
18 |
| -{% set actual_dictionary_with_lower_keys={} %} |
19 |
| -{% for key, values in actual_dictionary.items() %} |
20 |
| - {% do actual_dictionary_with_lower_keys.update({(key | lower): values}) %} |
21 |
| -{% endfor %} |
| 27 | +{% set ns = namespace( |
| 28 | + pass=True, |
| 29 | + err_msg = "" |
| 30 | +) %} |
| 31 | +{% if execute %} |
| 32 | +{#- Check that the dictionaries have the same keys -#} |
| 33 | +{% set expected_keys=expected_dictionary.keys() | list | sort %} |
| 34 | +{% set actual_keys=actual_dictionary.keys() | list | sort %} |
22 | 35 |
|
| 36 | +{% if expected_keys != actual_keys %} |
| 37 | + {% set ns.pass=False %} |
| 38 | + {% set ns.err_msg %} |
| 39 | + The two dictionaries have different keys: |
| 40 | + expected_dictionary has keys: {{ expected_keys }} |
| 41 | + actual_dictionary has keys: {{ actual_keys }} |
| 42 | + {% endset %} |
23 | 43 |
|
24 |
| -{% if actual_dictionary_with_lower_keys['col_1'] | map('int',default=none) | list != expected_dictionary['col_1'] %} |
25 |
| - {# select > 0 rows for test to fail #} |
26 |
| - select 'fail 1' |
| 44 | +{% else %} |
27 | 45 |
|
28 |
| -{% elif actual_dictionary_with_lower_keys['col_2'] | list != expected_dictionary['col_2'] %} |
29 |
| - {# select > 0 rows for test to fail #} |
30 |
| - select 'fail 2' |
| 46 | +{% for key, value in expected_dictionary.items() %} |
| 47 | + {% set expected_length=expected_dictionary[key] | length %} |
| 48 | + {% set actual_length=actual_dictionary[key] | length %} |
31 | 49 |
|
32 |
| -{% elif actual_dictionary_with_lower_keys['col_3'] | map('float',default=none) | list != expected_dictionary['col_3'] %} |
33 |
| - {# select > 0 rows for test to fail #} |
34 |
| - select 'fail 3' |
| 50 | + {% if expected_length != actual_length %} |
| 51 | + {% set ns.pass=False %} |
| 52 | + {% set ns.err_msg %} |
| 53 | + The {{ key }} column has different lengths: |
| 54 | + expected_dictionary[{{ key }}] has length {{ expected_length }} |
| 55 | + actual_dictionary[{{ key }}] has length {{ actual_length }} |
| 56 | + {% endset %} |
35 | 57 |
|
36 |
| -{% else %} |
37 |
| - {# select 0 rows for test to pass #} |
38 |
| - select 'pass' limit 0 |
| 58 | + {% else %} |
| 59 | + |
| 60 | + {% for i in range(value | length) %} |
| 61 | + {% set expected_value=expected_dictionary[key][i] %} |
| 62 | + {% set actual_value=actual_dictionary[key][i] %} |
| 63 | + {% if expected_value != actual_value %} |
| 64 | + {% set ns.pass=False %} |
| 65 | + {% set ns.err_msg %} |
| 66 | + The {{ key }} column has differing values: |
| 67 | + expected_dictionary[{{ key }}][{{ i }}] == {{ expected_value }} |
| 68 | + actual_dictionary[{{ key }}][{{ i }}] == {{ actual_value }} |
| 69 | + {% endset %} |
| 70 | + |
| 71 | + {% endif %} |
| 72 | + {% endfor %} |
| 73 | + {% endif %} |
| 74 | + |
| 75 | +{% endfor %} |
| 76 | + |
| 77 | +{% endif %} |
39 | 78 |
|
| 79 | +{{ log(ns.err_msg, info=True) }} |
| 80 | +select 1 {% if ns.pass %} limit 0 {% endif %} |
40 | 81 | {% endif %}
|
0 commit comments