Build browsable norm table with inline composition

This commit is contained in:
anor
2026-12-04 15:46:19 +10:00
parent 30955ccfc9
commit 6520ce9357
7 changed files with 403 additions and 39 deletions
+50
View File
@@ -1,9 +1,41 @@
"""Source-backed norm search and price preview."""
import pytest
import importlib
import sqlite3
from fastapi.testclient import TestClient
def test_browse_keeps_sections_and_base_types_separate(tmp_path, monkeypatch):
import norm_finder
import fsnb_ref
db = tmp_path / "fsnb.sqlite"
with sqlite3.connect(db) as con:
con.execute("CREATE TABLE sources (source_id INTEGER)")
con.execute("CREATE TABLE works (base TEXT, base_type TEXT, code TEXT, name TEXT, "
"unit TEXT, cost REAL, price_level TEXT, section_path TEXT)")
con.executemany("INSERT INTO works VALUES (?,?,?,?,?,?,?,?)", [
("ГЭСН-2022", "ГЭСН", "01-01-001-01", "Разработка грунта", "1000 м3", None,
"норма", "Земляные работы > Раздел 1 > Таблица 1"),
("ГЭСН-2022", "ГЭСН", "01-01-001-02", "Разработка грунта 2", "1000 м3", None,
"норма", "Земляные работы > Раздел 1 > Таблица 1"),
("ГЭСН-2022", "ГЭСНм", "01-01-001-01", "Монтаж", "шт", None,
"норма", "Оборудование > Раздел 1"),
])
monkeypatch.setattr(fsnb_ref, "db_path", lambda: db)
root = norm_finder.browse("2022", "ГЭСН", "")
assert [row["name"] for row in root["children"]] == ["Земляные работы"]
section = norm_finder.browse("2022", "ГЭСН", "Земляные работы")
assert [row["name"] for row in section["children"]] == ["Раздел 1"]
leaf = norm_finder.browse("2022", "ГЭСН", "Земляные работы > Раздел 1 > Таблица 1", limit=1)
assert [row["code"] for row in leaf["items"]] == ["01-01-001-01"]
assert leaf["has_more"] is True
assert norm_finder.browse("2022", "ГЭСН", leaf["path"], limit=1, offset=1)["items"][0]["code"] == "01-01-001-02"
with pytest.raises(ValueError):
norm_finder.browse("2022", "ГЭСН", "Земляные работы > Несуществующий")
with pytest.raises(ValueError):
norm_finder.browse("2022", "ГЭСН", "%")
def test_search_rejects_empty_query_and_uses_existing_fsnb(monkeypatch):
import fsnb_ref
import norm_finder
@@ -254,6 +286,24 @@ def test_finder_page_is_reachable_from_site(api):
assert client.get("/static/norm-finder.js").headers["Cache-Control"] == "no-cache, must-revalidate"
def test_browse_routes_bound_public_pages_and_require_auth(api, monkeypatch):
import norm_finder
client, headers, _ = api
calls = []
def fake_browse(base, base_type, path, limit, offset):
calls.append((base, base_type, path, limit, offset))
return {"children": [], "items": [{"code": "01-01-001-01"}], "has_more": False}
monkeypatch.setattr(norm_finder, "browse", fake_browse)
url = "/api/public/norm-finder/browse?base=2022&base_type=ГЭСН&path=Земляные работы&limit=20"
response = client.get(url)
assert response.status_code == 200
assert response.json()["items"][0]["code"] == "01-01-001-01"
assert calls == [("2022", "ГЭСН", "Земляные работы", 20, 0)]
assert client.get(url.replace("limit=20", "limit=21")).status_code == 400
assert client.get("/api/norm-finder/browse").status_code == 401
assert client.get("/api/norm-finder/browse", headers=headers).status_code == 200
def test_finder_draft_is_scoped_revisioned_and_never_totals_missing_costs(api, monkeypatch):
import auth
import engine_prices