mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-23 11:01:59 -05:00
previously we checked notifications table, and if the results were zero, checked the notification history table to see if there's data in there. When we know that data isn't in notifications, we're still checking. These queries take half a second per service, and we're doing at least ten for each of the five thousand services we have in notify. Most of these services have no data in either table for any given day, and we can reduce the amount of queries we do by only checking one table. Check the data retention for a service, and then if the date is older than the retention, get from history table. NOTE: This requires that the delete tasks haven't run yet for the day! If your retention is three days, this will look in the Notification table for data from three days ago - expecting that shortly after the task finishes, we'll delete that data.
101 lines
4.4 KiB
Python
101 lines
4.4 KiB
Python
from datetime import datetime, date
|
|
|
|
import pytest
|
|
from freezegun import freeze_time
|
|
|
|
from app.utils import (
|
|
get_london_midnight_in_utc,
|
|
get_midnight_for_day_before,
|
|
midnight_n_days_ago,
|
|
get_notification_table_to_use,
|
|
)
|
|
from app.models import Notification, NotificationHistory
|
|
|
|
from tests.app.db import create_service_data_retention
|
|
|
|
|
|
@pytest.mark.parametrize('date, expected_date', [
|
|
(datetime(2016, 1, 15, 0, 30), datetime(2016, 1, 15, 0, 0)),
|
|
(datetime(2016, 6, 15, 0, 0), datetime(2016, 6, 14, 23, 0)),
|
|
(datetime(2016, 9, 15, 11, 59), datetime(2016, 9, 14, 23, 0)),
|
|
# 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)),
|
|
])
|
|
def test_get_london_midnight_in_utc_returns_expected_date(date, expected_date):
|
|
assert get_london_midnight_in_utc(date) == expected_date
|
|
|
|
|
|
@pytest.mark.parametrize('date, expected_date', [
|
|
(datetime(2016, 1, 15, 0, 30), datetime(2016, 1, 14, 0, 0)),
|
|
(datetime(2016, 7, 15, 0, 0), datetime(2016, 7, 13, 23, 0)),
|
|
(datetime(2016, 8, 23, 11, 59), datetime(2016, 8, 21, 23, 0)),
|
|
])
|
|
def test_get_midnight_for_day_before_returns_expected_date(date, expected_date):
|
|
assert get_midnight_for_day_before(date) == expected_date
|
|
|
|
|
|
@pytest.mark.parametrize('current_time, arg, expected_datetime', [
|
|
# winter
|
|
('2018-01-10 23:59', 1, datetime(2018, 1, 9, 0, 0)),
|
|
('2018-01-11 00:00', 1, datetime(2018, 1, 10, 0, 0)),
|
|
|
|
# bst switchover at 1am 25th
|
|
('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)),
|
|
|
|
# summer
|
|
('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)),
|
|
])
|
|
def test_midnight_n_days_ago(current_time, arg, expected_datetime):
|
|
with freeze_time(current_time):
|
|
assert midnight_n_days_ago(arg) == expected_datetime
|
|
|
|
|
|
@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.
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2018, 12, 31)) == NotificationHistory
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 1)) == NotificationHistory
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 2)) == Notification
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 3)) == Notification
|
|
|
|
|
|
@freeze_time('2019-01-10 00:30')
|
|
@pytest.mark.parametrize('has_delete_task_run, table', [
|
|
(True, NotificationHistory),
|
|
(False, Notification),
|
|
])
|
|
def test_get_notification_table_to_use_knows_if_delete_task_has_run(sample_service, has_delete_task_run, table):
|
|
# it's currently early morning of Thurs 10th Jan.
|
|
# The delete task deletes/moves data from last wednesday 2nd.
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 2), has_delete_task_run) == table
|
|
|
|
|
|
@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
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 6, 1)) == NotificationHistory
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 6, 2)) == Notification
|
|
|
|
|
|
@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)
|
|
|
|
# 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)
|
|
assert get_notification_table_to_use(sample_service, 'email', date(2019, 1, 5)) == NotificationHistory
|
|
assert get_notification_table_to_use(sample_service, 'email', date(2019, 1, 6)) == Notification
|
|
|
|
# falls back to 7 days if not specified
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 1)) == NotificationHistory
|
|
assert get_notification_table_to_use(sample_service, 'sms', date(2019, 1, 2)) == Notification
|