test_objects.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """对象库、原始数据索引、血缘与 data_read 的集成测试。"""
  2. from __future__ import annotations
  3. import json
  4. import pytest
  5. from toolsvc.ledger import ledger
  6. from toolsvc.objects import ObjectStore, build_predicate
  7. from toolsvc.readers import ReadError, read_rows
  8. @pytest.fixture(scope="module")
  9. def isolated_store(tmp_path_factory):
  10. """独立数据目录的 ObjectStore(不影响默认 store 的数据)。"""
  11. directory = tmp_path_factory.mktemp("objects")
  12. return ObjectStore(directory)
  13. @pytest.fixture(scope="module")
  14. def workspace(isolated_store):
  15. return isolated_store.create_workspace("测试工作区", "objects 测试")
  16. @pytest.fixture(scope="module")
  17. def raw_object(isolated_store, workspace, sample_csv):
  18. payload = sample_csv.read_bytes()
  19. return isolated_store.register_upload(workspace["id"], "small.csv", payload)
  20. def test_workspace_crud(isolated_store, workspace):
  21. assert workspace["name"] == "测试工作区"
  22. items = isolated_store.list_workspaces()
  23. assert any(item["id"] == workspace["id"] for item in items)
  24. def test_upload_indexes_rows_and_columns(raw_object):
  25. assert raw_object["kind"] == "raw"
  26. assert raw_object["sha256"].startswith("sha256:")
  27. assert raw_object["rows"] > 1000
  28. assert "HS_CODE" in raw_object["columns"]
  29. assert raw_object["tables"][0]["name"] == "raw"
  30. def test_data_read_returns_subset_with_trace(raw_object):
  31. result = read_rows(raw_object["id"], table="raw", limit=5)
  32. assert result["total"] == raw_object["rows"]
  33. assert len(result["rows"]) == 5
  34. assert result["exhausted"] is False
  35. trace = result["rows"][0]["_trace"]
  36. assert trace["object_id"] == raw_object["id"]
  37. assert trace["row_index"] == 0
  38. assert trace["row_hash"].startswith("sha256:")
  39. def test_data_read_filters_and_sorts(raw_object):
  40. result = read_rows(
  41. raw_object["id"],
  42. filters=[{"column": "I_E_FLAG", "op": "=", "value": "I"}],
  43. order_by=[{"column": "MONORSUM", "dir": "desc"}],
  44. limit=3,
  45. )
  46. assert len(result["rows"]) == 3
  47. amounts = [float(row["values"]["MONORSUM"]) for row in result["rows"]]
  48. assert amounts == sorted(amounts, reverse=True)
  49. def test_data_read_contains_filter(raw_object):
  50. result = read_rows(raw_object["id"], filters=[{"column": "G_MODEL", "op": "contains", "value": "MURATA"}], limit=2)
  51. assert result["total"] >= 1
  52. for row in result["rows"]:
  53. assert "MURATA" in row["values"]["G_MODEL"]
  54. def test_data_read_aggregate_mode(raw_object):
  55. result = read_rows(
  56. raw_object["id"],
  57. aggregate={
  58. "group_by": ["HS_CODE"],
  59. "metrics": [{"column": "MONORSUM", "fn": "sum", "alias": "amount"}, {"column": "COUNT", "fn": "sum", "alias": "quantity"}],
  60. },
  61. )
  62. assert result["aggregate"] is True
  63. assert result["rows"]
  64. assert set(result["columns"]) == {"HS_CODE", "amount", "quantity"}
  65. from decimal import Decimal
  66. amounts = [Decimal(row["values"]["amount"]) for row in result["rows"]]
  67. assert amounts == sorted(amounts, reverse=True)
  68. def test_data_read_rejects_unknown_column(raw_object):
  69. with pytest.raises(ReadError):
  70. read_rows(raw_object["id"], filters=[{"column": "不存在", "op": "=", "value": 1}])
  71. with pytest.raises(ReadError):
  72. read_rows(raw_object["id"], order_by=[{"column": "不存在"}])
  73. def test_limit_is_capped(raw_object):
  74. result = read_rows(raw_object["id"], limit=10_000)
  75. assert result["limit"] <= 200
  76. def test_preview_and_lineage(raw_object, isolated_store):
  77. preview = isolated_store.preview(raw_object["id"], "raw", limit=3)
  78. assert len(preview["rows"]) == 3
  79. lineage = isolated_store.row_lineage(raw_object["id"], "raw", -1)
  80. assert lineage and lineage["source_object_id"] == raw_object["id"]
  81. def test_build_predicate_from_result_row():
  82. predicate = build_predicate(
  83. {
  84. "一级类目": "电容器",
  85. "二级类目": "多层瓷介电容器(MLCC)",
  86. "应用领域标签": "先进计算",
  87. "品牌国别": "日本",
  88. "品牌": "MURATA",
  89. "前十大型号": "GRM100K",
  90. "年度": "2025",
  91. "月度": "3",
  92. "_row_index": 4,
  93. "_row_hash": "sha256:x",
  94. }
  95. )
  96. assert predicate["c1"] == "电容器"
  97. assert predicate["app"] == "先进计算"
  98. assert predicate["brand"] == "MURATA"
  99. assert predicate["year"] == "2025"
  100. assert predicate["month"] == "2025-03"
  101. def test_delete_object_removes_files(isolated_store, workspace, sample_csv):
  102. item = isolated_store.register_upload(workspace["id"], "tmp.csv", sample_csv.read_bytes())
  103. from pathlib import Path
  104. path = Path(item["path"])
  105. assert path.exists()
  106. isolated_store.delete_object(item["id"])
  107. assert not path.exists()
  108. with pytest.raises(KeyError):
  109. isolated_store.get_object(item["id"])
  110. def test_ledger_records_and_finds_values():
  111. from toolsvc.objects import new_id
  112. call_id = new_id("call")
  113. rows = [
  114. {"values": {"金额": "123.45", "品牌": "MURATA"}, "_trace": {"object_id": "obj_x", "table": "t", "row_index": 7, "row_hash": "sha256:abc"}}
  115. ]
  116. ledger.record(call_id, "data_read", {"q": 1}, {"rows": rows}, session_id="s-obj", rows=rows)
  117. hits = ledger.find_value("123.45", "s-obj")
  118. assert hits and hits[0]["call_id"] == call_id
  119. assert hits[0]["row_index"] == 7
  120. assert ledger.find_value("MURATA", "s-obj")
  121. assert ledger.session_calls("s-obj")
  122. def test_ledger_hash_chain_is_consistent():
  123. assert ledger.chain_ok(limit=200) is True