Files
Aislo/resources/tester/test_854_2_m01_key_book.py
T
eomsangdonandClaude Opus 5.5 7e6190f8d3 refactor(855-E L3ⓐ): M01 기초 저장 · 검사 · 막힘 세기 = 수식 문서 기준 · 로직 줄 쓰기 = Store_Logic.mirror 한 자리
앞 세션 auto 커밋 cce57c298(Store_Logic 새 길 · /logic 답 머리만 · 판 = 문서 파일 지문 · 개정 = 문서 머리 대조 · 별칭 · 기계 등록 · [신규] · [삭제])에 이어
- Store_Cache: 막힘 세기(blocks · counted)를 문서 파일마다 · 저장 검사 logic_errors = 키 겹침 · 대장(요소 + 문서 키) · 연결 · 문서 막힘(check_logics 로직 줄 검사 걷음) · _docs_of = 문서 전부 · narrow 로직 갈래 걷음 · check_saved(부름 0) 지움
- Store_Save: 기초(로직 파일) 받지 않음(400) · gone · labor_errors(whole) 갈래 걷음
- Store_Logic: 정본 파일에도 새 기초(그 파일 표 키 — 옛 /save 와 같음) · 지움 검사에 조합 · 지운 기초를 부르던 문서 더함(옛 422 그대로) · 쓰는 곳 409 는 Doc.remove 로(옛 자리)
- 시험(옛 /save 로 로직 줄 셈 칸 고침 · /logic 답 셈 칸을 붙든 것 = 죽은 길): 새 test_855_l3_doc_only(로직 파일 없음 + 같이 쓰기 끔 = 켬과 답 같음 · 목록 · 상세 · 저장 · 개정 409 · 판 409 · 별칭 · 신규 · 복사 · 삭제 · 기계 등록)
  · test_m01_cache 셈 칸 /save 검사 셋 · test_794_1 식 고침 /save 422 걷음(셈 칸 고침 = 문서 저장 몫 · /save 는 머리만 씀)
  · test_m01_make 「시험 계산에 돈다」 → 빈 문서로 열림 · test_m01_api · test_m01_text 옛 셈 길 시험은 로직 줄 사본에서 읽음
  · test_854_2_m01_key_book 대장 끊김 = 인력 줄 + 기초 문서 쓰기 · test_794_1b · test_804_7 판 = 문서 파일 · test_802_3 문서 없는 키 = 404(빈 문서 안 지음)
  · test_826_2 좁힌 검사 = 판정만 견줌(기초 흠 글이 문서 꼴) · 로직 갈래 셋 뺌 · test_m01_copy 옛 시트 길 걷고 master_copy 왕복만 · test_854_2_m01_copy_doc 식 복사 → 기본정보 고친 복사
  · test_713_1 · test_796_1 row_print 줄 뺌(row_print 지움)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 08:37:53 +09:00

118 lines
4.8 KiB
Python

