From 3f2386df0ae90534ad5947f616340419a6cc97f0 Mon Sep 17 00:00:00 2001 From: Gerda Shank Date: Thu, 11 Apr 2024 16:04:28 -0400 Subject: [PATCH] Changes to support unit test sql format (#146) --- .changes/unreleased/Features-20240409-084844.yaml | 6 ++++++ .../global_project/macros/materializations/tests/unit.sql | 8 ++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 .changes/unreleased/Features-20240409-084844.yaml diff --git a/.changes/unreleased/Features-20240409-084844.yaml b/.changes/unreleased/Features-20240409-084844.yaml new file mode 100644 index 00000000..4fceebda --- /dev/null +++ b/.changes/unreleased/Features-20240409-084844.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Support for sql fixtures in unit testing +time: 2024-04-09T08:48:44.441106-04:00 +custom: + Author: gshank + Issue: "148" diff --git a/dbt/include/global_project/macros/materializations/tests/unit.sql b/dbt/include/global_project/macros/materializations/tests/unit.sql index 6d7b632c..78c6f6bc 100644 --- a/dbt/include/global_project/macros/materializations/tests/unit.sql +++ b/dbt/include/global_project/macros/materializations/tests/unit.sql @@ -3,6 +3,7 @@ {% set relations = [] %} {% 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 target_relation = this.incorporate(type='table') -%} @@ -11,10 +12,13 @@ {%- set columns_in_relation = adapter.get_columns_in_relation(temp_relation) -%} {%- set column_name_to_data_types = {} -%} {%- for column in columns_in_relation -%} - {%- do column_name_to_data_types.update({column.name|lower: 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) %} + {% if not expected_sql %} + {% set expected_sql = get_expected_sql(expected_rows, column_name_to_data_types) %} + {% endif %} + {% set unit_test_sql = get_unit_test_sql(sql, expected_sql, tested_expected_column_names) %} {% call statement('main', fetch_result=True) -%}