Revert "Revert "Merge pull request #2887 from alphagov/cache-the-serialised-things""

This reverts commit 7e85e37e1d.
This commit is contained in:
Chris Hill-Scott
2020-06-26 14:10:12 +01:00
parent 6d52d733a4
commit 3ffdb3093b
15 changed files with 215 additions and 50 deletions

View File

@@ -232,14 +232,14 @@ def test_should_cache_template_lookups_in_memory(mocker, client, sample_template
assert mock_get_template.call_count == 1
assert mock_get_template.call_args_list == [
call(service_id=sample_template.service_id, template_id=str(sample_template.id))
call(service_id=str(sample_template.service_id), template_id=str(sample_template.id))
]
assert Notification.query.count() == 5
def test_should_cache_template_lookups_in_redis(mocker, client, sample_template):
def test_should_cache_template_and_service_in_redis(mocker, client, sample_template):
from app.schemas import template_schema
from app.schemas import service_schema, template_schema
mock_redis_get = mocker.patch(
'app.redis_store.get',
@@ -263,34 +263,49 @@ def test_should_cache_template_lookups_in_redis(mocker, client, sample_template)
headers=[('Content-Type', 'application/json'), auth_header]
)
expected_key = f'template-{sample_template.id}-version-None'
expected_service_key = f'service-{sample_template.service_id}'
expected_templates_key = f'template-{sample_template.id}-version-None'
assert mock_redis_get.call_args_list == [call(
expected_key,
)]
assert mock_redis_get.call_args_list == [
call(expected_service_key),
call(expected_templates_key),
]
service_dict = service_schema.dump(sample_template.service).data
template_dict = template_schema.dump(sample_template).data
assert len(mock_redis_set.call_args_list) == 1
assert mock_redis_set.call_args[0][0] == expected_key
assert json.loads(mock_redis_set.call_args[0][1]) == {
'data': template_dict,
}
assert mock_redis_set.call_args[1]['ex'] == 604_800
assert len(mock_redis_set.call_args_list) == 2
service_call, templates_call = mock_redis_set.call_args_list
assert service_call[0][0] == expected_service_key
assert json.loads(service_call[0][1]) == {'data': service_dict}
assert service_call[1]['ex'] == 604_800
assert templates_call[0][0] == expected_templates_key
assert json.loads(templates_call[0][1]) == {'data': template_dict}
assert templates_call[1]['ex'] == 604_800
def test_should_return_template_if_found_in_redis(mocker, client, sample_template):
from app.schemas import template_schema
from app.schemas import service_schema, template_schema
service_dict = service_schema.dump(sample_template.service).data
template_dict = template_schema.dump(sample_template).data
mocker.patch(
'app.redis_store.get',
return_value=json.dumps({'data': template_dict}).encode('utf-8')
side_effect=[
json.dumps({'data': service_dict}).encode('utf-8'),
json.dumps({'data': template_dict}).encode('utf-8'),
],
)
mock_get_template = mocker.patch(
'app.dao.templates_dao.dao_get_template_by_id_and_service_id'
)
mock_get_service = mocker.patch(
'app.dao.services_dao.dao_fetch_service_by_id'
)
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
@@ -308,6 +323,7 @@ def test_should_return_template_if_found_in_redis(mocker, client, sample_templat
assert response.status_code == 201
assert mock_get_template.called is False
assert mock_get_service.called is False
@pytest.mark.parametrize("notification_type, key_send_to, send_to",
@@ -867,8 +883,8 @@ def test_post_notification_with_document_upload(client, notify_db_session, mocke
assert validate(resp_json, post_email_response) == resp_json
assert document_download_mock.upload_document.call_args_list == [
call(service.id, 'abababab', csv_param.get('is_csv')),
call(service.id, 'cdcdcdcd', csv_param.get('is_csv'))
call(str(service.id), 'abababab', csv_param.get('is_csv')),
call(str(service.id), 'cdcdcdcd', csv_param.get('is_csv'))
]
notification = Notification.query.one()
@@ -1017,7 +1033,10 @@ def test_post_notifications_saves_email_to_queue(client, notify_db_session, mock
save_email_task = mocker.patch("app.celery.tasks.save_api_email.apply_async")
mock_send_task = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
service = create_service(service_id='941b6f9a-50d7-4742-8d50-f365ca74bf27', service_name='high volume service')
service = create_service(
service_id=current_app.config['HIGH_VOLUME_SERVICE'][0],
service_name='high volume service',
)
template = create_template(service=service, content='((message))', template_type=EMAIL_TYPE)
data = {
"email_address": "joe.citizen@example.com",
@@ -1048,7 +1067,10 @@ def test_post_notifications_saves_email_normally_if_save_email_to_queue_fails(cl
)
mock_send_task = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
service = create_service(service_id='941b6f9a-50d7-4742-8d50-f365ca74bf27', service_name='high volume service')
service = create_service(
service_id=current_app.config['HIGH_VOLUME_SERVICE'][1],
service_name='high volume service',
)
template = create_template(service=service, content='((message))', template_type=EMAIL_TYPE)
data = {
"email_address": "joe.citizen@example.com",
@@ -1077,8 +1099,10 @@ def test_post_notifications_doesnt_save_email_to_queue_for_test_emails(client, n
save_email_task = mocker.patch("app.celery.tasks.save_api_email.apply_async")
mock_send_task = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
service = create_service(service_id='941b6f9a-50d7-4742-8d50-f365ca74bf27', service_name='high volume service')
# create_api_key(service=service, key_type='test')
service = create_service(
service_id=current_app.config['HIGH_VOLUME_SERVICE'][2],
service_name='high volume service',
)
template = create_template(service=service, content='((message))', template_type=EMAIL_TYPE)
data = {
"email_address": "joe.citizen@example.com",
@@ -1107,7 +1131,10 @@ def test_post_notifications_doesnt_save_email_to_queue_for_sms(client, notify_db
save_email_task = mocker.patch("app.celery.tasks.save_api_email.apply_async")
mock_send_task = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
service = create_service(service_id='941b6f9a-50d7-4742-8d50-f365ca74bf27', service_name='high volume service')
service = create_service(
service_id=current_app.config['HIGH_VOLUME_SERVICE'][3],
service_name='high volume service',
)
template = create_template(service=service, content='((message))', template_type=SMS_TYPE)
data = {
"phone_number": '+447700900855',