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)
This commit is contained in:
Leo Hemsted
2016-07-22 15:23:18 +01:00
parent 0622791e83
commit 3df0e8ee2d
2 changed files with 31 additions and 21 deletions

View File

@@ -4,6 +4,7 @@ import uuid
import pytest import pytest
from flask import json from flask import json
from notifications_python_client.authentication import create_jwt_token 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.notifications_dao import dao_update_notification
from app.dao.api_key_dao import save_model_api_key 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,
notify_db_session, notify_db_session,
sample_template_with_placeholders): 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, with freeze_time('2001-01-01T12:00:01'):
notify_db_session, create_sample_notification(notify_db,
service=sample_template_with_placeholders.service, notify_db_session,
template=sample_template_with_placeholders, service=sample_template_with_placeholders.service,
personalisation={"name": "merged with first"}, template=sample_template_with_placeholders,
created_at=datetime.utcnow() - timedelta(seconds=1)) personalisation={"name": "merged with second"})
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_request_context():
with notify_api.test_client() as client: 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]) headers=[auth_header])
assert response.status_code == 200 assert response.status_code == 200
resp = json.loads(response.get_data(as_text=True)) assert {noti['body'] for noti in json.loads(response.get_data(as_text=True))['notifications']} == {
assert len(resp['notifications']) == 2 'Hello merged with first\nYour thing is due soon',
assert resp['notifications'][0]['body'] == 'Hello merged with first\nYour thing is due soon' 'Hello merged with second\nYour thing is due soon'
assert resp['notifications'][1]['body'] == 'Hello merged with second\nYour thing is due soon' }
def _create_auth_header_from_key(api_key): def _create_auth_header_from_key(api_key):

View File

@@ -70,13 +70,22 @@ def test_should_be_able_to_update_priority(notify_db, notify_db_session, notify_
'/provider-details/{}'.format(provider_id), '/provider-details/{}'.format(provider_id),
headers=[('Content-Type', 'application/json'), auth_header], headers=[('Content-Type', 'application/json'), auth_header],
data=json.dumps({ data=json.dumps({
'priority': 10 'priority': 5
}) })
) )
assert update_resp.status_code == 200 assert update_resp.status_code == 200
update_json = json.loads(update_resp.get_data(as_text=True))['provider_details'] update_json = json.loads(update_resp.get_data(as_text=True))['provider_details']
assert update_json['identifier'] == 'firetext' 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): 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'] 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( update_resp_1 = client.post(
'/provider-details/{}'.format(provider_id), '/provider-details/{}'.format(firetext['id']),
headers=[('Content-Type', 'application/json'), auth_header], headers=[('Content-Type', 'application/json'), auth_header],
data=json.dumps({ data=json.dumps({
'active': False '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'] assert not update_resp_1['active']
update_resp_2 = client.post( update_resp_2 = client.post(
'/provider-details/{}'.format(provider_id), '/provider-details/{}'.format(firetext['id']),
headers=[('Content-Type', 'application/json'), auth_header], headers=[('Content-Type', 'application/json'), auth_header],
data=json.dumps({ data=json.dumps({
'active': True 'active': True