diff --git a/.changes/unreleased/Fixes-20240512-095350.yaml b/.changes/unreleased/Fixes-20240512-095350.yaml new file mode 100644 index 00000000..ef4b27c5 --- /dev/null +++ b/.changes/unreleased/Fixes-20240512-095350.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Apply column quoting to unit tests +time: 2024-05-12T09:53:50.381050004-04:00 +custom: + Author: mpatek dbeatty10 + Issue: "205" diff --git a/dbt/include/global_project/macros/materializations/tests/helpers.sql b/dbt/include/global_project/macros/materializations/tests/helpers.sql index a385d1ea..1718bbba 100644 --- a/dbt/include/global_project/macros/materializations/tests/helpers.sql +++ b/dbt/include/global_project/macros/materializations/tests/helpers.sql @@ -24,7 +24,7 @@ -- Build actual result given inputs with dbt_internal_unit_test_actual as ( select - {% for expected_column_name in expected_column_names %}{{expected_column_name}}{% if not loop.last -%},{% endif %}{%- endfor -%}, {{ dbt.string_literal("actual") }} as {{ adapter.quote("actual_or_expected") }} + {% for expected_column_name in expected_column_names %}{{ adapter.quote(expected_column_name) }}{% if not loop.last -%},{% endif %}{%- endfor -%}, {{ dbt.string_literal("actual") }} as {{ adapter.quote("actual_or_expected") }} from ( {{ main_sql }} ) _dbt_internal_unit_test_actual @@ -32,7 +32,7 @@ with dbt_internal_unit_test_actual as ( -- Build expected result dbt_internal_unit_test_expected as ( select - {% for expected_column_name in expected_column_names %}{{expected_column_name}}{% if not loop.last -%}, {% endif %}{%- endfor -%}, {{ dbt.string_literal("expected") }} as {{ adapter.quote("actual_or_expected") }} + {% for expected_column_name in expected_column_names %}{{ adapter.quote(expected_column_name) }}{% if not loop.last -%}, {% endif %}{%- endfor -%}, {{ dbt.string_literal("expected") }} as {{ adapter.quote("actual_or_expected") }} from ( {{ expected_fixture_sql }} ) _dbt_internal_unit_test_expected diff --git a/dbt/include/global_project/macros/materializations/tests/unit.sql b/dbt/include/global_project/macros/materializations/tests/unit.sql index 78c6f6bc..40795426 100644 --- a/dbt/include/global_project/macros/materializations/tests/unit.sql +++ b/dbt/include/global_project/macros/materializations/tests/unit.sql @@ -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) -%} diff --git a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql index a3a8173b..e71262d6 100644 --- a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql +++ b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql @@ -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 -%} @@ -29,7 +32,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 @@ -38,14 +41,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 @@ -54,7 +57,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