Skip to content

Commit 02c5dd0

Browse files
committed
Write fancy test
1 parent 233136a commit 02c5dd0

File tree

3 files changed

+63
-22
lines changed

3 files changed

+63
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ This macro returns a dictionary from a sql query, so that you don't need to inte
220220
Usage:
221221
```
222222
-- Returns a dictionary of the users table where the state is California
223-
{% set california_cities = dbt_utils.get_sql_results_as_dict("select * from" ~ ref('cities') ~ "where state = 'CA' ") %}
223+
{% set california_cities = dbt_utils.get_sql_results_as_dict("select * from" ~ ref('cities') ~ "where state = 'CA' and city is not null ") %}
224224
select
225225
city,
226226
{% for city in california_cities %}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
col_1,col_2,col_3
2-
1,a,4.1
3-
2,b,5.2
2+
1,a,True
3+
2,b,False
44
3,c,

integration_tests/tests/assert_get_sql_results_as_dict_objects_equal.sql

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,79 @@
33
{% set expected_dictionary={
44
'col_1': [1, 2, 3],
55
'col_2': ['a', 'b', 'c'],
6-
'col_3': [4.1, 5.2, none]
6+
'col_3': [True, False, none]
77
} %}
88

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+
918

1019
{% set actual_dictionary=dbt_utils.get_sql_results_as_dict(
1120
"select * from " ~ ref('data_get_sql_results_as_dict')
1221
) %}
13-
1422
{#-
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 this dictionaries are equivalent.
1625
-#}
1726

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 %}
2235

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 %}
2343

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 %}
2745

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 %}
3149

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 %}
3557

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 %}
3978

79+
{{ log(ns.err_msg, info=True) }}
80+
select 1 {% if ns.pass %} limit 0 {% endif %}
4081
{% endif %}

0 commit comments

Comments
 (0)