Skip to content

Commit

Permalink
Apply column quoting to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbeatty10 committed May 10, 2024
1 parent d831caa commit 2df4a14
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
13 changes: 10 additions & 3 deletions dbt/include/global_project/macros/materializations/tests/unit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@

{% set expected_rows = config.get('expected_rows') %}
{% set expected_sql = config.get('expected_sql') %}
{% set tested_expected_column_names = expected_rows[0].keys() if (expected_rows | length ) > 0 else get_columns_in_query(sql) %} %}
{% set tested_expected_column_names = expected_rows[0].keys() if (expected_rows | length ) > 0 else get_columns_in_query(sql) %}

{%- set target_relation = this.incorporate(type='table') -%}
{%- set temp_relation = make_temp_relation(target_relation)-%}
{% do run_query(get_create_table_as_sql(True, temp_relation, get_empty_subquery_sql(sql))) %}
{%- set columns_in_relation = adapter.get_columns_in_relation(temp_relation) -%}
{%- set column_name_to_data_types = {} -%}
{%- set column_name_to_quoted = {} -%}
{%- for column in columns_in_relation -%}
{%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%}
{%- do column_name_to_quoted.update({column.name|lower: column.quoted}) -%}
{%- endfor -%}

{%- set expected_column_names_quoted = [] -%}
{%- for column_name in tested_expected_column_names -%}
{%- do expected_column_names_quoted.append(column_name_to_quoted[column_name]) -%}
{%- endfor -%}

{% if not expected_sql %}
{% set expected_sql = get_expected_sql(expected_rows, column_name_to_data_types) %}
{% set expected_sql = get_expected_sql(expected_rows, column_name_to_data_types, column_name_to_quoted) %}
{% endif %}
{% set unit_test_sql = get_unit_test_sql(sql, expected_sql, tested_expected_column_names) %}
{% set unit_test_sql = get_unit_test_sql(sql, expected_sql, expected_column_names_quoted) %}

{% call statement('main', fetch_result=True) -%}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
{%- set columns_in_relation = adapter.get_columns_in_relation(this_or_defer_relation) -%}

{%- set column_name_to_data_types = {} -%}
{%- set column_name_to_quoted = {} -%}
{%- for column in columns_in_relation -%}

{#-- This needs to be a case-insensitive comparison --#}
{%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%}
{%- do column_name_to_quoted.update({column.name|lower: column.quoted}) -%}
{%- endfor -%}
{%- endif -%}

Expand All @@ -28,7 +31,7 @@
{%- set default_row_copy = default_row.copy() -%}
{%- do default_row_copy.update(formatted_row) -%}
select
{%- for column_name, column_value in default_row_copy.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%}, {%- endif %}
{%- for column_name, column_value in default_row_copy.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%}, {%- endif %}
{%- endfor %}
{%- if not loop.last %}
union all
Expand All @@ -37,14 +40,14 @@ union all

{%- if (rows | length) == 0 -%}
select
{%- for column_name, column_value in default_row.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%},{%- endif %}
{%- for column_name, column_value in default_row.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%},{%- endif %}
{%- endfor %}
limit 0
{%- endif -%}
{% endmacro %}


{% macro get_expected_sql(rows, column_name_to_data_types) %}
{% macro get_expected_sql(rows, column_name_to_data_types, column_name_to_quoted) %}

{%- if (rows | length) == 0 -%}
select * from dbt_internal_unit_test_actual
Expand All @@ -53,7 +56,7 @@ union all
{%- for row in rows -%}
{%- set formatted_row = format_row(row, column_name_to_data_types) -%}
select
{%- for column_name, column_value in formatted_row.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%}, {%- endif %}
{%- for column_name, column_value in formatted_row.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%}, {%- endif %}
{%- endfor %}
{%- if not loop.last %}
union all
Expand Down

0 comments on commit 2df4a14

Please sign in to comment.