tsn_stage(): нет базы, битая, есть и что внутри. 8 проверок. Co-Authored-By: Claude Code <noreply@anthropic.com>
88 lines
3.7 KiB
Python
88 lines
3.7 KiB
Python
"""Ежедневные проверки ТСН: новые документы, усыхание раздела, отставание сборки."""
|
|
|
|
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_check # noqa: E402
|
|
import niac_client # noqa: E402
|
|
import tsn_ref # noqa: E402
|
|
|
|
|
|
def _doc(key, section="collections_3"):
|
|
return {"key": key, "section": section, "kind": "rate_book", "book": "ТСН-2001.3"}
|
|
|
|
|
|
def test_first_run_is_info_not_flood(monkeypatch):
|
|
monkeypatch.setattr(niac_client, "discover", lambda client=None: [_doc("a"), _doc("b")])
|
|
findings, seen = ext_check.check_niac_tsn({})
|
|
assert [f["level"] for f in findings] == ["info"]
|
|
assert seen["niac_tsn_keys"] == ["a", "b"]
|
|
|
|
|
|
def test_new_document_is_update(monkeypatch):
|
|
monkeypatch.setattr(niac_client, "discover",
|
|
lambda client=None: [_doc("a"), _doc("b"), _doc("c")])
|
|
prev = {"niac_tsn_keys": ["a", "b"], "niac_tsn_counts": {"collections_3": 2}}
|
|
findings, seen = ext_check.check_niac_tsn(prev)
|
|
assert any(f["level"] == "update" and "c" in f["message"] for f in findings)
|
|
assert seen["niac_tsn_keys"] == ["a", "b", "c"]
|
|
|
|
|
|
def test_section_shrink_is_problem(monkeypatch):
|
|
monkeypatch.setattr(niac_client, "discover", lambda client=None: [_doc("a")])
|
|
prev = {"niac_tsn_keys": ["a", "b", "c", "d"], "niac_tsn_counts": {"collections_3": 4}}
|
|
findings, _ = ext_check.check_niac_tsn(prev)
|
|
assert any(f["level"] == "problem" and "collections_3" in f["message"] for f in findings)
|
|
|
|
|
|
def test_discovery_failure_is_problem(monkeypatch):
|
|
def boom(client=None):
|
|
raise niac_client.SourceEmptyError("раздел опустел")
|
|
monkeypatch.setattr(niac_client, "discover", boom)
|
|
findings, _ = ext_check.check_niac_tsn({})
|
|
assert findings[0]["level"] == "problem"
|
|
|
|
|
|
def test_missing_db_is_problem(monkeypatch, tmp_path):
|
|
monkeypatch.setattr(tsn_ref, "_problem_of", lambda path=None: "db_unavailable")
|
|
findings, _ = ext_check.check_tsn_ref({})
|
|
assert findings[0]["level"] == "problem"
|
|
|
|
|
|
def test_parsed_after_build_is_recurrent_update(monkeypatch):
|
|
monkeypatch.setattr(tsn_ref, "_problem_of", lambda path=None: None)
|
|
monkeypatch.setattr(tsn_ref, "latest_release", lambda: {"built_at": "2026-11-18T00:00:00Z"})
|
|
fake_state = {"sources": {"niac_tsn": {
|
|
"x": {"extract_ok": True, "fetched_at": "2026-11-18T01:00:00Z"}}}}
|
|
import ext_state
|
|
monkeypatch.setattr(ext_state, "load_state", lambda: fake_state)
|
|
f1, _ = ext_check.check_tsn_ref({})
|
|
f2, _ = ext_check.check_tsn_ref({})
|
|
assert f1 and f2, "отставание должно напоминать каждый прогон, без дедупа"
|
|
assert f1[0]["level"] == f2[0]["level"] == "update"
|
|
|
|
|
|
def test_fresh_db_has_no_findings(monkeypatch):
|
|
monkeypatch.setattr(tsn_ref, "_problem_of", lambda path=None: None)
|
|
monkeypatch.setattr(tsn_ref, "latest_release", lambda: {"built_at": "2026-11-18T02:00:00Z"})
|
|
import ext_state
|
|
monkeypatch.setattr(ext_state, "load_state", lambda: {"sources": {"niac_tsn": {
|
|
"x": {"extract_ok": True, "fetched_at": "2026-11-18T01:00:00Z"}}}})
|
|
findings, _ = ext_check.check_tsn_ref({})
|
|
assert findings == []
|
|
|
|
|
|
def test_tsn_stage_served_when_db_ok(monkeypatch):
|
|
import ext_status
|
|
monkeypatch.setattr(tsn_ref, "_problem_of", lambda path=None: None)
|
|
monkeypatch.setattr(tsn_ref, "verify_db", lambda p: {"ok": True, "works": 10, "values": 20})
|
|
monkeypatch.setattr(tsn_ref, "latest_release", lambda: {"built_at": "2026-11-18T00:00:00Z"})
|
|
st = ext_status.tsn_stage()
|
|
assert st["status"] == "served" and "10" in st["label"]
|