Skip to content

Commit fad8497

Browse files
authored
Merge pull request #147 from dcereijodo/feature/assert-expression-on-condition
support extra parameter 'condition' on 'expression_is_true' macro
2 parents 99e755b + fe37f7b commit fad8497

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,23 @@ models:
128128

129129
```
130130

131+
The macro accepts an optional parameter `condition` that allows for asserting
132+
the `expression` on a subset of all records.
133+
134+
Usage:
135+
```yaml
136+
version: 2
137+
138+
models:
139+
- name: model_name
140+
tests:
141+
- dbt_utils.expression_is_true:
142+
expression: "col_a + col_b = total"
143+
condition: "created_at > '2018-12-31'"
144+
145+
```
146+
147+
131148
#### recency ([source](macros/schema_tests/recency.sql))
132149
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.
133150

integration_tests/models/schema_tests/schema.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ models:
1717
tests:
1818
- dbt_utils.expression_is_true:
1919
expression: col_a + col_b = 1
20-
20+
- dbt_utils.expression_is_true:
21+
expression: col_a = 0.5
22+
condition: col_b = 0.5
23+
2124
- name: test_recency
2225
tests:
2326
- dbt_utils.recency:

macros/schema_tests/expression_is_true.sql

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
{% macro test_expression_is_true(model) %}
1+
{% macro test_expression_is_true(model, condition='true') %}
22

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

5-
with validation_errors as (
5+
with meet_condition as (
6+
7+
select * from {{ model }} where {{ condition }}
8+
9+
),
10+
validation_errors as (
611

712
select
813
*
9-
from {{model}}
14+
from meet_condition
1015
where not({{expression}})
1116

1217
)

0 commit comments

Comments
 (0)