-
Notifications
You must be signed in to change notification settings - Fork 7
/
conftest.py
executable file
·110 lines (78 loc) · 2.87 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
103
104
105
106
107
108
109
110
import pytest
from alvinchow_backend import app
from alvinchow_backend.db import base
from alvinchow_backend.db.session import get_session
from alvinchow_backend.app import config
from alvinchow_backend.lib.redis import get_redis_connection
app.initialize()
def pytest_addoption(parser):
parser.addoption(
'--integration', action="store_true", default=False, help="Also run the integration tests"
)
parser.addoption(
'--integration-only', action="store_true", default=False, help="Only run the integration tests"
)
@pytest.yield_fixture(autouse=True)
def common():
"""
Run this for all tests
"""
conn = get_redis_connection()
yield
# This is cheap enough to do that we can just do it for all tests
conn.flushdb()
def pytest_runtest_setup(item):
# Skip integration tests by default
allow_integration_tests = item.config.getoption("integration")
run_integration_only = item.config.getoption("integration_only")
is_integration_test = item.get_closest_marker('integration')
if is_integration_test and not (allow_integration_tests or run_integration_only):
# Integration tests can run either when --integration or --integration-only
pytest.skip()
if not is_integration_test and run_integration_only:
# Skip other tests --integration-only
pytest.skip()
@pytest.fixture(scope='session')
def testdatabase(testdatabase_factory):
# testdatabase_factory comes from alvin-python-lib
_testdatabase = testdatabase_factory(base, config.DATABASE_URL)
yield from _testdatabase
@pytest.yield_fixture(scope='function')
def db(testdatabase):
connection = testdatabase['connection']
session = testdatabase['session']
# Start a transaction for the test
transaction = connection.begin()
session.begin_nested()
yield
base.Session.remove()
transaction.rollback()
@pytest.fixture
def session(db):
return get_session()
@pytest.fixture(name='config')
def config_override():
""" Write to app.config in tests safely """
orig_values = config._values
yield config
for k, v in orig_values.items():
setattr(config, k, v)
config._values = orig_values
@pytest.fixture(autouse=True)
def fast_password_hashing(mocker, request):
from passlib.context import CryptContext
fast_crypt_context = CryptContext(schemes=['md5_crypt'])
mocker.patch('alvinchow_backend.service.authentication.password.crypt_context', fast_crypt_context)
@pytest.fixture(autouse=True)
def default_mocks(
request,
):
"""
Do specific things for unit-tests vs integration tests
- Block socket cipfalls unless integration test
- Allow socket API calls
"""
is_integration_test = bool(request.node.get_closest_marker('integration'))
# dynamically inject fixtures
if not is_integration_test:
request.getfixturevalue('socket_disabled')