diff --git a/tests/generic/test_date_is_recent.sql b/tests/generic/test_date_is_recent.sql new file mode 100644 index 0000000..986ab7b --- /dev/null +++ b/tests/generic/test_date_is_recent.sql @@ -0,0 +1,43 @@ +-- ported from https://github.com/dbt-labs/dbt-utils/blob/main/macros/generic_tests/recency.sql +-- The change is that we compare against current_date() instead of current_timestamp +{% test recency(model, field, datepart, interval, ignore_time_component=False, group_by_columns = []) %} + {{ return(adapter.dispatch('test_recency', 'dbt_utils')(model, field, datepart, interval, ignore_time_component, group_by_columns)) }} +{% endtest %} + +{% macro default__test_recency(model, field, datepart, interval, ignore_time_component, group_by_columns) %} + +{% set threshold = 'cast(' ~ dbt.dateadd(datepart, interval * -1, dbt.current_date()) ~ ' as ' ~ ('date' if ignore_time_component else dbt.type_timestamp()) ~ ')' %} + +{% if group_by_columns|length() > 0 %} + {% set select_gb_cols = group_by_columns|join(' ,') + ', ' %} + {% set groupby_gb_cols = 'group by ' + group_by_columns|join(',') %} +{% endif %} + + +with recency as ( + + select + + {{ select_gb_cols }} + {% if ignore_time_component %} + cast(max({{ field }}) as date) as most_recent + {%- else %} + max({{ field }}) as most_recent + {%- endif %} + + from {{ model }} + + {{ groupby_gb_cols }} + +) + +select + + {{ select_gb_cols }} + most_recent, + {{ threshold }} as threshold + +from recency +where most_recent < {{ threshold }} + +{% endmacro %} \ No newline at end of file