"""MCP-инструмент regional_rate: три состояния, отсутствие базы, регистрация.""" import sqlite3, sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) import smeta_mcp # noqa: E402 import regional_ref # noqa: E402 def _db(tmp_path: Path) -> Path: db = tmp_path / "regional_ref.sqlite" con = sqlite3.connect(db) con.executescript(""" create table subjects(code integer primary key, name text); create table sources(id integer primary key, subject_id int, kind text, edition text, act text, file_sha text, manifest_sha text, imported_at text); create table works(id integer primary key, subject_id int, kind text, code text, name text, unit text, price real, source_id int); create table prices(id integer primary key, subject_id int, kind text, code text, name text, unit text, price_otp real, price_smet real, source_id int); """) con.execute("insert into subjects values(337,'Оренбургская область')") con.execute("insert into sources values(1,337,'ТЕР','ред. 2014','приказ','sha1','sha2','now')") con.execute("insert into works values(1,337,'ТЕР','01-01-001-01','15 м³','м3',3270.04,1)") con.commit(); con.close(); return db def test_tool_registered(): assert "regional_rate" in smeta_mcp.TOOLS assert smeta_mcp.TOOLS["regional_rate"]["fn"] is smeta_mcp.tool_regional_rate def test_found_regional_label_and_provenance(tmp_path, monkeypatch): db = _db(tmp_path); monkeypatch.setenv("REGIONAL_REF_PATH", str(db)) r = smeta_mcp.tool_regional_rate(code="01-01-001-01", subject_id=337, kind="ТЕР") assert r["status"] == "ok" and r["base"] == "региональная" assert r["subject"] == "Оренбургская область" and r["price"] == 3270.04 assert r["file_sha"] == "sha1" and r["manifest_sha"] == "sha2" def test_code_missing_and_kind_missing(tmp_path, monkeypatch): db = _db(tmp_path); monkeypatch.setenv("REGIONAL_REF_PATH", str(db)) assert smeta_mcp.tool_regional_rate(code="99-99-999-99", subject_id=337)["status"] == "code_not_found" r = smeta_mcp.tool_regional_rate(code="101-0001", subject_id=337, kind="ТССЦ") assert r["status"] == "kind_not_loaded" def test_db_unavailable_not_silent(tmp_path, monkeypatch): monkeypatch.setenv("REGIONAL_REF_PATH", str(tmp_path / "missing.sqlite")) r = smeta_mcp.tool_regional_rate(code="01-01-001-01", subject_id=337) assert r["status"] == "db_unavailable" and "недоступна" in r["message"]