test_api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. """HTTP 接口测试(FastAPI TestClient):上传 → 处理 → 查询 → 台账。"""
  2. from __future__ import annotations
  3. import time
  4. import pytest
  5. from fastapi.testclient import TestClient
  6. from toolsvc.api import app
  7. @pytest.fixture(scope="module")
  8. def client():
  9. with TestClient(app) as test_client:
  10. yield test_client
  11. @pytest.fixture(scope="module")
  12. def workspace(client):
  13. response = client.post("/workspaces", json={"name": "API 测试工作区", "description": "接口测试"})
  14. assert response.status_code == 201, response.text
  15. return response.json()
  16. @pytest.fixture(scope="module")
  17. def raw_object(client, workspace, sample_csv):
  18. with sample_csv.open("rb") as handle:
  19. response = client.post(
  20. f"/workspaces/{workspace['id']}/objects",
  21. files={"file": ("small.csv", handle, "text/csv")},
  22. )
  23. assert response.status_code == 201, response.text
  24. return response.json()
  25. def test_health_and_meta(client):
  26. health = client.get("/health").json()
  27. assert health["ok"] is True and health["service"] == "tools"
  28. meta = client.get("/meta").json()
  29. assert meta["tools"] == ["customs_process", "data_read", "calculator"]
  30. assert "full_pipeline" in meta["operations"]
  31. assert "ledger" in meta
  32. def test_workspace_lifecycle(client):
  33. created = client.post("/workspaces", json={"name": "临时工作区"}).json()
  34. assert client.get("/workspaces").status_code == 200
  35. assert any(item["id"] == created["id"] for item in client.get("/workspaces").json()["items"])
  36. assert client.delete(f"/workspaces/{created['id']}").json()["ok"] is True
  37. assert client.get(f"/objects/obj_not_exist").status_code == 404
  38. def test_upload_and_preview(client, raw_object):
  39. assert raw_object["rows"] > 1000
  40. preview = client.get(f"/objects/{raw_object['id']}/preview", params={"table": "raw", "limit": 3}).json()
  41. assert len(preview["rows"]) == 3
  42. assert "HS_CODE" in preview["columns"]
  43. def test_data_read_tool(client, raw_object):
  44. response = client.post(
  45. "/tools/data_read",
  46. json={"object_id": raw_object["id"], "table": "raw", "limit": 2, "order_by": [{"column": "MONORSUM", "dir": "desc"}]},
  47. )
  48. assert response.status_code == 200, response.text
  49. payload = response.json()
  50. assert len(payload["rows"]) == 2
  51. assert payload["rows"][0]["_trace"]["row_hash"].startswith("sha256:")
  52. def test_data_read_rejects_bad_column(client, raw_object):
  53. response = client.post("/tools/data_read", json={"object_id": raw_object["id"], "filters": [{"column": "nope", "op": "=", "value": 1}]})
  54. assert response.status_code == 400
  55. def test_calculator_tool(client):
  56. response = client.post("/tools/calculator", json={"op": "sum", "inputs": [{"value": 1}, {"value": 2}], "session_id": "api-test"})
  57. assert response.status_code == 200
  58. payload = response.json()
  59. assert payload["result"] == "3"
  60. assert payload["_trace"]["calculation_id"]
  61. assert client.post("/tools/calculator", json={"op": "nope", "inputs": [{"value": 1}]}).status_code == 400
  62. def test_customs_process_sync_tool(client, workspace, raw_object):
  63. response = client.post(
  64. "/tools/customs_process",
  65. json={
  66. "workspace_id": workspace["id"],
  67. "input_object_id": raw_object["id"],
  68. "operation": "full_pipeline",
  69. "params": {"start_year": 2025, "end_year": 2026},
  70. "output_filename": "接口测试统计结果.xlsx",
  71. },
  72. )
  73. assert response.status_code == 200, response.text
  74. payload = response.json()
  75. assert payload["object_id"].startswith("obj_")
  76. assert len(payload["sheets"]) == 23
  77. assert payload["lineage"]["output_hash"].startswith("sha256:")
  78. assert "统计结果.xlsx" in " ".join(payload["files"])
  79. stat_object = client.get(f"/objects/{payload['object_id']}").json()
  80. assert stat_object["kind"] == "stat"
  81. assert stat_object["parents"] == [raw_object["id"]]
  82. # 结果对象可以被 data_read 读取
  83. table = stat_object["tables"][0]["name"]
  84. read = client.post("/tools/data_read", json={"object_id": payload["object_id"], "table": table, "limit": 2})
  85. assert read.status_code == 200
  86. assert read.json()["rows"]
  87. def test_run_async_and_progress(client, workspace, raw_object):
  88. created = client.post(
  89. "/runs",
  90. json={
  91. "workspace_id": workspace["id"],
  92. "input_object_id": raw_object["id"],
  93. "operation": "custom_aggregate",
  94. "options": {"group_by": ["c1", "c2"], "filters": {"direction": "I"}},
  95. "output_name": "自定义聚合.xlsx",
  96. },
  97. )
  98. assert created.status_code == 201, created.text
  99. run = created.json()
  100. deadline = time.time() + 300
  101. while time.time() < deadline:
  102. run = client.get(f"/runs/{run['id']}").json()
  103. if run["status"] in ("succeeded", "failed"):
  104. break
  105. time.sleep(0.5)
  106. assert run["status"] == "succeeded", run
  107. assert run["progress"] == 1.0
  108. assert run["object_id"]
  109. assert client.get("/runs").json()["items"]
  110. def test_run_rejects_bad_operation(client, workspace, raw_object):
  111. response = client.post(
  112. "/runs",
  113. json={"workspace_id": workspace["id"], "input_object_id": raw_object["id"], "operation": "nope"},
  114. )
  115. assert response.status_code == 400
  116. def test_run_failure_is_reported(client, workspace, tmp_path):
  117. """上传不校验业务字段,但处理阶段必须给出可读的失败原因。"""
  118. bad = tmp_path / "bad.csv"
  119. bad.write_text("I_E_FLAG,HS_CODE\nI,8532241000\n", encoding="utf-8")
  120. with bad.open("rb") as handle:
  121. upload = client.post(f"/workspaces/{workspace['id']}/objects", files={"file": ("bad.csv", handle, "text/csv")})
  122. assert upload.status_code == 201
  123. object_id = upload.json()["id"]
  124. response = client.post(
  125. "/tools/customs_process",
  126. json={"workspace_id": workspace["id"], "input_object_id": object_id},
  127. )
  128. assert response.status_code == 422
  129. detail = response.json()["detail"]
  130. assert "缺少必需字段" in detail or "INGEST_FAILED" in detail
  131. def test_lineage_endpoint(client, workspace, raw_object):
  132. response = client.post(
  133. "/tools/customs_process",
  134. json={"workspace_id": workspace["id"], "input_object_id": raw_object["id"], "params": {"start_year": 2025, "end_year": 2025}},
  135. )
  136. object_id = response.json()["object_id"]
  137. table = client.get(f"/objects/{object_id}").json()["tables"][0]["name"]
  138. lineage = client.get(f"/objects/{object_id}/lineage", params={"table": table, "row": 0})
  139. assert lineage.status_code == 200, lineage.text
  140. payload = lineage.json()
  141. assert payload["source_object_id"] == raw_object["id"]
  142. assert payload["row_hash"].startswith("sha256:")
  143. def test_file_download(client, workspace, raw_object):
  144. response = client.post("/tools/customs_process", json={"workspace_id": workspace["id"], "input_object_id": raw_object["id"]})
  145. object_id = response.json()["object_id"]
  146. download = client.get(f"/objects/{object_id}/file", params={"name": "统计结果.xlsx"})
  147. assert download.status_code == 200
  148. assert download.content[:2] == b"PK"
  149. assert client.get(f"/objects/{object_id}/file", params={"name": "../secret.txt"}).status_code in (400, 404)
  150. def test_ledger_endpoints(client):
  151. stats = client.get("/ledger/stats").json()
  152. assert stats["calls"] > 0
  153. assert stats["chain_ok"] is True
  154. calls = client.get("/ledger/calls", params={"session_id": "api-test"}).json()["items"]
  155. assert calls
  156. value = client.get("/ledger/value", params={"value": "3", "session_id": "api-test"}).json()["items"]
  157. assert value
  158. detail = client.get(f"/ledger/call/{calls[0]['call_id']}")
  159. assert detail.status_code == 200
  160. assert client.get("/ledger/export").json()["items"]
  161. def test_data_read_records_data_plane_ledger(client, raw_object):
  162. """BFF 传入 call_id 时,data_read 必须把调用与行值写进数据面台账。"""
  163. call_id = "call_test_data_read_1"
  164. response = client.post(
  165. "/tools/data_read",
  166. json={
  167. "object_id": raw_object["id"],
  168. "table": "raw",
  169. "limit": 2,
  170. "call_id": call_id,
  171. "session_id": "api-ledger",
  172. },
  173. )
  174. assert response.status_code == 200, response.text
  175. payload = response.json()
  176. assert payload["rows"], "查询应返回行"
  177. amount = payload["rows"][0]["values"].get("MONORSUM")
  178. detail = client.get(f"/ledger/call/{call_id}")
  179. assert detail.status_code == 200, detail.text
  180. entry = detail.json()
  181. assert entry["tool"] == "data_read"
  182. assert entry["object_ids"] == [raw_object["id"]]
  183. hit = client.get("/ledger/value", params={"value": str(amount), "session_id": "api-ledger"})
  184. assert hit.status_code == 200
  185. assert any(item["call_id"] == call_id for item in hit.json()["items"]), "返回值应可反查"
  186. def test_customs_process_records_data_plane_ledger(client, workspace, raw_object):
  187. """customs_process 也按 call_id 记账(对象 + 血缘摘要),便于审计。"""
  188. call_id = "call_test_process_1"
  189. response = client.post(
  190. "/tools/customs_process",
  191. json={
  192. "workspace_id": workspace["id"],
  193. "input_object_id": raw_object["id"],
  194. "params": {"start_year": 2025, "end_year": 2025},
  195. "call_id": call_id,
  196. "session_id": "api-ledger",
  197. },
  198. )
  199. assert response.status_code == 200, response.text
  200. output_object = response.json()["object_id"]
  201. entry = client.get(f"/ledger/call/{call_id}").json()
  202. assert entry["tool"] == "customs_process"
  203. assert raw_object["id"] in entry["object_ids"]
  204. assert output_object in entry["object_ids"]
  205. assert entry["result"]["sheet_count"] == 23
  206. def test_preview_supports_offset(client, raw_object):
  207. first = client.get(f"/objects/{raw_object['id']}/preview", params={"table": "raw", "limit": 3}).json()
  208. second = client.get(f"/objects/{raw_object['id']}/preview", params={"table": "raw", "limit": 3, "offset": 2}).json()
  209. assert first["row_indexes"] == [0, 1, 2]
  210. assert second["offset"] == 2
  211. assert second["row_indexes"] == [2, 3, 4]
  212. assert first["rows"][2] == second["rows"][0]
  213. def test_row_endpoint_returns_text_and_lineage(client, workspace, raw_object):
  214. """溯源 chip 点开时按行号取原文:值、行哈希、下钻谓词都要有。"""
  215. # 先处理出统计结果,stat 行才有血缘谓词
  216. processed = client.post(
  217. "/tools/customs_process",
  218. json={"workspace_id": workspace["id"], "input_object_id": raw_object["id"], "params": {"start_year": 2025, "end_year": 2026}},
  219. ).json()
  220. stat_id = processed["object_id"]
  221. response = client.get(f"/objects/{stat_id}/row", params={"table": "1.0-3.0_MLCC", "row": 0})
  222. assert response.status_code == 200, response.text
  223. payload = response.json()
  224. assert payload["row_index"] == 0
  225. assert payload["row_hash"].startswith("sha256:")
  226. assert payload["values"]["基础类目"]
  227. assert payload["lineage"]["source_object_id"] == raw_object["id"]
  228. assert payload["lineage"]["source_rows"], "统计行应带下钻谓词"
  229. missing = client.get(f"/objects/{stat_id}/row", params={"table": "1.0-3.0_MLCC", "row": 999999})
  230. assert missing.status_code == 404
  231. def test_sheet_meta_endpoint(client, workspace, raw_object):
  232. processed = client.post(
  233. "/tools/customs_process",
  234. json={"workspace_id": workspace["id"], "input_object_id": raw_object["id"]},
  235. ).json()
  236. meta = client.get(f"/objects/{processed['object_id']}/sheet_meta").json()
  237. assert len(meta["tables"]) == 23
  238. assert meta["lineage"]["output_hash"].startswith("sha256:")
  239. assert meta["parents"] == [raw_object["id"]]