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 to compare only date(And not timestamp) #107

Closed
wants to merge 1 commit 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
43 changes: 43 additions & 0 deletions tests/generic/test_date_is_recent.sql
Original file line number Diff line number Diff line change
@@ -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 %}
Loading