-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
conftest.py
46 lines (38 loc) · 1.17 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
import random
import pytest
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
from pytest_docker_service import docker_container
from sqlalchemy_nested_mutable.testing.utils import wait_pg_ready
PG_PORT = "5432/tcp"
pg_container = docker_container(
scope="session",
image_name="postgres:15",
container_name=f"pytest-svc-pg15-{random.randint(0, 1e8)}",
ports={PG_PORT: None},
environment={
"POSTGRES_USER": "test",
"POSTGRES_PASSWORD": "test",
"POSTGRES_DB": "test",
})
@pytest.fixture(scope="session")
def pg_dbinfo(pg_container):
port = pg_container.port_map[PG_PORT]
port = int(port[0] if isinstance(port, list) else port)
dbinfo = {
"port": port,
"host": '127.0.0.1',
"user": "test",
"password": "test",
"database": "test",
}
wait_pg_ready(dbinfo)
print(f"Prepared PostgreSQL: {dbinfo}")
yield dbinfo
@pytest.fixture(scope="session")
def session(pg_dbinfo):
engine = sa.create_engine(
"postgresql://{user}:{password}@{host}:{port}/{database}".format(**pg_dbinfo)
)
with sessionmaker(bind=engine)() as session:
yield session