feat: 开发测试接口
This commit is contained in:
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
15
app/main.py
Normal file
15
app/main.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI(title="CI/CD Demo", version="1.0.0")
|
||||
|
||||
@app.get("/")
|
||||
def root():
|
||||
return {"status": "ok", "version": "1.0.0"}
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return {"healthy": True}
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
def get_item(item_id: int):
|
||||
return {"item_id": item_id, "name": f"Item {item_id}"}
|
||||
23
app/test_main.py
Normal file
23
app/test_main.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from fastapi.testclient import TestClient
|
||||
from app.main import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
def test_root():
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"status": "ok", "version": "1.0.0"}
|
||||
|
||||
def test_health():
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"healthy": True}
|
||||
|
||||
def test_get_item():
|
||||
response = client.get("/items/1")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"item_id": 1, "name": "Item 1"}
|
||||
|
||||
def test_get_item_invalid():
|
||||
response = client.get("/items/abc")
|
||||
assert response.status_code == 422
|
||||
Reference in New Issue
Block a user