тесты

develop
Сергей Ванюшкин 2024-02-05 19:13:40 +03:00
parent 43eca19d91
commit a5eebd15ba
7 changed files with 78 additions and 15 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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')

View File

@ -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(

View File

@ -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 {}

View File

@ -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):
"""Проверяет создание меню"""