tests: test init
This commit is contained in:
parent
1a81be210a
commit
19145271ae
5 changed files with 78 additions and 4 deletions
|
|
@ -6,7 +6,7 @@ Exports:
|
|||
- Base (sqlalchemy base model)
|
||||
"""
|
||||
from typing import Annotated
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy import create_engine, StaticPool
|
||||
from sqlalchemy.orm import DeclarativeBase, sessionmaker, Session
|
||||
|
||||
from fastapi import Depends
|
||||
|
|
@ -16,10 +16,10 @@ from src.config import SQLALCHEMY_DATABASE_URI, settings as global_settings
|
|||
|
||||
if global_settings.ENVIRONMENT == Environment.TESTING:
|
||||
connect_args = {"check_same_thread": False}
|
||||
engine = create_engine(SQLALCHEMY_DATABASE_URI.get_secret_value(), connect_args=connect_args, poolclass=StaticPool)
|
||||
else:
|
||||
connect_args = {}
|
||||
engine = create_engine(SQLALCHEMY_DATABASE_URI.get_secret_value())
|
||||
|
||||
engine = create_engine(SQLALCHEMY_DATABASE_URI.get_secret_value(), connect_args=connect_args)
|
||||
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ app.add_middleware(
|
|||
allow_headers=settings.CORS_HEADERS,
|
||||
)
|
||||
|
||||
if settings.DISABLE_AUTH and (settings.ENVIRONMENT == Environment.LOCAL or settings.ENVIRONMENT == Environment.TESTING):
|
||||
if settings.DISABLE_AUTH and (settings.ENVIRONMENT == Environment.LOCAL):
|
||||
app.dependency_overrides[get_current_user] = get_dev_user
|
||||
|
||||
|
||||
|
|
|
|||
0
test/__init__.py
Normal file
0
test/__init__.py
Normal file
63
test/conftest.py
Normal file
63
test/conftest.py
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
from typing import AsyncGenerator
|
||||
|
||||
import pytest
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from src.user.models import User
|
||||
from src.auth.service import get_current_user, get_dev_user
|
||||
|
||||
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():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
except:
|
||||
db.rollback()
|
||||
raise
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client(db_session) -> AsyncGenerator[AsyncClient, None]:
|
||||
def get_db_override():
|
||||
return db_session
|
||||
app.dependency_overrides[get_db] = get_db_override
|
||||
app.dependency_overrides[get_current_user] = get_dev_user
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://localhost:8000/api/v1") as ac:
|
||||
yield ac
|
||||
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def setup_database():
|
||||
Base.metadata.create_all(bind=engine)
|
||||
yield
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def seed_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
db.add(User(email="admin@test.com", first_name="Admin", last_name="Test", oidc_id="abcd-efgh-ijkl-mnop"))
|
||||
db.commit()
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def seed_data(setup_database, seed_db):
|
||||
yield
|
||||
|
||||
11
test/test_healthcheck.py
Normal file
11
test/test_healthcheck.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import pytest
|
||||
from httpx import AsyncClient
|
||||
|
||||
from .conftest import client
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_healthcheck(client: AsyncClient):
|
||||
resp = await client.get("/healthcheck")
|
||||
|
||||
assert resp.status_code == 200
|
||||
assert resp.json() == {"status": "ok"}
|
||||
Loading…
Add table
Add a link
Reference in a new issue