90 lines
3.5 KiB
Python
90 lines
3.5 KiB
Python
"""
|
||
tests/test_shipping_notice_openapi_contract.py
|
||
|
||
用途:锁定发货通知动态订单字段、证据观察值与库存连接器 OpenAPI 命名契约。
|
||
依赖:Flask 测试 client、静态 Dify OpenAPI JSON。
|
||
逻辑:分别校验动态 builder 输出与脚本生成的静态快照,防止两者发生漂移。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
from pathlib import Path
|
||
from typing import Any
|
||
|
||
|
||
def _assert_connector_contract(spec: dict[str, Any]) -> None:
|
||
"""
|
||
校验订单行与库存连接器的字段边界。
|
||
|
||
核心逻辑:订单行只接受动态 fields 数组,每个字段携带稳定 field_id 和 observations;
|
||
任务状态说明使用 inventory_connector_type,并拒绝旧 inventory_source_type 命名。
|
||
|
||
Args:
|
||
spec: 动态接口或静态快照读取出的 OpenAPI 对象。
|
||
|
||
Returns:
|
||
None;契约不符时由 pytest assertion 报错。
|
||
"""
|
||
order_schema = spec["paths"]["/tools/submit_order_extraction"]["post"]["requestBody"]["content"][
|
||
"application/json"
|
||
]["schema"]["properties"]["orders"]["items"]
|
||
line_properties = order_schema["properties"]["lines"]["items"]["properties"]
|
||
assert order_schema["properties"]["lines"]["items"]["additionalProperties"] is False
|
||
assert set(line_properties) == {"fields"}
|
||
assert line_properties["fields"]["type"] == "array"
|
||
field_item = line_properties["fields"]["items"]
|
||
assert {"field_id", "observations"} <= set(field_item["properties"])
|
||
observation = field_item["properties"]["observations"]["items"]
|
||
assert {"value", "document_id", "quote", "confidence"} <= set(observation["properties"])
|
||
assert not {"sku", "vendor_part_number", "source_type", "hub_code"} & set(line_properties)
|
||
|
||
request_schema = spec["paths"]["/tools/submit_order_extraction"]["post"]["requestBody"]["content"][
|
||
"application/json"
|
||
]["schema"]
|
||
assert "schema_version" not in request_schema["properties"]
|
||
assert request_schema["required"] == ["agent_runtime_context", "input_snapshot_hash", "orders"]
|
||
|
||
status_description = spec["paths"]["/tools/get_task_status"]["post"]["description"]
|
||
assert "inventory_connector_type" in status_description
|
||
assert "inventory_source_type" not in status_description
|
||
|
||
confirm_description = spec["paths"]["/tools/confirm_inventory_source"]["post"]["description"]
|
||
assert "connector_type" in confirm_description
|
||
assert "source_code、source_type" not in confirm_description
|
||
|
||
|
||
def test_dynamic_openapi_uses_connector_contract(app, client) -> None:
|
||
"""
|
||
校验公开 OpenAPI 接口使用新的连接器命名。
|
||
|
||
核心逻辑:通过 Flask 路由获取 builder 实时结果,再复用统一契约断言。
|
||
|
||
Args:
|
||
app: pytest Flask 应用 fixture。
|
||
client: pytest Flask 测试客户端 fixture。
|
||
|
||
Returns:
|
||
None。
|
||
"""
|
||
app.config["HXOPS_TOOLS_API_BASE"] = "http://tool-api.example.com"
|
||
response = client.get("/api/shipping-notice/tools/openapi.json")
|
||
|
||
assert response.status_code == 200
|
||
_assert_connector_contract(response.get_json())
|
||
|
||
|
||
def test_static_openapi_uses_connector_contract() -> None:
|
||
"""
|
||
校验仓库脚本生成的 Dify 静态快照与动态契约一致。
|
||
|
||
核心逻辑:读取 docs/openapi 下的 JSON,并复用统一契约断言。
|
||
|
||
Returns:
|
||
None。
|
||
"""
|
||
spec_path = Path(__file__).resolve().parents[2] / "docs" / "openapi" / "shipping_notice_tools.dify.json"
|
||
spec = json.loads(spec_path.read_text(encoding="utf-8"))
|
||
|
||
_assert_connector_contract(spec)
|