-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconftest.py
More file actions
66 lines (52 loc) · 1.67 KB
/
conftest.py
File metadata and controls
66 lines (52 loc) · 1.67 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
import os
import sys
import subprocess
import shlex
import pytest
from aiohttp import web
from sqlalchemy import create_engine
from . import server
here = os.path.abspath(os.path.dirname(__file__))
def execute_migrate(command):
subprocess.run(
shlex.split('migrate -path=migrate/ -database postgres://%s:%s@%s:5432/%s?sslmode=disable %s' % (
os.environ['POLLS_DB_USER'],
os.environ['POLLS_DB_PASSWORD'],
os.environ['POLLS_DB_HOST'],
os.environ['POLLS_DB_NAME'],
command
)),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
def execute_sqlfile(engine, sql_file):
with open(os.path.join(here, 'sql-fixtures', sql_file), "r") as f:
engine.execute(f.read())
@pytest.fixture()
def test_db():
os.environ['POLLS_DB_NAME'] = 'polls-test'
os.environ['POLLS_DB_USER'] = 'polls-test'
os.environ['POLLS_DB_PASSWORD'] = 'password'
os.environ['POLLS_DB_HOST'] = 'postgres-test'
execute_migrate('drop')
execute_migrate('up')
return create_engine(
'postgresql://%s:%s@%s:5432/%s' % (
os.environ['POLLS_DB_USER'],
os.environ['POLLS_DB_PASSWORD'],
os.environ['POLLS_DB_HOST'],
os.environ['POLLS_DB_NAME']
)
)
@pytest.fixture()
def app(loop):
return server.get_app(loop)
@pytest.fixture
def http_client(test_db, app, loop, aiohttp_client):
return loop.run_until_complete(aiohttp_client(app))
@pytest.fixture
def questions_fixtures(test_db):
execute_sqlfile(test_db, 'questions.sql')
@pytest.fixture
def choices_fixtures(test_db, questions_fixtures):
execute_sqlfile(test_db, 'choices.sql')