Skip to content

Commit

Permalink
Added ability to use expression_is_true as a column test
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliott O'Hara authored and clrcrl committed Dec 23, 2020
1 parent 8728a7a commit 0cd7ade
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ models:

```

This macro can also be used at the column level. When this is done, the `expression` is evaluated against the column.

```yaml
version: 2
models:
- name: model_name
columns:
- name: col_a
tests:
- dbt_utils.expression_is_true:
expression: '>= 1'
- name: col_b
tests:
- dbt_utils.expression_is_true:
expression: '= 1'
condition: col_a = 1

```


#### 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
10 changes: 10 additions & 0 deletions integration_tests/models/schema_tests/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ models:
- dbt_utils.expression_is_true:
expression: col_a = 0.5
condition: col_b = 0.5
columns:
- name: col_a
tests:
- dbt_utils.expression_is_true:
expression: + col_b = 1
- name: col_b
tests:
- dbt_utils.expression_is_true:
expression: = 0.5
condition: col_a = 0.5

- name: test_recency
tests:
Expand Down
3 changes: 2 additions & 1 deletion macros/schema_tests/expression_is_true.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% macro test_expression_is_true(model, condition='true') %}

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

with meet_condition as (

Expand All @@ -12,7 +13,7 @@ validation_errors as (
select
*
from meet_condition
where not({{expression}})
where not( {{ expression if column_name is none else [column_name, expression] | join(' ') }})

)

Expand Down

0 comments on commit 0cd7ade

Please sign in to comment.