Skip to content

Commit

Permalink
Merge pull request #35 from dwallace0723/add-cardinality-test
Browse files Browse the repository at this point in the history
Add cardinality equality schema test
  • Loading branch information
drewbanin authored Jan 4, 2018
2 parents 196f340 + 3b822d1 commit af8102f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ model_name:
```

#### at_least_one ([source](macros/schema_tests/at_least_one.sql))
This schema test asserts if column has at least one value.
This schema test asserts if column has at least one value.

Usage:
```
Expand All @@ -95,6 +95,17 @@ model_name:
```

#### cardinality_equality ([source](macros/schema_tests/cardinality_equality.sql))
This schema test asserts if values in a given column have exactly the same cardinality as values from a different column in a different model.

Usage:
```
model_name:
constraints:
cardinality_equality:
- {from: column_name, to: ref('other_model_name'), field: other_column_name}
```

---
### SQL helpers
#### group_by ([source](macros/sql/groupby.sql))
Expand Down
46 changes: 46 additions & 0 deletions macros/schema_tests/cardinality_equality.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% macro test_cardinality_equality(model, from, to, field) %}

with table_a as (
select
{{ from }},
count(*) as num_rows
from {{ model }}
group by 1
),

table_b as (
select
{{ field }},
count(*) as num_rows
from {{ to }}
group by 1
),

except_a as (
select *
from table_a
except
select *
from table_b
),

except_b as (
select *
from table_b
except
select *
from table_a
),

unioned as (
select *
from except_a
union all
select *
from except_b
)

select count(*)
from unioned

{% endmacro %}

0 comments on commit af8102f

Please sign in to comment.