Skip to content

Commit

Permalink
Column names can be used for grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
wouter committed Aug 24, 2023
1 parent 6841d4e commit db50506
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
17 changes: 8 additions & 9 deletions macros/tests.sql
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,20 @@
{% set expectations = test_configuration.expectations %}
{% set model_node = dbt_unit_testing.model_node(test_configuration.model_node) %}
{%- set model_complete_sql = dbt_unit_testing.build_model_complete_sql(model_node, test_configuration.mocks, test_configuration.options) -%}
{% set columns = dbt_unit_testing.quote_and_join_columns(dbt_unit_testing.extract_columns_list(expectations.input_values)) %}
{% set convert_columns = test_configuration.options.convert_columns | default("convert_columns") %}
{% set select_columns = dbt_unit_testing.quote_and_join_columns(dbt_unit_testing.extract_columns_list(expectations.input_values), convert_columns, add_alias=true) %}
{% set group_columns = dbt_unit_testing.quote_and_join_columns(dbt_unit_testing.extract_columns_list(expectations.input_values), convert_columns) %}
{% set result_columns = dbt_unit_testing.quote_and_join_columns(dbt_unit_testing.extract_columns_list(expectations.input_values)) %}
{% set select_columns = dbt_unit_testing.quote_and_join_columns(dbt_unit_testing.extract_columns_list(expectations.input_values), convert_columns) %}

{% set diff_column = test_configuration.options.diff_column | default("diff") %}
{% set count_column = test_configuration.options.count_column | default("count") %}
{% set udf_definitions = test_configuration.options.udf_definitions | default("udf_definitions") %}

{%- set actual_query -%}
select count(1) as {{ count_column }}, {{ select_columns }} from ( {{ model_complete_sql }} ) as s group by {{ group_columns }}
select count(1) as {{ count_column }}, {{ select_columns }} from ( {{ model_complete_sql }} ) as s group by {{ columns }}
{% endset %}

{%- set expectations_query -%}
select count(1) as {{ count_column }}, {{ select_columns }} from ({{ expectations.input_values }}) as s group by {{ group_columns }}
select count(1) as {{ count_column }}, {{ select_columns }} from ({{ expectations.input_values }}) as s group by {{ columns }}
{% endset %}

{%- set test_query -%}
Expand All @@ -106,14 +105,14 @@
),

extra_entries as (
select '+' as {{ diff_column }}, {{ count_column }}, {{ result_columns }} from actual
select '+' as {{ diff_column }}, {{ count_column }}, {{ columns }} from actual
{{ except() }}
select '+' as {{ diff_column }}, {{ count_column }}, {{ result_columns }} from expectations),
select '+' as {{ diff_column }}, {{ count_column }}, {{ columns }} from expectations),

missing_entries as (
select '-' as {{ diff_column }}, {{ count_column }}, {{ result_columns }} from expectations
select '-' as {{ diff_column }}, {{ count_column }}, {{ columns }} from expectations
{{ except() }}
select '-' as {{ diff_column }}, {{ count_column }}, {{ result_columns }} from actual)
select '-' as {{ diff_column }}, {{ count_column }}, {{ columns }} from actual)

select * from extra_entries
UNION ALL
Expand Down
7 changes: 2 additions & 5 deletions macros/utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@
{{ return(columns) }}
{% endmacro %}

{% macro quote_and_join_columns(columns, convert_columns, add_alias = false) %}
{% macro quote_and_join_columns(columns, convert_columns) %}
{% set result = [] %}
{% for column in columns %}
{% if column in convert_columns %}
{% set modified_column = "TO_JSON_STRING(" ~ dbt_unit_testing.quote_identifier(column) ~ ")" %}
{% if add_alias %}
{% set modified_column = modified_column ~ " AS " ~ dbt_unit_testing.quote_identifier(column) %}
{% endif %}
{% set modified_column = "TO_JSON_STRING(" ~ dbt_unit_testing.quote_identifier(column) ~ ")" ~ " AS " ~ dbt_unit_testing.quote_identifier(column) %}
{% else %}
{% set modified_column = dbt_unit_testing.quote_identifier(column) %}
{% endif %}
Expand Down

0 comments on commit db50506

Please sign in to comment.