From 541ffd99c937030dcdd6c63bb788bd4c9b65730f Mon Sep 17 00:00:00 2001 From: Bair Erendzhenov Date: Mon, 25 Mar 2024 22:36:48 +0300 Subject: [PATCH] 2nd commit --- tests/conftest.py | 28 +++++++++++++--------------- tests/test_app.py | 28 ++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 19ba7e7..e109ffb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,7 @@ import asyncio from typing import AsyncGenerator -import pytest -#from fastapi.testclient import TestClient +import pytest_asyncio from httpx import AsyncClient from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker from sqlalchemy.orm import sessionmaker @@ -14,25 +13,24 @@ 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 +TestingSessionLocal = async_sessionmaker(engine_test, expire_on_commit=False) async def override_get_async_session() -> AsyncGenerator[AsyncSession, None]: - async with AsyncSessionLocal() as session: + async with TestingSessionLocal() as session: yield session app.dependency_overrides[get_async_session] = override_get_async_session -@pytest.fixture(autouse=True, scope='session') +@pytest_asyncio.fixture(autouse=True) async def prepare_database(): - async with engine_test.begin() as conn: - await conn.run_sync(Base.metadata.drop_all) + async with engine_test.begin() as conn: await conn.run_sync(Base.metadata.create_all) - await load_models_data() - yield + yield + async with engine_test.begin() as conn: + await conn.run_sync(Base.metadata.drop_all) # SETUP -@pytest.fixture(scope='session') +@pytest_asyncio.fixture(scope='session') def event_loop(): loop = asyncio.get_event_loop_policy().new_event_loop() yield loop @@ -40,10 +38,10 @@ def event_loop(): #client = TestClient(app) -@pytest.fixture(scope="session") -async def ac() -> AsyncGenerator[AsyncClient, None]: +@pytest_asyncio.fixture(scope="session") +async def client() -> AsyncGenerator[AsyncClient, None]: async with AsyncClient( app=app, base_url="http://localhost/api/v1/hrspace" - ) as ac: - yield ac + ) as client: + yield client diff --git a/tests/test_app.py b/tests/test_app.py index fd599fb..a991454 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -2,10 +2,30 @@ from httpx import AsyncClient import app.main +from app.repositories import crud from tests import conftest -@pytest.mark.asyncio -async def test_get_cities(ac: AsyncClient): - params = {"id": 2, "name": "Москва"} - response = await ac.get("/cities") +pytest_mark_anyio = pytest.mark.asyncio + +@pytest_mark_anyio +async def test_get_cities(client: AsyncClient): + response = await client.get("/cities") + assert response.status_code == 200 + +@pytest_mark_anyio +async def test_get_vacancy_names(client: AsyncClient): + response = await client.get("/vacancy_names") + assert response.status_code == 200 + +@pytest_mark_anyio +async def test_get_categories(client: AsyncClient): + response = await client.get("/categories") + assert response.status_code == 200 + +@pytest_mark_anyio +async def test_get_specialization(client: AsyncClient): + specialization = await crud.create_specialization( + conftest.override_get_async_session(), vacancy_name_id=1) + response = await client.get(f"/specializations/{specialization.id}") assert response.status_code == 200 + assert response.json()["id"] == specialization.id