"""Расчёт по ТСН (T8): базис есть всегда, текущая цена — только с коэффициентом.""" import json import os import sys import pytest ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if ROOT not in sys.path: sys.path.insert(0, ROOT) import ext_state # noqa: E402 import scripts.build_tsn_ref as build_mod # noqa: E402 import tsn_calc # noqa: E402 def _db(tmp_path, monkeypatch, with_index: bool): monkeypatch.setattr(ext_state, "ext_dir", lambda: tmp_path / "ext") (tmp_path / "ext").mkdir() base = tmp_path / "ext" / "snapshots" / "niac_tsn" / "s" d = base / "rates" d.mkdir(parents=True) (d / "extract.json").write_text(json.dumps({"works": [{ "code": "1-5-1", "name": "Разработка грунта", "unit": "100 м3", "pz": 320.63, "ot": 5.79, "em": 314.84, "mat": 0.0, "labor": 0.55, "page": 20, "resources": []}], "stats": {"works_parsed": 1}}), encoding="utf-8") (d / "meta.json").write_text(json.dumps({"key": "rates", "kind": "rate_book", "book": "ТСН-2001.3", "chapter": "3-1", "edition": "", "addendum_no": ""}), encoding="utf-8") if with_index: i = base / "index" i.mkdir(parents=True) (i / "extract.json").write_text(json.dumps({"issue_no": "222", "quarter": 4, "year": 2026, "decree": "МКЭ-ОД/26-76", "addendum_no": "83", "values": [{"code": "3.1-5-1", "value": 5.5, "page": 12}], "stats": {"values_parsed": 1}}), encoding="utf-8") (i / "meta.json").write_text(json.dumps({"key": "index", "kind": "index_release", "book": "", "chapter": "", "edition": "", "addendum_no": "83", "issue_no": "222"}), encoding="utf-8") out = tmp_path / "tsn.sqlite" build_mod.build(tmp_path / "ext", out) monkeypatch.setenv("TSN_REF_PATH", str(out)) return out def test_without_mapping_no_current_price(tmp_path, monkeypatch): _db(tmp_path, monkeypatch, with_index=False) rep = tsn_calc.calc_position("1-5-1") assert rep["status"] == "index_unavailable" assert rep["base"]["pz"] == 320.63, "базис обязан возвращаться" assert rep["current"] is None, "текущей цены без коэффициента быть не может" def test_with_mapping_computes_current_price(tmp_path, monkeypatch): _db(tmp_path, monkeypatch, with_index=True) rep = tsn_calc.calc_position("1-5-1") assert rep["status"] == "ok", rep # округление до копейки — ROUND_HALF_UP (банковское округление float дало бы 1763.46) assert rep["current"]["pz"] == tsn_calc._money(320.63 * 5.5) == 1763.47 assert rep["index"]["issue_no"] == "222" def test_estimate_refuses_when_any_position_unpriced(tmp_path, monkeypatch): _db(tmp_path, monkeypatch, with_index=False) rep = tsn_calc.calc_estimate([{"code": "1-5-1", "qty": 2}]) assert rep["status"] == "index_unavailable" assert rep["grand_total"] is None assert rep["missing"][0]["code"] == "1-5-1"