from fastapi.testclient import TestClient def test_health_check(): """测试健康检查端点""" from app.main import app client = TestClient(app) response = client.get("/health") assert response.status_code == 200 assert response.json() == {"status": "ok"} def test_create_item(): """测试创建项目""" from app.main import app client = TestClient(app) response = client.post("/items", json={"name": "Test Item", "description": "A test"}) assert response.status_code == 200 data = response.json() assert data["name"] == "Test Item" assert data["id"] == 1 def test_list_items(): """测试列出项目""" from app.main import app client = TestClient(app) client.post("/items", json={"name": "Item 1"}) response = client.get("/items") assert response.status_code == 200 assert len(response.json()) >= 1 def test_get_item(): """测试获取单个项目""" from app.main import app client = TestClient(app) created = client.post("/items", json={"name": "Item 2"}).json() response = client.get(f"/items/{created['id']}") assert response.status_code == 200 assert response.json()["name"] == "Item 2" def test_get_item_not_found(): """测试获取不存在的项目""" from app.main import app client = TestClient(app) response = client.get("/items/99999") assert response.status_code == 404