From 1ff474c11ad9a863c7caa3ff17c52461b1205dda Mon Sep 17 00:00:00 2001 From: dcereijodo Date: Fri, 28 Jun 2019 15:55:58 +0200 Subject: [PATCH 1/2] support extra parameter 'condition' on 'expression_is_true' macro An optional parameter 'condition' can be passed to the 'expression_is_true' macro to assert the expression for all records which meet a condition. Closes #146 --- README.md | 17 +++++++++++++++++ .../models/schema_tests/schema.yml | 5 ++++- macros/schema_tests/expression_is_true.sql | 10 ++++++++-- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2bcd6985..98a71712 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/integration_tests/models/schema_tests/schema.yml b/integration_tests/models/schema_tests/schema.yml index 7158865c..8e4b974a 100644 --- a/integration_tests/models/schema_tests/schema.yml +++ b/integration_tests/models/schema_tests/schema.yml @@ -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: diff --git a/macros/schema_tests/expression_is_true.sql b/macros/schema_tests/expression_is_true.sql index a473cce0..3094892b 100644 --- a/macros/schema_tests/expression_is_true.sql +++ b/macros/schema_tests/expression_is_true.sql @@ -1,12 +1,18 @@ {% macro test_expression_is_true(model) %} {% set expression = kwargs.get('expression', kwargs.get('arg')) %} +{% set condition = kwargs.get('condition', 'true') %} -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}}) ) From fe37f7b4b645d6223f34ff890643cba6c94b4276 Mon Sep 17 00:00:00 2001 From: dcereijodo Date: Fri, 28 Jun 2019 18:48:01 +0200 Subject: [PATCH 2/2] use a standard macro argument for the condition expression (default to 'true') --- macros/schema_tests/expression_is_true.sql | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/macros/schema_tests/expression_is_true.sql b/macros/schema_tests/expression_is_true.sql index 3094892b..5ff9c334 100644 --- a/macros/schema_tests/expression_is_true.sql +++ b/macros/schema_tests/expression_is_true.sql @@ -1,7 +1,6 @@ -{% macro test_expression_is_true(model) %} +{% macro test_expression_is_true(model, condition='true') %} {% set expression = kwargs.get('expression', kwargs.get('arg')) %} -{% set condition = kwargs.get('condition', 'true') %} with meet_condition as (