Skip to content

Commit

Permalink
added unit test for uniqueness, fixed quoting bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin committed Jan 1, 2024
1 parent 1b858a3 commit c878026
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


{% set actual_yaml = toyaml(fromjson(tojson(
testgen.get_uniqueness_test_suggestions(
ref('colnames_with_spaces'),
compound_key_length = 1
)
)))
%}

{% set expected_yaml %}
models:
- name: colnames_with_spaces
columns:
- name: First Name
description: Uniqueness test generated by dbt-testgen
tests:
- unique
- not_null
- name: Age (Years)
description: Uniqueness test generated by dbt-testgen
tests:
- unique
- not_null
- name: Current City
description: Uniqueness test generated by dbt-testgen
tests:
- unique
- not_null
{% endset %}

{{ assert_equal (actual_yaml | trim, expected_yaml | trim) }}
File renamed without changes.
6 changes: 5 additions & 1 deletion macros/test_generation/generate_uniqueness_tests.sql
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@

{% set count_distinct_exprs = [] %}
{% for column_combo in column_combinations %}
{% set column_combo_quoted = [] %}
{% for col in column_combo %}
{% do column_combo_quoted.append(adapter.quote(col))%}
{% endfor %}
{% do count_distinct_exprs.append(
"SELECT " ~ loop.index ~ " AS ordering, count(1) AS cardinality
from (
SELECT 1 FROM " ~ table_relation ~ "
GROUP BY " ~ column_combo|join(", ") ~ "
GROUP BY " ~ column_combo_quoted|join(", ") ~ "
) t"
) %}
{% endfor %}
Expand Down

0 comments on commit c878026

Please sign in to comment.