test_calc.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """``calculator`` 工具的单元测试:计算正确性 + 输入溯源。"""
  2. from __future__ import annotations
  3. from decimal import Decimal
  4. import pytest
  5. from toolsvc.calc import CalcError, calculate
  6. from toolsvc.ledger import ledger
  7. from toolsvc.objects import new_id
  8. def test_sum_and_mean():
  9. assert calculate("sum", [{"value": "1.5"}, {"value": "2.5"}])["result"] == "4.0"
  10. assert calculate("mean", [{"value": 1}, {"value": 2}, {"value": 3}])["result"] == "2"
  11. assert calculate("median", [{"value": 1}, {"value": 5}, {"value": 3}])["result"] == "3"
  12. assert calculate("min", [{"value": 4}, {"value": 2}])["result"] == "2"
  13. assert calculate("max", [{"value": 4}, {"value": 2}])["result"] == "4"
  14. assert calculate("count", [{"value": 4}, {"value": 2}])["result"] == "2"
  15. def _result(payload: dict) -> Decimal:
  16. return Decimal(payload["result"])
  17. def test_growth_ratio_index_share():
  18. assert _result(calculate("growth", [{"value": 120}, {"value": 100}])) == Decimal("0.2")
  19. assert _result(calculate("ratio", [{"value": 10}, {"value": 4}])) == Decimal("2.5")
  20. assert _result(calculate("index", [{"value": 150}, {"value": 100}])) == Decimal("1500")
  21. assert _result(calculate("share", [{"value": 25}, {"value": 100}])) == Decimal("0.25")
  22. def test_division_by_zero_is_rejected_not_silently_zero():
  23. with pytest.raises(CalcError):
  24. calculate("growth", [{"value": 1}, {"value": 0}])
  25. with pytest.raises(CalcError):
  26. calculate("ratio", [{"value": 1}, {"value": 0}])
  27. with pytest.raises(CalcError):
  28. calculate("expression", expression="1/0")
  29. def test_expression_evaluator_is_whitelisted():
  30. payload = calculate("expression", [{"value": 2}, {"value": 3}], expression="(a + b) * 2")
  31. assert payload["result"] == "10"
  32. # 不能调用函数、不能访问属性、不能注入名字
  33. for expression in ["__import__('os')", "abs(-1)", "a.real", "sum([1,2])"]:
  34. with pytest.raises(CalcError):
  35. calculate("expression", [{"value": 2}], expression=expression)
  36. def test_unknown_operation_and_bad_inputs():
  37. with pytest.raises(CalcError):
  38. calculate("nope", [{"value": 1}])
  39. with pytest.raises(CalcError):
  40. calculate("sum", [])
  41. with pytest.raises(CalcError):
  42. calculate("sum", [{"value": "abc"}])
  43. def test_result_is_registered_in_ledger_and_has_trace():
  44. payload = calculate("sum", [{"value": 7}, {"value": 8}], session_id="s-test")
  45. assert payload["result"] == "15"
  46. assert payload["_trace"]["calculation_id"].startswith("calc_")
  47. assert payload["_trace"]["result_hash"].startswith("sha256:")
  48. entry = ledger.get(payload["call_id"])
  49. assert entry is not None and entry["tool"] == "calculator"
  50. assert ledger.find_calculation(payload["call_id"]) is not None
  51. def test_inputs_without_provenance_are_marked_unverified():
  52. payload = calculate("sum", [{"value": 1}, {"value": 2}])
  53. assert payload["all_inputs_verified"] is False
  54. assert payload["unverified_inputs"] == 2
  55. def test_inputs_can_reference_a_previous_calculator_call():
  56. first = calculate("sum", [{"value": 1}, {"value": 2}], session_id="s-chain")
  57. second = calculate(
  58. "sum",
  59. [
  60. {"value": 3, "from_call_id": first["call_id"], "row_hash": first["_trace"]["result_hash"]},
  61. {"value": 4},
  62. ],
  63. session_id="s-chain",
  64. )
  65. assert second["result"] == "7"
  66. assert second["inputs"][0]["verified"] is True
  67. assert second["inputs"][1]["verified"] is False
  68. def test_inputs_with_wrong_row_hash_are_not_verified():
  69. first = calculate("sum", [{"value": 1}, {"value": 2}], session_id="s-bad")
  70. second = calculate(
  71. "sum",
  72. [{"value": 3, "from_call_id": first["call_id"], "row_hash": "sha256:deadbeef"}],
  73. session_id="s-bad",
  74. )
  75. assert second["inputs"][0]["verified"] is False