映射和判断规则共用一个不可变版本,试算可以直接取已选查询的结果。 Co-authored-by: Cursor <cursoragent@cursor.com>
78 lines
4.5 KiB
Python
78 lines
4.5 KiB
Python
"""S4 truth-table tests for the pure mapping judgment evaluator."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
from decimal import Decimal
|
|
|
|
import pytest
|
|
|
|
from watchman.services.judgment import evaluate_rows
|
|
|
|
CONFIGS = [
|
|
{"source_column_name": "n", "value_type": "number", "is_identity": False, "numeric_multiply": None, "numeric_offset": None},
|
|
{"source_column_name": "s", "value_type": "string", "is_identity": True, "numeric_multiply": None, "numeric_offset": None},
|
|
{"source_column_name": "at", "value_type": "datetime", "is_identity": False, "numeric_multiply": None, "numeric_offset": None},
|
|
]
|
|
CURRENT = ["n", "s", "at"]
|
|
|
|
|
|
def test_truth_table_and_or_and_missing_invalid() -> None:
|
|
and_tree = {"op": "and", "items": [{"column": "n", "operator": "gt", "value": 2}, {"column": "n", "operator": "lt", "value": 10}]}
|
|
assert evaluate_rows([{"n": 1, "s": "a"}], CONFIGS, and_tree, CURRENT)["code"] == "normal"
|
|
assert evaluate_rows([{"n": 5, "s": "a"}], CONFIGS, and_tree, CURRENT)["code"] == "abnormal"
|
|
missing = evaluate_rows([{"n": None, "s": "a"}], CONFIGS, and_tree, CURRENT)
|
|
assert missing["code"] == "unknown" and missing["field_status"]["n"] == "missing"
|
|
invalid = evaluate_rows([{"n": "bad", "s": "a"}], CONFIGS, and_tree, CURRENT)
|
|
assert invalid["code"] == "unknown" and invalid["field_status"]["n"] == "invalid"
|
|
or_tree = {"op": "or", "items": [{"column": "n", "operator": "gt", "value": 2}, {"column": "n", "operator": "lt", "value": 0}]}
|
|
assert evaluate_rows([{"n": 1, "s": "a"}], CONFIGS, or_tree, CURRENT)["code"] == "normal"
|
|
|
|
|
|
def test_empty_first_row_missing_column_and_datetime_rules() -> None:
|
|
tree = {"column": "n", "operator": "eq", "value": 2}
|
|
assert evaluate_rows([], CONFIGS, tree, CURRENT)["outcome"] == "empty"
|
|
no_identity = [{**CONFIGS[0]}, {**CONFIGS[2]}]
|
|
result = evaluate_rows([{"n": 2}, {"n": 1}], no_identity, tree, ["n", "at"])
|
|
assert result["code"] == "abnormal" and result["skipped_rows"] == 1
|
|
assert evaluate_rows([{"n": 2}], CONFIGS, tree, ["s"]) == {"outcome": "error", "missing_columns": ["at", "n"]}
|
|
time_tree = {"column": "at", "operator": "eq", "value": "2026-01-01T00:00:00+00:00"}
|
|
assert evaluate_rows([{"at": "2026-01-01T00:00:00+00:00", "s": "a"}], CONFIGS, time_tree, CURRENT)["code"] == "abnormal"
|
|
assert evaluate_rows([{"n": 5, "s": "a"}], CONFIGS, {"column": "n", "operator": "eq", "value": "5"}, CURRENT)["code"] == "abnormal"
|
|
assert evaluate_rows([{"n": 1, "s": "a"}], CONFIGS, {}, CURRENT)["outcome"] == "error"
|
|
assert evaluate_rows([{"at": datetime(2025, 12, 31, 19, tzinfo=timezone(timedelta(hours=-5))), "s": "a"}], CONFIGS, time_tree, CURRENT)["code"] == "abnormal"
|
|
assert evaluate_rows([{"at": datetime(2026, 1, 1), "s": "a"}], CONFIGS, time_tree, CURRENT)["field_status"]["at"] == "invalid"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("op", "value", "code"),
|
|
[("eq", 2, "abnormal"), ("ne", 2, "normal"), ("gte", 2, "abnormal"), ("lte", 2, "abnormal")],
|
|
)
|
|
def test_single_leaf_boundaries(op: str, value: int, code: str) -> None:
|
|
assert evaluate_rows([{"n": 2, "s": "a"}], CONFIGS, {"column": "n", "operator": op, "value": value}, CURRENT)["code"] == code
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("op", "row", "code"),
|
|
[
|
|
("and", {"n": 5, "s": None}, "unknown"),
|
|
("and", {"n": 1, "s": None}, "normal"),
|
|
("or", {"n": 5, "s": None}, "abnormal"),
|
|
("or", {"n": 5, "s": "a"}, "abnormal"),
|
|
("or", {"n": 1, "s": None}, "unknown"),
|
|
],
|
|
)
|
|
def test_truth_table_unknown_combinations(op: str, row: dict[str, object], code: str) -> None:
|
|
tree = {"op": op, "items": [{"column": "n", "operator": "gt", "value": 2}, {"column": "s", "operator": "eq", "value": "x"}]}
|
|
assert evaluate_rows([row], CONFIGS, tree, CURRENT)["code"] == code
|
|
|
|
|
|
def test_numeric_conversion_is_optional_and_accepts_decimal() -> None:
|
|
converted = [{**CONFIGS[0], "numeric_multiply": Decimal("2"), "numeric_offset": Decimal("1")}, *CONFIGS[1:]]
|
|
tree = {"column": "n", "operator": "eq", "value": 5}
|
|
assert evaluate_rows([{"n": Decimal("2"), "s": "a"}], converted, tree, CURRENT)["code"] == "abnormal"
|
|
assert evaluate_rows([{"n": 2.0, "s": "a"}], converted, tree, CURRENT)["code"] == "abnormal"
|
|
assert evaluate_rows([{"n": Decimal("2"), "s": "a"}], CONFIGS, {"column": "n", "operator": "eq", "value": 2}, CURRENT)["code"] == "abnormal"
|
|
result = evaluate_rows([{"n": Decimal("sNaN"), "s": "a"}], CONFIGS, tree, CURRENT)
|
|
assert result["code"] == "unknown" and result["field_status"]["n"] == "invalid"
|