From da000ed086e704c1d0d81b89eb19685bf6a19318 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Tue, 3 Dec 2024 18:45:34 +0300 Subject: [PATCH 1/3] update aiohttp & rm unreadable params --- pyproject.toml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2bc5cdd..3f01465 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,6 +82,4 @@ strict_optional = true ignore_missing_imports = true [tool.pytest.ini_options] -pythonpath = [ ".", "src",] -asyncio_mode="auto" -env_files = [".env.test"] +asyncio_mode = "auto" From 3e5adec43394037a3dec78feaab4971a0b0cb172 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Tue, 3 Dec 2024 18:46:15 +0300 Subject: [PATCH 2/3] fix connection test --- tests/connections/database/test_postgres.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/connections/database/test_postgres.py b/tests/connections/database/test_postgres.py index d9cc142..321b0f8 100644 --- a/tests/connections/database/test_postgres.py +++ b/tests/connections/database/test_postgres.py @@ -1,7 +1,8 @@ -from src.core.config import settings from sqlalchemy import text from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker +from src.core.config import settings + test_engine = create_async_engine(settings.db_url, echo=True) sessionmaker_test = async_sessionmaker(test_engine) From 208c8f365756d918eae1f043d91b6814a297f876 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Tue, 3 Dec 2024 18:49:12 +0300 Subject: [PATCH 3/3] fix --- Makefile | 6 +++--- src/repositories/database/base.py | 25 +++++++++++++++---------- tests/conftest.py | 1 + 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 389c4ab..793584e 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,13 @@ run-local: poetry run uvicorn src.api.app:app --host 0.0.0.0 --port 8000 --reload --reload-dir . --log-config=log_config.ini --log-level=debug build: - poetry run sudo docker compose -f docker-compose.$(for).yml build + poetry run docker-compose -f docker-compose.$(for).yml build start: - poetry run sudo docker compose -f docker-compose.$(for).yml up --force-recreate --remove-orphans + poetry run docker-compose -f docker-compose.$(for).yml up --force-recreate --remove-orphans up: - poetry run sudo docker compose -f docker-compose.$(for).yml up --force-recreate --remove-orphans -d + poetry run docker-compose -f docker-compose.$(for).yml up --force-recreate --remove-orphans -d stop: poetry run docker-compose -f docker-compose.$(for).yml stop diff --git a/src/repositories/database/base.py b/src/repositories/database/base.py index e17dd4c..0d1d07d 100644 --- a/src/repositories/database/base.py +++ b/src/repositories/database/base.py @@ -8,6 +8,15 @@ from src.core.config import settings from src.models.base import Base +engine = create_async_engine( + url=settings.db_url, + echo=settings.echo, +) +sessionmaker = async_sessionmaker( + bind=engine, + expire_on_commit=False, +) + @dataclass class BaseDatabaseRepository(ABC): @@ -22,34 +31,30 @@ class DatabaseRepositories(ABC): @dataclass class SQLAlchemyRepository(BaseDatabaseRepository): - engine = create_async_engine(url=settings.db_url, echo=settings.echo, pool_pre_ping=True) - sessionmaker = async_sessionmaker(engine, expire_on_commit=False) - async def add(self, obj: Base) -> None: - async with self.sessionmaker() as session: + async with sessionmaker() as session: session.add(obj) await session.commit() - await self.engine.dispose() async def merge(self, obj: Base) -> None: - async with self.sessionmaker() as session: + async with sessionmaker() as session: await session.merge(obj) async def execute(self, statement: Executable) -> None: - async with self.sessionmaker() as session: + async with sessionmaker() as session: await session.execute(statement) async def get(self, table: type[Base], primary_key: Any): # type: ignore[no-untyped-def] - async with self.sessionmaker() as session: + async with sessionmaker() as session: model = await session.get(table, primary_key) return model async def scalar(self, statement: Executable) -> Any: - async with self.sessionmaker() as session: + async with sessionmaker() as session: item = await session.scalar(statement) return item async def scalars(self, statement: Executable) -> Iterable: - async with self.sessionmaker() as session: + async with sessionmaker() as session: items = await session.scalars(statement) return items diff --git a/tests/conftest.py b/tests/conftest.py index f2d6648..a1c47cd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ import pytest from fastapi.testclient import TestClient + from src.api.app import app