mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-05 06:00:53 -04:00
Merge pull request #2303 from alphagov/ft-status-template-statistics
Change template statistics endpoint to use fact_notification_status_dao
This commit is contained in:
@@ -1,73 +0,0 @@
|
||||
from datetime import datetime
|
||||
|
||||
from freezegun import freeze_time
|
||||
import pytest
|
||||
|
||||
from app.commands import populate_redis_template_usage
|
||||
|
||||
from tests.conftest import set_config
|
||||
from tests.app.db import create_notification, create_template, create_service
|
||||
|
||||
|
||||
def test_populate_redis_template_usage_does_nothing_if_redis_disabled(mocker, notify_api, sample_service):
|
||||
mock_redis = mocker.patch('app.commands.redis_store')
|
||||
with set_config(notify_api, 'REDIS_ENABLED', False):
|
||||
with pytest.raises(SystemExit) as exit_signal:
|
||||
populate_redis_template_usage.callback.__wrapped__(sample_service.id, datetime.utcnow())
|
||||
|
||||
assert mock_redis.mock_calls == []
|
||||
# sys.exit with nonzero exit code
|
||||
assert exit_signal.value.code != 0
|
||||
|
||||
|
||||
def test_populate_redis_template_usage_does_nothing_if_no_data(mocker, notify_api, sample_service):
|
||||
mock_redis = mocker.patch('app.commands.redis_store')
|
||||
with set_config(notify_api, 'REDIS_ENABLED', True):
|
||||
populate_redis_template_usage.callback.__wrapped__(sample_service.id, datetime.utcnow())
|
||||
|
||||
assert mock_redis.mock_calls == []
|
||||
|
||||
|
||||
@freeze_time('2017-06-12')
|
||||
def test_populate_redis_template_usage_only_populates_for_today(mocker, notify_api, sample_template):
|
||||
mock_redis = mocker.patch('app.commands.redis_store')
|
||||
# created at in utc
|
||||
create_notification(sample_template, created_at=datetime(2017, 6, 9, 23, 0, 0))
|
||||
create_notification(sample_template, created_at=datetime(2017, 6, 9, 23, 0, 0))
|
||||
create_notification(sample_template, created_at=datetime(2017, 6, 10, 0, 0, 0))
|
||||
create_notification(sample_template, created_at=datetime(2017, 6, 10, 23, 0, 0)) # actually on 11th BST
|
||||
|
||||
with set_config(notify_api, 'REDIS_ENABLED', True):
|
||||
populate_redis_template_usage.callback.__wrapped__(sample_template.service_id, datetime(2017, 6, 10))
|
||||
|
||||
mock_redis.set_hash_and_expire.assert_called_once_with(
|
||||
'service-{}-template-usage-2017-06-10'.format(sample_template.service_id),
|
||||
{str(sample_template.id): 3},
|
||||
notify_api.config['EXPIRE_CACHE_EIGHT_DAYS'],
|
||||
raise_exception=True
|
||||
)
|
||||
|
||||
|
||||
@freeze_time('2017-06-12')
|
||||
def test_populate_redis_template_usage_only_populates_for_given_service(mocker, notify_api, notify_db_session):
|
||||
mock_redis = mocker.patch('app.commands.redis_store')
|
||||
# created at in utc
|
||||
s1 = create_service(service_name='a')
|
||||
s2 = create_service(service_name='b')
|
||||
t1 = create_template(s1)
|
||||
t2 = create_template(s2)
|
||||
|
||||
create_notification(t1, created_at=datetime(2017, 6, 10))
|
||||
create_notification(t1, created_at=datetime(2017, 6, 10))
|
||||
|
||||
create_notification(t2, created_at=datetime(2017, 6, 10))
|
||||
|
||||
with set_config(notify_api, 'REDIS_ENABLED', True):
|
||||
populate_redis_template_usage.callback.__wrapped__(s1.id, datetime(2017, 6, 10))
|
||||
|
||||
mock_redis.set_hash_and_expire.assert_called_once_with(
|
||||
'service-{}-template-usage-2017-06-10'.format(s1.id),
|
||||
{str(t1.id): 2},
|
||||
notify_api.config['EXPIRE_CACHE_EIGHT_DAYS'],
|
||||
raise_exception=True
|
||||
)
|
||||
@@ -16,7 +16,6 @@ from app.dao.notifications_dao import (
|
||||
dao_get_last_template_usage,
|
||||
dao_get_notifications_by_to_field,
|
||||
dao_get_scheduled_notifications,
|
||||
dao_get_template_usage,
|
||||
dao_timeout_notifications,
|
||||
dao_update_notification,
|
||||
dao_update_notifications_by_reference,
|
||||
@@ -70,7 +69,6 @@ from tests.app.db import (
|
||||
|
||||
def test_should_have_decorated_notifications_dao_functions():
|
||||
assert dao_get_last_template_usage.__wrapped__.__name__ == 'dao_get_last_template_usage' # noqa
|
||||
assert dao_get_template_usage.__wrapped__.__name__ == 'dao_get_template_usage' # noqa
|
||||
assert dao_create_notification.__wrapped__.__name__ == 'dao_create_notification' # noqa
|
||||
assert update_notification_status_by_id.__wrapped__.__name__ == 'update_notification_status_by_id' # noqa
|
||||
assert dao_update_notification.__wrapped__.__name__ == 'dao_update_notification' # noqa
|
||||
|
||||
@@ -1,23 +1,7 @@
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, date
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
import pytest
|
||||
from freezegun import freeze_time
|
||||
|
||||
from app.dao.notifications_dao import (
|
||||
dao_get_last_template_usage,
|
||||
dao_get_template_usage
|
||||
)
|
||||
from app.models import (
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEST,
|
||||
KEY_TYPE_TEAM
|
||||
)
|
||||
from tests.app.db import (
|
||||
create_notification,
|
||||
create_service,
|
||||
create_template
|
||||
)
|
||||
from app.dao.notifications_dao import dao_get_last_template_usage
|
||||
from tests.app.db import create_notification, create_template
|
||||
|
||||
|
||||
def test_last_template_usage_should_get_right_data(sample_notification):
|
||||
@@ -70,117 +54,3 @@ def test_last_template_usage_should_be_able_to_get_no_template_usage_history_if_
|
||||
sample_template):
|
||||
results = dao_get_last_template_usage(sample_template.id, 'sms', sample_template.service_id)
|
||||
assert not results
|
||||
|
||||
|
||||
@freeze_time('2018-01-01')
|
||||
def test_should_by_able_to_get_template_count(sample_template, sample_email_template):
|
||||
create_notification(sample_template)
|
||||
create_notification(sample_template)
|
||||
create_notification(sample_template)
|
||||
create_notification(sample_email_template)
|
||||
create_notification(sample_email_template)
|
||||
|
||||
results = dao_get_template_usage(sample_template.service_id, date.today())
|
||||
assert results[0].name == sample_email_template.name
|
||||
assert results[0].template_type == sample_email_template.template_type
|
||||
assert results[0].count == 2
|
||||
|
||||
assert results[1].name == sample_template.name
|
||||
assert results[1].template_type == sample_template.template_type
|
||||
assert results[1].count == 3
|
||||
|
||||
|
||||
@freeze_time('2018-01-01')
|
||||
def test_template_usage_should_ignore_test_keys(
|
||||
sample_team_api_key,
|
||||
sample_test_api_key,
|
||||
sample_api_key,
|
||||
sample_template
|
||||
):
|
||||
|
||||
create_notification(sample_template, api_key=sample_api_key, key_type=KEY_TYPE_NORMAL)
|
||||
create_notification(sample_template, api_key=sample_team_api_key, key_type=KEY_TYPE_TEAM)
|
||||
create_notification(sample_template, api_key=sample_test_api_key, key_type=KEY_TYPE_TEST)
|
||||
create_notification(sample_template)
|
||||
|
||||
results = dao_get_template_usage(sample_template.service_id, date.today())
|
||||
assert results[0].name == sample_template.name
|
||||
assert results[0].template_type == sample_template.template_type
|
||||
assert results[0].count == 3
|
||||
|
||||
|
||||
def test_template_usage_should_filter_by_service(notify_db_session):
|
||||
service_1 = create_service(service_name='test1')
|
||||
service_2 = create_service(service_name='test2')
|
||||
service_3 = create_service(service_name='test3')
|
||||
|
||||
template_1 = create_template(service_1)
|
||||
template_2 = create_template(service_2) # noqa
|
||||
template_3a = create_template(service_3, template_name='a')
|
||||
template_3b = create_template(service_3, template_name='b') # noqa
|
||||
|
||||
# two for service_1, one for service_3
|
||||
create_notification(template_1)
|
||||
create_notification(template_1)
|
||||
|
||||
create_notification(template_3a)
|
||||
|
||||
res1 = dao_get_template_usage(service_1.id, date.today())
|
||||
res2 = dao_get_template_usage(service_2.id, date.today())
|
||||
res3 = dao_get_template_usage(service_3.id, date.today())
|
||||
|
||||
assert len(res1) == 1
|
||||
assert res1[0].count == 2
|
||||
|
||||
assert len(res2) == 1
|
||||
assert res2[0].count == 0
|
||||
|
||||
assert len(res3) == 2
|
||||
assert res3[0].count == 1
|
||||
assert res3[1].count == 0
|
||||
|
||||
|
||||
def test_template_usage_should_by_able_to_get_zero_count_from_notifications_history_if_no_rows(sample_service):
|
||||
results = dao_get_template_usage(sample_service.id, date.today())
|
||||
assert len(results) == 0
|
||||
|
||||
|
||||
def test_template_usage_should_by_able_to_get_zero_count_from_notifications_history_if_no_service():
|
||||
results = dao_get_template_usage(str(uuid.uuid4()), date.today())
|
||||
assert len(results) == 0
|
||||
|
||||
|
||||
def test_template_usage_should_by_able_to_get_template_count_for_specific_day(sample_template):
|
||||
# too early
|
||||
create_notification(sample_template, created_at=datetime(2017, 6, 7, 22, 59, 0))
|
||||
# just right
|
||||
create_notification(sample_template, created_at=datetime(2017, 6, 7, 23, 0, 0))
|
||||
create_notification(sample_template, created_at=datetime(2017, 6, 7, 23, 0, 0))
|
||||
create_notification(sample_template, created_at=datetime(2017, 6, 8, 22, 59, 0))
|
||||
create_notification(sample_template, created_at=datetime(2017, 6, 8, 22, 59, 0))
|
||||
create_notification(sample_template, created_at=datetime(2017, 6, 8, 22, 59, 0))
|
||||
# too late
|
||||
create_notification(sample_template, created_at=datetime(2017, 6, 8, 23, 0, 0))
|
||||
|
||||
results = dao_get_template_usage(sample_template.service_id, day=date(2017, 6, 8))
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0].count == 5
|
||||
|
||||
|
||||
def test_template_usage_should_by_able_to_get_template_count_for_specific_timezone_boundary(sample_template):
|
||||
# too early
|
||||
create_notification(sample_template, created_at=datetime(2018, 3, 24, 23, 59, 0))
|
||||
# just right
|
||||
create_notification(sample_template, created_at=datetime(2018, 3, 25, 0, 0, 0))
|
||||
create_notification(sample_template, created_at=datetime(2018, 3, 25, 0, 0, 0))
|
||||
create_notification(sample_template, created_at=datetime(2018, 3, 25, 22, 59, 0))
|
||||
create_notification(sample_template, created_at=datetime(2018, 3, 25, 22, 59, 0))
|
||||
create_notification(sample_template, created_at=datetime(2018, 3, 25, 22, 59, 0))
|
||||
# too late
|
||||
create_notification(sample_template, created_at=datetime(2018, 3, 25, 23, 0, 0))
|
||||
|
||||
results = dao_get_template_usage(sample_template.service_id, day=date(2018, 3, 25))
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0].count == 5
|
||||
|
||||
@@ -2,6 +2,7 @@ from datetime import timedelta, datetime, date
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
import mock
|
||||
|
||||
from app.dao.fact_notification_status_dao import (
|
||||
update_fact_notification_status,
|
||||
@@ -188,6 +189,7 @@ def test_fetch_notification_status_for_service_for_day(notify_db_session):
|
||||
def test_fetch_notification_status_for_service_for_today_and_7_previous_days(notify_db_session):
|
||||
service_1 = create_service(service_name='service_1')
|
||||
sms_template = create_template(service=service_1, template_type=SMS_TYPE)
|
||||
sms_template_2 = create_template(service=service_1, template_type=SMS_TYPE)
|
||||
email_template = create_template(service=service_1, template_type=EMAIL_TYPE)
|
||||
|
||||
create_ft_notification_status(date(2018, 10, 29), 'sms', service_1, count=10)
|
||||
@@ -197,6 +199,7 @@ def test_fetch_notification_status_for_service_for_today_and_7_previous_days(not
|
||||
create_ft_notification_status(date(2018, 10, 26), 'letter', service_1, count=5)
|
||||
|
||||
create_notification(sms_template, created_at=datetime(2018, 10, 31, 11, 0, 0))
|
||||
create_notification(sms_template_2, created_at=datetime(2018, 10, 31, 11, 0, 0))
|
||||
create_notification(sms_template, created_at=datetime(2018, 10, 31, 12, 0, 0), status='delivered')
|
||||
create_notification(email_template, created_at=datetime(2018, 10, 31, 13, 0, 0), status='delivered')
|
||||
|
||||
@@ -220,13 +223,54 @@ def test_fetch_notification_status_for_service_for_today_and_7_previous_days(not
|
||||
|
||||
assert results[2].notification_type == 'sms'
|
||||
assert results[2].status == 'created'
|
||||
assert results[2].count == 2
|
||||
assert results[2].count == 3
|
||||
|
||||
assert results[3].notification_type == 'sms'
|
||||
assert results[3].status == 'delivered'
|
||||
assert results[3].count == 19
|
||||
|
||||
|
||||
@freeze_time('2018-10-31T18:00:00')
|
||||
def test_fetch_notification_status_by_template_for_service_for_today_and_7_previous_days(notify_db_session):
|
||||
service_1 = create_service(service_name='service_1')
|
||||
sms_template = create_template(template_name='sms Template 1', service=service_1, template_type=SMS_TYPE)
|
||||
sms_template_2 = create_template(template_name='sms Template 2', service=service_1, template_type=SMS_TYPE)
|
||||
email_template = create_template(service=service_1, template_type=EMAIL_TYPE)
|
||||
|
||||
# create unused email template
|
||||
create_template(service=service_1, template_type=EMAIL_TYPE)
|
||||
|
||||
create_ft_notification_status(date(2018, 10, 29), 'sms', service_1, count=10)
|
||||
create_ft_notification_status(date(2018, 10, 29), 'sms', service_1, count=11)
|
||||
create_ft_notification_status(date(2018, 10, 24), 'sms', service_1, count=8)
|
||||
create_ft_notification_status(date(2018, 10, 29), 'sms', service_1, notification_status='created')
|
||||
create_ft_notification_status(date(2018, 10, 29), 'email', service_1, count=3)
|
||||
create_ft_notification_status(date(2018, 10, 26), 'letter', service_1, count=5)
|
||||
|
||||
create_notification(sms_template, created_at=datetime(2018, 10, 31, 11, 0, 0))
|
||||
create_notification(sms_template, created_at=datetime(2018, 10, 31, 12, 0, 0), status='delivered')
|
||||
create_notification(sms_template_2, created_at=datetime(2018, 10, 31, 12, 0, 0), status='delivered')
|
||||
create_notification(email_template, created_at=datetime(2018, 10, 31, 13, 0, 0), status='delivered')
|
||||
|
||||
# too early, shouldn't be included
|
||||
create_notification(service_1.templates[0], created_at=datetime(2018, 10, 30, 12, 0, 0), status='delivered')
|
||||
|
||||
results = fetch_notification_status_for_service_for_today_and_7_previous_days(service_1.id, by_template=True)
|
||||
|
||||
assert [
|
||||
('email Template Name', False, mock.ANY, 'email', 'delivered', 1),
|
||||
('email Template Name', False, mock.ANY, 'email', 'delivered', 3),
|
||||
('letter Template Name', False, mock.ANY, 'letter', 'delivered', 5),
|
||||
('sms Template 1', False, mock.ANY, 'sms', 'created', 1),
|
||||
('sms Template Name', False, mock.ANY, 'sms', 'created', 1),
|
||||
('sms Template 1', False, mock.ANY, 'sms', 'delivered', 1),
|
||||
('sms Template 2', False, mock.ANY, 'sms', 'delivered', 1),
|
||||
('sms Template Name', False, mock.ANY, 'sms', 'delivered', 8),
|
||||
('sms Template Name', False, mock.ANY, 'sms', 'delivered', 10),
|
||||
('sms Template Name', False, mock.ANY, 'sms', 'delivered', 11),
|
||||
] == sorted(results, key=lambda x: (x.notification_type, x.status, x.template_name, x.count))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"start_date, end_date, expected_email, expected_letters, expected_sms, expected_created_sms",
|
||||
[
|
||||
|
||||
@@ -11,7 +11,6 @@ from app.dao.templates_dao import (
|
||||
dao_get_all_templates_for_service,
|
||||
dao_update_template,
|
||||
dao_get_template_versions,
|
||||
dao_get_multiple_template_details,
|
||||
dao_redact_template, dao_update_template_reply_to
|
||||
)
|
||||
from app.models import (
|
||||
@@ -511,21 +510,6 @@ def test_get_template_versions_is_empty_for_hidden_templates(notify_db, notify_d
|
||||
assert len(versions) == 0
|
||||
|
||||
|
||||
def test_get_multiple_template_details_returns_templates_for_list_of_ids(sample_service):
|
||||
t1 = create_template(sample_service)
|
||||
t2 = create_template(sample_service)
|
||||
create_template(sample_service) # t3
|
||||
|
||||
res = dao_get_multiple_template_details([t1.id, t2.id])
|
||||
|
||||
assert {x.id for x in res} == {t1.id, t2.id}
|
||||
# make sure correct properties are on each row
|
||||
assert res[0].id
|
||||
assert res[0].template_type
|
||||
assert res[0].name
|
||||
assert not res[0].is_precompiled_letter
|
||||
|
||||
|
||||
@pytest.mark.parametrize("template_type,postage", [('letter', 'third'), ('sms', 'second')])
|
||||
def test_template_postage_constraint_on_create(sample_service, sample_user, template_type, postage):
|
||||
data = {
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
import datetime
|
||||
import uuid
|
||||
from unittest.mock import call
|
||||
|
||||
import pytest
|
||||
from boto3.exceptions import Boto3Error
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from freezegun import freeze_time
|
||||
from collections import namedtuple
|
||||
from flask import current_app
|
||||
|
||||
from app.models import (
|
||||
Notification,
|
||||
@@ -25,7 +23,6 @@ from app.notifications.process_notifications import (
|
||||
simulated_recipient
|
||||
)
|
||||
from notifications_utils.recipients import validate_and_format_phone_number, validate_and_format_email_address
|
||||
from app.utils import cache_key_for_service_template_counter
|
||||
from app.v2.errors import BadRequestError
|
||||
from tests.app.conftest import sample_api_key as create_api_key
|
||||
|
||||
@@ -172,8 +169,6 @@ def test_persist_notification_with_optionals(sample_job, sample_api_key, mocker)
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 0
|
||||
mocked_redis = mocker.patch('app.notifications.process_notifications.redis_store.get')
|
||||
mock_service_template_cache = mocker.patch(
|
||||
'app.notifications.process_notifications.redis_store.get_all_from_hash')
|
||||
n_id = uuid.uuid4()
|
||||
created_at = datetime.datetime(2016, 11, 11, 16, 8, 18)
|
||||
persist_notification(
|
||||
@@ -200,7 +195,6 @@ def test_persist_notification_with_optionals(sample_job, sample_api_key, mocker)
|
||||
assert persisted_notification.job_row_number == 10
|
||||
assert persisted_notification.created_at == created_at
|
||||
mocked_redis.assert_called_once_with(str(sample_job.service_id) + "-2016-01-01-count")
|
||||
mock_service_template_cache.assert_called_once_with(cache_key_for_service_template_counter(sample_job.service_id))
|
||||
assert persisted_notification.client_reference == "ref from client"
|
||||
assert persisted_notification.reference is None
|
||||
assert persisted_notification.international is False
|
||||
@@ -213,7 +207,6 @@ def test_persist_notification_with_optionals(sample_job, sample_api_key, mocker)
|
||||
@freeze_time("2016-01-01 11:09:00.061258")
|
||||
def test_persist_notification_doesnt_touch_cache_for_old_keys_that_dont_exist(sample_template, sample_api_key, mocker):
|
||||
mock_incr = mocker.patch('app.notifications.process_notifications.redis_store.incr')
|
||||
mock_incr_hash_value = mocker.patch('app.notifications.process_notifications.redis_store.increment_hash_value')
|
||||
mocker.patch('app.notifications.process_notifications.redis_store.get', return_value=None)
|
||||
mocker.patch('app.notifications.process_notifications.redis_store.get_all_from_hash', return_value=None)
|
||||
|
||||
@@ -229,16 +222,11 @@ def test_persist_notification_doesnt_touch_cache_for_old_keys_that_dont_exist(sa
|
||||
reference="ref"
|
||||
)
|
||||
mock_incr.assert_not_called()
|
||||
mock_incr_hash_value.assert_called_once_with(
|
||||
"service-{}-template-usage-2016-01-01".format(sample_template.service_id),
|
||||
sample_template.id
|
||||
)
|
||||
|
||||
|
||||
@freeze_time("2016-01-01 11:09:00.061258")
|
||||
def test_persist_notification_increments_cache_if_key_exists(sample_template, sample_api_key, mocker):
|
||||
mock_incr = mocker.patch('app.notifications.process_notifications.redis_store.incr')
|
||||
mock_incr_hash_value = mocker.patch('app.notifications.process_notifications.redis_store.increment_hash_value')
|
||||
mocker.patch('app.notifications.process_notifications.redis_store.get', return_value=1)
|
||||
mocker.patch('app.notifications.process_notifications.redis_store.get_all_from_hash',
|
||||
return_value={sample_template.id, 1})
|
||||
@@ -255,10 +243,6 @@ def test_persist_notification_increments_cache_if_key_exists(sample_template, sa
|
||||
reference="ref2")
|
||||
|
||||
mock_incr.assert_called_once_with(str(sample_template.service_id) + "-2016-01-01-count", )
|
||||
assert mock_incr_hash_value.mock_calls == [
|
||||
call("{}-template-counter-limit-7-days".format(sample_template.service_id), sample_template.id),
|
||||
call("service-{}-template-usage-2016-01-01".format(sample_template.service_id), sample_template.id),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize((
|
||||
@@ -517,44 +501,6 @@ def test_persist_letter_notification_finds_correct_postage(
|
||||
assert persisted_notification.postage == expected_postage
|
||||
|
||||
|
||||
@pytest.mark.parametrize('utc_time, day_in_key', [
|
||||
('2016-01-01 23:00:00', '2016-01-01'),
|
||||
('2016-06-01 22:59:00', '2016-06-01'),
|
||||
('2016-06-01 23:00:00', '2016-06-02'),
|
||||
])
|
||||
def test_persist_notification_increments_and_expires_redis_template_usage(
|
||||
utc_time,
|
||||
day_in_key,
|
||||
sample_template,
|
||||
sample_api_key,
|
||||
mocker
|
||||
):
|
||||
mock_incr_hash_value = mocker.patch('app.notifications.process_notifications.redis_store.increment_hash_value')
|
||||
mock_expire = mocker.patch('app.notifications.process_notifications.redis_store.expire')
|
||||
mocker.patch('app.notifications.process_notifications.redis_store.get', return_value=None)
|
||||
mocker.patch('app.notifications.process_notifications.redis_store.get_all_from_hash', return_value=None)
|
||||
|
||||
with freeze_time(utc_time):
|
||||
persist_notification(
|
||||
template_id=sample_template.id,
|
||||
template_version=sample_template.version,
|
||||
recipient='+447111111122',
|
||||
service=sample_template.service,
|
||||
personalisation={},
|
||||
notification_type='sms',
|
||||
api_key_id=sample_api_key.id,
|
||||
key_type=sample_api_key.key_type,
|
||||
)
|
||||
mock_incr_hash_value.assert_called_once_with(
|
||||
'service-{}-template-usage-{}'.format(str(sample_template.service_id), day_in_key),
|
||||
sample_template.id
|
||||
)
|
||||
mock_expire.assert_called_once_with(
|
||||
'service-{}-template-usage-{}'.format(str(sample_template.service_id), day_in_key),
|
||||
current_app.config['EXPIRE_CACHE_EIGHT_DAYS']
|
||||
)
|
||||
|
||||
|
||||
def test_persist_notification_with_billable_units_stores_correct_info(
|
||||
sample_template,
|
||||
):
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from unittest.mock import Mock, call, ANY
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
from flask import current_app
|
||||
from freezegun import freeze_time
|
||||
|
||||
from tests.app.db import (
|
||||
create_notification,
|
||||
create_template,
|
||||
)
|
||||
from tests.app.db import create_notification
|
||||
|
||||
|
||||
def set_up_get_all_from_hash(mock_redis, side_effect):
|
||||
@@ -80,169 +75,46 @@ def test_get_template_statistics_for_service_by_day_accepts_old_query_string(
|
||||
assert len(json_resp['data']) == 1
|
||||
|
||||
|
||||
@freeze_time('2018-01-01 12:00:00')
|
||||
def test_get_template_statistics_for_service_by_day_gets_out_of_redis_if_available(
|
||||
admin_request,
|
||||
mocker,
|
||||
sample_template
|
||||
):
|
||||
mock_redis = mocker.patch('app.template_statistics.rest.redis_store')
|
||||
set_up_get_all_from_hash(mock_redis, [
|
||||
{sample_template.id: 3}
|
||||
])
|
||||
|
||||
json_resp = admin_request.get(
|
||||
'template_statistics.get_template_statistics_for_service_by_day',
|
||||
service_id=sample_template.service_id,
|
||||
whole_days=0
|
||||
)
|
||||
|
||||
assert len(json_resp['data']) == 1
|
||||
assert json_resp['data'][0]['count'] == 3
|
||||
assert json_resp['data'][0]['template_id'] == str(sample_template.id)
|
||||
mock_redis.get_all_from_hash.assert_called_once_with(
|
||||
'service-{}-template-usage-{}'.format(sample_template.service_id, '2018-01-01')
|
||||
)
|
||||
|
||||
|
||||
@freeze_time('2018-01-02 12:00:00')
|
||||
def test_get_template_statistics_for_service_by_day_goes_to_db_if_not_in_redis(
|
||||
def test_get_template_statistics_for_service_by_day_goes_to_db(
|
||||
admin_request,
|
||||
mocker,
|
||||
sample_template
|
||||
):
|
||||
mock_redis = mocker.patch('app.template_statistics.rest.redis_store')
|
||||
|
||||
# first time it is called redis returns data, second time returns none
|
||||
set_up_get_all_from_hash(mock_redis, [
|
||||
{sample_template.id: 2},
|
||||
None
|
||||
])
|
||||
mock_dao = mocker.patch(
|
||||
'app.template_statistics.rest.dao_get_template_usage',
|
||||
'app.template_statistics.rest.fetch_notification_status_for_service_for_today_and_7_previous_days',
|
||||
return_value=[
|
||||
Mock(id=sample_template.id, count=3)
|
||||
Mock(
|
||||
template_id=sample_template.id,
|
||||
count=3,
|
||||
template_name=sample_template.name,
|
||||
notification_type=sample_template.template_type,
|
||||
status='created',
|
||||
is_precompiled_letter=False
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
json_resp = admin_request.get(
|
||||
'template_statistics.get_template_statistics_for_service_by_day',
|
||||
service_id=sample_template.service_id,
|
||||
whole_days=1
|
||||
)
|
||||
|
||||
assert len(json_resp['data']) == 1
|
||||
assert json_resp['data'][0]['count'] == 5
|
||||
assert json_resp['data'][0]['template_id'] == str(sample_template.id)
|
||||
# first redis call
|
||||
assert mock_redis.get_all_from_hash.mock_calls == [
|
||||
call('service-{}-template-usage-{}'.format(sample_template.service_id, '2018-01-01')),
|
||||
call('service-{}-template-usage-{}'.format(sample_template.service_id, '2018-01-02'))
|
||||
]
|
||||
assert json_resp['data'] == [{
|
||||
"template_id": str(sample_template.id),
|
||||
"count": 3,
|
||||
"template_name": sample_template.name,
|
||||
"template_type": sample_template.template_type,
|
||||
"status": "created",
|
||||
"is_precompiled_letter": False
|
||||
|
||||
}]
|
||||
# dao only called for 2nd, since redis returned values for first call
|
||||
mock_dao.assert_called_once_with(
|
||||
str(sample_template.service_id), day=datetime(2018, 1, 2)
|
||||
str(sample_template.service_id), limit_days=1, by_template=True
|
||||
)
|
||||
mock_redis.set_hash_and_expire.assert_called_once_with(
|
||||
'service-{}-template-usage-{}'.format(sample_template.service_id, '2018-01-02'),
|
||||
# sets the data that the dao returned
|
||||
{str(sample_template.id): 3},
|
||||
current_app.config['EXPIRE_CACHE_EIGHT_DAYS']
|
||||
)
|
||||
|
||||
|
||||
def test_get_template_statistics_for_service_by_day_combines_templates_correctly(
|
||||
admin_request,
|
||||
mocker,
|
||||
sample_service
|
||||
):
|
||||
t1 = create_template(sample_service, template_name='1')
|
||||
t2 = create_template(sample_service, template_name='2')
|
||||
t3 = create_template(sample_service, template_name='3') # noqa
|
||||
mock_redis = mocker.patch('app.template_statistics.rest.redis_store')
|
||||
|
||||
# first time it is called redis returns data, second time returns none
|
||||
set_up_get_all_from_hash(mock_redis, [
|
||||
{t1.id: 2},
|
||||
None,
|
||||
{t1.id: 1, t2.id: 4},
|
||||
])
|
||||
mock_dao = mocker.patch(
|
||||
'app.template_statistics.rest.dao_get_template_usage',
|
||||
return_value=[
|
||||
Mock(id=t1.id, count=8)
|
||||
]
|
||||
)
|
||||
|
||||
json_resp = admin_request.get(
|
||||
'template_statistics.get_template_statistics_for_service_by_day',
|
||||
service_id=sample_service.id,
|
||||
whole_days=2
|
||||
)
|
||||
|
||||
assert len(json_resp['data']) == 2
|
||||
assert json_resp['data'][0]['template_id'] == str(t1.id)
|
||||
assert json_resp['data'][0]['count'] == 11
|
||||
assert json_resp['data'][1]['template_id'] == str(t2.id)
|
||||
assert json_resp['data'][1]['count'] == 4
|
||||
|
||||
assert mock_redis.get_all_from_hash.call_count == 3
|
||||
# dao only called for 2nd day
|
||||
assert mock_dao.call_count == 1
|
||||
|
||||
|
||||
@freeze_time('2018-03-28 00:00:00')
|
||||
def test_get_template_statistics_for_service_by_day_gets_stats_for_correct_days(
|
||||
admin_request,
|
||||
mocker,
|
||||
sample_template
|
||||
):
|
||||
mock_redis = mocker.patch('app.template_statistics.rest.redis_store')
|
||||
|
||||
# first time it is called redis returns data, second time returns none
|
||||
set_up_get_all_from_hash(mock_redis, [
|
||||
{sample_template.id: 1}, # last weds
|
||||
None,
|
||||
{sample_template.id: 1},
|
||||
{sample_template.id: 1},
|
||||
{sample_template.id: 1},
|
||||
{sample_template.id: 1},
|
||||
None,
|
||||
None, # current day
|
||||
])
|
||||
mock_dao = mocker.patch(
|
||||
'app.template_statistics.rest.dao_get_template_usage',
|
||||
return_value=[
|
||||
Mock(id=sample_template.id, count=2)
|
||||
]
|
||||
)
|
||||
|
||||
json_resp = admin_request.get(
|
||||
'template_statistics.get_template_statistics_for_service_by_day',
|
||||
service_id=sample_template.service_id,
|
||||
whole_days=7
|
||||
)
|
||||
|
||||
assert len(json_resp['data']) == 1
|
||||
assert json_resp['data'][0]['count'] == 11
|
||||
assert json_resp['data'][0]['template_id'] == str(sample_template.id)
|
||||
|
||||
assert mock_redis.get_all_from_hash.call_count == 8
|
||||
|
||||
assert '2018-03-21' in mock_redis.get_all_from_hash.mock_calls[0][1][0] # last wednesday
|
||||
assert '2018-03-22' in mock_redis.get_all_from_hash.mock_calls[1][1][0]
|
||||
assert '2018-03-23' in mock_redis.get_all_from_hash.mock_calls[2][1][0]
|
||||
assert '2018-03-24' in mock_redis.get_all_from_hash.mock_calls[3][1][0]
|
||||
assert '2018-03-25' in mock_redis.get_all_from_hash.mock_calls[4][1][0]
|
||||
assert '2018-03-26' in mock_redis.get_all_from_hash.mock_calls[5][1][0]
|
||||
assert '2018-03-27' in mock_redis.get_all_from_hash.mock_calls[6][1][0]
|
||||
assert '2018-03-28' in mock_redis.get_all_from_hash.mock_calls[7][1][0] # current day (wednesday)
|
||||
|
||||
mock_dao.mock_calls == [
|
||||
call(ANY, day=datetime(2018, 3, 22)),
|
||||
call(ANY, day=datetime(2018, 3, 27)),
|
||||
call(ANY, day=datetime(2018, 3, 28))
|
||||
]
|
||||
|
||||
|
||||
def test_get_template_statistics_for_service_by_day_returns_empty_list_if_no_templates(
|
||||
@@ -250,7 +122,6 @@ def test_get_template_statistics_for_service_by_day_returns_empty_list_if_no_tem
|
||||
mocker,
|
||||
sample_service
|
||||
):
|
||||
mock_redis = mocker.patch('app.template_statistics.rest.redis_store')
|
||||
|
||||
json_resp = admin_request.get(
|
||||
'template_statistics.get_template_statistics_for_service_by_day',
|
||||
@@ -259,9 +130,7 @@ def test_get_template_statistics_for_service_by_day_returns_empty_list_if_no_tem
|
||||
)
|
||||
|
||||
assert len(json_resp['data']) == 0
|
||||
assert mock_redis.get_all_from_hash.call_count == 8
|
||||
# make sure we don't try and set any empty hashes in redis
|
||||
assert mock_redis.set_hash_and_expire.call_count == 0
|
||||
|
||||
|
||||
# get_template_statistics_for_template
|
||||
|
||||
|
||||
Reference in New Issue
Block a user