Compare commits
2 commits
f30c06b522
...
fc10307cc2
| Author | SHA1 | Date | |
|---|---|---|---|
| fc10307cc2 | |||
| 6fb841516c |
4 changed files with 55 additions and 0 deletions
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
43
tests/conftest.py
Normal file
43
tests/conftest.py
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from typing import AsyncGenerator
|
||||||
|
from httpx import AsyncClient, ASGITransport
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
from src.main import app # inited FastAPI app
|
||||||
|
from src.database import engine, Base, get_db
|
||||||
|
|
||||||
|
|
||||||
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def db_session():
|
||||||
|
Base.metadata.drop_all(bind=engine)
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
_seed(db)
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.rollback()
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def default_client(db_session) -> AsyncGenerator[AsyncClient, None]:
|
||||||
|
def get_db_override():
|
||||||
|
return db_session
|
||||||
|
|
||||||
|
app.dependency_overrides[get_db] = get_db_override
|
||||||
|
transport = ASGITransport(app=app)
|
||||||
|
async with AsyncClient(
|
||||||
|
transport=transport, base_url="http://localhost:8000/api/v1"
|
||||||
|
) as ac:
|
||||||
|
yield ac
|
||||||
|
|
||||||
|
app.dependency_overrides.clear()
|
||||||
|
|
||||||
|
|
||||||
|
def _seed(db):
|
||||||
|
pass
|
||||||
2
tests/pytest.toml
Normal file
2
tests/pytest.toml
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
[tool.pytest]
|
||||||
|
markers = []
|
||||||
10
tests/test_healthcheck.py
Normal file
10
tests/test_healthcheck.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
import pytest
|
||||||
|
from httpx import AsyncClient
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_healthcheck(default_client: AsyncClient):
|
||||||
|
resp = await default_client.get("/healthcheck")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert resp.json() == {"status": "ok"}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue