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 mock
|
|
|
|
|
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-01-08 12:18:12 +00:00
|
|
|
from sqlalchemy.schema import MetaData
|
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-01-07 17:31:17 +00:00
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
|
def notify_db(notify_api, request):
|
|
|
|
|
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()
|
2016-02-02 14:16:08 +00:00
|
|
|
db.engine.execute("drop sequence services_id_seq cascade")
|
2016-01-07 17:31:17 +00:00
|
|
|
db.drop_all()
|
|
|
|
|
db.engine.execute("drop table alembic_version")
|
|
|
|
|
db.get_engine(notify_api).dispose()
|
|
|
|
|
|
|
|
|
|
request.addfinalizer(teardown)
|
|
|
|
|
|
|
|
|
|
|
2016-01-08 12:18:12 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def notify_db_session(request):
|
|
|
|
|
def teardown():
|
|
|
|
|
db.session.remove()
|
|
|
|
|
for tbl in reversed(meta.sorted_tables):
|
2016-02-25 12:11:51 +00:00
|
|
|
db.engine.execute(tbl.delete())
|
|
|
|
|
db.session.commit()
|
2016-01-08 12:18:12 +00:00
|
|
|
|
|
|
|
|
meta = MetaData(bind=db.engine, reflect=True)
|
|
|
|
|
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')
|