forked from cosmicpython/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
100 lines (80 loc) · 2.46 KB
/
conftest.py
File metadata and controls
100 lines (80 loc) · 2.46 KB
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
# pylint: disable=redefined-outer-name
import shutil
import subprocess
import time
from pathlib import Path
import pytest
import redis
import requests
from requests.exceptions import RequestException
from redis.exceptions import RedisError
from sqlalchemy.exc import OperationalError
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, clear_mappers
from allocation.orm import metadata, start_mappers
from allocation import config
@pytest.fixture
def in_memory_db():
engine = create_engine('sqlite:///:memory:')
metadata.create_all(engine)
return engine
@pytest.fixture
def session_factory(in_memory_db):
start_mappers()
yield sessionmaker(bind=in_memory_db)
clear_mappers()
@pytest.fixture
def session(session_factory):
return session_factory()
def wait_for_postgres_to_come_up(engine):
deadline = time.time() + 10
while time.time() < deadline:
try:
return engine.connect()
except OperationalError:
time.sleep(0.5)
pytest.fail('Postgres never came up')
def wait_for_webapp_to_come_up():
deadline = time.time() + 10
url = config.get_api_url()
while time.time() < deadline:
try:
return requests.get(url)
except RequestException:
time.sleep(0.5)
pytest.fail('API never came up')
def wait_for_redis_to_come_up():
deadline = time.time() + 5
r = redis.Redis(**config.get_redis_host_and_port())
while time.time() < deadline:
try:
return r.ping()
except RedisError:
time.sleep(0.5)
pytest.fail('Redis never came up')
@pytest.fixture(scope='session')
def postgres_db():
engine = create_engine(config.get_postgres_uri())
wait_for_postgres_to_come_up(engine)
metadata.create_all(engine)
return engine
@pytest.fixture
def postgres_session_factory(postgres_db):
start_mappers()
yield sessionmaker(bind=postgres_db)
clear_mappers()
@pytest.fixture
def postgres_session(postgres_session_factory):
return postgres_session_factory()
@pytest.fixture
def restart_api():
(Path(__file__).parent / '../src/allocation/flask_app.py').touch()
time.sleep(0.5)
wait_for_webapp_to_come_up()
@pytest.fixture
def restart_redis_pubsub():
wait_for_redis_to_come_up()
if not shutil.which('docker-compose'):
print('skipping restart, assumes running in container')
return
subprocess.run(['docker-compose', 'restart', '-t', '0', 'redis_pubsub'])