diff --git a/Dockerfile b/Dockerfile index 634d067..dc34eac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,10 @@ RUN mkdir -p /usr/src/fastfood WORKDIR /usr/src/fastfood -COPY . . +COPY ./pyproject.toml . + +COPY ./poetry.lock . + +RUN touch /usr/src/RUN_IN_DOCKER RUN poetry install diff --git a/compose_app.yml b/compose_app.yml index a167cdb..a8ad9a8 100644 --- a/compose_app.yml +++ b/compose_app.yml @@ -1,5 +1,19 @@ version: "3.8" services: + redis: + container_name: redis_test + + image: redis:7.2.4-alpine3.19 + + ports: + - '6380:6379' + + healthcheck: + test: [ "CMD", "redis-cli","ping" ] + interval: 10s + timeout: 5s + retries: 5 + db: container_name: pgdb @@ -38,6 +52,8 @@ services: depends_on: db: condition: service_healthy + redis: + condition: service_healthy restart: always diff --git a/compose_test.yml b/compose_test.yml index b7beb91..43cea1b 100644 --- a/compose_test.yml +++ b/compose_test.yml @@ -1,5 +1,19 @@ version: "3.8" services: + redis: + container_name: redis_test + + image: redis:7.2.4-alpine3.19 + + ports: + - '6380:6379' + + healthcheck: + test: [ "CMD", "redis-cli","ping" ] + interval: 10s + timeout: 5s + retries: 5 + db: container_name: pgdb_test @@ -38,6 +52,11 @@ services: depends_on: db: condition: service_healthy + redis: + condition: service_healthy + + volumes: + - .:/usr/src/fastfood restart: always diff --git a/fastfood/config.py b/fastfood/config.py index 524da54..e180dc7 100644 --- a/fastfood/config.py +++ b/fastfood/config.py @@ -1,3 +1,5 @@ +import os + from pydantic_settings import BaseSettings, SettingsConfigDict @@ -11,10 +13,20 @@ class Settings(BaseSettings): REDIS_DB: str = '' @property - def DATABASE_URL_asyncpg(self): + def DATABASE_URL_asyncpg(self) -> str: """ Возвращает строку подключения к БД необходимую для SQLAlchemy """ + # Проверяем, в DOCKER или нет + + file_path = '/usr/src/RUN_IN_DOCKER' + if os.path.exists(file_path): + return ( + 'postgresql+asyncpg://' + f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}' + f'@db:{self.DB_PORT}/{self.POSTGRES_DB}' + ) + return ( 'postgresql+asyncpg://' f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}' @@ -26,12 +38,28 @@ class Settings(BaseSettings): """ Возвращает строку подключения к БД необходимую для SQLAlchemy """ + file_path = '/usr/src/RUN_IN_DOCKER' + if os.path.exists(file_path): + return ( + 'postgresql+asyncpg://' + f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}' + f'@db:{self.DB_PORT}/{self.POSTGRES_DB_TEST}' + ) + return ( 'postgresql+asyncpg://' f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}' f'@{self.DB_HOST}:{self.DB_PORT}/{self.POSTGRES_DB_TEST}' ) + @property + def REDIS_URL(self): + file_path = '/usr/src/RUN_IN_DOCKER' + if os.path.exists(file_path): + return 'redis://redis:6379/0' + + return self.REDIS_DB + model_config = SettingsConfigDict(env_file='.env') diff --git a/fastfood/dbase.py b/fastfood/dbase.py index f7b38d3..73bc929 100644 --- a/fastfood/dbase.py +++ b/fastfood/dbase.py @@ -20,7 +20,7 @@ async def get_async_session() -> AsyncGenerator[AsyncSession, None]: def get_redis_pool(): - return redis.from_url(settings.REDIS_DB, decode_responses=False) + return redis.from_url(settings.REDIS_URL, decode_responses=False) async def get_async_redis_client( diff --git a/tests/conftest.py b/tests/conftest.py index 4dd381e..30f1296 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,5 @@ import asyncio -from typing import AsyncGenerator, Generator +from typing import AsyncGenerator import pytest import pytest_asyncio @@ -45,22 +45,13 @@ async def get_test_session() -> AsyncGenerator[AsyncSession, None]: yield session -@pytest.fixture(scope='session') -def app(event_loop) -> Generator[FastAPI, None, None]: +@pytest_asyncio.fixture(scope='session') +async def client() -> AsyncGenerator[AsyncClient, None]: app: FastAPI = create_app() app.dependency_overrides[get_async_session] = get_test_session - yield app - -@pytest_asyncio.fixture(scope='session') -async def client(app) -> AsyncGenerator[AsyncClient, None]: async with AsyncClient( app=app, base_url='http://localhost:8000/api/v1/menus', ) as async_client: yield async_client - - -@pytest.fixture(scope='session') -def session_data() -> dict: - return {} diff --git a/tests/test_postman.py b/tests/test_postman.py index c507bb1..da61dbf 100644 --- a/tests/test_postman.py +++ b/tests/test_postman.py @@ -4,6 +4,11 @@ from httpx import AsyncClient from .repository import Repository as Repo +@pytest.fixture(scope='module', autouse=True) +def session_data() -> dict: + return {} + + @pytest.mark.asyncio async def test_01(client: AsyncClient, session_data: dict): """Проверяет создание меню"""