refactor: декомпозиция монолита в пакет smeta-railway

- Пакетная структура src/smeta_railway/ с модулями calc/, data/, output/
- Pydantic-конфиг: всё через YAML, сметчик не трогает Python
- EstimateCalculator: ядро расчёта, параметризованные коэффициенты
- FGISLookup: pickle cache (4.7с → ~50мс при повторном запуске)
- CLI: smeta generate/fetch-resources/validate (typer)
- 28 тестов (coefficients, overhead, fgis, estimate)
- Исправлен баг: НР/СП для земляных работ (92%/46% вместо 109%/65%)
- ПЗ совпадают бит-в-бит со старым генератором

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-20 18:42:19 +10:00
co-authored by Claude Opus 4.6
parent bfe5479424
commit 897d2d1b76
25 changed files with 4663 additions and 0 deletions
View File
+70
View File
@@ -0,0 +1,70 @@
"""Tests for coefficient calculation."""
import pytest
from smeta_railway.calc.coefficients import get_coefficients
class TestCoefficients:
"""Test coefficient calculation for various norm codes."""
def test_full_coefficients_037(self):
"""28-01-037-02: all 3 coefficients applied."""
c = get_coefficients("ГЭСН 28-01-037-02")
assert c.zt == 1.60
assert c.em == pytest.approx(1.60 * 1.20 * 1.81, rel=1e-3)
assert c.ztm == pytest.approx(1.60 * 1.20 * 1.81, rel=1e-3)
assert set(c.applied) == {"dvizhenie", "vl", "okno"}
def test_full_coefficients_068(self):
"""28-01-068-07: all 3 coefficients (machine norm)."""
c = get_coefficients("ГЭСН 28-01-068-07")
assert c.zt == 1.60
assert c.em == pytest.approx(3.4752, rel=1e-3)
assert len(c.applied) == 3
def test_no_okno_155(self):
"""28-01-155-01: manual work, no okno coefficient."""
c = get_coefficients("ГЭСН 28-01-155-01")
assert c.zt == 1.60
assert c.em == pytest.approx(1.60 * 1.20, rel=1e-3)
assert c.ztm == pytest.approx(1.60 * 1.20, rel=1e-3)
assert "okno" not in c.applied
assert "dvizhenie" in c.applied
assert "vl" in c.applied
def test_no_okno_043(self):
"""28-01-043-01: rail cutting, no okno."""
c = get_coefficients("ГЭСН 28-01-043-01")
assert "okno" not in c.applied
assert c.em == pytest.approx(1.92, rel=1e-3)
def test_no_okno_044(self):
"""28-01-044-01: insulating joints, no okno."""
c = get_coefficients("ГЭСН 28-01-044-01")
assert "okno" not in c.applied
def test_earthwork_no_coefficients(self):
"""01-02-057-02: earthwork, no collection 28 coefficients."""
c = get_coefficients("ГЭСН 01-02-057-02")
assert c.zt == 1.0
assert c.em == 1.0
assert c.ztm == 1.0
assert c.applied == []
def test_formulas_display(self):
"""Check formula strings for display."""
c = get_coefficients("ГЭСН 28-01-037-02")
assert "1.60" in c.formulas["zt"]
assert "1.20" in c.formulas["em"]
assert "1.81" in c.formulas["em"]
def test_zt_only_dvizhenie(self):
"""ZT gets only dvizhenie (not vl, not okno)."""
c = get_coefficients("ГЭСН 28-01-037-02")
assert c.zt == 1.60 # only dvizhenie
def test_em_value(self):
"""EM = 1.60 * 1.20 * 1.81 = 3.4752."""
c = get_coefficients("ГЭСН 28-01-037-02")
assert c.em == pytest.approx(3.4752, rel=1e-4)
+195
View File
@@ -0,0 +1,195 @@
"""Tests for EstimateCalculator."""
import pytest
from smeta_railway.calc.estimate import EstimateCalculator
from smeta_railway.config import Section, Subsection, WorkItem
from smeta_railway.data.fgis import FGISLookup
from smeta_railway.models import (
LaborResource,
MachineResource,
MaterialResource,
NormResources,
)
def _make_fgis() -> FGISLookup:
"""Create a minimal FGIS lookup for testing."""
return FGISLookup(
index_map={
"91.09.03-031": 1.64,
"91.09.05-023": 1.64,
},
base_price_map={
"91.09.03-031": 3771.54,
"91.09.05-023": 2200.0,
},
current_price_map={},
labor_price_map={
"1-100-31": 550.0,
"4-100-060": 700.0,
},
)
def _make_resources() -> dict[str, NormResources]:
"""Create minimal resources for testing."""
return {
"28-01-031-01": NormResources(
norm_code="28-01-031-01",
norm_name="Выправка пути ВПО",
unit="км",
ozp=LaborResource(razryad=3.1, chel_hours=20.0),
machines=[
MachineResource(
code="91.09.03-031",
name="Платформы моторные",
unit="маш.-ч",
qty=5.0,
cost_base=3771.54,
razryad_mash=6.0,
),
],
materials=[],
),
}
class TestEstimateCalculator:
def test_single_gesn_position(self):
"""Test calculation of a single GESN position."""
fgis = _make_fgis()
resources = _make_resources()
calc = EstimateCalculator(fgis, resources)
sections = [
Section(
name="Test",
subsections=[
Subsection(items=[
WorkItem(
code="ГЭСН 28-01-031-01",
name="Выправка пути",
unit="км",
qty=2.0,
),
]),
],
),
]
result = calc.calculate(sections)
assert len(result.positions) == 1
pos = result.positions[0]
assert pos.gesn_code == "ГЭСН 28-01-031-01"
assert pos.qty == 2.0
assert pos.ot_cost > 0
assert pos.em_cost > 0
assert pos.pz > 0
assert pos.nr > 0
assert pos.sp > 0
assert pos.total > pos.pz
def test_transport_position(self):
"""Test transport position calculation."""
fgis = FGISLookup()
calc = EstimateCalculator(fgis, {})
sections = [
Section(
name="Transport",
subsections=[
Subsection(items=[
WorkItem(
code="ФССЦпг 236019-00-0071-0080",
name="Перевозка щебня",
unit="1 т груза",
qty=100,
),
]),
],
),
]
result = calc.calculate(sections)
assert len(result.positions) == 1
pos = result.positions[0]
assert pos.is_transport
assert pos.transport_cost == pytest.approx(100 * 815.96, rel=1e-3)
def test_kp_position(self):
"""Test commercial proposal position."""
fgis = FGISLookup()
calc = EstimateCalculator(fgis, {})
sections = [
Section(
name="KP",
subsections=[
Subsection(items=[
WorkItem(
code="КП РСП-19",
name="Сварка на РСП",
unit="стык",
qty=10,
),
]),
],
),
]
result = calc.calculate(sections)
pos = result.positions[0]
assert pos.is_kp
assert pos.pz == pytest.approx(10 * 8129.21, rel=1e-3)
def test_material_position_skipped(self):
"""Material positions have zero costs."""
fgis = FGISLookup()
calc = EstimateCalculator(fgis, {})
sections = [
Section(
name="Mat",
subsections=[
Subsection(items=[
WorkItem(
code="Материал",
name="Шпала Ш1",
unit="шт.",
qty=44,
),
]),
],
),
]
result = calc.calculate(sections)
pos = result.positions[0]
assert pos.is_material
assert pos.pz == 0
def test_grand_totals_accumulate(self):
"""Grand totals should accumulate across positions."""
fgis = FGISLookup()
calc = EstimateCalculator(fgis, {})
sections = [
Section(
name="Transport",
subsections=[
Subsection(items=[
WorkItem(code="ФССЦпг 236019-00-0071-0080",
name="A", unit="т", qty=100),
WorkItem(code="ФССЦпг 236019-00-0001-0005",
name="B", unit="т", qty=200),
]),
],
),
]
result = calc.calculate(sections)
expected = 100 * 815.96 + 200 * 731.17
assert result.grand_transport == pytest.approx(expected, rel=1e-3)
assert result.grand_pz == pytest.approx(expected, rel=1e-3)
+49
View File
@@ -0,0 +1,49 @@
"""Tests for FGIS lookup utilities."""
import pytest
from smeta_railway.data.fgis import FGISLookup, get_labor_code, get_labor_current_price
class TestLaborCode:
def test_razryad_30(self):
assert get_labor_code(3.0) == "1-100-30"
def test_razryad_31(self):
assert get_labor_code(3.1) == "1-100-31"
def test_razryad_50(self):
assert get_labor_code(5.0) == "1-100-50"
def test_machinist_50(self):
assert get_labor_code(5.0, "4-100-") == "4-100-050"
def test_machinist_60(self):
assert get_labor_code(6.0, "4-100-") == "4-100-060"
class TestLaborPrice:
def test_default_price(self):
"""Without FGIS data, returns default 545.28."""
fgis = FGISLookup()
price = get_labor_current_price(fgis, 3.0)
assert price == 545.28
def test_with_fgis_data(self):
fgis = FGISLookup(
labor_price_map={"1-100-30": 600.0}
)
price = get_labor_current_price(fgis, 3.0)
assert price == 600.0
class TestFGISLookup:
def test_get_index(self):
fgis = FGISLookup(index_map={"91.09.07-014": 1.64})
assert fgis.get_index("91.09.07-014") == 1.64
assert fgis.get_index("nonexistent") is None
def test_get_base_price(self):
fgis = FGISLookup(base_price_map={"91.09.07-014": 1000.0})
assert fgis.get_base_price("91.09.07-014") == 1000.0
assert fgis.get_base_price("nonexistent", 0) == 0
+35
View File
@@ -0,0 +1,35 @@
"""Tests for overhead calculation."""
import pytest
from smeta_railway.calc.overhead import calc_overhead, get_overhead_percents
class TestOverhead:
def test_railway_percents(self):
nr_pct, sp_pct, *_ = get_overhead_percents("ГЭСН 28-01-037-02")
assert nr_pct == 109
assert sp_pct == 65
def test_earthwork_percents(self):
nr_pct, sp_pct, *_ = get_overhead_percents("ГЭСН 01-02-057-02")
assert nr_pct == 92
assert sp_pct == 46
def test_calc_overhead(self):
fot = 100_000.0
nr, sp = calc_overhead(fot, 109, 65)
assert nr == 109_000.0
assert sp == 65_000.0
def test_calc_overhead_rounding(self):
fot = 12345.67
nr, sp = calc_overhead(fot, 109, 65)
assert nr == pytest.approx(13456.78, abs=0.01)
assert sp == pytest.approx(8024.69, abs=0.01)
def test_railway_labels(self):
_, _, nr_code, nr_label, sp_code, sp_label = get_overhead_percents("ГЭСН 28-01-031-01")
assert "812" in nr_code
assert "774" in sp_code
assert "Железные дороги" in nr_label