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
import requests
import json
import time
 
# API的基础URL
BASE_URL = "http://localhost:9676/api/v1"
 
def test_health_check():
    """测试健康检查端点"""
    print("测试健康检查端点...")
 
    try:
        response = requests.get("http://localhost:9676/health")
        print(f"健康检查响应: {response.status_code}")
        print(f"健康检查响应正文: {response.text}")
        return response
    except Exception as e:
        print(f"健康检查时出错: {e}")
        return None
 
 
def test_save_contenttype():
    """测试ContentType保存端点"""
    print("\n测试ContentType保存端点...")
 
    # 要保存的ContentType数据
    data_to_save = {"code": "FAQ", "name": "常见问题"}
 
    try:
        response = requests.post(f"{BASE_URL}/type/save", json=data_to_save)
        print(f"ContentType保存响应: {response.status_code}")
        print(f"ContentType保存响应正文: {response.text}")
        return response
    except Exception as e:
        print(f"ContentType保存测试时出错: {e}")
        return None
 
 
def test_get_contenttype():
    """测试ContentType检索端点"""
    print("\n测试ContentType检索端点...")
 
    try:
        # 获取所有ContentType
        response = requests.get(f"{BASE_URL}/type/get")
        print(f"ContentType获取响应: {response.status_code}")
        print(f"ContentType获取响应正文: {response.text}")
        return response
    except Exception as e:
        print(f"ContentType获取测试时出错: {e}")
        return None
 
 
def test_save_content():
    """测试Content保存端点"""
    print("\n测试Content保存端点...")
 
    # 先获取FAQ类型的ID
    try:
        response = requests.get(f"{BASE_URL}/type/get?code=FAQ")
        if response.status_code == 200:
            data = response.json()
            if data["count"] > 0:
                content_type_id = data["data"][0]["id"]
 
                # 要保存的Content数据
                data_to_save = {
                    "type": content_type_id,
                    "question": "这是什么系统?",
                    "answer": "这是一个AI管理系统。",
                }
 
                response = requests.post(f"{BASE_URL}/content/save", json=data_to_save)
                print(f"Content保存响应: {response.status_code}")
                print(f"Content保存响应正文: {response.text}")
                return response
            else:
                print("未找到FAQ ContentType,无法测试Content保存")
                return None
        else:
            print("获取FAQ ContentType失败,无法测试Content保存")
            return None
    except Exception as e:
        print(f"Content保存测试时出错: {e}")
        return None
 
 
def test_get_content():
    """测试Content检索端点"""
    print("\n测试Content检索端点...")
 
    try:
        # 获取所有Content
        response = requests.get(f"{BASE_URL}/content/get")
        print(f"Content获取响应: {response.status_code}")
        print(f"Content获取响应正文: {response.text}")
        return response
    except Exception as e:
        print(f"Content获取测试时出错: {e}")
        return None
 
 
def test_get_content_by_type():
    """测试按类型检索Content"""
    print("\n测试按类型检索Content...")
 
    try:
        # 获取FAQ类型的Content
        response = requests.get(f"{BASE_URL}/content/get?type=1")
        print(f"按类型Content获取响应: {response.status_code}")
        print(f"按类型Content获取响应正文: {response.text}")
        return response
    except Exception as e:
        print(f"按类型Content获取测试时出错: {e}")
        return None
 
 
if __name__ == "__main__":
    print("开始API测试...\n")
 
    # 等待片刻以便服务器准备就绪(如果正在运行)
    time.sleep(1)
 
    # 首先测试健康检查
    test_health_check()
 
    # 测试ContentType保存
    test_save_contenttype()
 
    # 测试ContentType检索
    test_get_contenttype()
 
    # 测试Content保存
    test_save_content()
 
    # 测试Content检索
    test_get_content()
 
    # 测试按类型检索Content
    test_get_content_by_type()
 
    print("\n测试完成!")