Skip to content

Commit 9b22332

Browse files
authored
Merge pull request #92 from fishtown-analytics/expression-is-true-test
Add expression_is_true schema test
2 parents 0d2276b + 3f0bb5a commit 9b22332

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ models:
9393

9494
```
9595

96+
#### expression_is_true ([source](macros/schema_tests/expression_is_true.sql))
97+
This schema test asserts that a valid sql expression is true for all records. This is useful when checking integrity across columns, for example, that a total is equal to the sum of its parts, or that at least one column is true.
98+
99+
Usage:
100+
```yaml
101+
version: 2
102+
103+
models:
104+
- name: model_name
105+
tests:
106+
- dbt_utils.expression_is_true:
107+
expression: "col_a + col_b = total"
108+
109+
```
110+
96111
#### recency ([source](macros/schema_tests/recency.sql))
97112
This schema test asserts that there is data in the referenced model at least as recent as the defined interval prior to the current timestamp.
98113

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
col_a,col_b
2+
0,1
3+
1,0
4+
0.5,0.5

integration_tests/models/schema_tests/schema.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ models:
1313
tests:
1414
- dbt_utils.at_least_one
1515

16+
- name: data_test_expression_is_true
17+
tests:
18+
- dbt_utils.expression_is_true:
19+
expression: col_a + col_b = 1
20+
1621
- name: test_recency
1722
tests:
1823
- dbt_utils.recency:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% macro test_expression_is_true(model) %}
2+
3+
{% set expression = kwargs.get('expression', kwargs.get('arg')) %}
4+
5+
with validation_errors as (
6+
7+
select
8+
*
9+
from {{model}}
10+
where not({{expression}})
11+
12+
)
13+
14+
select count(*)
15+
from validation_errors
16+
17+
{% endmacro %}

0 commit comments

Comments
 (0)