forked from redislabs-training/ru102py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
71 lines (48 loc) · 1.71 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
import pytest
from redis import client as redis_client
from redisolar import create_app
from redisolar.core.connections import get_redis_connection
from redisolar.core.connections import get_redis_timeseries_connection
from redisolar.dao.redis.key_schema import KeySchema
TEST_CONFIG = 'testing.cfg'
CI_CONFIG = 'ci.cfg'
def pytest_addoption(parser):
parser.addoption(
"--ci",
action="store_true",
help="use the CI configuration",
)
def make_app(request, config):
"""Yield a Flask app from the config file specified in `config`."""
if request.config.getoption('ci'):
config = CI_CONFIG
app = create_app(config)
with app.app_context():
yield app
@pytest.fixture
def app(request):
yield from make_app(request, TEST_CONFIG)
@pytest.fixture
def client(app):
with app.test_client() as client:
yield client
@pytest.fixture
def redis(app):
yield get_redis_connection(app.config['REDIS_HOST'], app.config['REDIS_PORT'])
@pytest.fixture
def redis_timeseries(app):
yield get_redis_timeseries_connection(app.config['REDIS_HOST'],
app.config['REDIS_PORT'])
@pytest.fixture
def key_schema(app):
yield KeySchema(app.config['REDIS_KEY_PREFIX'])
def _delete_test_keys(prefix: str, conn: redis_client.Redis):
for key in conn.scan_iter(f"{prefix}:*"):
conn.delete(key)
@pytest.fixture(scope="function", autouse=True)
def delete_test_keys(request):
def cleanup():
app = next(make_app(request, TEST_CONFIG))
conn = get_redis_connection(app.config['REDIS_HOST'], app.config['REDIS_PORT'])
_delete_test_keys(app.config['REDIS_KEY_PREFIX'], conn)
request.addfinalizer(cleanup)