diff --git a/integration_tests/models/schema_tests/schema.yml b/integration_tests/models/schema_tests/schema.yml index 146ea81..458023b 100644 --- a/integration_tests/models/schema_tests/schema.yml +++ b/integration_tests/models/schema_tests/schema.yml @@ -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 diff --git a/macros/schema_tests/_generalized/equal_expression.sql b/macros/schema_tests/_generalized/equal_expression.sql index ad4c234..31eb8be 100644 --- a/macros/schema_tests/_generalized/equal_expression.sql +++ b/macros/schema_tests/_generalized/equal_expression.sql @@ -29,8 +29,12 @@ 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, @@ -38,10 +42,12 @@ 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, @@ -49,7 +55,8 @@ 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 -%} @@ -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 @@ -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 -%} diff --git a/macros/schema_tests/table_shape/expect_table_row_count_to_equal_other_table _times_factor.sql b/macros/schema_tests/table_shape/expect_table_row_count_to_equal_other_table _times_factor.sql index e0301da..f5456f2 100644 --- a/macros/schema_tests/table_shape/expect_table_row_count_to_equal_other_table _times_factor.sql +++ b/macros/schema_tests/table_shape/expect_table_row_count_to_equal_other_table _times_factor.sql @@ -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 -%} diff --git a/macros/schema_tests/table_shape/expect_table_row_count_to_equal_other_table.sql b/macros/schema_tests/table_shape/expect_table_row_count_to_equal_other_table.sql index 0b19914..64e3acc 100644 --- a/macros/schema_tests/table_shape/expect_table_row_count_to_equal_other_table.sql +++ b/macros/schema_tests/table_shape/expect_table_row_count_to_equal_other_table.sql @@ -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 -%}