test_prompts_api.py
9.6 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""提示词模板 API 测试"""
import pytest
from httpx import AsyncClient
class TestPromptAPI:
"""提示词模板 API 测试"""
PROMPT_CREATE_DATA = {
"template_type": "general",
"title": "通用质检提示词",
"code": "TEMPLATE-GEN-001",
"system_prompt": "你是一个专业的质检审核助手。请根据标准对以下内容进行审核。",
"user_prompt": "请审核以下内容:{{content}}",
"variables": [{"name": "content", "type": "string"}],
}
PROMPT_UPDATE_DATA = {
"title": "更新后的质检提示词",
"system_prompt": "你是一个高级质检审核助手。",
}
@pytest.mark.asyncio
async def _create_prompt(self, client: AsyncClient) -> dict:
"""辅助方法:创建一个提示词模板并返回完整响应"""
resp = await client.post("/api/v1/prompts", json=self.PROMPT_CREATE_DATA)
return resp.json()
@pytest.mark.asyncio
async def test_create_prompt(self, client: AsyncClient):
"""测试创建提示词模板"""
resp = await client.post("/api/v1/prompts", json=self.PROMPT_CREATE_DATA)
assert resp.status_code == 201
result = resp.json()
assert result["title"] == self.PROMPT_CREATE_DATA["title"]
assert result["template_type"] == "general"
assert result["system_prompt"] == self.PROMPT_CREATE_DATA["system_prompt"]
assert result["user_prompt"] == self.PROMPT_CREATE_DATA["user_prompt"]
assert result["version"] == 1
assert result["status"] == "published"
assert "id" in result
@pytest.mark.asyncio
async def test_create_prompt_no_title(self, client: AsyncClient):
"""测试创建模板时缺少标题应返回 422"""
data = self.PROMPT_CREATE_DATA.copy()
del data["title"]
resp = await client.post("/api/v1/prompts", json=data)
assert resp.status_code == 422
@pytest.mark.asyncio
async def test_create_prompt_invalid_type(self, client: AsyncClient):
"""测试创建模板时使用无效类型应返回 422"""
data = self.PROMPT_CREATE_DATA.copy()
data["template_type"] = "invalid_type"
resp = await client.post("/api/v1/prompts", json=data)
assert resp.status_code == 422
@pytest.mark.asyncio
async def test_list_prompts(self, client: AsyncClient):
"""测试查询模板列表"""
await self._create_prompt(client)
resp = await client.get("/api/v1/prompts")
assert resp.status_code == 200
result = resp.json()
assert result["total"] >= 1
assert len(result["items"]) >= 1
@pytest.mark.asyncio
async def test_list_prompts_filter_by_type(self, client: AsyncClient):
"""测试按类型筛选模板"""
await self._create_prompt(client)
resp = await client.get("/api/v1/prompts?template_type=general")
assert resp.status_code == 200
result = resp.json()
for item in result["items"]:
assert item["template_type"] == "general"
@pytest.mark.asyncio
async def test_list_prompts_search_by_title(self, client: AsyncClient):
"""测试按标题模糊搜索"""
await self._create_prompt(client)
resp = await client.get("/api/v1/prompts?title=质检")
assert resp.status_code == 200
result = resp.json()
assert result["total"] >= 1
@pytest.mark.asyncio
async def test_get_prompt(self, client: AsyncClient):
"""测试查询模板详情"""
created = await self._create_prompt(client)
prompt_id = created["id"]
resp = await client.get(f"/api/v1/prompts/{prompt_id}")
assert resp.status_code == 200
result = resp.json()
assert result["id"] == prompt_id
assert result["title"] == self.PROMPT_CREATE_DATA["title"]
@pytest.mark.asyncio
async def test_get_prompt_not_found(self, client: AsyncClient):
"""测试查询不存在的模板应返回 404"""
resp = await client.get("/api/v1/prompts/nonexistent-id")
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_update_prompt(self, client: AsyncClient):
"""测试更新模板"""
created = await self._create_prompt(client)
prompt_id = created["id"]
resp = await client.put(
f"/api/v1/prompts/{prompt_id}",
json=self.PROMPT_UPDATE_DATA,
)
assert resp.status_code == 200
result = resp.json()
assert result["title"] == self.PROMPT_UPDATE_DATA["title"]
assert result["system_prompt"] == self.PROMPT_UPDATE_DATA["system_prompt"]
# 版本号应增加
assert result["version"] == 2
@pytest.mark.asyncio
async def test_update_prompt_not_found(self, client: AsyncClient):
"""测试更新不存在的模板应返回 404"""
resp = await client.put("/api/v1/prompts/nonexistent-id", json=self.PROMPT_UPDATE_DATA)
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_prompt_pagination(self, client: AsyncClient):
"""测试模板列表分页"""
for i in range(3):
data = self.PROMPT_CREATE_DATA.copy()
data["title"] = f"提示词 {i}"
data["code"] = f"TEMPLATE-GEN-{i:03d}"
await client.post("/api/v1/prompts", json=data)
resp = await client.get("/api/v1/prompts?page=1&page_size=2")
assert resp.status_code == 200
result = resp.json()
assert result["total"] == 3
assert len(result["items"]) == 2
assert result["total_pages"] == 2
# === CHG-017: 软删除和版本历史 ===
@pytest.mark.asyncio
async def test_delete_prompt_soft(self, client: AsyncClient):
"""测试软删除模板"""
created = await self._create_prompt(client)
prompt_id = created["id"]
resp = await client.delete(f"/api/v1/prompts/{prompt_id}")
assert resp.status_code == 200
result = resp.json()
assert result["status"] == "deleted"
assert result["id"] == prompt_id
# 删除后查询详情应仍能查到
detail = await client.get(f"/api/v1/prompts/{prompt_id}")
assert detail.status_code == 200
assert detail.json()["status"] == "deleted"
# 列表默认不包含 deleted
lst = await client.get("/api/v1/prompts")
ids = [item["id"] for item in lst.json()["items"]]
assert prompt_id not in ids
@pytest.mark.asyncio
async def test_delete_prompt_not_found(self, client: AsyncClient):
"""测试删除不存在的模板应返回 404"""
resp = await client.delete("/api/v1/prompts/nonexistent-id")
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_delete_already_deleted(self, client: AsyncClient):
"""测试重复删除已软删除的模板应返回 400"""
created = await self._create_prompt(client)
prompt_id = created["id"]
await client.delete(f"/api/v1/prompts/{prompt_id}")
resp = await client.delete(f"/api/v1/prompts/{prompt_id}")
assert resp.status_code == 400
@pytest.mark.asyncio
async def test_update_prompt_creates_version_snapshot(self, client: AsyncClient):
"""测试更新模板时自动创建版本快照"""
created = await self._create_prompt(client)
prompt_id = created["id"]
assert created["version"] == 1
# 第一次更新
resp1 = await client.put(
f"/api/v1/prompts/{prompt_id}",
json={"title": "v2 标题", "system_prompt": "v2 system prompt"},
)
assert resp1.status_code == 200
assert resp1.json()["version"] == 2
# 第二次更新
resp2 = await client.put(
f"/api/v1/prompts/{prompt_id}",
json={"title": "v3 标题"},
)
assert resp2.status_code == 200
assert resp2.json()["version"] == 3
# 查询详情应包含历史版本记录
detail = await client.get(f"/api/v1/prompts/{prompt_id}")
assert detail.status_code == 200
detail_data = detail.json()
assert "versions" in detail_data
assert len(detail_data["versions"]) == 2 # v1 和 v2 的快照
# v1 版本快照
v1 = detail_data["versions"][1] # 倒序,version 1 在最后
assert v1["version"] == 1
assert v1["system_prompt"] == self.PROMPT_CREATE_DATA["system_prompt"]
# v2 版本快照
v2 = detail_data["versions"][0] # 倒序,version 2 在最前
assert v2["version"] == 2
assert v2["system_prompt"] == "v2 system prompt"
@pytest.mark.asyncio
async def test_list_prompts_excludes_deleted_by_default(self, client: AsyncClient):
"""测试列表默认不包含已删除的模板"""
# 创建 2 个模板
created1 = await self._create_prompt(client)
data2 = self.PROMPT_CREATE_DATA.copy()
data2["code"] = "TEMPLATE-GEN-002"
data2["title"] = "第二个模板"
resp2 = await client.post("/api/v1/prompts", json=data2)
created2 = resp2.json()
# 删除第一个
await client.delete(f"/api/v1/prompts/{created1['id']}")
# 列表应只包含第二个
lst = await client.get("/api/v1/prompts")
items = lst.json()["items"]
ids = [item["id"] for item in items]
assert created1["id"] not in ids
assert created2["id"] in ids
# 指定 status=deleted 可查到
lst = await client.get("/api/v1/prompts?status=deleted")
items = lst.json()["items"]
ids = [item["id"] for item in items]
assert created1["id"] in ids