Находка зайки: сбой чтения state.json проглатывался с pending=0, и сигнал «база отстала от разобранного» исчезал без следа. Теперь нечитаемое состояние — находка problem («отставание проверить не удалось»), а пустая дата загрузки не роняет сравнение и не прячет сигнал. Co-Authored-By: Claude Code <noreply@anthropic.com>
127 lines
5.8 KiB
Python
127 lines
5.8 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_only_when_tsn_enabled(monkeypatch):
|
|
"""Контракт T12: нет базы при включённой ТСН — поломка; при выключенной — справка."""
|
|
import norm_base
|
|
monkeypatch.setattr(tsn_ref, "_problem_of", lambda path=None: "db_unavailable")
|
|
monkeypatch.setattr(norm_base, "tsn_enabled", lambda: True)
|
|
findings, _ = ext_check.check_tsn_ref({})
|
|
assert findings[0]["level"] == "problem"
|
|
monkeypatch.setattr(norm_base, "tsn_enabled", lambda: False)
|
|
findings, _ = ext_check.check_tsn_ref({})
|
|
assert findings[0]["level"] == "info"
|
|
|
|
|
|
def test_corrupt_db_is_problem_even_when_disabled(monkeypatch):
|
|
import norm_base
|
|
monkeypatch.setattr(tsn_ref, "_problem_of", lambda path=None: "db_corrupt")
|
|
monkeypatch.setattr(norm_base, "tsn_enabled", lambda: False)
|
|
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"]
|
|
|
|
|
|
def test_unreadable_state_is_reported_not_swallowed(monkeypatch):
|
|
"""Состояние загрузок не читается — про отставание базы проверить нельзя; молчать нельзя."""
|
|
import ext_state
|
|
monkeypatch.setattr(tsn_ref, "_problem_of", lambda path=None: None)
|
|
monkeypatch.setattr(tsn_ref, "latest_release", lambda: {"built_at": "2026-11-18T00:00:00Z"})
|
|
|
|
def boom():
|
|
raise OSError("state.json битый")
|
|
monkeypatch.setattr(ext_state, "load_state", boom)
|
|
findings, _ = ext_check.check_tsn_ref({})
|
|
assert findings and findings[0]["level"] == "problem", findings
|
|
assert "состояние" in findings[0]["message"].lower() or "не читается" in findings[0]["message"].lower()
|
|
|
|
|
|
def test_record_without_date_does_not_break_compare(monkeypatch):
|
|
"""fetched_at=None не должен ронять сравнение и прятать сигнал отставания."""
|
|
import ext_state
|
|
monkeypatch.setattr(tsn_ref, "_problem_of", lambda path=None: None)
|
|
monkeypatch.setattr(tsn_ref, "latest_release", lambda: {"built_at": "2026-11-18T00:00:00Z"})
|
|
monkeypatch.setattr(ext_state, "load_state", lambda: {"sources": {"niac_tsn": {
|
|
"x": {"extract_ok": True, "fetched_at": None}}}})
|
|
findings, _ = ext_check.check_tsn_ref({})
|
|
assert findings == []
|