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 53d7a93b..db6e2375 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 -%} @@ -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 @@ -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 @@ -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