Skip to content

Commit

Permalink
Fix calls to last_month and next_month (#95)
Browse files Browse the repository at this point in the history
* Fix calls to last_month macros

* Add tests for last_month

* Fix next_month and add tests
  • Loading branch information
clausherther authored Dec 20, 2022
1 parent 7310f6b commit 8474083
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
21 changes: 18 additions & 3 deletions integration_tests/macros/get_test_dates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ select
cast('2020-11-23' as date) as iso_week_start_date,
cast('2020-11-29' as date) as iso_week_end_date,
48 as iso_week_of_year,
11 as month_number,
'November' as month_name,
'Nov' as month_name_short,
1623076520 as unix_epoch,
cast('{{ get_test_timestamps()[0] }}' as {{ type_timestamp() }}) as time_stamp,
cast('{{ get_test_timestamps()[1] }}' as {{ type_timestamp() }}) as time_stamp_utc,
cast('2021-06-07' as {{ type_timestamp() }}) as rounded_timestamp,
cast('2021-06-08' as {{ type_timestamp() }}) as rounded_timestamp_utc
cast('2021-06-08' as {{ type_timestamp() }}) as rounded_timestamp_utc,
-- These columns are here to make sure these macros get run during testing:
{{ dbt_date.last_month_number() }} as last_month_number,
{{ dbt_date.last_month_name(short=False) }} as last_month_name,
{{ dbt_date.last_month_name(short=True) }} as last_month_name_short,
{{ dbt_date.next_month_number() }} as next_month_number,
{{ dbt_date.next_month_name(short=False) }} as next_month_name,
{{ dbt_date.next_month_name(short=True) }} as next_month_name_short

union all

Expand All @@ -42,14 +50,21 @@ select
cast('2020-11-30' as date) as iso_week_start_date,
cast('2020-12-06' as date) as iso_week_end_date,
49 as iso_week_of_year,
12 as month_number,
'December' as month_name,
'Dec' as month_name_short,
{# 1623051320 as unix_epoch, #}
1623076520 as unix_epoch,
cast('{{ get_test_timestamps()[0] }}' as {{ type_timestamp() }}) as time_stamp,
cast('{{ get_test_timestamps()[1] }}' as {{ type_timestamp() }}) as time_stamp_utc,
cast('2021-06-07' as {{ type_timestamp() }}) as rounded_timestamp,
cast('2021-06-08' as {{ type_timestamp() }}) as rounded_timestamp_utc
cast('2021-06-08' as {{ type_timestamp() }}) as rounded_timestamp_utc,
-- These columns are here to make sure these macros get run during testing:
{{ dbt_date.last_month_number() }} as last_month_number,
{{ dbt_date.last_month_name(short=False) }} as last_month_name,
{{ dbt_date.last_month_name(short=True) }} as last_month_name_short,
{{ dbt_date.next_month_number() }} as next_month_number,
{{ dbt_date.next_month_name(short=False) }} as next_month_name,
{{ dbt_date.next_month_name(short=True) }} as next_month_name_short

{%- endmacro %}

Expand Down
19 changes: 19 additions & 0 deletions integration_tests/models/test_dates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ models:
expression: "iso_week_end_date = {{ dbt_date.iso_week_end('date_day') }}"
- expression_is_true:
expression: "iso_week_of_year = {{ dbt_date.iso_week_of_year('date_day') }}"
- expression_is_true:
expression: "month_number = {{ dbt_date.date_part('month', 'date_day') }}"
- expression_is_true:
expression: "month_name = {{ dbt_date.month_name('date_day', short=False) }}"
- expression_is_true:
expression: "month_name_short = {{ dbt_date.month_name('date_day', short=True) }}"
- expression_is_true:
expression: "time_stamp_utc = {{ dbt_date.from_unixtimestamp('unix_epoch') }}"
- expression_is_true:
Expand All @@ -47,6 +53,19 @@ models:
expression: "rounded_timestamp = {{ dbt_date.round_timestamp('time_stamp') }}"
- expression_is_true:
expression: "rounded_timestamp_utc = {{ dbt_date.round_timestamp('time_stamp_utc') }}"
- expression_is_true:
expression: "last_month_number = {{ dbt_date.last_month_number() }}"
- expression_is_true:
expression: "last_month_name = {{ dbt_date.last_month_name(short=False) }}"
- expression_is_true:
expression: "last_month_name_short = {{ dbt_date.last_month_name(short=True) }}"
- expression_is_true:
expression: "next_month_number = {{ dbt_date.next_month_number() }}"
- expression_is_true:
expression: "next_month_name = {{ dbt_date.next_month_name(short=False) }}"
- expression_is_true:
expression: "next_month_name_short = {{ dbt_date.next_month_name(short=True) }}"


columns:
- name: date_day
Expand Down
2 changes: 1 addition & 1 deletion macros/calendar_date/last_month_name.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{%- macro last_month_name(short=True, tz=None) -%}
{{ dbt_date.month_name(dbt_date.last_month(1, tz), short=short) }}
{{ dbt_date.month_name(dbt_date.last_month(tz), short=short) }}
{%- endmacro -%}
4 changes: 2 additions & 2 deletions macros/calendar_date/last_month_number.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{%- macro last_month_number(tz=None) -%}
{{ dbt_date.date_part('month', dbt_date.last_month(1, tz)) }}
{%- endmacro -%}
{{ dbt_date.date_part('month', dbt_date.last_month(tz)) }}
{%- endmacro -%}
4 changes: 2 additions & 2 deletions macros/calendar_date/next_month_name.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{%- macro next_month_name(short=True, tz=None) -%}
{{ dbt_date.month_name(dbt_date.next_month(1, tz), short=short) }}
{%- endmacro -%}
{{ dbt_date.month_name(dbt_date.next_month(tz), short=short) }}
{%- endmacro -%}
4 changes: 2 additions & 2 deletions macros/calendar_date/next_month_number.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{%- macro next_month_number(tz=None) -%}
{{ dbt_date.date_part('month', dbt_date.next_month(1, tz)) }}
{%- endmacro -%}
{{ dbt_date.date_part('month', dbt_date.next_month(tz)) }}
{%- endmacro -%}

0 comments on commit 8474083

Please sign in to comment.