64 lines
2.8 KiB
Python
64 lines
2.8 KiB
Python
from copy import deepcopy
|
|
import pytest
|
|
from normalize import normalize_tasks
|
|
from summary import summarize_tasks
|
|
from render import render_tasks
|
|
|
|
|
|
def test_normalization_trims_and_retains_only_contract_fields():
|
|
assert normalize_tasks([{"id": " api ", "title": " 编写接口 ", "done": True, "hours": 1.5, "extra": 9}]) == [
|
|
{"id": "api", "title": "编写接口", "done": True, "hours": 1.5}]
|
|
|
|
|
|
def test_missing_optional_fields_have_stable_defaults():
|
|
assert normalize_tasks([{"id": "one", "title": "任务"}]) == [{"id": "one", "title": "任务", "done": False, "hours": 0}]
|
|
|
|
|
|
def test_invalid_containers_rows_and_strings_raise_value_error():
|
|
for value in [None, {}, "rows", [None], [{}], [{"id": " ", "title": "x"}], [{"id": "x", "title": 1}], [{"id": 1, "title": "x"}], [{"id": "x", "title": " "}]]:
|
|
with pytest.raises(ValueError):
|
|
normalize_tasks(value)
|
|
|
|
|
|
def test_duplicate_trimmed_id_is_rejected():
|
|
with pytest.raises(ValueError):
|
|
normalize_tasks([{"id": "x", "title": "a"}, {"id": " x ", "title": "b"}])
|
|
|
|
|
|
def test_status_and_hours_are_not_silently_coerced():
|
|
for extra in [{"done": "false"}, {"done": 0}, {"hours": True}, {"hours": "2"}, {"hours": -1}, {"hours": float("nan")}, {"hours": float("inf")}]:
|
|
with pytest.raises(ValueError):
|
|
normalize_tasks([{"id": "x", "title": "a", **extra}])
|
|
|
|
|
|
def test_normalization_does_not_mutate_caller_rows():
|
|
rows = [{"id": " x ", "title": " a ", "hours": 2, "done": False}]
|
|
before = deepcopy(rows)
|
|
result = normalize_tasks(rows)
|
|
assert rows == before and result is not rows and result[0] is not rows[0]
|
|
|
|
|
|
def test_summary_uses_states_and_pending_hours():
|
|
rows = [{"id": "a", "title": "A", "done": False, "hours": 2}, {"id": "b", "title": "B", "done": True, "hours": 9}, {"id": "c", "title": "C", "done": False, "hours": 1.5}]
|
|
before = deepcopy(rows)
|
|
assert summarize_tasks(rows) == {"total": 3, "completed": 1, "pending_hours": 3.5}
|
|
assert rows == before
|
|
|
|
|
|
def test_empty_summary_and_render_are_exact():
|
|
assert summarize_tasks([]) == {"total": 0, "completed": 0, "pending_hours": 0}
|
|
assert render_tasks([]) == ""
|
|
|
|
|
|
def test_report_sorts_without_mutating_and_formats_status_and_hours():
|
|
rows = [{"id": "b", "title": "乙", "done": False, "hours": 2.5}, {"id": "a", "title": "甲", "done": True, "hours": 3.0}]
|
|
before = deepcopy(rows)
|
|
assert render_tasks(rows) == "[x] a: 甲 (3h)\n[ ] b: 乙 (2.5h)"
|
|
assert rows == before
|
|
|
|
|
|
def test_complete_flow_keeps_unicode_and_exact_aggregate():
|
|
rows = normalize_tasks([{"id": " z ", "title": "检查中文🙂", "hours": 2}, {"id": "a", "title": "已验证", "done": True, "hours": 5}])
|
|
assert summarize_tasks(rows) == {"total": 2, "completed": 1, "pending_hours": 2}
|
|
assert render_tasks(rows) == "[x] a: 已验证 (5h)\n[ ] z: 检查中文🙂 (2h)"
|