Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds better support for integer division issue in equal_expression macro #44

Merged
merged 4 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions integration_tests/models/schema_tests/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ models:
expression: sum(col_numeric_a)
compare_model: ref("data_test")
group_by: [idx]
- dbt_expectations.equal_expression:
expression: sum(col_numeric_a)
compare_expression: sum(col_numeric_a * .5)
compare_model: ref("data_test")
group_by: [idx]
tolerance_percent: .5
- dbt_expectations.expect_column_pair_values_to_be_in_set:
column_A: col_numeric_a
column_B: col_numeric_b
Expand Down
32 changes: 23 additions & 9 deletions macros/schema_tests/_generalized/equal_expression.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,34 @@
row_condition=None,
compare_row_condition=None,
tolerance=0.0,
tolerance_percent=None) -%}
{{ adapter.dispatch('test_equal_expression', packages = dbt_expectations._get_namespaces()) (model, expression,
tolerance_percent=None,
return_difference=False
) -%}

{{ adapter.dispatch('test_equal_expression', packages = dbt_expectations._get_namespaces()) (
model, expression,
compare_model,
compare_expression,
group_by,
compare_group_by,
row_condition,
compare_row_condition,
tolerance,
tolerance_percent) }}
tolerance_percent,
return_difference) }}
{%- endmacro %}

{%- macro default__test_equal_expression(model, expression,
{%- macro default__test_equal_expression(
model, expression,
compare_model,
compare_expression,
group_by,
compare_group_by,
row_condition,
compare_row_condition,
tolerance,
tolerance_percent) -%}
tolerance_percent,
return_difference) -%}

{%- set compare_model = model if not compare_model else compare_model -%}
{%- set compare_expression = expression if not compare_expression else compare_expression -%}
Expand All @@ -73,7 +80,7 @@
b.expression as compare_expression,
abs(coalesce(a.expression, 0) - coalesce(b.expression, 0)) as expression_difference,
abs(coalesce(a.expression, 0) - coalesce(b.expression, 0))/
nullif(a.expression, 0) as expression_difference_percent
nullif(a.expression * 1.0, 0) as expression_difference_percent
from
a
full outer join
Expand All @@ -84,10 +91,17 @@
)
-- DEBUG:
-- select * from final
select count(*) from final
select
{% if return_difference %}
coalesce(sum(expression_difference), 0)
{% else %}
count(*)
{% endif %}
from final
where
expression_difference > {{ tolerance }}
{% if tolerance_percent %}
and expression_difference_percent > {{ tolerance_percent }}
expression_difference_percent > {{ tolerance_percent }}
{% else %}
expression_difference > {{ tolerance }}
{% endif %}
{%- endmacro -%}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{%- macro test_expect_table_row_count_to_equal_other_table_times_factor(model, compare_model, factor) -%}
{{ dbt_expectations.test_equal_expression(model, "count(*)", compare_model=compare_model, compare_expression="count(*) * " + factor|string) }}
{{ dbt_expectations.test_equal_expression(model, "count(*)", compare_model=compare_model, compare_expression="count(*) * " + factor|string, return_difference=True) }}
{%- endmacro -%}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{%- macro test_expect_table_row_count_to_equal_other_table(model, compare_model) -%}
{{ dbt_expectations.test_equal_expression(model, "count(*)", compare_model=compare_model) }}
{{ dbt_expectations.test_equal_expression(model, "count(*)", compare_model=compare_model, return_difference=True) }}
{%- endmacro -%}