dingdanquanliucheng/backend/tests/test_ai_extended.py

62 lines
2.3 KiB
Python
Raw Normal View History

2026-06-19 23:03:14 +08:00
"""AI 识别扩展测试。
测试用例:
- AI-002: 识别失败处理
- AI-004: 确认识别结果入库
- AI-006: 文本解析订单
- AI-007c: 业务员可调用文本解析
"""
from __future__ import annotations
import pytest
@pytest.mark.ai
@pytest.mark.p1
class TestAIRecognitionExtended:
"""AI 识别扩展测试。"""
def test_ai_recognition_invalid_image(self, client, admin_headers):
"""AI-002: 无效图片 URL 识别应返回错误或降级处理。"""
resp = client.post("/api/ai/recognize", headers=admin_headers, json={
"image_url": "https://invalid.example.com/not_exist.jpg",
})
# 应返回 200(降级) / 400 / 422 / 500
assert resp.status_code in (200, 400, 422, 500)
def test_parse_order_from_text(self, client, salesman_headers):
"""AI-006: 文本解析订单。"""
resp = client.post("/api/ai/parse-order", headers=salesman_headers, json={
"text": "张三 13800001234 需要100张A4纸 200g",
})
assert resp.status_code in (200, 404, 422, 500)
def test_parse_order_from_empty_text(self, client, salesman_headers):
"""AI-006c: 空文本解析应返回错误。"""
resp = client.post("/api/ai/parse-order", headers=salesman_headers, json={
"text": "",
})
assert resp.status_code in (400, 422, 200)
def test_salesman_can_parse_order(self, client, salesman_headers):
"""AI-007c: 业务员可以调用文本解析。"""
resp = client.post("/api/ai/parse-order", headers=salesman_headers, json={
"text": "测试订单文本",
})
# 不应返回 403
assert resp.status_code != 403
def test_manager_can_recognize(self, client, manager_headers):
"""经理可以调用 AI 识别。"""
resp = client.post("/api/ai/recognize", headers=manager_headers, json={
"image_url": "https://example.com/test.jpg",
})
assert resp.status_code in (200, 400, 422, 500)
def test_salesman_cannot_recognize(self, client, salesman_headers):
"""业务员不能调用 AI 识别(只能用 parse-order"""
resp = client.post("/api/ai/recognize", headers=salesman_headers, json={
"image_url": "https://example.com/test.jpg",
})
assert resp.status_code == 403