From ed187f7fada04284dd5d43477154cd8663fb144e Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Fri, 4 Nov 2016 10:11:09 +0000 Subject: [PATCH 1/2] remove 500/unplanned exception handlers in test this means if your code throws an exception, in the test you'll see the full stack trace to help debugging --- tests/conftest.py | 60 +++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 11f570de5..36ad8c58b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,7 +2,6 @@ from contextlib import contextmanager import os import boto3 -from unittest import mock import pytest from alembic.command import upgrade from alembic.config import Config @@ -13,16 +12,31 @@ from app import create_app, db @pytest.fixture(scope='session') -def notify_api(request): +def notify_api(): app = create_app() + + # deattach server-error error handlers - error_handler_spec looks like: + # {'blueprint_name': { + # status_code: [error_handlers], + # None: [ tuples of (exception, )] + # }} + for error_handlers in app.error_handler_spec.values(): + error_handlers.pop(500, None) + if None in error_handlers: + error_handlers[None] = [ + exception_handler + for exception_handler in error_handlers[None] + if exception_handler[0] != Exception + ] + if error_handlers[None] == []: + error_handlers.pop(None) + ctx = app.app_context() ctx.push() - def teardown(): - ctx.pop() + yield app - request.addfinalizer(teardown) - return app + ctx.pop() @pytest.fixture(scope='function') @@ -32,7 +46,7 @@ def client(notify_api): @pytest.fixture(scope='session') -def notify_db(notify_api, request): +def notify_db(notify_api): Migrate(notify_api, db) Manager(db, MigrateCommand) BASE_DIR = os.path.dirname(os.path.dirname(__file__)) @@ -43,36 +57,30 @@ def notify_db(notify_api, request): with notify_api.app_context(): upgrade(config, 'head') - def teardown(): - db.session.remove() - db.get_engine(notify_api).dispose() + yield db - request.addfinalizer(teardown) - return db + db.session.remove() + db.get_engine(notify_api).dispose() @pytest.fixture(scope='function') -def notify_db_session(request, notify_db): - def teardown(): - notify_db.session.remove() - for tbl in reversed(notify_db.metadata.sorted_tables): - if tbl.name not in ["provider_details", "key_types", "branding_type", "job_status"]: - notify_db.engine.execute(tbl.delete()) - notify_db.session.commit() +def notify_db_session(notify_db): + yield notify_db - request.addfinalizer(teardown) + notify_db.session.remove() + for tbl in reversed(notify_db.metadata.sorted_tables): + if tbl.name not in ["provider_details", "key_types", "branding_type", "job_status"]: + notify_db.engine.execute(tbl.delete()) + notify_db.session.commit() @pytest.fixture(scope='function') -def os_environ(request): - env_patch = mock.patch('os.environ', {}) - request.addfinalizer(env_patch.stop) - - return env_patch.start() +def os_environ(mocker): + mocker.patch('os.environ', {}) @pytest.fixture(scope='function') -def sqs_client_conn(request): +def sqs_client_conn(): boto3.setup_default_session(region_name='eu-west-1') return boto3.resource('sqs') From f842a8c8935aa8dab9843e0729e9b7b5fbe66e9f Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Fri, 4 Nov 2016 17:04:51 +0000 Subject: [PATCH 2/2] update test to pytest.raises instaed of assert response == 500 --- .../app/notifications/rest/test_send_notification.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/app/notifications/rest/test_send_notification.py b/tests/app/notifications/rest/test_send_notification.py index a28590c40..78c45b748 100644 --- a/tests/app/notifications/rest/test_send_notification.py +++ b/tests/app/notifications/rest/test_send_notification.py @@ -698,14 +698,15 @@ def test_should_delete_notification_and_return_error_if_sqs_fails( save_model_api_key(api_key) auth_header = create_jwt_token(secret=api_key.unsigned_secret, client_id=str(api_key.service_id)) - response = client.post( - path='/notifications/{}'.format(template_type), - data=json.dumps(data), - headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))]) + with pytest.raises(Exception) as exc: + response = client.post( + path='/notifications/{}'.format(template_type), + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))]) mocked.assert_called_once_with([fake_uuid], queue='send-{}'.format(template_type)) + assert str(exc.value) == 'failed to talk to SQS' - assert response.status_code == 500 assert not notifications_dao.get_notification_by_id(fake_uuid) assert not NotificationHistory.query.get(fake_uuid)