-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fewer_rows_than schema test (#343)
- Loading branch information
Showing
6 changed files
with
76 additions
and
0 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
4 changes: 4 additions & 0 deletions
4
integration_tests/data/schema_tests/data_test_fewer_rows_than_table_1.csv
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,4 @@ | ||
field | ||
1 | ||
2 | ||
3 |
5 changes: 5 additions & 0 deletions
5
integration_tests/data/schema_tests/data_test_fewer_rows_than_table_2.csv
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,5 @@ | ||
field | ||
1 | ||
2 | ||
3 | ||
4 |
9 changes: 9 additions & 0 deletions
9
integration_tests/models/schema_tests/test_fewer_rows_than.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,9 @@ | ||
with data as ( | ||
|
||
select * from {{ ref('data_test_fewer_rows_than_table_1') }} | ||
|
||
) | ||
|
||
select | ||
field | ||
from data |
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,43 @@ | ||
{% macro test_fewer_rows_than(model) %} | ||
{{ return(adapter.dispatch('test_fewer_rows_than', packages = dbt_utils._get_utils_namespaces())(model, combination_of_columns, quote_columns, where)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__test_fewer_rows_than(model) %} | ||
|
||
{% set compare_model = kwargs.get('compare_model', kwargs.get('arg')) %} | ||
|
||
with a as ( | ||
|
||
select count(*) as count_ourmodel from {{ model }} | ||
|
||
), | ||
b as ( | ||
|
||
select count(*) as count_comparisonmodel from {{ compare_model }} | ||
|
||
), | ||
counts as ( | ||
|
||
select | ||
(select count_ourmodel from a) as count_model_with_fewer_rows, | ||
(select count_comparisonmodel from b) as count_model_with_more_rows | ||
|
||
), | ||
final as ( | ||
|
||
select | ||
case | ||
-- fail the test if we have more rows than the reference model and return the row count delta | ||
when count_model_with_fewer_rows > count_model_with_more_rows then (count_model_with_fewer_rows - count_model_with_more_rows) | ||
-- fail the test if they are the same number | ||
when count_model = count_comparison then 1 | ||
-- pass the test if the delta is positive (i.e. return the number 0) | ||
else 0 | ||
end as row_count_delta | ||
from counts | ||
|
||
) | ||
|
||
select row_count_delta from final | ||
|
||
{% endmacro %} |