Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ErendzhenovBair committed Mar 25, 2024
1 parent bb154ae commit 3906b00
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements/test.requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ alembic
asyncpg
email-validator
fastapi
httpx
pydantic-settings
SQLAlchemy
uvicorn[standard]
Expand Down
Empty file added tests/__init__.py
Empty file.
49 changes: 49 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3906b00

Please sign in to comment.