Skip to content

Commit db44368

Browse files
committed
Добавлена фикстура db_container, для поднятия и настройки контейенера
1 parent df3f934 commit db44368

1 file changed

Lines changed: 47 additions & 4 deletions

File tree

tests/conftest.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,50 @@
1111
from calendar_backend.models.db import Event, Group, Lecturer, Room
1212
from calendar_backend.routes import app
1313
from calendar_backend.settings import get_settings
14+
from _pytest.monkeypatch import MonkeyPatch
15+
from alembic import command
16+
from alembic.config import Config as AlembicConfig
17+
from pathlib import Path
18+
from testcontainers.postgres import PostgresContainer
19+
20+
21+
class PostgresConfig:
22+
"""Дата-класс со значениями для контейнера с тестовой БД и для alembic-миграции."""
23+
24+
container_name: str = "modal-service-api_test"
25+
username: str = "postgres"
26+
host: str = "localhost"
27+
external_port: int = 5432
28+
image: str = "postgres:15"
29+
host_auth_method: str = "trust"
30+
alembic_ini: str = Path(__file__).resolve().parent.parent / "alembic.ini"
31+
32+
@classmethod
33+
def get_url(cls) -> str:
34+
"""Возвращает URI для подключения к БД."""
35+
return f"postgresql://{cls.username}@{cls.host}:{cls.external_port}/postgres"
36+
37+
38+
39+
@pytest.fixture(scope="session")
40+
def db_container():
41+
"""Фикстура настройки БД для тестов в Docker-контейнере."""
42+
container = (
43+
PostgresContainer(
44+
image=PostgresConfig.image, username=PostgresConfig.username, dbname=PostgresConfig.container_name
45+
)
46+
.with_bind_ports(5432, PostgresConfig.external_port)
47+
.with_env("POSTGRES_HOST_AUTH_METHOD", PostgresConfig.host_auth_method)
48+
)
49+
container.start()
50+
alembic_ini = PostgresConfig.alembic_ini
51+
cfg = AlembicConfig(str(alembic_ini.resolve()))
52+
cfg.set_main_option("script_location", "%(here)s/migrations")
53+
command.upgrade(cfg, "head")
54+
try:
55+
yield PostgresConfig.get_url()
56+
finally:
57+
container.stop()
1458

1559

1660
@pytest.fixture()
@@ -21,7 +65,7 @@ def client():
2165

2266
@pytest.fixture()
2367
def client_auth(mocker: MockerFixture):
24-
user_mock = mocker.patch('auth_lib.fastapi.UnionAuth.__call__')
68+
user_mock = mocker.patch('auth_lib.fastapi.UnionAuth.__call__', autospec=True)
2569
user_mock.return_value = {
2670
"session_scopes": [{"id": 0, "name": "string", "comment": "string"}],
2771
"user_scopes": [{"id": 0, "name": "string", "comment": "string"}],
@@ -35,9 +79,8 @@ def client_auth(mocker: MockerFixture):
3579

3680

3781
@pytest.fixture()
38-
def dbsession():
39-
settings = get_settings()
40-
engine = create_engine(str(settings.DB_DSN), isolation_level='AUTOCOMMIT')
82+
def dbsession(db_container):
83+
engine = create_engine(str(db_container), isolation_level='AUTOCOMMIT')
4184
TestingSessionLocal = sessionmaker(bind=engine)
4285
DeclarativeBase.metadata.create_all(bind=engine)
4386
return TestingSessionLocal()

0 commit comments

Comments
 (0)