-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
functional_dependency
generic test macro
* Provide passing/failing seed CSVs (`data_test_functional_dependency_pass.csv` and `data_test_functional_dependency_fail.csv`) * Add example `schema.yml` usage demonstrating `data_tests` with multiple determinants and an optional `where` clause * Write documentation block for `functional_dependency`. * Upgrade postgres in Docker to 13.19 to be able to run the tests on ARM architectures.
- Loading branch information
Edwin de Jong
committed
Feb 20, 2025
1 parent
5c9dc0d
commit 178a49a
Showing
6 changed files
with
95 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
version: "3.7" | ||
services: | ||
postgres: | ||
image: cimg/postgres:9.6 | ||
image: cimg/postgres:13.19 | ||
environment: | ||
- POSTGRES_USER=root | ||
- POSTGRES_DB=dbt_utils_test | ||
ports: | ||
- "5432:5432" |
5 changes: 5 additions & 0 deletions
5
integration_tests/data/schema_tests/data_test_functional_dependency_fail.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 @@ | ||
idx,col_a,col_b,col_y | ||
1,1,1,X | ||
2,1,1,Y | ||
3,2,1,X | ||
4,2,1,X |
5 changes: 5 additions & 0 deletions
5
integration_tests/data/schema_tests/data_test_functional_dependency_pass.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 @@ | ||
col_a,col_b,col_y | ||
1,1,X | ||
1,2,X | ||
2,1,X | ||
2,2,Y |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{% test functional_dependency(model, determinants, dependent, where_clause=None) %} | ||
{{ return(adapter.dispatch('test_functional_dependency', 'dbt_utils')(model, determinants, dependent, where_clause)) }} | ||
{% endtest %} | ||
|
||
{% macro default__test_functional_dependency(model, determinants, dependent, where_clause=None) %} | ||
|
||
with filtered as ( | ||
select * | ||
from {{ model }} | ||
{% if where_clause %} | ||
where {{ where_clause }} | ||
{% endif %} | ||
), | ||
|
||
violations as ( | ||
select | ||
{% for col in determinants %} | ||
{{ col }}{% if not loop.last %}, {% endif %} | ||
{% endfor %}, | ||
count(distinct {{ dependent }}) as distinct_dependent_count | ||
from filtered | ||
group by | ||
{% for col in determinants %} | ||
{{ col }}{% if not loop.last %}, {% endif %} | ||
{% endfor %} | ||
having count(distinct {{ dependent }}) > 1 | ||
) | ||
|
||
select * | ||
from violations | ||
|
||
{% endmacro %} |