import requests
|
import json
|
|
# 测试基础URL
|
BASE_URL = "http://localhost:9676"
|
|
def test_single_contenttype_save():
|
"""测试保存单个ContentType"""
|
print("测试保存单个ContentType...")
|
single_data = {
|
"code": "TEST001",
|
"name": "测试类型1"
|
}
|
|
response = requests.post(f"{BASE_URL}/api/v1/type/save", json=single_data)
|
print(f"状态码: {response.status_code}")
|
print(f"响应: {response.json()}")
|
print()
|
|
def test_multiple_contenttype_save():
|
"""测试保存多个ContentType"""
|
print("测试保存多个ContentType...")
|
multiple_data = [
|
{
|
"code": "TEST002",
|
"name": "测试类型2"
|
},
|
{
|
"code": "TEST003",
|
"name": "测试类型3"
|
}
|
]
|
|
response = requests.post(f"{BASE_URL}/api/v1/type/save", json=multiple_data)
|
print(f"状态码: {response.status_code}")
|
print(f"响应: {response.json()}")
|
print()
|
|
def test_contenttype_update_by_id():
|
"""测试通过ID更新ContentType"""
|
print("测试通过ID更新ContentType...")
|
# 首先获取一个类型ID用于更新测试
|
response = requests.get(f"{BASE_URL}/api/v1/type/get")
|
types = response.json().get('data', [])
|
if types:
|
type_id = types[0]['id']
|
update_data = {
|
"id": type_id,
|
"code": "TEST001-UPDATED",
|
"name": "更新的测试类型1"
|
}
|
|
response = requests.post(f"{BASE_URL}/api/v1/type/save", json=update_data)
|
print(f"状态码: {response.status_code}")
|
print(f"响应: {response.json()}")
|
else:
|
print("没有找到类型,无法测试更新")
|
print()
|
|
def test_single_content_save():
|
"""测试保存单个Content"""
|
print("测试保存单个Content...")
|
# 首先获取一个类型ID用于测试
|
response = requests.get(f"{BASE_URL}/api/v1/type/get")
|
types = response.json().get('data', [])
|
if types:
|
type_id = types[0]['id']
|
single_data = {
|
"type": type_id,
|
"question": "测试问题1",
|
"answer": "测试答案1"
|
}
|
|
response = requests.post(f"{BASE_URL}/api/v1/content/save", json=single_data)
|
print(f"状态码: {response.status_code}")
|
print(f"响应: {response.json()}")
|
else:
|
print("没有找到类型,无法测试Content保存")
|
print()
|
|
def test_multiple_content_save():
|
"""测试保存多个Content"""
|
print("测试保存多个Content...")
|
# 首先获取一个类型ID用于测试
|
response = requests.get(f"{BASE_URL}/api/v1/type/get")
|
types = response.json().get('data', [])
|
if types:
|
type_id = types[0]['id']
|
multiple_data = [
|
{
|
"type": type_id,
|
"question": "测试问题2",
|
"answer": "测试答案2"
|
},
|
{
|
"type": type_id,
|
"question": "测试问题3",
|
"answer": "测试答案3"
|
}
|
]
|
|
response = requests.post(f"{BASE_URL}/api/v1/content/save", json=multiple_data)
|
print(f"状态码: {response.status_code}")
|
print(f"响应: {response.json()}")
|
else:
|
print("没有找到类型,无法测试Content保存")
|
print()
|
|
def test_content_update_by_id():
|
"""测试通过ID更新Content"""
|
print("测试通过ID更新Content...")
|
# 首先获取一个内容ID用于更新测试
|
response = requests.get(f"{BASE_URL}/api/v1/content/get")
|
contents = response.json().get('data', [])
|
if contents:
|
content_id = contents[0]['id']
|
type_id = contents[0]['type_id'] # 使用正确的字段名
|
update_data = {
|
"id": content_id,
|
"type": type_id,
|
"question": "更新的测试问题",
|
"answer": "更新的测试答案"
|
}
|
|
response = requests.post(f"{BASE_URL}/api/v1/content/save", json=update_data)
|
print(f"状态码: {response.status_code}")
|
print(f"响应: {response.json()}")
|
else:
|
print("没有找到内容,无法测试更新")
|
print()
|
|
def test_get_all_contenttypes():
|
"""测试获取所有ContentType"""
|
print("测试获取所有ContentType...")
|
response = requests.get(f"{BASE_URL}/api/v1/type/get")
|
print(f"状态码: {response.status_code}")
|
print(f"响应: {response.json()}")
|
print()
|
|
def test_get_all_content():
|
"""测试获取所有Content"""
|
print("测试获取所有Content...")
|
response = requests.get(f"{BASE_URL}/api/v1/content/get")
|
print(f"状态码: {response.status_code}")
|
print(f"响应: {response.json()}")
|
print()
|
|
if __name__ == "__main__":
|
print("开始测试多记录保存和ID更新功能...")
|
print("=" * 50)
|
|
# 测试功能
|
test_single_contenttype_save()
|
test_multiple_contenttype_save()
|
test_contenttype_update_by_id()
|
|
test_single_content_save()
|
test_multiple_content_save()
|
test_content_update_by_id()
|
|
# 获取所有数据以验证保存结果
|
test_get_all_contenttypes()
|
test_get_all_content()
|
|
print("测试完成!")
|