from io import BytesIO import pytest from openpyxl import load_workbook from finance.exporting import build_formula_workbook pytestmark = pytest.mark.django_db def test_reconcile_status_guards_non_numeric_difference(workspace): ds, member, *_ = workspace ds.periods = ["2025-08"] ds.save(update_fields=["periods"]) output = BytesIO() build_formula_workbook( ds, member, {"start": "2026-08", "end": "2026-08", "comparison": "yoy", "group": "department"}, output, ) workbook = load_workbook(BytesIO(output.getvalue()), data_only=False) status_cells = [row[7] for row in workbook["对账检查"].iter_rows(min_row=2)] formulas = [ cell.value for cell in status_cells if isinstance(cell.value, str) and cell.value.startswith("=IF(D") ] assert formulas assert all("ISNUMBER(F" in formula for formula in formulas) assert all('"通过"' in formula for formula in formulas) # 容差列独立成列,状态按容差比较而不是统一 0.01 assert all(row[6].value is not None for row in workbook["对账检查"].iter_rows(min_row=2))