feat: add FastAPI example project with CRUD API
- Add FastAPI application with health check and item CRUD endpoints - Add Pydantic models for request/response validation - Add pytest test suite with 5 passing tests - Add project documentation and run script Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
23e94ada3b
commit
67d8c6bf30
10 files changed
+185
No files matched your search
@@ -0,0 +1 @@
|
||||
# tests package
|
||||
@@ -0,0 +1,49 @@
|
||||
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
|
||||
Reference in new issue
Block a user