75b1d16453
Made-with: Cursor
66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
import requests
|
|
import json
|
|
|
|
BASE_URL = "http://localhost:8002"
|
|
|
|
def test_api(endpoint, method="GET", data=None, params=None):
|
|
"""测试 API 接口"""
|
|
url = f"{BASE_URL}{endpoint}"
|
|
try:
|
|
if method == "GET":
|
|
response = requests.get(url, params=params)
|
|
elif method == "POST":
|
|
response = requests.post(url, json=data, params=params)
|
|
elif method == "PUT":
|
|
response = requests.put(url, json=data)
|
|
elif method == "DELETE":
|
|
response = requests.delete(url)
|
|
|
|
result = response.json()
|
|
print(f"\n{method} {endpoint}")
|
|
print(f"Status: {response.status_code}")
|
|
print(f"Response: {json.dumps(result, ensure_ascii=False, indent=2)}")
|
|
return result
|
|
except Exception as e:
|
|
print(f"\n{method} {endpoint} - Error: {e}")
|
|
return None
|
|
|
|
# 测试品牌接口
|
|
print("=" * 60)
|
|
print("测试品牌接口")
|
|
print("=" * 60)
|
|
|
|
# 1. 获取品牌列表(成功,有数据)
|
|
test_api("/api/brands/", params={"skip": 0, "limit": 5})
|
|
|
|
# 2. 模糊查询品牌(成功,有数据)
|
|
test_api("/api/brands/", params={"skip": 0, "limit": 10, "name": "Audio"})
|
|
|
|
# 3. 模糊查询品牌(无数据)
|
|
test_api("/api/brands/", params={"skip": 0, "limit": 10, "name": "不存在的品牌XYZ"})
|
|
|
|
# 4. 获取单个品牌(成功)
|
|
test_api("/api/brands/1")
|
|
|
|
# 5. 获取单个品牌(不存在)
|
|
test_api("/api/brands/999999")
|
|
|
|
# 6. 创建品牌(成功)
|
|
test_api("/api/brands/", method="POST", data={"name": "Test Brand 测试"})
|
|
|
|
# 7. 创建品牌(重复)
|
|
test_api("/api/brands/", method="POST", data={"name": "1MORE"})
|
|
|
|
# 8. 更新品牌(成功)
|
|
test_api("/api/brands/1", method="PUT", data={"name": "1MORE Updated"})
|
|
|
|
# 9. 删除品牌(成功)
|
|
test_api("/api/brands/623", method="DELETE")
|
|
|
|
# 10. 删除品牌(不存在)
|
|
test_api("/api/brands/999999", method="DELETE")
|
|
|
|
print("\n" + "=" * 60)
|
|
print("测试完成!")
|
|
print("=" * 60)
|