"""857-1 지킴 — 불러오기 책 한 번 · 종류 한 말 · 일위대가 시트 없는 엑셀 올림 · 「고를 재료」 판 무효 지문 · 판 셈 동안 M01 한 벌(`M01_MasterData_Store_Cache.steady`). 임시 storage 개인 층 · 실무 엑셀 견본(없는 PC = 건너뜀). """ from __future__ import annotations import json import sys from pathlib import Path import pytest HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) import helper_857_import as h # noqa: E402 def _path(name: str) -> Path: p = h.sources().get(f"엑셀:{name}") if not p or not p.exists(): pytest.skip(f"견본 없음 — {name}") return p def _opens(monkeypatch) -> list[str]: import openpyxl import xlrd seen: list[str] = [] for mod, attr in ((openpyxl, "load_workbook"), (xlrd, "open_workbook")): real = getattr(mod, attr) def wrap(*a, _real=real, _tag=attr, **k): seen.append(_tag) return _real(*a, **k) monkeypatch.setattr(mod, attr, wrap) return seen def test_종류_한_말(): from M02_MasterTemplete import M02_Hopyo_Stmate as hs from M02_MasterTemplete import M02_San_Book as sb from M02_MasterTemplete import M02_San_Workbook as wb for names, kind in ( (["일위대가표", "단가산출근거"], "STmate"), (["단가산출근거"], "STmate"), (["일위대가", "단가산출"], "EST"), (["일위대가"], None), ): assert wb.kind_of(names) == kind assert sb.kind_of(dict.fromkeys(names)) == kind assert hs.is_stmate(names) == (kind == "STmate") @pytest.mark.parametrize("name", ["봉화", "북부청사방"]) def test_올림_한_번에_책_한_번(monkeypatch, name): from M02_MasterTemplete import M02_Hopyo_Router as router path = _path(name) seen = _opens(monkeypatch) with h.personal(): row = router._upload(path.name, path.read_bytes(), {"id": 42, "이름": "시험"}) assert row["호표수"] > 0 and not row.get("읽기불가") assert len(seen) == 1, seen def test_일위대가_시트_없는_엑셀도_올라감_호표_빈_목록_단산은_보임(): from M02_MasterTemplete import M02_Hopyo_Excel as hx from M02_MasterTemplete import M02_Hopyo_Router as router from M02_MasterTemplete import M02_Hopyo_Store as hstore from M02_MasterTemplete import M02_MasterTemplete_Store as store from M02_MasterTemplete import M02_Sangeun_FromHopyo as fh path = _path("2026수목") data = path.read_bytes() with pytest.raises(hx.ExcelError): # B09 내역서 길(그대로) — 일위대가 시트 없음 = 거절 hx.read_book(path.name, data) with h.personal(): row = router._upload(path.name, data, {"id": 42, "이름": "시험"}) hp = hstore.read(store.root(), row["키"]) assert row["호표수"] == 0 and hp["호표"] == [] assert len(hp["단산"]) == 142 # 단가산출 시트 블록(둘 꼴 산근 수와 같음) assert fh.has(store.root(), row["키"]) # 가져온 식 열기 자료 보관 def test_재료판_무효_지문_M01_이_바뀌면_다시_셈(monkeypatch): from M02_MasterTemplete import M02_Hopyo_Router as router from M02_MasterTemplete import M02_MasterTemplete_Store as store from M02_MasterTemplete import M02_San_MatPick as mp from M02_MasterTemplete import M02_Sangeun_FromHopyo as fh path = _path("영덕계류") with h.personal(): key = router._upload(path.name, path.read_bytes(), {"id": 42, "이름": "시험"})["키"] first = mp.census(store.root(), key) saved = json.loads(fh._path(store.root(), key).read_bytes()) assert saved[mp.MARK] == mp._mark() and saved[mp.CENSUS] == first runs: list[int] = [] from M02_MasterTemplete import M02_Doc_FromNative as fn emit = fn.emit monkeypatch.setattr(fn, "emit", lambda *a, **k: runs.append(1) or emit(*a, **k)) assert mp.census(store.root(), key) == first and not runs # 같은 M01 = 저장본 monkeypatch.setattr(mp, "_mark", lambda: "다른 M01") assert mp.census(store.root(), key) == first and runs # 지문 다름 = 다시 셈 · 답 같음 def test_판_셈_동안_M01_파일_자국_한_번(monkeypatch): from M01_MasterData import M01_MasterData_Store_Cache as cache from M02_MasterTemplete import M02_Doc_FromNative as fn from M02_MasterTemplete import M02_Hopyo_Router as router from M02_MasterTemplete import M02_MasterTemplete_Store as store from M02_MasterTemplete import M02_San_MatPick as mp from M02_MasterTemplete import M02_Sangeun_FromHopyo as fh path = _path("영덕계류") counts: list[int] = [] real = cache._stamps monkeypatch.setattr(cache, "_stamps", lambda folder: counts.append(1) or real(folder)) with h.personal(): key = router._upload(path.name, path.read_bytes(), {"id": 42, "이름": "시험"})["키"] counts.clear() held = mp.census(store.root(), key) with_hold = len(counts) at = fh._path(store.root(), key) src = fh._excel_of(at, json.loads(at.read_bytes())) counts.clear() for no in sorted(fn.numbers(src)): # 안 쥔 셈(지금 길) — 블록마다 자국을 다시 잼 with mp.using(None): fn.emit(src, no) bare = len(counts) assert held and with_hold <= 3 < bare