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

Add test for relative change in column values #282

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{% test expect_column_value_running_abs_change_ratio_to_be(model, column_name,change_threshold,
sort_column=None,
row_condition=None,
strictly=True,
group_by=None) %}

{%- set sort_column = column_name if not sort_column else sort_column -%}
{%- set operator = "<" if strictly else "<=" %}

with all_values as (

select
{{ sort_column }},
{%- if group_by -%}
{{ group_by | join(", ") }},
{%- endif %}
{{ column_name }}
from {{ model }}
{% if row_condition %}
where {{ row_condition }}
{% endif %}

),
add_lag_values as (

select
{{ group_by | join(", ") }}
, {{ sort_column }} as current_{{ sort_column | trim }}
, lag( {{- sort_column -}} ) over
{%- if not group_by -%}
(order by sort_column)
{%- else -%}
(partition by {{ group_by | join(", ") }} order by {{ sort_column }})
{%- endif %} as previous_{{ sort_column | trim }}
, {{ column_name }} as current_{{ column_name | trim }}
, lag( {{- column_name -}} ) over
{%- if not group_by -%}
(order by sort_column)
{%- else -%}
(partition by {{ group_by | join(", ") }} order by {{ sort_column }})
{%- endif %} as previous_{{ column_name | trim}}
from
all_values

),
validation_errors as (

select
*
, current_{{ column_name | trim}} / nullif(previous_{{ column_name | trim}}, 0) - 1 as actual_change_ratio
, abs(current_{{ column_name | trim}} / nullif(previous_{{ column_name | trim}}, 0)) - 1 as abs_change_ratio
, '{{ operator }} {{ change_threshold }}' as criteria
from
add_lag_values
where
previous_{{ column_name | trim}} is not null
and
not ((abs(current_{{ column_name | trim}} / nullif(previous_{{ column_name | trim}}, 0) )-1) {{ operator }} {{ change_threshold }} )

)
select *
from validation_errors
{% endtest %}