Changed db queries to use one, which throws NoResultFound exception, this exception is dealt with in our error handlers.

Now a lot of the if none checks can be removed.
This commit is contained in:
Rebecca Law
2016-03-11 12:39:55 +00:00
parent 209244ff19
commit e055590b07
19 changed files with 66 additions and 112 deletions

View File

@@ -11,7 +11,7 @@ from app.dao.services_dao import (
)
from app.dao.users_dao import save_model_user
from app.models import Service, User
from sqlalchemy.orm.exc import FlushError
from sqlalchemy.orm.exc import FlushError, NoResultFound
from sqlalchemy.exc import IntegrityError
@@ -145,7 +145,9 @@ def test_get_all_user_services_should_return_empty_list_if_no_services_for_user(
def test_get_service_by_id_returns_none_if_no_service(notify_db):
assert not dao_fetch_service_by_id(str(uuid.uuid4()))
with pytest.raises(NoResultFound) as e:
dao_fetch_service_by_id(str(uuid.uuid4()))
assert 'No row was found for one()' in str(e)
def test_get_service_by_id_returns_service(service_factory):

View File

@@ -155,4 +155,6 @@ def test_get_template_by_id_and_service(notify_db, notify_db_session, sample_ser
def test_get_template_by_id_and_service_returns_none_if_no_template(sample_service):
assert not dao_get_template_by_id_and_service_id(template_id=999, service_id=sample_service.id)
with pytest.raises(NoResultFound) as e:
dao_get_template_by_id_and_service_id(template_id=999, service_id=sample_service.id)
assert 'No row was found for one' in str(e.value)

View File

@@ -1,5 +1,7 @@
from datetime import datetime, timedelta
from sqlalchemy.exc import DataError
from sqlalchemy.orm.exc import NoResultFound
from app import db
import pytest
@@ -55,7 +57,7 @@ def test_get_user_not_exists(notify_api, notify_db, notify_db_session):
try:
get_model_users(user_id="12345")
pytest.fail("NoResultFound exception not thrown.")
except:
except NoResultFound as e:
pass

View File

@@ -68,7 +68,7 @@ def test_get_job_with_unknown_id_returns404(notify_api, sample_template):
assert response.status_code == 404
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {
'message': 'Job {} not found for service {}'.format(random_id, service_id),
'message': 'No result found',
'result': 'error'
}
@@ -182,7 +182,7 @@ def test_create_job_returns_404_if_missing_service(notify_api, sample_template,
app.celery.tasks.process_job.apply_async.assert_not_called()
assert resp_json['result'] == 'error'
assert resp_json['message'] == 'Service {} not found'.format(random_id)
assert resp_json['message'] == 'No result found'
def test_get_update_job(notify_api, sample_job):

View File

@@ -45,7 +45,7 @@ def test_get_notifications_empty_result(notify_api, sample_api_key):
notification = json.loads(response.get_data(as_text=True))
assert notification['result'] == "error"
assert notification['message'] == "not found"
assert notification['message'] == "No result found"
assert response.status_code == 404
@@ -329,9 +329,8 @@ def test_send_notification_invalid_template_id(notify_api, sample_template, mock
app.celery.tasks.send_sms.apply_async.assert_not_called()
assert response.status_code == 404
assert len(json_resp['message'].keys()) == 1
test_string = 'Template {} not found for service {}'.format(9999, sample_template.service.id)
assert test_string in json_resp['message']['template']
test_string = 'No result found'
assert test_string in json_resp['message']
@freeze_time("2016-01-01 11:09:00.061258")
@@ -495,8 +494,8 @@ def test_should_not_allow_template_from_another_service(notify_api, service_fact
app.celery.tasks.send_sms.apply_async.assert_not_called()
assert response.status_code == 404
test_string = 'Template {} not found for service {}'.format(service_2_templates[0].id, service_1.id)
assert test_string in json_resp['message']['template']
test_string = 'No result found'
assert test_string in json_resp['message']
@freeze_time("2016-01-01 11:09:00.061258")
@@ -612,11 +611,8 @@ def test_should_reject_email_notification_with_template_id_that_cant_be_found(
app.celery.tasks.send_email.apply_async.assert_not_called()
assert response.status_code == 404
assert data['result'] == 'error'
test_string = 'Template {} not found for service {}'.format(
1234,
sample_email_template.service.id
)
assert test_string in data['message']['template']
test_string = 'No result found'
assert test_string in data['message']
def test_should_not_allow_email_template_from_another_service(notify_api, service_factory, sample_user, mocker):
@@ -649,8 +645,8 @@ def test_should_not_allow_email_template_from_another_service(notify_api, servic
app.celery.tasks.send_email.apply_async.assert_not_called()
assert response.status_code == 404
test_string = 'Template {} not found for service {}'.format(service_2_templates[0].id, service_1.id)
assert test_string in json_resp['message']['template']
test_string = 'No result found'
assert test_string in json_resp['message']
def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api, sample_email_template, mocker):

View File

@@ -130,7 +130,7 @@ def test_get_service_by_id_should_404_if_no_service(notify_api, notify_db):
assert resp.status_code == 404
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'Service not found for service id: {} '.format(service_id)
assert json_resp['message'] == 'No result found'
def test_get_service_by_id_and_user(notify_api, service_factory, sample_user):
@@ -467,7 +467,7 @@ def test_get_users_for_service_returns_404_when_service_does_not_exist(notify_ap
assert response.status_code == 404
result = json.loads(response.get_data(as_text=True))
assert result['result'] == 'error'
assert result['message'] == 'Service not found for id: {}'.format(service_id)
assert result['message'] == 'No result found'
def test_default_permissions_are_added_for_user_service(notify_api,
@@ -779,7 +779,7 @@ def test_add_existing_user_to_non_existing_service_returns404(notify_api,
)
result = json.loads(resp.get_data(as_text=True))
expected_message = 'Service not found for id: {}'.format(incorrect_id)
expected_message = 'No result found'
assert resp.status_code == 404
assert result['result'] == 'error'
@@ -833,7 +833,7 @@ def test_add_unknown_user_to_service_returns404(notify_api, notify_db, notify_db
)
result = json.loads(resp.get_data(as_text=True))
expected_message = 'User not found for id: {}'.format(incorrect_id)
expected_message = 'No result found'
assert resp.status_code == 404
assert result['result'] == 'error'

View File

@@ -92,7 +92,7 @@ def test_should_be_error_if_service_does_not_exist_on_create(notify_api):
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 404
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'Service not found'
assert json_resp['message'] == 'No result found'
def test_should_be_error_if_service_does_not_exist_on_update(notify_api):
@@ -117,7 +117,7 @@ def test_should_be_error_if_service_does_not_exist_on_update(notify_api):
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 404
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'Template not found'
assert json_resp['message'] == 'No result found'
def test_must_have_a_subject_on_an_email_template(notify_api, sample_service):
@@ -397,17 +397,18 @@ def test_should_return_404_if_no_templates_for_service_with_id(notify_api, sampl
with notify_api.test_request_context():
with notify_api.test_client() as client:
uuid_ = uuid.uuid4()
auth_header = create_authorization_header(
path='/service/{}/template/{}'.format(sample_service.id, 111),
path='/service/{}/template/{}'.format(sample_service.id, 9999),
method='GET'
)
response = client.get(
'/service/{}/template/{}'.format(sample_service.id, 111),
'/service/{}/template/{}'.format(sample_service.id, 9999),
headers=[auth_header]
)
assert response.status_code == 404
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'Template not found'
assert json_resp['message'] == 'No result found'

View File

@@ -258,7 +258,7 @@ def test_put_user_not_exists(notify_api, notify_db, notify_db_session, sample_us
user = User.query.filter_by(id=sample_user.id).first()
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['result'] == "error"
assert json_resp['message'] == "User not found for id: {}".format("9999")
assert json_resp['message'] == 'No result found'
assert user == sample_user
assert user.email_address != new_email
@@ -299,7 +299,7 @@ def test_get_user_by_email_not_found_returns_404(notify_api,
assert resp.status_code == 404
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'User not found for email address'
assert json_resp['message'] == 'No result found'
def test_get_user_by_email_bad_url_returns_404(notify_api,
@@ -469,7 +469,7 @@ def test_send_user_reset_password_should_return_400_when_user_doesnot_exist(noti
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 404
assert json.loads(resp.get_data(as_text=True))['message'] == 'User not found for email address'
assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'
def test_send_user_reset_password_should_return_400_when_data_is_not_email_address(notify_api, mocker):

View File

@@ -298,7 +298,7 @@ def test_send_sms_code_returns_404_for_bad_input_data(notify_api, notify_db, not
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 404
assert json.loads(resp.get_data(as_text=True))['message'] == 'User not found for id: {}'.format(int(uuid_))
assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'
def test_send_user_email_code(notify_api,
@@ -340,4 +340,4 @@ def test_send_user_email_code_returns_404_for_when_user_does_not_exist(notify_ap
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 404
assert json.loads(resp.get_data(as_text=True))['message'] == 'User not found for id: {}'.format(1)
assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'