forked from mathesar-foundation/mathesar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
57 lines (46 loc) · 1.72 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
"""
This file should provide utilities for setting up test DBs and the like. It's
intended to be the containment zone for anything specific about the testing
environment (e.g., the login info for the Postgres instance for testing)
"""
import pytest
from sqlalchemy import create_engine, text
from config.settings import DATABASES
TEST_DB = "mathesar_db_test"
@pytest.fixture(scope="session")
def test_db_name():
return TEST_DB
@pytest.fixture(scope="session", autouse=True)
def test_db():
superuser_engine = _get_superuser_engine()
with superuser_engine.connect() as conn:
conn.execution_options(isolation_level="AUTOCOMMIT")
conn.execute(text(f"DROP DATABASE IF EXISTS {TEST_DB} WITH (FORCE)"))
conn.execute(text(f"CREATE DATABASE {TEST_DB}"))
yield TEST_DB
with superuser_engine.connect() as conn:
conn.execution_options(isolation_level="AUTOCOMMIT")
conn.execute(text(f"DROP DATABASE {TEST_DB} WITH (FORCE)"))
@pytest.fixture(scope="session")
def engine(test_db):
return create_engine(
_get_connection_string(
DATABASES["default"]["USER"],
DATABASES["default"]["PASSWORD"],
DATABASES["default"]["HOST"],
test_db,
),
future=True,
)
def _get_superuser_engine():
return create_engine(
_get_connection_string(
username=DATABASES["default"]["USER"],
password=DATABASES["default"]["PASSWORD"],
hostname=DATABASES["default"]["HOST"],
database=DATABASES["default"]["NAME"],
),
future=True,
)
def _get_connection_string(username, password, hostname, database):
return f"postgresql://{username}:{password}@{hostname}/{database}"