From 0cd7ade90ee8b33915505e41b59b8089c40a510f Mon Sep 17 00:00:00 2001 From: Elliott O'Hara Date: Thu, 4 Jun 2020 10:20:48 -0500 Subject: [PATCH] Added ability to use expression_is_true as a column test --- README.md | 19 +++++++++++++++++++ .../models/schema_tests/schema.yml | 10 ++++++++++ macros/schema_tests/expression_is_true.sql | 3 ++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7bd4a1c2..514c1bc2 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/integration_tests/models/schema_tests/schema.yml b/integration_tests/models/schema_tests/schema.yml index 0026b957..cbc9cff9 100644 --- a/integration_tests/models/schema_tests/schema.yml +++ b/integration_tests/models/schema_tests/schema.yml @@ -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: diff --git a/macros/schema_tests/expression_is_true.sql b/macros/schema_tests/expression_is_true.sql index 5ff9c334..5c1abc8e 100644 --- a/macros/schema_tests/expression_is_true.sql +++ b/macros/schema_tests/expression_is_true.sql @@ -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 ( @@ -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(' ') }}) )