Skip to content

Commit

Permalink
Add date utility for retention queries (#2299)
Browse files Browse the repository at this point in the history
* chore: add utility function to calculate date based on retention period

* test(date_utils): add test scenarios for `test_get_query_date_based_on_retention_period()`
  • Loading branch information
andrewleith authored Sep 25, 2024
1 parent 465baff commit 793e947
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/dao/date_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from datetime import date, datetime, time, timedelta
from datetime import date, datetime, time, timedelta, timezone

import pytz
from notifications_utils.strftime_codes import no_pad_month
Expand Down Expand Up @@ -104,3 +104,10 @@ def utc_midnight_n_days_ago(number_of_days):
Returns utc midnight a number of days ago.
"""
return get_midnight(datetime.utcnow() - timedelta(days=number_of_days))


def get_query_date_based_on_retention_period(retention_period):
"""
Computes a date to be used when querying for notifications based on retention period
"""
return datetime.combine(datetime.now(timezone.utc) - timedelta(days=retention_period), time.max)
48 changes: 48 additions & 0 deletions tests/app/dao/test_date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import pytest
import pytz
from freezegun import freeze_time

from app.dao.date_util import (
get_april_fools,
get_financial_year,
get_financial_year_for_datetime,
get_midnight,
get_month_start_and_end_date_in_utc,
get_query_date_based_on_retention_period,
)


Expand Down Expand Up @@ -128,3 +130,49 @@ class TestMidnightDateTime:
def test_get_midnight(self, current_time, expected_midnight):
actual = get_midnight(current_time)
assert expected_midnight == actual


@freeze_time("2024-09-25 12:25:00")
@pytest.mark.parametrize(
"current_time, retention_period, expected_date",
[
(
datetime(2024, 9, 25, 12, 25, 00),
7,
datetime(2024, 9, 18, 23, 59, 59, 999999),
),
(
datetime(2024, 9, 20, 0, 0, 0),
7,
datetime(2024, 9, 13, 23, 59, 59, 999999),
),
(
datetime(2024, 9, 10, 23, 59, 59),
7,
datetime(2024, 9, 3, 23, 59, 59, 999999),
),
(
datetime(2020, 5, 7, 5, 59, 59),
3,
datetime(2020, 5, 4, 23, 59, 59, 999999),
),
(
datetime(2017, 8, 8, 12, 33, 6),
3,
datetime(2017, 8, 5, 23, 59, 59, 999999),
),
(
datetime(2014, 8, 8, 18, 0, 59),
3,
datetime(2014, 8, 5, 23, 59, 59, 999999),
),
(
datetime(2012, 1, 30, 22, 0, 59),
3,
datetime(2012, 1, 27, 23, 59, 59, 999999),
),
],
)
def test_get_query_date_based_on_retention_period(current_time, retention_period, expected_date):
with freeze_time(current_time):
assert get_query_date_based_on_retention_period(retention_period) == expected_date

0 comments on commit 793e947

Please sign in to comment.