Skip to content

Commit

Permalink
support extra parameter 'condition' on 'expression_is_true' macro
Browse files Browse the repository at this point in the history
An optional parameter 'condition' can be passed to the 
'expression_is_true' macro to assert the expression for all records 
which meet a condition. Closes dbt-labs#146
  • Loading branch information
dcereijodo committed Jun 28, 2019
1 parent 99e755b commit 1ff474c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ models:

```

The macro accepts an optional parameter `condition` that allows for asserting
the `expression` on a subset of all records.

Usage:
```yaml
version: 2

models:
- name: model_name
tests:
- dbt_utils.expression_is_true:
expression: "col_a + col_b = total"
condition: "created_at > '2018-12-31'"

```


#### 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
5 changes: 4 additions & 1 deletion integration_tests/models/schema_tests/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ models:
tests:
- dbt_utils.expression_is_true:
expression: col_a + col_b = 1

- dbt_utils.expression_is_true:
expression: col_a = 0.5
condition: col_b = 0.5

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

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

with validation_errors as (
with meet_condition as (

select * from {{ model }} where {{ condition }}

),
validation_errors as (

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

)
Expand Down

0 comments on commit 1ff474c

Please sign in to comment.