Skip to content

Commit

Permalink
Added ability to use expression_is_true as a column test (#313)
Browse files Browse the repository at this point in the history
Tidy up Jinja

Update changelog
  • Loading branch information
Elliott O'Hara authored and clrcrl committed May 18, 2021
1 parent 3d61f89 commit 9d9e720
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# dbt-utils v0.6.5 (unreleased)
## Features
* Add new `accepted_range` test ([#276](https:/fishtown-analytics/dbt-utils/pull/276) [@joellabes](https:/joellabes))
* Make `expression_is_true` work as a column test (code originally in [#226](https:/fishtown-analytics/dbt-utils/pull/226/) from [@elliottohara](https:/elliottohara), merged via [#313])

## Fixes

Expand Down
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
7 changes: 6 additions & 1 deletion macros/schema_tests/expression_is_true.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{% macro default__test_expression_is_true(model, condition) %}

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

with meet_condition as (

Expand All @@ -18,7 +19,11 @@ validation_errors as (
select
*
from meet_condition
where not({{expression}})
{% if column_name is none %}
where not({{ expression }})
{%- else %}
where not({{ column_name }} {{ expression }})
{%- endif %}

)

Expand Down

0 comments on commit 9d9e720

Please sign in to comment.