2019-04-02 11:52:37 +01:00
|
|
|
from datetime import datetime, date
|
2018-04-12 10:47:16 +01:00
|
|
|
|
2017-01-27 12:21:28 +00:00
|
|
|
import pytest
|
2018-04-12 10:47:16 +01:00
|
|
|
from freezegun import freeze_time
|
2017-01-27 12:21:28 +00:00
|
|
|
|
|
|
|
|
from app.utils import (
|
2020-12-04 16:00:20 +00:00
|
|
|
format_sequential_number,
|
2017-01-27 15:57:25 +00:00
|
|
|
get_london_midnight_in_utc,
|
2017-04-03 15:49:23 +01:00
|
|
|
get_midnight_for_day_before,
|
2019-08-15 15:03:49 +01:00
|
|
|
get_notification_table_to_use,
|
2020-12-04 16:00:20 +00:00
|
|
|
midnight_n_days_ago
|
2018-04-12 10:47:16 +01:00
|
|
|
)
|
2019-08-15 15:03:49 +01:00
|
|
|
from app.models import Notification, NotificationHistory
|
|
|
|
|
|
|
|
|
|
from tests.app.db import create_service_data_retention
|
2017-01-27 12:21:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('date, expected_date', [
|
|
|
|
|
(datetime(2016, 1, 15, 0, 30), datetime(2016, 1, 15, 0, 0)),
|
2017-01-27 15:57:25 +00:00
|
|
|
(datetime(2016, 6, 15, 0, 0), datetime(2016, 6, 14, 23, 0)),
|
|
|
|
|
(datetime(2016, 9, 15, 11, 59), datetime(2016, 9, 14, 23, 0)),
|
2019-04-02 11:52:37 +01:00
|
|
|
# works for both dates and datetimes
|
|
|
|
|
(date(2016, 1, 15), datetime(2016, 1, 15, 0, 0)),
|
|
|
|
|
(date(2016, 6, 15), datetime(2016, 6, 14, 23, 0)),
|
2017-01-27 12:21:28 +00:00
|
|
|
])
|
2017-01-27 15:57:25 +00:00
|
|
|
def test_get_london_midnight_in_utc_returns_expected_date(date, expected_date):
|
|
|
|
|
assert get_london_midnight_in_utc(date) == expected_date
|
2017-01-27 12:21:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('date, expected_date', [
|
|
|
|
|
(datetime(2016, 1, 15, 0, 30), datetime(2016, 1, 14, 0, 0)),
|
2017-01-27 15:57:25 +00:00
|
|
|
(datetime(2016, 7, 15, 0, 0), datetime(2016, 7, 13, 23, 0)),
|
|
|
|
|
(datetime(2016, 8, 23, 11, 59), datetime(2016, 8, 21, 23, 0)),
|
2017-01-27 12:21:28 +00:00
|
|
|
])
|
|
|
|
|
def test_get_midnight_for_day_before_returns_expected_date(date, expected_date):
|
|
|
|
|
assert get_midnight_for_day_before(date) == expected_date
|
2017-04-03 15:49:23 +01:00
|
|
|
|
|
|
|
|
|
2018-04-30 11:50:56 +01:00
|
|
|
@pytest.mark.parametrize('current_time, arg, expected_datetime', [
|
2018-04-12 10:47:16 +01:00
|
|
|
# winter
|
2018-04-30 11:50:56 +01:00
|
|
|
('2018-01-10 23:59', 1, datetime(2018, 1, 9, 0, 0)),
|
|
|
|
|
('2018-01-11 00:00', 1, datetime(2018, 1, 10, 0, 0)),
|
2018-04-12 10:47:16 +01:00
|
|
|
|
|
|
|
|
# bst switchover at 1am 25th
|
2018-04-30 11:50:56 +01:00
|
|
|
('2018-03-25 10:00', 1, datetime(2018, 3, 24, 0, 0)),
|
|
|
|
|
('2018-03-26 10:00', 1, datetime(2018, 3, 25, 0, 0)),
|
|
|
|
|
('2018-03-27 10:00', 1, datetime(2018, 3, 25, 23, 0)),
|
2018-04-12 10:47:16 +01:00
|
|
|
|
|
|
|
|
# summer
|
2018-04-30 11:50:56 +01:00
|
|
|
('2018-06-05 10:00', 1, datetime(2018, 6, 3, 23, 0)),
|
|
|
|
|
|
|
|
|
|
# zero days ago
|
|
|
|
|
('2018-01-11 00:00', 0, datetime(2018, 1, 11, 0, 0)),
|
|
|
|
|
('2018-06-05 10:00', 0, datetime(2018, 6, 4, 23, 0)),
|
2018-04-12 10:47:16 +01:00
|
|
|
])
|
2018-04-30 11:50:56 +01:00
|
|
|
def test_midnight_n_days_ago(current_time, arg, expected_datetime):
|
2018-04-12 10:47:16 +01:00
|
|
|
with freeze_time(current_time):
|
2018-04-30 11:50:56 +01:00
|
|
|
assert midnight_n_days_ago(arg) == expected_datetime
|
2019-08-15 15:03:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time('2019-01-10 00:30')
|
|
|
|
|
def test_get_notification_table_to_use(sample_service):
|
|
|
|
|
# it's currently early morning of Thurs 10th Jan.
|
|
|
|
|
# When the delete task runs a bit later, it'll delete data from last wednesday 2nd.
|
2019-08-22 17:24:51 +01:00
|
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2018, 12, 31), False) == NotificationHistory
|
|
|
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 1), False) == NotificationHistory
|
|
|
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 2), False) == Notification
|
|
|
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 3), False) == Notification
|
2019-08-15 15:03:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time('2019-01-10 00:30')
|
2019-08-22 17:24:51 +01:00
|
|
|
def test_get_notification_table_to_use_knows_if_delete_task_has_run(sample_service):
|
2019-08-15 15:03:49 +01:00
|
|
|
# it's currently early morning of Thurs 10th Jan.
|
|
|
|
|
# The delete task deletes/moves data from last wednesday 2nd.
|
2019-08-22 17:24:51 +01:00
|
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 2), False) == Notification
|
|
|
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 2), True) == NotificationHistory
|
2019-08-15 15:03:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time('2019-06-09 23:30')
|
|
|
|
|
def test_get_notification_table_to_use_respects_daylight_savings_time(sample_service):
|
|
|
|
|
# current time is 12:30am on 10th july in BST
|
2019-08-22 17:24:51 +01:00
|
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 6, 1), False) == NotificationHistory
|
|
|
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 6, 2), False) == Notification
|
2019-08-15 15:03:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time('2019-01-10 00:30')
|
|
|
|
|
def test_get_notification_table_to_use_checks_service_data_retention(sample_service):
|
|
|
|
|
create_service_data_retention(sample_service, 'email', days_of_retention=3)
|
2019-12-04 15:26:26 +00:00
|
|
|
create_service_data_retention(sample_service, 'letter', days_of_retention=9)
|
2019-08-15 15:03:49 +01:00
|
|
|
|
|
|
|
|
# it's currently early morning of Thurs 10th Jan.
|
|
|
|
|
# three days retention means we'll delete sunday 6th's data when the delete task runs (so there's still three full
|
|
|
|
|
# days of monday, tuesday and wednesday left over)
|
2019-08-22 17:24:51 +01:00
|
|
|
assert get_notification_table_to_use(sample_service, 'email', date(2019, 1, 5), False) == NotificationHistory
|
|
|
|
|
assert get_notification_table_to_use(sample_service, 'email', date(2019, 1, 6), False) == Notification
|
2019-08-15 15:03:49 +01:00
|
|
|
|
2019-12-04 15:26:26 +00:00
|
|
|
assert get_notification_table_to_use(sample_service, 'letter', date(2018, 12, 30), False) == NotificationHistory
|
|
|
|
|
assert get_notification_table_to_use(sample_service, 'letter', date(2018, 12, 31), False) == Notification
|
|
|
|
|
|
2019-08-15 15:03:49 +01:00
|
|
|
# falls back to 7 days if not specified
|
2019-08-22 17:24:51 +01:00
|
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 1), False) == NotificationHistory
|
|
|
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 2), False) == Notification
|
2020-12-04 16:00:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_format_sequential_number():
|
|
|
|
|
assert format_sequential_number(123) == '0000007b'
|