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-08-31 12:39:11 +01:00
|
|
|
from unittest import mock
|
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')
|
|
|
|
|
def notify_api(request):
|
2016-02-16 15:25:46 +00:00
|
|
|
app = create_app()
|
2015-12-11 16:57:00 +00:00
|
|
|
ctx = app.app_context()
|
|
|
|
|
ctx.push()
|
|
|
|
|
|
|
|
|
|
def teardown():
|
|
|
|
|
ctx.pop()
|
|
|
|
|
|
|
|
|
|
request.addfinalizer(teardown)
|
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
def notify_db(notify_api, request):
|
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')
|
|
|
|
|
|
|
|
|
|
def teardown():
|
|
|
|
|
db.session.remove()
|
|
|
|
|
db.get_engine(notify_api).dispose()
|
|
|
|
|
|
|
|
|
|
request.addfinalizer(teardown)
|
2016-04-01 11:12:44 +01:00
|
|
|
return db
|
2016-01-07 17:31:17 +00:00
|
|
|
|
|
|
|
|
|
2016-01-08 12:18:12 +00:00
|
|
|
@pytest.fixture(scope='function')
|
2016-06-22 15:11:47 +01:00
|
|
|
def notify_db_session(request, notify_db):
|
2016-01-08 12:18:12 +00:00
|
|
|
def teardown():
|
2016-06-22 15:11:47 +01:00
|
|
|
notify_db.session.remove()
|
|
|
|
|
for tbl in reversed(notify_db.metadata.sorted_tables):
|
2016-08-24 14:35:22 +01:00
|
|
|
if tbl.name not in ["provider_details", "key_types", "branding_type", "job_status"]:
|
2016-06-22 15:11:47 +01:00
|
|
|
notify_db.engine.execute(tbl.delete())
|
|
|
|
|
notify_db.session.commit()
|
2016-01-08 12:18:12 +00:00
|
|
|
|
|
|
|
|
request.addfinalizer(teardown)
|
|
|
|
|
|
|
|
|
|
|
2015-12-11 16:57:00 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def os_environ(request):
|
|
|
|
|
env_patch = mock.patch('os.environ', {})
|
|
|
|
|
request.addfinalizer(env_patch.stop)
|
|
|
|
|
|
|
|
|
|
return env_patch.start()
|
2016-02-03 13:16:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sqs_client_conn(request):
|
|
|
|
|
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
|