This repository has been archived by the owner on Dec 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathconftest.py
102 lines (79 loc) · 2.97 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import pytest
import sys
root = os.path.abspath(os.path.join(os.path.dirname(__file__)))
if root not in sys.path:
sys.path.insert(0, root)
from alembic.config import Config
from alembic import command
from sqlalchemy import event
from sqlalchemy.orm import Session
alembic_cfg = Config(os.path.join(root, 'alembic.ini'))
from changes.config import create_app, db
from changes.storage.mock import FileStorageCache
@pytest.fixture(scope='session')
def session_config(request):
db_name = 'test_changes'
return {
'db_name': db_name,
# TODO(dcramer): redis db is shared
'redis_db': '9',
}
@pytest.fixture(scope='session')
def app(request, session_config):
app = create_app(
_read_config=False,
TESTING=True,
SQLALCHEMY_DATABASE_URI='postgresql:///' + session_config['db_name'],
REDIS_URL='redis://localhost/' + session_config['redis_db'],
GROUPER_API_URL='https://localhost/',
WEB_BASE_URI='http://example.com',
INTERNAL_BASE_URI='http://changes-int.example.com',
REPO_ROOT='/tmp',
GREEN_BUILD_URL='https://foo.example.com',
GREEN_BUILD_AUTH=('username', 'password'),
JENKINS_URL='http://jenkins.example.com',
PHABRICATOR_LINK_HOST='http://phabricator.example.com',
PHABRICATOR_API_HOST='http://phabricator.example.com',
GOOGLE_CLIENT_ID='a' * 12,
GOOGLE_CLIENT_SECRET='b' * 40,
DEFAULT_FILE_STORAGE='changes.storage.mock.FileStorageCache',
LXC_PRE_LAUNCH='echo pre',
LXC_POST_LAUNCH='echo post',
SNAPSHOT_S3_BUCKET='snapshot-bucket',
MAX_EXECUTORS=10,
BAZEL_ARTIFACT_SUFFIX='.bazel',
SELECTIVE_TESTING_PROPAGATION_LIMIT=1,
SELECTIVE_TESTING_ENABLED=True,
)
app_context = app.test_request_context()
context = app_context.push()
# request.addfinalizer(app_context.pop)
return app
@pytest.fixture(scope='session', autouse=True)
def setup_db(request, app, session_config):
db_name = session_config['db_name']
# 9.1 does not support --if-exists
if os.system("psql -l | grep '%s'" % db_name) == 0:
assert not os.system('dropdb %s' % db_name)
assert not os.system('createdb -E utf-8 %s' % db_name)
command.upgrade(alembic_cfg, 'head')
@event.listens_for(Session, "after_transaction_end")
def restart_savepoint(session, transaction):
if transaction.nested and not transaction._parent.nested:
session.begin_nested()
# TODO: find a way to kill db connections so we can dropdob
# def teardown():
# os.system('dropdb %s' % db_name)
# request.addfinalizer(teardown)
@pytest.fixture(autouse=True)
def db_session(request):
request.addfinalizer(db.session.remove)
db.session.begin_nested()
@pytest.fixture(autouse=True)
def redis_session(request, app):
import redis
conn = redis.from_url(app.config['REDIS_URL'])
conn.flushdb()
def pytest_runtest_setup(item):
FileStorageCache.clear()