Compare commits

...

4 Commits

Author SHA1 Message Date
Jeremy Cohen
f0dc98a780 Play with test_metadata.kwargs 2022-05-14 02:06:54 +02:00
Josh Devlin
85db3bdd6a remove whitespace 2022-04-27 10:04:24 +10:00
Josh Devlin
6a46d83ac0 Add changelog 2022-04-27 09:56:23 +10:00
Josh Devlin
f2b33cf261 Add percentage support for error_if, warn_if 2022-04-27 09:46:09 +10:00
3 changed files with 26 additions and 2 deletions

View File

@@ -0,0 +1,7 @@
kind: Features
body: Add support for percentages in error_if, warn_if
time: 2022-04-27T09:56:09.707636+10:00
custom:
Author: jaypeedevlin ehmartens
Issue: "4723"
PR: "5172"

View File

@@ -630,5 +630,7 @@ def add_rendered_test_kwargs(
# The test_metadata.kwargs come from the test builder, and were set
# when the test node was created in _parse_generic_test.
# Overwrite them with the rendered versions during test compilation
kwargs = deep_map_render(_convert_function, node.test_metadata.kwargs)
node.test_metadata.kwargs = kwargs
context[GENERIC_TEST_KWARGS_NAME] = kwargs

View File

@@ -3,12 +3,27 @@
{%- endmacro %}
{% macro default__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
{% set model_name = model.test_metadata.kwargs.model %}
{% set warn_if, warn_pct_division = get_pct_division(warn_if, model_name) %}
{% set error_if, error_pct_division = get_pct_division(error_if, model_name) %}
select
{{ fail_calc }} as failures,
{{ fail_calc }} {{ warn_if }} as should_warn,
{{ fail_calc }} {{ error_if }} as should_error
{{ fail_calc }} {{ warn_pct_division }} {{ warn_if }} as should_warn,
{{ fail_calc }} {{ error_pct_division }} {{ error_if }} as should_error
from (
{{ main_sql }}
{{ "limit " ~ limit if limit != none }}
) dbt_internal_test
{%- endmacro %}
{% macro get_pct_division(threshold, model_name) %}
{% if threshold[-1] == '%' %}
{% set threshold = threshold[:-1] %}
{% set pct_division = '/ (select count(*) from ' ~ ref(model_name) ~ ') * 100' %}
{% else %}
{% set pct_division = '' %}
{% endif %}
{% do return((threshold, pct_division)) %}
{% endmacro %}