
- Set up project structure and dependencies - Create task model and schema - Implement Alembic migrations - Add CRUD API endpoints for task management - Add health endpoint with database connectivity check - Add comprehensive error handling - Add tests for API endpoints - Update README with API documentation
53 lines
1.2 KiB
Python
53 lines
1.2 KiB
Python
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.db.base import Base
|
|
from app.db.session import get_db
|
|
from main import app
|
|
|
|
|
|
# Create test database
|
|
TEST_SQLALCHEMY_DATABASE_URL = "sqlite:///./test_db.sqlite"
|
|
|
|
engine = create_engine(
|
|
TEST_SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
|
)
|
|
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def db():
|
|
"""
|
|
Create a fresh database for each test.
|
|
"""
|
|
# Create tables
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
# Create session
|
|
db = TestingSessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
# Drop tables after test
|
|
Base.metadata.drop_all(bind=engine)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def client(db):
|
|
"""
|
|
Create a test client with a test database session.
|
|
"""
|
|
def override_get_db():
|
|
try:
|
|
yield db
|
|
finally:
|
|
pass
|
|
|
|
app.dependency_overrides[get_db] = override_get_db
|
|
with TestClient(app) as c:
|
|
yield c
|
|
app.dependency_overrides.clear() |