Skip to content

Commit

Permalink
add row_conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebruyn committed Oct 7, 2022
1 parent c280e7f commit 79c411d
Showing 1 changed file with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,66 @@
{% macro sqlserver__test_expect_grouped_row_values_to_have_recent_data(model, group_by, timestamp_column, datepart, interval) %}
{% macro sqlserver__test_expect_grouped_row_values_to_have_recent_data(model, group_by, timestamp_column, datepart, interval, row_condition=None) %}
with latest_grouped_timestamps as (

select
{%- for g in group_by %}
{{ g }},
{%- endfor %}
max(1) as join_key,
max({{ timestamp_column }}) as latest_timestamp_column
from
{{ model }}
group by
{%- for g in group_by %}
{{g}}{%- if not loop.last %}, {%- endif %}
{%- endfor %}
where
-- to exclude erroneous future dates
cast({{ timestamp_column }} as {{ type_timestamp() }}) <= {{ dbt_date.now() }}
{% if row_condition %}
and {{ row_condition }}
{% endif %}
group by
{%- for g in group_by %}
{{g}}{%- if not loop.last %}, {%- endif %}
{%- endfor %}

),
validation_errors as (
total_row_counts as (

select
max(1) as join_key,
count(*) as row_count
from
latest_grouped_timestamps

),
outdated_grouped_timestamps as (

select *
from
latest_grouped_timestamps
where
latest_timestamp_column < {{ dbt_utils.dateadd(datepart, interval * -1, dbt_date.now()) }}
-- are the max timestamps per group older than the specified cutoff?
latest_timestamp_column <
cast(
{{ dateadd(datepart, interval * -1, dbt_date.now()) }}
as {{ type_timestamp() }}
)

),
validation_errors as (

select
r.row_count,
t.*
from
total_row_counts r
left join
outdated_grouped_timestamps t
on r.join_key = t.join_key
where
-- fail if either no rows were returned due to row_condition,
-- or the recency test returned failed rows
r.row_count = 0
or
t.join_key is not null

)
select count(*) from validation_errors
select * from validation_errors
{% endmacro %}

0 comments on commit 79c411d

Please sign in to comment.