"""HTTP 接口测试(FastAPI TestClient):上传 → 处理 → 查询 → 台账。""" from __future__ import annotations import time import pytest from fastapi.testclient import TestClient from toolsvc.api import app @pytest.fixture(scope="module") def client(): with TestClient(app) as test_client: yield test_client @pytest.fixture(scope="module") def workspace(client): response = client.post("/workspaces", json={"name": "API 测试工作区", "description": "接口测试"}) assert response.status_code == 201, response.text return response.json() @pytest.fixture(scope="module") def raw_object(client, workspace, sample_csv): with sample_csv.open("rb") as handle: response = client.post( f"/workspaces/{workspace['id']}/objects", files={"file": ("small.csv", handle, "text/csv")}, ) assert response.status_code == 201, response.text return response.json() def test_health_and_meta(client): health = client.get("/health").json() assert health["ok"] is True and health["service"] == "tools" meta = client.get("/meta").json() assert meta["tools"] == ["customs_process", "data_read", "calculator"] assert "full_pipeline" in meta["operations"] assert "ledger" in meta def test_workspace_lifecycle(client): created = client.post("/workspaces", json={"name": "临时工作区"}).json() assert client.get("/workspaces").status_code == 200 assert any(item["id"] == created["id"] for item in client.get("/workspaces").json()["items"]) assert client.delete(f"/workspaces/{created['id']}").json()["ok"] is True assert client.get(f"/objects/obj_not_exist").status_code == 404 def test_upload_and_preview(client, raw_object): assert raw_object["rows"] > 1000 preview = client.get(f"/objects/{raw_object['id']}/preview", params={"table": "raw", "limit": 3}).json() assert len(preview["rows"]) == 3 assert "HS_CODE" in preview["columns"] def test_data_read_tool(client, raw_object): response = client.post( "/tools/data_read", json={"object_id": raw_object["id"], "table": "raw", "limit": 2, "order_by": [{"column": "MONORSUM", "dir": "desc"}]}, ) assert response.status_code == 200, response.text payload = response.json() assert len(payload["rows"]) == 2 assert payload["rows"][0]["_trace"]["row_hash"].startswith("sha256:") def test_data_read_rejects_bad_column(client, raw_object): response = client.post("/tools/data_read", json={"object_id": raw_object["id"], "filters": [{"column": "nope", "op": "=", "value": 1}]}) assert response.status_code == 400 def test_calculator_tool(client): response = client.post("/tools/calculator", json={"op": "sum", "inputs": [{"value": 1}, {"value": 2}], "session_id": "api-test"}) assert response.status_code == 200 payload = response.json() assert payload["result"] == "3" assert payload["_trace"]["calculation_id"] assert client.post("/tools/calculator", json={"op": "nope", "inputs": [{"value": 1}]}).status_code == 400 def test_customs_process_sync_tool(client, workspace, raw_object): response = client.post( "/tools/customs_process", json={ "workspace_id": workspace["id"], "input_object_id": raw_object["id"], "operation": "full_pipeline", "params": {"start_year": 2025, "end_year": 2026}, "output_filename": "接口测试统计结果.xlsx", }, ) assert response.status_code == 200, response.text payload = response.json() assert payload["object_id"].startswith("obj_") assert len(payload["sheets"]) == 25 assert payload["lineage"]["output_hash"].startswith("sha256:") assert "统计结果.xlsx" in " ".join(payload["files"]) stat_object = client.get(f"/objects/{payload['object_id']}").json() assert stat_object["kind"] == "stat" assert stat_object["parents"] == [raw_object["id"]] # 结果对象可以被 data_read 读取 table = stat_object["tables"][0]["name"] read = client.post("/tools/data_read", json={"object_id": payload["object_id"], "table": table, "limit": 2}) assert read.status_code == 200 assert read.json()["rows"] def test_run_async_and_progress(client, workspace, raw_object): created = client.post( "/runs", json={ "workspace_id": workspace["id"], "input_object_id": raw_object["id"], "operation": "custom_aggregate", "options": {"group_by": ["c1", "c2"], "filters": {"direction": "I"}}, "output_name": "自定义聚合.xlsx", }, ) assert created.status_code == 201, created.text run = created.json() deadline = time.time() + 300 while time.time() < deadline: run = client.get(f"/runs/{run['id']}").json() if run["status"] in ("succeeded", "failed"): break time.sleep(0.5) assert run["status"] == "succeeded", run assert run["progress"] == 1.0 assert run["object_id"] assert client.get("/runs").json()["items"] def test_run_rejects_bad_operation(client, workspace, raw_object): response = client.post( "/runs", json={"workspace_id": workspace["id"], "input_object_id": raw_object["id"], "operation": "nope"}, ) assert response.status_code == 400 def test_run_failure_is_reported(client, workspace, tmp_path): """上传不校验业务字段,但处理阶段必须给出可读的失败原因。""" bad = tmp_path / "bad.csv" bad.write_text("I_E_FLAG,HS_CODE\nI,8532241000\n", encoding="utf-8") with bad.open("rb") as handle: upload = client.post(f"/workspaces/{workspace['id']}/objects", files={"file": ("bad.csv", handle, "text/csv")}) assert upload.status_code == 201 object_id = upload.json()["id"] response = client.post( "/tools/customs_process", json={"workspace_id": workspace["id"], "input_object_id": object_id}, ) assert response.status_code == 422 detail = response.json()["detail"] assert "缺少必需字段" in detail or "INGEST_FAILED" in detail def test_lineage_endpoint(client, workspace, raw_object): response = client.post( "/tools/customs_process", json={"workspace_id": workspace["id"], "input_object_id": raw_object["id"], "params": {"start_year": 2025, "end_year": 2025}}, ) object_id = response.json()["object_id"] table = client.get(f"/objects/{object_id}").json()["tables"][0]["name"] lineage = client.get(f"/objects/{object_id}/lineage", params={"table": table, "row": 0}) assert lineage.status_code == 200, lineage.text payload = lineage.json() assert payload["source_object_id"] == raw_object["id"] assert payload["row_hash"].startswith("sha256:") def test_file_download(client, workspace, raw_object): response = client.post("/tools/customs_process", json={"workspace_id": workspace["id"], "input_object_id": raw_object["id"]}) object_id = response.json()["object_id"] download = client.get(f"/objects/{object_id}/file", params={"name": "统计结果.xlsx"}) assert download.status_code == 200 assert download.content[:2] == b"PK" assert client.get(f"/objects/{object_id}/file", params={"name": "../secret.txt"}).status_code in (400, 404) def test_ledger_endpoints(client): stats = client.get("/ledger/stats").json() assert stats["calls"] > 0 assert stats["chain_ok"] is True calls = client.get("/ledger/calls", params={"session_id": "api-test"}).json()["items"] assert calls value = client.get("/ledger/value", params={"value": "3", "session_id": "api-test"}).json()["items"] assert value detail = client.get(f"/ledger/call/{calls[0]['call_id']}") assert detail.status_code == 200 assert client.get("/ledger/export").json()["items"] def test_data_read_records_data_plane_ledger(client, raw_object): """BFF 传入 call_id 时,data_read 必须把调用与行值写进数据面台账。""" call_id = "call_test_data_read_1" response = client.post( "/tools/data_read", json={ "object_id": raw_object["id"], "table": "raw", "limit": 2, "call_id": call_id, "session_id": "api-ledger", }, ) assert response.status_code == 200, response.text payload = response.json() assert payload["rows"], "查询应返回行" amount = payload["rows"][0]["values"].get("MONORSUM") detail = client.get(f"/ledger/call/{call_id}") assert detail.status_code == 200, detail.text entry = detail.json() assert entry["tool"] == "data_read" assert entry["object_ids"] == [raw_object["id"]] hit = client.get("/ledger/value", params={"value": str(amount), "session_id": "api-ledger"}) assert hit.status_code == 200 assert any(item["call_id"] == call_id for item in hit.json()["items"]), "返回值应可反查" def test_customs_process_records_data_plane_ledger(client, workspace, raw_object): """customs_process 也按 call_id 记账(对象 + 血缘摘要),便于审计。""" call_id = "call_test_process_1" response = client.post( "/tools/customs_process", json={ "workspace_id": workspace["id"], "input_object_id": raw_object["id"], "params": {"start_year": 2025, "end_year": 2025}, "call_id": call_id, "session_id": "api-ledger", }, ) assert response.status_code == 200, response.text output_object = response.json()["object_id"] entry = client.get(f"/ledger/call/{call_id}").json() assert entry["tool"] == "customs_process" assert raw_object["id"] in entry["object_ids"] assert output_object in entry["object_ids"] assert entry["result"]["sheet_count"] == 25 def test_preview_supports_offset(client, raw_object): first = client.get(f"/objects/{raw_object['id']}/preview", params={"table": "raw", "limit": 3}).json() second = client.get(f"/objects/{raw_object['id']}/preview", params={"table": "raw", "limit": 3, "offset": 2}).json() assert first["row_indexes"] == [0, 1, 2] assert second["offset"] == 2 assert second["row_indexes"] == [2, 3, 4] assert first["rows"][2] == second["rows"][0] def test_row_endpoint_returns_text_and_lineage(client, workspace, raw_object): """溯源 chip 点开时按行号取原文:值、行哈希、下钻谓词都要有。""" # 先处理出统计结果,stat 行才有血缘谓词 processed = client.post( "/tools/customs_process", json={"workspace_id": workspace["id"], "input_object_id": raw_object["id"], "params": {"start_year": 2025, "end_year": 2026}}, ).json() stat_id = processed["object_id"] response = client.get(f"/objects/{stat_id}/row", params={"table": "1.0-3.0_MLCC", "row": 0}) assert response.status_code == 200, response.text payload = response.json() assert payload["row_index"] == 0 assert payload["row_hash"].startswith("sha256:") assert payload["values"]["基础类目"] assert payload["lineage"]["source_object_id"] == raw_object["id"] assert payload["lineage"]["source_rows"], "统计行应带下钻谓词" missing = client.get(f"/objects/{stat_id}/row", params={"table": "1.0-3.0_MLCC", "row": 999999}) assert missing.status_code == 404 def test_sheet_meta_endpoint(client, workspace, raw_object): processed = client.post( "/tools/customs_process", json={"workspace_id": workspace["id"], "input_object_id": raw_object["id"]}, ).json() meta = client.get(f"/objects/{processed['object_id']}/sheet_meta").json() assert len(meta["tables"]) == 25 assert meta["lineage"]["output_hash"].startswith("sha256:") assert meta["parents"] == [raw_object["id"]] def test_archive_packages_all_result_files(client, workspace, raw_object): """结果文件打包下载:Excel + 逐表 CSV + 明细,一次拿走。""" processed = client.post( "/tools/customs_process", json={"workspace_id": workspace["id"], "input_object_id": raw_object["id"]}, ).json() object_id = processed["object_id"] response = client.get(f"/objects/{object_id}/archive", params={"include": "all"}) assert response.status_code == 200, response.text assert response.headers["content-type"] == "application/zip" import io, zipfile archive = zipfile.ZipFile(io.BytesIO(response.content)) names = archive.namelist() assert "统计结果.xlsx" in names assert any(name.startswith("精确结果CSV/") for name in names), names[:10] assert any(name.endswith("issues.csv") for name in names) assert not any("working.sqlite" in name for name in names), "工作库不应打进结果包" only_csv = client.get(f"/objects/{object_id}/archive", params={"include": "csv"}) csv_names = zipfile.ZipFile(io.BytesIO(only_csv.content)).namelist() assert all(name.endswith((".csv", ".json")) for name in csv_names), csv_names[:5] def test_create_workspace_returns_object_count(client): created = client.post("/workspaces", json={"name": "计数测试"}).json() assert created["object_count"] == 0 assert client.get("/workspaces").json()["items"][0]["object_count"] >= 0