From 3906b008cc68c2e3e922b6902850b7488c54d5b2 Mon Sep 17 00:00:00 2001 From: Bair Erendzhenov Date: Mon, 25 Mar 2024 18:06:09 +0300 Subject: [PATCH] first commit --- requirements/test.requirements.txt | 1 + tests/__init__.py | 0 tests/conftest.py | 49 ++++++++++++++++++++++++++++++ tests/test_app.py | 11 +++++++ 4 files changed, 61 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_app.py diff --git a/requirements/test.requirements.txt b/requirements/test.requirements.txt index e765ad7..c42b2db 100644 --- a/requirements/test.requirements.txt +++ b/requirements/test.requirements.txt @@ -2,6 +2,7 @@ alembic asyncpg email-validator fastapi +httpx pydantic-settings SQLAlchemy uvicorn[standard] diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..19ba7e7 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,49 @@ +import asyncio +from typing import AsyncGenerator + +import pytest +#from fastapi.testclient import TestClient +from httpx import AsyncClient +from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker +from sqlalchemy.orm import sessionmaker + +from app.core.dependencies import get_async_session +from app.data.load_data import load_models_data +from app.models.models import Base +from app.core.config import settings +from app.main import app + +engine_test = create_async_engine(settings.database_url, echo=True) +AsyncSessionLocal = async_sessionmaker(engine_test, expire_on_commit=False) +Base.metadata.bind = engine_test + +async def override_get_async_session() -> AsyncGenerator[AsyncSession, None]: + async with AsyncSessionLocal() as session: + yield session + +app.dependency_overrides[get_async_session] = override_get_async_session + +@pytest.fixture(autouse=True, scope='session') +async def prepare_database(): + async with engine_test.begin() as conn: + await conn.run_sync(Base.metadata.drop_all) + await conn.run_sync(Base.metadata.create_all) + await load_models_data() + yield + +# SETUP +@pytest.fixture(scope='session') +def event_loop(): + loop = asyncio.get_event_loop_policy().new_event_loop() + yield loop + loop.close() + +#client = TestClient(app) + +@pytest.fixture(scope="session") +async def ac() -> AsyncGenerator[AsyncClient, None]: + async with AsyncClient( + app=app, + base_url="http://localhost/api/v1/hrspace" + ) as ac: + yield ac diff --git a/tests/test_app.py b/tests/test_app.py new file mode 100644 index 0000000..fd599fb --- /dev/null +++ b/tests/test_app.py @@ -0,0 +1,11 @@ +import pytest +from httpx import AsyncClient + +import app.main +from tests import conftest + +@pytest.mark.asyncio +async def test_get_cities(ac: AsyncClient): + params = {"id": 2, "name": "Москва"} + response = await ac.get("/cities") + assert response.status_code == 200