From 3df0e8ee2de432bbd516bb50cb74957b6a74846e Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Fri, 22 Jul 2016 15:23:18 +0100 Subject: [PATCH] make tests run consistently * tests on API endpoints that we do not explicitly sort should either sort the results or compare results in an orderless way (e.g. converting to a set) * tests that touch the provider_details should reset values after running, since the provider_details table is not torn down and re-created between tests (unlike most tables) --- tests/app/notifications/test_rest.py | 33 +++++++++++++------------ tests/app/provider_details/test_rest.py | 19 ++++++++++---- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index f3f49b797..981def879 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -4,6 +4,7 @@ import uuid import pytest from flask import json from notifications_python_client.authentication import create_jwt_token +from freezegun import freeze_time from app.dao.notifications_dao import dao_update_notification from app.dao.api_key_dao import save_model_api_key @@ -584,19 +585,19 @@ def test_get_notifications_for_service_returns_merged_template_content(notify_ap notify_db, notify_db_session, sample_template_with_placeholders): + with freeze_time('2001-01-01T12:00:00'): + create_sample_notification(notify_db, + notify_db_session, + service=sample_template_with_placeholders.service, + template=sample_template_with_placeholders, + personalisation={"name": "merged with first"}) - create_sample_notification(notify_db, - notify_db_session, - service=sample_template_with_placeholders.service, - template=sample_template_with_placeholders, - personalisation={"name": "merged with first"}, - created_at=datetime.utcnow() - timedelta(seconds=1)) - - create_sample_notification(notify_db, - notify_db_session, - service=sample_template_with_placeholders.service, - template=sample_template_with_placeholders, - personalisation={"name": "merged with second"}) + with freeze_time('2001-01-01T12:00:01'): + create_sample_notification(notify_db, + notify_db_session, + service=sample_template_with_placeholders.service, + template=sample_template_with_placeholders, + personalisation={"name": "merged with second"}) with notify_api.test_request_context(): with notify_api.test_client() as client: @@ -608,10 +609,10 @@ def test_get_notifications_for_service_returns_merged_template_content(notify_ap headers=[auth_header]) assert response.status_code == 200 - resp = json.loads(response.get_data(as_text=True)) - assert len(resp['notifications']) == 2 - assert resp['notifications'][0]['body'] == 'Hello merged with first\nYour thing is due soon' - assert resp['notifications'][1]['body'] == 'Hello merged with second\nYour thing is due soon' + assert {noti['body'] for noti in json.loads(response.get_data(as_text=True))['notifications']} == { + 'Hello merged with first\nYour thing is due soon', + 'Hello merged with second\nYour thing is due soon' + } def _create_auth_header_from_key(api_key): diff --git a/tests/app/provider_details/test_rest.py b/tests/app/provider_details/test_rest.py index 13aa8a03f..b6a26a3e6 100644 --- a/tests/app/provider_details/test_rest.py +++ b/tests/app/provider_details/test_rest.py @@ -70,13 +70,22 @@ def test_should_be_able_to_update_priority(notify_db, notify_db_session, notify_ '/provider-details/{}'.format(provider_id), headers=[('Content-Type', 'application/json'), auth_header], data=json.dumps({ - 'priority': 10 + 'priority': 5 }) ) assert update_resp.status_code == 200 update_json = json.loads(update_resp.get_data(as_text=True))['provider_details'] assert update_json['identifier'] == 'firetext' - assert update_json['priority'] == 10 + assert update_json['priority'] == 5 + + update_resp = client.post( + '/provider-details/{}'.format(provider_id), + headers=[('Content-Type', 'application/json'), auth_header], + data=json.dumps({ + 'priority': 20 + }) + ) + assert update_resp.status_code == 200 def test_should_be_able_to_update_status(notify_db, notify_db_session, notify_api): @@ -89,10 +98,10 @@ def test_should_be_able_to_update_status(notify_db, notify_db_session, notify_ap ) fetch_resp = json.loads(response.get_data(as_text=True))['provider_details'] - provider_id = fetch_resp[2]['id'] + firetext = next(x for x in fetch_resp if x['identifier'] == 'firetext') update_resp_1 = client.post( - '/provider-details/{}'.format(provider_id), + '/provider-details/{}'.format(firetext['id']), headers=[('Content-Type', 'application/json'), auth_header], data=json.dumps({ 'active': False @@ -104,7 +113,7 @@ def test_should_be_able_to_update_status(notify_db, notify_db_session, notify_ap assert not update_resp_1['active'] update_resp_2 = client.post( - '/provider-details/{}'.format(provider_id), + '/provider-details/{}'.format(firetext['id']), headers=[('Content-Type', 'application/json'), auth_header], data=json.dumps({ 'active': True