From 3c032859294f831f283426767b38c41bbc5a616f Mon Sep 17 00:00:00 2001 From: anor Date: Fri, 17 Jul 2026 21:20:24 +1000 Subject: [PATCH] =?UTF-8?q?test(=D1=81=D1=80=D0=B5=D0=B7=204.1):=20strict-?= =?UTF-8?q?=D1=80=D0=B5=D0=B6=D0=B8=D0=BC=20=D0=B4=D0=B2=D0=B8=D0=B6=D0=BA?= =?UTF-8?q?=D0=B0=20=E2=80=94=208=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=20(missing/metamorphic/=D1=80=D0=B5=D0=B5=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=B5=D0=BB=D1=8C=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C/=D1=80=D0=B5=D0=B3=D1=80=D0=B5=D1=81=D1=81=D0=B8=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/test_engine_strict.py | 90 +++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 tests/test_engine_strict.py diff --git a/tests/test_engine_strict.py b/tests/test_engine_strict.py new file mode 100644 index 0000000..ad473a3 --- /dev/null +++ b/tests/test_engine_strict.py @@ -0,0 +1,90 @@ +"""Срез 4.1: strict-режим движка (fail-loud вместо тихих нулей). + +Нет ресурса/цены → result.ok=False + result.missing, а не молчаливый 0. strict=False +сохраняет прежнее поведение (регрессия SSR/wizard). Metamorphic: qty×2 → итог×2. +""" +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent / "smeta-railway" / "src")) +from smeta_railway.calc.estimate import EstimateCalculator # noqa: E402 +from smeta_railway.data.fgis import FGISLookup # noqa: E402 +from smeta_railway.models import NormResources, MaterialResource, LaborResource # noqa: E402 +from smeta_railway.config import Section, Subsection, WorkItem # noqa: E402 + + +def _section(qty: float, code: str = "ГЭСН 01-01-001-01") -> Section: + item = WorkItem(code=code, name="Тест", unit="м3", qty=qty) + return Section(name="Раздел 1", subsections=[Subsection(name="п", items=[item])]) + + +def _res_with_material(cost_base: float) -> dict: + return {"01-01-001-01": NormResources( + norm_code="01-01-001-01", unit="м3", + materials=[MaterialResource(code="M1", name="Материал", unit="кг", + qty=10.0, cost_base=cost_base)])} + + +def test_missing_code_strict_fails_loud(): + calc = EstimateCalculator(fgis=FGISLookup(), resources={}, strict=True) + r = calc.calculate([_section(2)]) + assert r.ok is False + assert any("01-01-001-01" in m["code"] for m in r.missing) + assert r.positions[0].unresolved is True + + +def test_missing_code_non_strict_preserves_old_behaviour(): + calc = EstimateCalculator(fgis=FGISLookup(), resources={}, strict=False) + r = calc.calculate([_section(2)]) + assert r.ok is True and r.missing == [] # регрессия: молча как раньше + assert r.positions[0].unresolved is True # но позиция помечена (для UI) + + +def test_missing_material_price_strict(): + calc = EstimateCalculator(fgis=FGISLookup(), resources=_res_with_material(0), strict=True) + r = calc.calculate([_section(2)]) + assert r.ok is False + assert any(m["code"] == "M1" for m in r.missing) + + +def test_metamorphic_qty_double_doubles_total(): + calc = EstimateCalculator(fgis=FGISLookup(), resources=_res_with_material(100.0)) + r2 = calc.calculate([_section(2)]) + r4 = calc.calculate([_section(4)]) + assert r2.grand_total_with_limits > 0 + assert abs(r4.grand_total_with_limits - 2 * r2.grand_total_with_limits) < 0.05 + assert r2.ok is True # цена есть → сходится + + +def test_resolved_estimate_ok(): + calc = EstimateCalculator(fgis=FGISLookup(), resources=_res_with_material(100.0), strict=True) + r = calc.calculate([_section(2)]) + assert r.ok is True and r.missing == [] + + +def test_missing_labor_price_strict(): + # ozp есть, но в FGIS нет цены разряда → strict ловит (дыра дефолта 545.28) + res = {"01-01-001-01": NormResources(norm_code="01-01-001-01", unit="м3", + ozp=LaborResource(razryad=3.0, chel_hours=10.0))} + calc = EstimateCalculator(fgis=FGISLookup(), resources=res, strict=True) + r = calc.calculate([_section(2)]) + assert r.ok is False + assert any("труд" in m["code"] for m in r.missing) + + +def test_missing_transport_price_strict(): + calc = EstimateCalculator(fgis=FGISLookup(), resources={}, strict=True) + item = WorkItem(code="ФССЦпг 03-21-01-999", name="Перевозка", unit="т", qty=5.0) + sec = Section(name="Р", subsections=[Subsection(name="п", items=[item])]) + r = calc.calculate([sec]) + assert r.ok is False + assert any("ФССЦпг" in m["code"] for m in r.missing) + assert r.positions[0].unresolved is True + + +def test_missing_not_accumulated_across_calls(): + # реентерабельность: два расчёта на одном strict-калькуляторе — missing не копится + calc = EstimateCalculator(fgis=FGISLookup(), resources={}, strict=True) + r1 = calc.calculate([_section(2)]) + r2 = calc.calculate([_section(2)]) + assert len(r1.missing) == 1 and len(r2.missing) == 1 # не 1 и 2