"""Срез 1.3: verify + diff + активация. Гейт аномалий (fail-loud) — red-green первым. Синтетические xlsx с кодами в колонке A (без сети, без реального ФГИС-файла). """ import sys from pathlib import Path import pytest sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) import ext_apply as ea # noqa: E402 def _make_xlsx(path, codes): import openpyxl wb = openpyxl.Workbook() ws = wb.active ws.append(["Код", "Наименование", "Цена"]) # шапка for c in codes: ws.append([c, "материал", 100.0]) wb.save(str(path)) def _codes(n, prefix="M"): return [f"{prefix}-{i:05d}" for i in range(n)] def test_verify_ok_and_too_small(tmp_path): big = tmp_path / "big.xlsx"; _make_xlsx(big, _codes(1200)) assert ea.verify_snapshot(big)["ok"] is True small = tmp_path / "small.xlsx"; _make_xlsx(small, _codes(10)) r = ea.verify_snapshot(small) assert r["ok"] is False and "мало" in r["errors"][0] def test_verify_rejects_non_xlsx(tmp_path): bad = tmp_path / "bad.xlsx"; bad.write_bytes(b"not really xlsx") assert ea.verify_snapshot(bad)["ok"] is False def test_diff_codes_ratio(): d = ea.diff_codes(set(_codes(100)), set(_codes(80)) | set(_codes(5, "NEW"))) assert d["removed"] == 20 and d["added"] == 5 assert d["removed_ratio"] == 0.2 def test_fresh_activate_creates_target(tmp_path): snap = tmp_path / "snap.xlsx"; _make_xlsx(snap, _codes(1200)) tdir = tmp_path / "railway"; tdir.mkdir() rep = ea.activate(snap, target_file=tdir / "fgis_split_form.xlsx") assert rep["activated"] is True assert (tdir / "fgis_split_form.xlsx").exists() def test_gate_blocks_massive_removal_without_force(tmp_path, monkeypatch): monkeypatch.setenv("EXT_SNAPSHOT_DIR", str(tmp_path / "_ext")) tdir = tmp_path / "railway"; tdir.mkdir() target = tdir / "fgis_split_form.xlsx" _make_xlsx(target, _codes(2000)) # текущий: 2000 кодов snap = tmp_path / "snap.xlsx"; _make_xlsx(snap, _codes(2000)[:1200]) # новый: пропало 40% rep = ea.activate(snap, target_file=target) assert rep["activated"] is False and "АНОМАЛИЯ" in rep["reason"] # целевой файл НЕ тронут (всё ещё 1200 кодов) assert len(ea.read_codes(target)) == 2000 def test_gate_overridden_by_force(tmp_path, monkeypatch): monkeypatch.setenv("EXT_SNAPSHOT_DIR", str(tmp_path / "_ext")) tdir = tmp_path / "railway"; tdir.mkdir() target = tdir / "fgis_split_form.xlsx" _make_xlsx(target, _codes(2000)) snap = tmp_path / "snap.xlsx"; _make_xlsx(snap, _codes(2000)[:1200]) rep = ea.activate(snap, target_file=target, force=True) assert rep["activated"] is True assert len(ea.read_codes(target)) == 1200 # применилось # старый забэкаплен в снапшоты (не потерян) backups = list((tmp_path / "_ext/snapshots/fgis/activated_backup").glob("*.xlsx")) assert backups and len(ea.read_codes(backups[0])) == 2000 def test_cache_warning_without_yes(tmp_path, monkeypatch): monkeypatch.setenv("EXT_SNAPSHOT_DIR", str(tmp_path / "_ext")) tdir = tmp_path / "railway"; tdir.mkdir() (tdir / "fgis_cache.pkl").write_bytes(b"cache") snap = tmp_path / "snap.xlsx"; _make_xlsx(snap, _codes(1200)) rep = ea.activate(snap, target_file=tdir / "fgis_split_form.xlsx") assert "cache_warning" in rep and (tdir / "fgis_cache.pkl").exists() # не удалён без --yes rep2 = ea.activate(snap, target_file=tdir / "fgis_split_form.xlsx", drop_cache=True) assert rep2.get("cache_dropped") is True and not (tdir / "fgis_cache.pkl").exists()