Skip to content

Commit 4b1c124

Browse files
authored
Merge pull request #161 from MartinGuindon/feature/relationships_where
Add a new schema test: relationships_where
2 parents 6f2aae4 + 24bed14 commit 4b1c124

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,25 @@ models:
212212

213213
```
214214

215+
#### relationships_where ([source](macros/schema_tests/relationships_where.sql))
216+
This test validates the referential integrity between two tables (same as the core relationships schema test) with an added predicate to filter out some rows from the test. This is useful to exclude records such as test entities, rows created in the last X minutes/hours to account for temporary gaps due to ETL limitations, etc.
217+
218+
Usage:
219+
```yaml
220+
version: 2
221+
222+
models:
223+
- name: model_name
224+
columns:
225+
- name: id
226+
tests:
227+
- dbt_utils.relationships_where:
228+
to: ref('other_model_name')
229+
field: client_id
230+
from_condition: id <> '4ca448b8-24bf-4b88-96c6-b1609499c38b'
231+
232+
```
233+
215234
---
216235
### SQL helpers
217236
#### get_column_values ([source](macros/sql/get_column_values.sql))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id
2+
1
3+
2
4+
3
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id
2+
1
3+
2
4+
4

integration_tests/models/schema_tests/schema.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ models:
66
- name: field
77
tests:
88
- dbt_utils.not_constant
9-
9+
1010
- name: data_test_at_least_one
1111
columns:
1212
- name: field
1313
tests:
1414
- dbt_utils.at_least_one
15-
15+
1616
- name: data_test_expression_is_true
1717
tests:
1818
- dbt_utils.expression_is_true:
@@ -32,11 +32,21 @@ models:
3232
tests:
3333
- dbt_utils.equal_rowcount:
3434
compare_model: ref('test_equal_rowcount')
35-
35+
3636
- name: data_people
3737
columns:
3838
- name: is_active
3939
tests:
4040
- dbt_utils.cardinality_equality:
4141
field: is_active
42-
to: ref('data_people')
42+
to: ref('data_people')
43+
44+
- name: data_test_relationships_where_table_2
45+
columns:
46+
- name: id
47+
tests:
48+
- dbt_utils.relationships_where:
49+
from: id
50+
to: ref('data_test_relationships_where_table_1')
51+
field: id
52+
from_condition: id <> 4
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{% macro test_relationships_where(model, to, field) %}
2+
3+
{% set column_name = kwargs.get('column_name', kwargs.get('from')) %}
4+
{% set from_condition = kwargs.get('from_condition', "true") %}
5+
{% set to_condition = kwargs.get('to_condition', "true") %}
6+
7+
with left_table as (
8+
9+
select
10+
{{column_name}} as id
11+
12+
from {{model}}
13+
14+
where {{column_name}} is not null
15+
and {{from_condition}}
16+
17+
),
18+
19+
right_table as (
20+
21+
select
22+
{{field}} as id
23+
24+
from {{to}}
25+
26+
where {{field}} is not null
27+
and {{to_condition}}
28+
29+
),
30+
31+
exceptions as (
32+
33+
select
34+
left_table.id,
35+
right_table.id as right_id
36+
37+
from left_table
38+
39+
left join right_table
40+
on left_table.id = right_table.id
41+
42+
where right_table.id is null
43+
44+
)
45+
46+
select count(*) from exceptions
47+
48+
{% endmacro %}

0 commit comments

Comments
 (0)