Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support extra parameter 'condition' on 'expression_is_true' macro #147

Merged
merged 2 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 8 additions & 3 deletions macros/schema_tests/expression_is_true.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
{% macro test_expression_is_true(model) %}
{% macro test_expression_is_true(model, condition='true') %}

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

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