"""对象库、原始数据索引、血缘与 data_read 的集成测试。""" from __future__ import annotations import json import pytest from toolsvc.ledger import ledger from toolsvc.objects import ObjectStore, build_predicate from toolsvc.readers import ReadError, read_rows @pytest.fixture(scope="module") def isolated_store(tmp_path_factory): """独立数据目录的 ObjectStore(不影响默认 store 的数据)。""" directory = tmp_path_factory.mktemp("objects") return ObjectStore(directory) @pytest.fixture(scope="module") def workspace(isolated_store): return isolated_store.create_workspace("测试工作区", "objects 测试") @pytest.fixture(scope="module") def raw_object(isolated_store, workspace, sample_csv): payload = sample_csv.read_bytes() return isolated_store.register_upload(workspace["id"], "small.csv", payload) def test_workspace_crud(isolated_store, workspace): assert workspace["name"] == "测试工作区" items = isolated_store.list_workspaces() assert any(item["id"] == workspace["id"] for item in items) def test_upload_indexes_rows_and_columns(raw_object): assert raw_object["kind"] == "raw" assert raw_object["sha256"].startswith("sha256:") assert raw_object["rows"] > 1000 assert "HS_CODE" in raw_object["columns"] assert raw_object["tables"][0]["name"] == "raw" def test_data_read_returns_subset_with_trace(raw_object): result = read_rows(raw_object["id"], table="raw", limit=5) assert result["total"] == raw_object["rows"] assert len(result["rows"]) == 5 assert result["exhausted"] is False trace = result["rows"][0]["_trace"] assert trace["object_id"] == raw_object["id"] assert trace["row_index"] == 0 assert trace["row_hash"].startswith("sha256:") def test_data_read_filters_and_sorts(raw_object): result = read_rows( raw_object["id"], filters=[{"column": "I_E_FLAG", "op": "=", "value": "I"}], order_by=[{"column": "MONORSUM", "dir": "desc"}], limit=3, ) assert len(result["rows"]) == 3 amounts = [float(row["values"]["MONORSUM"]) for row in result["rows"]] assert amounts == sorted(amounts, reverse=True) def test_data_read_contains_filter(raw_object): result = read_rows(raw_object["id"], filters=[{"column": "G_MODEL", "op": "contains", "value": "MURATA"}], limit=2, count_mode="exact") assert result["total"] >= 1 for row in result["rows"]: assert "MURATA" in row["values"]["G_MODEL"] def test_data_read_aggregate_mode(raw_object): result = read_rows( raw_object["id"], aggregate={ "group_by": ["HS_CODE"], "metrics": [{"column": "MONORSUM", "fn": "sum", "alias": "amount"}, {"column": "COUNT", "fn": "sum", "alias": "quantity"}], }, ) assert result["aggregate"] is True assert result["rows"] assert set(result["columns"]) == {"HS_CODE", "amount", "quantity"} from decimal import Decimal amounts = [Decimal(row["values"]["amount"]) for row in result["rows"]] assert amounts == sorted(amounts, reverse=True) def test_data_read_rejects_unknown_column(raw_object): with pytest.raises(ReadError): read_rows(raw_object["id"], filters=[{"column": "不存在", "op": "=", "value": 1}]) with pytest.raises(ReadError): read_rows(raw_object["id"], order_by=[{"column": "不存在"}]) def test_limit_is_capped(raw_object): result = read_rows(raw_object["id"], limit=10_000) assert result["limit"] <= 200 def test_preview_and_lineage(raw_object, isolated_store): preview = isolated_store.preview(raw_object["id"], "raw", limit=3) assert len(preview["rows"]) == 3 lineage = isolated_store.row_lineage(raw_object["id"], "raw", -1) assert lineage and lineage["source_object_id"] == raw_object["id"] def test_build_predicate_from_result_row(): predicate = build_predicate( { "一级类目": "电容器", "二级类目": "多层瓷介电容器(MLCC)", "应用领域标签": "先进计算", "品牌国别": "日本", "品牌": "MURATA", "前十大型号": "GRM100K", "年度": "2025", "月度": "3", "_row_index": 4, "_row_hash": "sha256:x", } ) assert predicate["c1"] == "电容器" assert predicate["app"] == "先进计算" assert predicate["brand"] == "MURATA" assert predicate["year"] == "2025" assert predicate["month"] == "2025-03" def test_delete_object_removes_files(isolated_store, workspace, sample_csv): item = isolated_store.register_upload(workspace["id"], "tmp.csv", sample_csv.read_bytes()) from pathlib import Path path = Path(item["path"]) assert path.exists() isolated_store.delete_object(item["id"]) assert not path.exists() with pytest.raises(KeyError): isolated_store.get_object(item["id"]) def test_ledger_records_and_finds_values(): from toolsvc.objects import new_id call_id = new_id("call") rows = [ {"values": {"金额": "123.45", "品牌": "MURATA"}, "_trace": {"object_id": "obj_x", "table": "t", "row_index": 7, "row_hash": "sha256:abc"}} ] ledger.record(call_id, "data_read", {"q": 1}, {"rows": rows}, session_id="s-obj", rows=rows) hits = ledger.find_value("123.45", "s-obj") assert hits and hits[0]["call_id"] == call_id assert hits[0]["row_index"] == 7 assert ledger.find_value("MURATA", "s-obj") assert ledger.session_calls("s-obj") def test_ledger_hash_chain_is_consistent(): assert ledger.chain_ok(limit=200) is True