"""M01 저장 안전(PLAN 854-2 · 요구 894 걸음 1) — 마스터 사본 폴더에서만 씀.
- 키 대장 · 파일 쓰기: 중간에 끊겨도 대장 · 파일이 저장 전 그대로(대장만 앞서가지 않음).
"""
from __future__ import annotations
import json
import shutil
from pathlib import Path
import pytest
from M01_MasterData import M01_MasterData_Store as store
from M01_MasterData import M01_MasterData_Store_Save as save_mod
REAL = store.FOLDER
LABOR_FILE = "인력.json"
MACHINE = "EQ000002"
LABOR = "LB000002" # 보통인부
@pytest.fixture
def master(tmp_path: Path, monkeypatch) -> Path:
for path in REAL.glob("*.json"):
if not path.name.startswith("_") or path.name == store.mk.BOOK.name:
shutil.copy(path, tmp_path / path.name)
shutil.copytree(REAL / "기계", tmp_path / "기계")
monkeypatch.setattr(store, "FOLDER", tmp_path)
return tmp_path
def _bytes(folder: Path) -> dict[str, bytes]:
out = {p.name: p.read_bytes() for p in folder.glob("*.json")}
out |= {f"기계/{p.name}": p.read_bytes() for p in (folder / "기계").glob("*.json")}
return out
def _new_labor() -> dict:
"""새 인력 줄 — LB 키 발급 = 대장 바뀜(기초 저장은 `Store_Logic` 길 · 855 L3 ⓐ)."""
rows = json.loads((store.FOLDER / LABOR_FILE).read_text(encoding="utf-8"))["줄"]
seed = next(r for r in rows if r["키"] == LABOR)
row = {k: v for k, v in seed.items() if k != "키"}
return {
"op": "add",
"row": {**row, "원문번호": "9999", "이름": "sub7임시 인부", "출처": "자체"},
}
def _batch() -> list[dict]:
"""기계 한 대 고침(기계 폴더) + 인력 새 줄(LB 키 발급 = 대장 바뀜)."""
machine = json.loads((store.FOLDER / "기계" / f"{MACHINE}.json").read_text(encoding="utf-8"))
machine["비고"] = "sub5임시"
return [
{
"file": "기계.json",
"version": store.version("기계.json"),
"changes": [{"op": "edit", "key": MACHINE, "row": machine}],
},
{"file": LABOR_FILE, "version": store.version(LABOR_FILE), "changes": [_new_labor()]},
]
@pytest.mark.parametrize("fail_at", [0, 1])
def test_파일_쓰기가_끊기면_대장과_파일이_저장_전_그대로(
master: Path, monkeypatch, fail_at
) -> None:
before = _bytes(master)
real = save_mod._write
calls = []
def broken(name, was, now):
calls.append(name)
if len(calls) - 1 == fail_at:
if fail_at: # 둘째 파일은 쓴 뒤에 끊김(반쯤 쓴 꼴)
real(name, was, now)
raise OSError("디스크 흉내 실패")
return real(name, was, now)
monkeypatch.setattr(save_mod, "_write", broken)
with pytest.raises(OSError):
store.save(_batch())
after = _bytes(master)
assert after == before # 대장 · 기계 · 인력 파일 모두 저장 전 글 그대로
assert not list(master.glob(".*.tmp")) and not list((master / "기계").glob(".*.tmp"))
def test_끊김_없으면_대장과_파일이_같이_간다(master: Path) -> None:
book_before = store.mk.load_book(master / store.mk.BOOK.name)["다음"].get("LB", 1)
store.save(_batch())
book = store.mk.load_book(master / store.mk.BOOK.name)
keys = [r["키"] for r in json.loads((master / LABOR_FILE).read_text(encoding="utf-8"))["줄"]]
assert f"LB{book_before:06d}" in keys and book["다음"]["LB"] == book_before + 1
machine = json.loads((master / "기계" / f"{MACHINE}.json").read_text(encoding="utf-8"))
assert machine["비고"] == "sub5임시"
def test_기초_문서_쓰기가_끊기면_대장과_문서가_저장_전_그대로(master: Path, monkeypatch) -> None:
"""855 L3 ⓐ — 새 기초 = 대장 GX 발급 → 자체 문서 파일 · 문서 쓰기가 끊기면 둘 다 저장 전 글."""
from M01_MasterData import M01_MasterData_Doc as doc_store
from M01_MasterData import M01_MasterData_Store_Logic as logic_store
shutil.copytree(REAL / "수식문서", master / "수식문서")
monkeypatch.setattr(doc_store.ml, "FOLDER", master / "수식문서")
doc_store._memo.clear()
before = _bytes(master) | {p.name: p.read_bytes() for p in (master / "수식문서").glob("*.json")}
def broken(path, data):
raise OSError("디스크 흉내 실패")
monkeypatch.setattr(doc_store, "put", broken)
row = {"키": "", "이름": "sub7임시 기초", "원문번호": "", "결과단위": "원", "출처": "자체"}
part = {"file": "로직_자체.json", "version": doc_store.file_version("로직_자체.json")}
with pytest.raises(OSError):
logic_store.save([{**part, "changes": [{"op": "add", "row": row}]}])
after = _bytes(master) | {p.name: p.read_bytes() for p in (master / "수식문서").glob("*.json")}
assert after == before