-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a88f640
commit 0ab9222
Showing
16 changed files
with
466 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
core/dbt/include/global_project/macros/materializations/tests/unit.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{%- materialization unit, default -%} | ||
|
||
{% set relations = [] %} | ||
|
||
{% set expected_rows = config.get('expected_rows') %} | ||
{% set all_expected_column_names = get_columns_in_query(sql) %} | ||
{% set tested_expected_column_names = expected_rows[0].keys() if (expected_rows | length ) > 0 else all_expected_column_names %} | ||
{% set unit_test_sql = get_unit_test_sql(sql, get_fixture_sql(expected_rows, all_expected_column_names), tested_expected_column_names) %} | ||
|
||
{% if should_store_failures() %} | ||
|
||
{% set identifier = model['alias'] %} | ||
{% set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) %} | ||
{% set target_relation = api.Relation.create( | ||
identifier=identifier, schema=schema, database=database, type='table') -%} %} | ||
|
||
{% if old_relation %} | ||
{% do adapter.drop_relation(old_relation) %} | ||
{% endif %} | ||
|
||
{% call statement(auto_begin=True) %} | ||
{{ create_table_as(False, target_relation, unit_test_sql) }} | ||
{% endcall %} | ||
|
||
{% do relations.append(target_relation) %} | ||
|
||
{% set main_sql %} | ||
select * | ||
from {{ target_relation }} | ||
{% endset %} | ||
|
||
{{ adapter.commit() }} | ||
|
||
{% else %} | ||
|
||
{% set main_sql = unit_test_sql %} | ||
|
||
{% endif %} | ||
|
||
{% call statement('main', fetch_result=True) -%} | ||
|
||
{{ main_sql }} | ||
|
||
{%- endcall %} | ||
|
||
{{ return({'relations': relations}) }} | ||
|
||
{%- endmaterialization -%} |
27 changes: 27 additions & 0 deletions
27
core/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{% macro get_fixture_sql(rows, column_names) %} | ||
|
||
{% set default_row = {} %} | ||
{%- for column_name in column_names -%} | ||
{%- do default_row.update({column_name: "null"}) -%} | ||
{%- endfor -%} | ||
|
||
{% for row in rows -%} | ||
{%- set default_row_copy = default_row.copy() -%} | ||
{%- do default_row_copy.update(row) -%} | ||
SELECT | ||
{%- for column_name, column_value in default_row_copy.items() %} {{ column_value }} AS {{ column_name }}{% if not loop.last -%},{%- endif %} | ||
{%- endfor %} | ||
{%- if not loop.last %} | ||
UNION ALL | ||
{% endif %} | ||
{%- endfor %} | ||
|
||
|
||
{%- if (rows | length) == 0 %} | ||
SELECT | ||
{%- for column_name, column_value in default_row.items() %} {{ column_value }} AS {{ column_name }}{% if not loop.last -%},{%- endif %} | ||
{%- endfor %} | ||
LIMIT 0 | ||
{% endif %} | ||
|
||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.