2016-09-21 17:13:26 +01:00
|
|
|
from contextlib import contextmanager
|
2016-01-07 17:31:17 +00:00
|
|
|
import os
|
2016-02-16 15:25:46 +00:00
|
|
|
|
2016-02-03 13:16:19 +00:00
|
|
|
import boto3
|
2016-02-16 15:25:46 +00:00
|
|
|
import pytest
|
2016-01-07 17:31:17 +00:00
|
|
|
from alembic.command import upgrade
|
|
|
|
|
from alembic.config import Config
|
|
|
|
|
from flask.ext.migrate import Migrate, MigrateCommand
|
|
|
|
|
from flask.ext.script import Manager
|
2016-02-16 15:25:46 +00:00
|
|
|
|
2016-01-07 17:31:17 +00:00
|
|
|
from app import create_app, db
|
2015-12-11 16:57:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
2016-11-04 10:11:09 +00:00
|
|
|
def notify_api():
|
2016-02-16 15:25:46 +00:00
|
|
|
app = create_app()
|
2016-11-04 10:11:09 +00:00
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
2015-12-11 16:57:00 +00:00
|
|
|
ctx = app.app_context()
|
|
|
|
|
ctx.push()
|
|
|
|
|
|
2016-11-04 10:11:09 +00:00
|
|
|
yield app
|
2015-12-11 16:57:00 +00:00
|
|
|
|
2016-11-04 10:11:09 +00:00
|
|
|
ctx.pop()
|
2015-12-11 16:57:00 +00:00
|
|
|
|
|
|
|
|
|
2016-08-30 11:18:30 +01:00
|
|
|
@pytest.fixture(scope='function')
|
2016-08-30 10:41:23 +01:00
|
|
|
def client(notify_api):
|
|
|
|
|
with notify_api.test_request_context(), notify_api.test_client() as client:
|
|
|
|
|
yield client
|
|
|
|
|
|
|
|
|
|
|
2016-01-07 17:31:17 +00:00
|
|
|
@pytest.fixture(scope='session')
|
2016-11-04 10:11:09 +00:00
|
|
|
def notify_db(notify_api):
|
2016-11-08 13:48:14 +00:00
|
|
|
assert db.engine.url.database != 'notification_api', 'dont run tests against main db'
|
2016-04-08 16:13:10 +01:00
|
|
|
Migrate(notify_api, db)
|
|
|
|
|
Manager(db, MigrateCommand)
|
|
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
|
|
|
|
ALEMBIC_CONFIG = os.path.join(BASE_DIR, 'migrations')
|
|
|
|
|
config = Config(ALEMBIC_CONFIG + '/alembic.ini')
|
|
|
|
|
config.set_main_option("script_location", ALEMBIC_CONFIG)
|
|
|
|
|
|
|
|
|
|
with notify_api.app_context():
|
|
|
|
|
upgrade(config, 'head')
|
|
|
|
|
|
2016-11-04 10:11:09 +00:00
|
|
|
yield db
|
2016-04-08 16:13:10 +01:00
|
|
|
|
2016-11-04 10:11:09 +00:00
|
|
|
db.session.remove()
|
|
|
|
|
db.get_engine(notify_api).dispose()
|
2016-01-07 17:31:17 +00:00
|
|
|
|
|
|
|
|
|
2016-01-08 12:18:12 +00:00
|
|
|
@pytest.fixture(scope='function')
|
2016-11-04 10:11:09 +00:00
|
|
|
def notify_db_session(notify_db):
|
|
|
|
|
yield notify_db
|
2016-01-08 12:18:12 +00:00
|
|
|
|
2016-11-04 10:11:09 +00:00
|
|
|
notify_db.session.remove()
|
|
|
|
|
for tbl in reversed(notify_db.metadata.sorted_tables):
|
2017-01-13 12:14:34 +00:00
|
|
|
if tbl.name not in ["provider_details",
|
|
|
|
|
"key_types",
|
|
|
|
|
"branding_type",
|
|
|
|
|
"job_status",
|
|
|
|
|
"provider_details_history",
|
2017-04-20 18:21:56 +01:00
|
|
|
"template_process_type"]:
|
2016-11-04 10:11:09 +00:00
|
|
|
notify_db.engine.execute(tbl.delete())
|
|
|
|
|
notify_db.session.commit()
|
2016-01-08 12:18:12 +00:00
|
|
|
|
|
|
|
|
|
2016-12-08 12:12:45 +00:00
|
|
|
@pytest.fixture
|
|
|
|
|
def os_environ():
|
|
|
|
|
"""
|
|
|
|
|
clear os.environ, and restore it after the test runs
|
|
|
|
|
"""
|
|
|
|
|
# for use whenever you expect code to edit environment variables
|
|
|
|
|
old_env = os.environ.copy()
|
|
|
|
|
os.environ = {}
|
|
|
|
|
yield
|
|
|
|
|
os.environ = old_env
|
2016-02-03 13:16:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2016-11-04 10:11:09 +00:00
|
|
|
def sqs_client_conn():
|
2016-02-03 13:16:19 +00:00
|
|
|
boto3.setup_default_session(region_name='eu-west-1')
|
|
|
|
|
return boto3.resource('sqs')
|
2016-07-18 12:02:27 +01:00
|
|
|
|
2016-07-18 12:03:44 +01:00
|
|
|
|
2016-07-18 12:02:27 +01:00
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
|
# Copied from https://gist.github.com/pfctdayelise/5719730
|
|
|
|
|
idparametrize = getattr(metafunc.function, 'idparametrize', None)
|
|
|
|
|
if idparametrize:
|
|
|
|
|
argnames, testdata = idparametrize.args
|
|
|
|
|
ids, argvalues = zip(*sorted(testdata.items()))
|
|
|
|
|
metafunc.parametrize(argnames, argvalues, ids=ids)
|
2016-09-21 17:13:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
|
def set_config(app, name, value):
|
|
|
|
|
old_val = app.config.get(name)
|
|
|
|
|
app.config[name] = value
|
|
|
|
|
yield
|
|
|
|
|
app.config[name] = old_val
|
2017-02-13 15:46:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
|
def set_config_values(app, dict):
|
|
|
|
|
old_values = {}
|
|
|
|
|
|
|
|
|
|
for key in dict:
|
|
|
|
|
old_values[key] = app.config.get(key)
|
|
|
|
|
app.config[key] = dict[key]
|
|
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
for key in dict:
|
|
|
|
|
app.config[key] = old_values[key]
|