Skip to content

Commit

Permalink
2nd commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ErendzhenovBair committed Mar 25, 2024
1 parent 3906b00 commit 541ffd9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
28 changes: 13 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,36 +13,35 @@
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
loop.close()

#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
28 changes: 24 additions & 4 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 541ffd9

Please sign in to comment.