yj
2026-03-31 4becf7b5eba5de2d9889f379eb88aeb6cb16f760
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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("测试完成!")