53 lines
1.5 KiB
Python
53 lines
1.5 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}")
|
||
|
|
import traceback
|
||
|
|
traceback.print_exc()
|
||
|
|
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")
|
||
|
|
|
||
|
|
print("\n" + "=" * 60)
|
||
|
|
print("测试完成!")
|
||
|
|
print("=" * 60)
|