-
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.
Unit testing feature branch pull request (#8411)
* Initial implementation of unit testing (from pr #2911) Co-authored-by: Michelle Ark <michelle.ark@dbtlabs.com> * 8295 unit testing artifacts (#8477) * unit test config: tags & meta (#8565) * Add additional functional test for unit testing selection, artifacts, etc (#8639) * Enable inline csv format in unit testing (#8743) * Support unit testing incremental models (#8891) * update unit test key: unit -> unit-tests (#8988) * convert to use unit test name at top level key (#8966) * csv file fixtures (#9044) * Unit test support for `state:modified` and `--defer` (#9032) Co-authored-by: Michelle Ark <michelle.ark@dbtlabs.com> * Allow use of sources as unit testing inputs (#9059) * Use daff for diff formatting in unit testing (#8984) * Fix #8652: Use seed file from disk for unit testing if rows not specified in YAML config (#9064) Co-authored-by: Michelle Ark <MichelleArk@users.noreply.github.com> Fix #8652: Use seed value if rows not specified * Move unit testing to test and build commands (#9108) * Enable unit testing in non-root packages (#9184) * convert test to data_test (#9201) * Make fixtures files full-fledged members of manifest and enable partial parsing (#9225) * In build command run unit tests before models (#9273) --------- Co-authored-by: Michelle Ark <michelle.ark@dbtlabs.com> Co-authored-by: Michelle Ark <MichelleArk@users.noreply.github.com> Co-authored-by: Emily Rockman <emily.rockman@dbtlabs.com> Co-authored-by: Jeremy Cohen <jeremy@dbtlabs.com> Co-authored-by: Kshitij Aranke <kshitij.aranke@dbtlabs.com>
- Loading branch information
1 parent
15704ab
commit b5a0c4c
Showing
167 changed files
with
12,171 additions
and
4,083 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Initial implementation of unit testing | ||
time: 2023-08-02T14:50:11.391992-04:00 | ||
custom: | ||
Author: gshank | ||
Issue: "8287" |
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,6 @@ | ||
kind: Features | ||
body: Unit test manifest artifacts and selection | ||
time: 2023-08-28T10:18:25.958929-04:00 | ||
custom: | ||
Author: gshank | ||
Issue: "8295" |
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,6 @@ | ||
kind: Features | ||
body: Support config with tags & meta for unit tests | ||
time: 2023-09-06T23:47:41.059915-04:00 | ||
custom: | ||
Author: michelleark | ||
Issue: "8294" |
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,6 @@ | ||
kind: Features | ||
body: Enable inline csv fixtures in unit tests | ||
time: 2023-09-28T16:32:05.573776-04:00 | ||
custom: | ||
Author: gshank | ||
Issue: "8626" |
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,6 @@ | ||
kind: Features | ||
body: Support unit testing incremental models | ||
time: 2023-11-01T10:18:45.341781-04:00 | ||
custom: | ||
Author: michelleark | ||
Issue: "8422" |
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,6 @@ | ||
kind: Features | ||
body: Add support of csv file fixtures to unit testing | ||
time: 2023-11-06T19:47:52.501495-06:00 | ||
custom: | ||
Author: emmyoop | ||
Issue: "8290" |
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,6 @@ | ||
kind: Features | ||
body: Unit tests support --defer and state:modified | ||
time: 2023-11-07T23:10:06.376588-05:00 | ||
custom: | ||
Author: jtcohen6 | ||
Issue: "8517" |
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,6 @@ | ||
kind: Features | ||
body: Support source inputs in unit tests | ||
time: 2023-11-11T19:11:50.870494-05:00 | ||
custom: | ||
Author: gshank | ||
Issue: "8507" |
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,6 @@ | ||
kind: Features | ||
body: Use daff to render diff displayed in stdout when unit test fails | ||
time: 2023-11-14T10:15:55.689307-05:00 | ||
custom: | ||
Author: michelleark | ||
Issue: "8558" |
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,6 @@ | ||
kind: Features | ||
body: Move unit testing to test command | ||
time: 2023-11-16T14:40:06.121336-05:00 | ||
custom: | ||
Author: gshank | ||
Issue: "8979" |
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,6 @@ | ||
kind: Features | ||
body: Support unit tests in non-root packages | ||
time: 2023-11-30T13:09:48.206007-05:00 | ||
custom: | ||
Author: gshank | ||
Issue: "8285" |
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,7 @@ | ||
kind: Features | ||
body: Convert the `tests` config to `data_tests` in both dbt_project.yml and schema files. | ||
in schema files. | ||
time: 2023-12-05T13:17:17.647765-06:00 | ||
custom: | ||
Author: emmyoop | ||
Issue: "8699" |
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,6 @@ | ||
kind: Features | ||
body: Make fixture files full-fledged parts of the manifest and enable partial parsing | ||
time: 2023-12-05T20:04:47.117029-05:00 | ||
custom: | ||
Author: gshank | ||
Issue: "9067" |
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,6 @@ | ||
kind: Features | ||
body: In build command run unit tests before models | ||
time: 2023-12-12T15:05:56.778829-05:00 | ||
custom: | ||
Author: gshank | ||
Issue: "9128" |
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,6 @@ | ||
kind: Fixes | ||
body: Use seed file from disk for unit testing if rows not specified in YAML config | ||
time: 2023-11-13T15:45:35.008565Z | ||
custom: | ||
Author: aranke | ||
Issue: "8652" |
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,6 @@ | ||
kind: Under the Hood | ||
body: Add unit testing functional tests | ||
time: 2023-09-12T19:05:06.023126-04:00 | ||
custom: | ||
Author: gshank | ||
Issue: "8512" |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
29 changes: 29 additions & 0 deletions
29
core/dbt/adapters/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,29 @@ | ||
{%- materialization unit, default -%} | ||
|
||
{% set relations = [] %} | ||
|
||
{% set expected_rows = config.get('expected_rows') %} | ||
{% 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 = {} -%} | ||
{%- for column in columns_in_relation -%} | ||
{%- do column_name_to_data_types.update({column.name: column.dtype}) -%} | ||
{%- endfor -%} | ||
|
||
{% set unit_test_sql = get_unit_test_sql(sql, get_expected_sql(expected_rows, column_name_to_data_types), tested_expected_column_names) %} | ||
|
||
{% call statement('main', fetch_result=True) -%} | ||
|
||
{{ unit_test_sql }} | ||
|
||
{%- endcall %} | ||
|
||
{% do adapter.drop_relation(temp_relation) %} | ||
|
||
{{ return({'relations': relations}) }} | ||
|
||
{%- endmaterialization -%} |
76 changes: 76 additions & 0 deletions
76
core/dbt/adapters/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,76 @@ | ||
{% macro get_fixture_sql(rows, column_name_to_data_types) %} | ||
-- Fixture for {{ model.name }} | ||
{% set default_row = {} %} | ||
|
||
{%- if not column_name_to_data_types -%} | ||
{%- set columns_in_relation = adapter.get_columns_in_relation(this) -%} | ||
{%- set column_name_to_data_types = {} -%} | ||
{%- for column in columns_in_relation -%} | ||
{%- do column_name_to_data_types.update({column.name: column.dtype}) -%} | ||
{%- endfor -%} | ||
{%- endif -%} | ||
|
||
{%- if not column_name_to_data_types -%} | ||
{{ exceptions.raise_compiler_error("Not able to get columns for unit test '" ~ model.name ~ "' from relation " ~ this) }} | ||
{%- endif -%} | ||
|
||
{%- for column_name, column_type in column_name_to_data_types.items() -%} | ||
{%- do default_row.update({column_name: (safe_cast("null", column_type) | trim )}) -%} | ||
{%- endfor -%} | ||
|
||
{%- for row in rows -%} | ||
{%- do format_row(row, column_name_to_data_types) -%} | ||
{%- 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 %} | ||
|
||
|
||
{% macro get_expected_sql(rows, column_name_to_data_types) %} | ||
|
||
{%- if (rows | length) == 0 -%} | ||
select * FROM dbt_internal_unit_test_actual | ||
limit 0 | ||
{%- else -%} | ||
{%- for row in rows -%} | ||
{%- do format_row(row, column_name_to_data_types) -%} | ||
select | ||
{%- for column_name, column_value in row.items() %} {{ column_value }} AS {{ column_name }}{% if not loop.last -%}, {%- endif %} | ||
{%- endfor %} | ||
{%- if not loop.last %} | ||
union all | ||
{% endif %} | ||
{%- endfor -%} | ||
{%- endif -%} | ||
|
||
{% endmacro %} | ||
|
||
{%- macro format_row(row, column_name_to_data_types) -%} | ||
|
||
{#-- wrap yaml strings in quotes, apply cast --#} | ||
{%- 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]) } -%} | ||
{%- elif column_value is none -%} | ||
{%- set row_update = {column_name: safe_cast('null', column_name_to_data_types[column_name]) } -%} | ||
{%- else -%} | ||
{%- set row_update = {column_name: safe_cast(column_value, column_name_to_data_types[column_name]) } -%} | ||
{%- endif -%} | ||
{%- do row.update(row_update) -%} | ||
{%- endfor -%} | ||
|
||
{%- 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
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.