"""Срез 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