from fastapi.testclient import TestClient import pytest from sqlalchemy import create_engine, Column, Integer, String, DateTime, Text, func from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker DATABASE_URL = "sqlite:///./test.db" engine = create_engine(DATABASE_URL) TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() class Task(Base): __tablename__ = "tasks" id = Column(Integer, primary_key=True, index=True) title = Column(String(100), nullable=False) description = Column(Text) status = Column(String(20), default="pending") created_at = Column(DateTime, server_default=func.now()) Base.metadata.create_all(bind=engine) def override_get_db(): try: yield TestingSessionLocal() finally: pass from main import app, get_db app.dependency_overrides[get_db] = override_get_db client = TestClient(app) @pytest.fixture(autouse=True) def clean_database(): with engine.begin() as conn: conn.execute("DELETE FROM tasks") def test_create_task(): response = client.post("/tasks/", json={"title": "Test Task", "description": "This is a test task"}) assert response.status_code == 201 data = response.json() assert data["id"] > 0 assert data["title"] == "Test Task" assert data["description"] == "This is a test task" def test_list_tasks(): client.post("/tasks/", json={"title": "Task 1", "description": "First task"}) client.post("/tasks/", json={"title": "Task 2", "description": "Second task"}) response = client.get("/tasks/") assert response.status_code == 200 data = response.json() assert len(data) == 2 def test_get_task(): response = client.post("/tasks/", json={"title": "Get Task", "description": "Task to get"}) task_id = response.json()["id"] response = client.get(f"/tasks/{task_id}") assert response.status_code == 200 data = response.json() assert data["id"] == task_id def test_get_task_not_found(): response = client.get("/tasks/999") assert response.status_code == 404 def test_update_task(): response = client.post("/tasks/", json={"title": "Update Task", "description": "Task to update"}) task_id = response.json()["id"] updated_data = {"title": "Updated Title"} response = client.put(f"/tasks/{task_id}", json=updated_data) assert response.status_code == 200 data = response.json() assert data["id"] == task_id assert data["title"] == "Updated Title" def test_update_task_not_found(): updated_data = {"title": "Updated Title"} response = client.put("/tasks/999", json=updated_data) assert response.status_code == 404 def test_delete_task(): response = client.post("/tasks/", json={"title": "Delete Task", "description": "Task to delete"}) task_id = response.json()["id"] response = client.delete(f"/tasks/{task_id}") assert response.status_code == 204 response = client.get(f"/tasks/{task_id}") assert response.status_code == 404 def test_delete_task_not_found(): response = client.delete("/tasks/999") assert response.status_code == 404 This `test_main.py` file includes tests for all CRUD operations using the FastAPI TestClient. Each test creates its own data and ensures that there are no dependencies between tests. The database is reset before each test to ensure isolation.