Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cardinality equality schema test #35

Merged
merged 6 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 44 additions & 0 deletions macros/schema_tests/cardinality_equality.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{% macro test_cardinality_equality(model, from, to, field) %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dwallace0723 the from, to, and field args can be named arbitrarily -- we used these for the referential integrity test, but you can pick other names if you'd like!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll probably just leave them as is. The current naming makes logical sense to me. LMK if you think there are more suitable arg names for this specific test.


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

table_b as (
select
{ field }},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo here

count(1) 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
)

select count(1)
from (
select *
from except_a
union all
select *
from except_b
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you alias this subquery, then this schema test will also work on Postgres. At present, it errors out with:

subquery in FROM must have an alias
  LINE 36: from (
                ^
  HINT:  For example, FROM (SELECT ...) [AS] foo.

So just change it to

) as sbq

and you should be good! Or, you could make another CTE for the subquery

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch. I just created an additional CTE for the subquery.


{% endmacro %}