Skip to content

Commit

Permalink
Merge pull request #92 from fishtown-analytics/expression-is-true-test
Browse files Browse the repository at this point in the history
Add expression_is_true schema test
  • Loading branch information
drewbanin authored Nov 30, 2018
2 parents 0d2276b + 3f0bb5a commit 9b22332
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ models:

```

#### expression_is_true ([source](macros/schema_tests/expression_is_true.sql))
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.

Usage:
```yaml
version: 2

models:
- name: model_name
tests:
- dbt_utils.expression_is_true:
expression: "col_a + col_b = total"

```

#### recency ([source](macros/schema_tests/recency.sql))
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.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
col_a,col_b
0,1
1,0
0.5,0.5
5 changes: 5 additions & 0 deletions integration_tests/models/schema_tests/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ models:
tests:
- dbt_utils.at_least_one

- name: data_test_expression_is_true
tests:
- dbt_utils.expression_is_true:
expression: col_a + col_b = 1

- name: test_recency
tests:
- dbt_utils.recency:
Expand Down
17 changes: 17 additions & 0 deletions macros/schema_tests/expression_is_true.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% macro test_expression_is_true(model) %}

{% set expression = kwargs.get('expression', kwargs.get('arg')) %}

with validation_errors as (

select
*
from {{model}}
where not({{expression}})

)

select count(*)
from validation_errors

{% endmacro %}

0 comments on commit 9b22332

Please sign in to comment.