Description
expression_is_true
test is really useful, but sometimes I am missing the option of using this test to assert on a subset of the rows of the model. For example, I would like to be able to assert that price
is higher than 5K if product_type
is a car. For these reason I ended up creating a custom test for this like the one below, thought it feels like this kind of functionality could be in the expression_is_true
test already.
{#
Checks that an expression is true on those rows that meet a previous condition
This is different from the 'expression_is_true' test from dbt-utils in that can
limit the input dataset to be tested by using a 'condition'
Arguments:
- model: the name of the model to be tested
- expression: the expression to be asserted in the data
- condition: filters to a subset of the model rows
#}
{% macro test_true_on_condition(model, expression, condition) %}
with meet_condition as (
select * from {{ model }} where {{ condition }}
)
select count(*)
from meet_condition
where not({{ expression }})
{% endmacro %}
Proposal: extend the expression_is_true
test with an optional parameter condition
that takes a none
default value. When nothing is passed in the condition
argument the expression is tested against the whole model. When a condition
is passed the the model is subset by the condition prior to asserting the expression.
I'd be happy to send a PR this fits.