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

Added ability to use expression_is_true as a column test #226

Closed
wants to merge 1 commit into from
Closed
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
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(' ') }})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this is a little hard to read for me, I think I'd prefer an if statement:

Suggested change
where not( {{ expression if column_name is none else [column_name, expression] | join(' ') }})
{% if column_name is none %}
where not({{ expression }})
{% else %}
where not({{ column_name }} {{ expression }})
{% endif %}


)

Expand Down