forked from umd-lib/patsy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
53 lines (44 loc) · 1.51 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
import pytest
import pytest_alembic
from argparse import Namespace
from patsy.core.db_gateway import DbGateway
from pytest_alembic.config import Config
from sqlalchemy.exc import OperationalError
def pytest_addoption(parser):
"""
Adds an (optional) "--base-url" command-line option to the "pytest" command
so that a SQLite database file, or database connection URL can be specified
to use for testing.
"""
parser.addoption(
'--base-url',
action='store',
default=':memory:',
help='Base Database URL for the tests'
)
@pytest.fixture
def addr(request):
"""
Returns the (optional) "--base-url" command-line option
"""
return request.config.getoption('--base-url')
@pytest.fixture
def db_gateway(addr):
"""
Sets up the database using Alembic migrations and returns the
"DbGateway" object to use to connect to the database.
"""
args = Namespace()
args.database = addr
gateway = DbGateway(args)
try:
engine = gateway.session.get_bind()
with pytest_alembic.runner(config=Config(), engine=engine) as runner:
runner.migrate_up_to('head')
yield gateway
# Drop the "alembic_versions" table Alembic so that running the migrations
# will update the database
gateway.session.execute("DROP TABLE IF EXISTS alembic_version;")
gateway.session.commit()
except OperationalError:
pytest.exit(reason=f"ERROR - Cannot connect to database.", returncode=5)