diff --git a/core/dbt/include/global_project/macros/materializations/tests/helpers.sql b/core/dbt/include/global_project/macros/materializations/tests/helpers.sql index 579def4f17c..f999e548bfb 100644 --- a/core/dbt/include/global_project/macros/materializations/tests/helpers.sql +++ b/core/dbt/include/global_project/macros/materializations/tests/helpers.sql @@ -22,7 +22,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 actual_or_expected + {% 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") }} from ( {{ main_sql }} ) _dbt_internal_unit_test_actual @@ -30,7 +30,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 actual_or_expected + {% 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") }} from ( {{ expected_fixture_sql }} ) _dbt_internal_unit_test_expected diff --git a/core/dbt/include/global_project/macros/materializations/tests/unit.sql b/core/dbt/include/global_project/macros/materializations/tests/unit.sql index e3cf7f1fb5c..2d69c0248c2 100644 --- a/core/dbt/include/global_project/macros/materializations/tests/unit.sql +++ b/core/dbt/include/global_project/macros/materializations/tests/unit.sql @@ -10,7 +10,7 @@ {% set columns_in_relation = adapter.get_column_schema_from_query(get_empty_subquery_sql(sql)) %} {%- set column_name_to_data_types = {} -%} {%- for column in columns_in_relation -%} - {%- do column_name_to_data_types.update({column.name: column.data_type}) -%} + {%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%} {%- endfor -%} {% set unit_test_sql = get_unit_test_sql(sql, get_expected_sql(expected_rows, column_name_to_data_types), tested_expected_column_names) %} diff --git a/core/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql b/core/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql index 9138330b8ff..1a50242b567 100644 --- a/core/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql +++ b/core/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql @@ -9,7 +9,8 @@ {%- set column_name_to_data_types = {} -%} {%- for column in columns_in_relation -%} -{%- do column_name_to_data_types.update({column.name: column.data_type}) -%} +{#-- This needs to be a case-insensitive comparison --#} +{%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%} {%- endfor -%} {%- endif -%} @@ -67,7 +68,7 @@ union all {%- for column_name, column_value in row.items() -%} {% set row_update = {column_name: column_value} %} {%- if column_value is string -%} -{%- set row_update = {column_name: safe_cast(dbt.string_literal(column_value), column_name_to_data_types[column_name]) } -%} +{%- set row_update = {column_name: safe_cast(dbt.string_literal(dbt.escape_single_quotes(column_value)), column_name_to_data_types[column_name]) } -%} {%- elif column_value is none -%} {%- set row_update = {column_name: safe_cast('null', column_name_to_data_types[column_name]) } -%} {%- else -%}