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

[dbt] add custom tests #570

Merged
merged 11 commits into from
May 6, 2024
56 changes: 56 additions & 0 deletions tests/generic/custom_relationships.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{% test custom_relationships(
model,
column_name,
to,
field,
ignore_values=None,
proportion_allowed_failures=0
) %}

{{ config(severity="error") }}

with
child as (
select {{ column_name }} as child_value
from {{ model }}
{% if ignore_values %}
where {{ column_name }} not in ({{ ignore_values | join(", ") }})
{% endif %}
),
parent as (select {{ field }} as parent_value from {{ to }}),
validation as (
select child.child_value
from child
left join parent on child.child_value = parent.parent_value
where parent.parent_value is null
),
summary as (
select
count(*) as total_missing,
(select count(*) from child) as total_child_records,
round(
((count(*) * 100) / (select count(*) from child)), 2
) as failure_rate
from validation
)

select
total_missing,
total_child_records,
failure_rate,
case
when failure_rate > {{ proportion_allowed_failures }}
then
'Test failed: Failure rate of '
|| failure_rate
|| '% exceeds allowed proportion of '
|| '{{ proportion_allowed_failures }}%'
else
'Test passed: Failure rate of '
|| failure_rate
|| '% within acceptable limits'
end as result_message
from summary
where failure_rate > {{ proportion_allowed_failures }}

{% endtest %}
49 changes: 49 additions & 0 deletions tests/generic/custom_unique_combination_of_columns.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{% test custom_unique_combinations_of_columns(
model, combination_of_columns, proportion_allowed_failures=5
) %}

{{ config(severity="error") }}

{%- set column_list = combination_of_columns %}
{%- set columns_csv = column_list | join(", ") %}

with
validation_data as (
select {{ columns_csv }}, count(*) as duplicates_count
from {{ model }}
group by {{ columns_csv }}
having count(*) > 1
),
summary as (
select duplicates_count, (select count(*) from {{ model }}) as total_rows
from validation_data
),

final_summary as (
select
duplicates_count,
total_rows,
round(((duplicates_count * 100) / total_rows), 2) as proportion
from summary
)

select
duplicates_count,
total_rows,
proportion,
case
when proportion > {{ proportion_allowed_failures }}
then
'Test failed: Proportion of non-unique '
|| proportion
|| '% exceeds allowed proportion '
|| '{{ proportion_allowed_failures }}%'
else
'Test passed: Proportion of non-unique '
|| proportion
|| '% within acceptable limits'
end as log_message
from final_summary
where proportion > {{ proportion_allowed_failures }}

{% endtest %}
Loading