- Doc.put 한 곳으로 씀(save · drop · Machine._register_docs) · Doc._LOCK 안 · 임시 파일 → os.replace - 판 = 문서 한 장 저장 글 지문 · 읽기가 「판」 을 줌 · /logic/doc/save 가 받은 판과 다르면 409(화면 Save_Stale 과 같은 글) · 판 없음 = 대조 안 함(옛 부름 · 시험) - M01 줄 쌓기 [저장]이 판을 보냄(M02 사본 길은 그대로) — 기계 목록 등록이 바꾼 문서를 옛 화면이 덮지 않음 - 수식 문서 폴더가 AISLO_MASTER_DIR 사본을 따름(마스터 사본 · 문서 정본이 갈리던 것) · 마스터만 사본인 시험은 정본 문서를 안 씀 - 시험 test_854_2_m01_doc_write — 옛 판 409 · 동시 두 저장 = 하나 ok 하나 409 · 기계 등록 뒤 옛 판 409 · 정본 안 씀 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
4.9 KiB
Python
142 lines
4.9 KiB
Python
"""M01 수식 문서 쓰기 안전(PLAN 854-2 · 요구 894 걸음 1) — 잠금 · 판 대조(409) · 임시 파일 → 교체.
|
|
|
|
마스터 · 수식 문서 사본 폴더에서만 씀.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from M01_MasterData import M01_MasterData_Store as store
|
|
|
|
REAL = store.FOLDER
|
|
|
|
|
|
@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
|
|
|
|
|
|
DOC_KEY = "GF000219" # 산림 13-4-1 메쌓기(인력)
|
|
|
|
|
|
@pytest.fixture
|
|
def docs(master: Path, monkeypatch) -> Path:
|
|
from M01_MasterData import M01_MasterData_Doc as doc_screen
|
|
|
|
folder = master / "수식문서"
|
|
shutil.copytree(REAL / "수식문서", folder)
|
|
monkeypatch.setattr(doc_screen.ml, "FOLDER", folder)
|
|
doc_screen._memo.clear()
|
|
return folder
|
|
|
|
|
|
def _client():
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
from M01_MasterData import M01_MasterData_Router as router_module
|
|
|
|
app = FastAPI()
|
|
app.include_router(router_module.router)
|
|
return TestClient(app)
|
|
|
|
|
|
def _renamed(doc: dict, tail: str) -> dict:
|
|
return {**doc, "이름": f"{doc['이름']} {tail}"}
|
|
|
|
|
|
def test_옛_판으로_저장하면_409_이고_앞_저장을_덮지_않는다(docs: Path) -> None:
|
|
client = _client()
|
|
got = client.get("/api/m01/logic/doc", params={"key": DOC_KEY}).json()
|
|
assert got["판"] and not got["새"]
|
|
first = client.post(
|
|
"/api/m01/logic/doc/save",
|
|
json={"문서": _renamed(got["문서"], "sub5임시A"), "판": got["판"]},
|
|
)
|
|
assert first.status_code == 200, first.text
|
|
again = client.get("/api/m01/logic/doc", params={"key": DOC_KEY}).json()
|
|
assert again["판"] == first.json()["판"] != got["판"] # 저장 뒤 판 = 다시 읽은 판
|
|
late = client.post(
|
|
"/api/m01/logic/doc/save",
|
|
json={"문서": _renamed(got["문서"], "sub5임시B"), "판": got["판"]},
|
|
)
|
|
assert late.status_code == 409 and "그 사이 파일이 바뀜" in late.json()["detail"]
|
|
assert again["문서"]["이름"].endswith("sub5임시A")
|
|
now = client.get("/api/m01/logic/doc", params={"key": DOC_KEY}).json()
|
|
assert now["문서"]["이름"].endswith("sub5임시A") # 늦은 저장이 소리 없이 덮지 않음
|
|
assert not list(docs.glob(".*.tmp"))
|
|
|
|
|
|
def test_동시에_두_저장이면_하나만_되고_하나는_409(docs: Path, monkeypatch) -> None:
|
|
import threading
|
|
import time
|
|
|
|
from M01_MasterData import M01_MasterData_Doc as doc_screen
|
|
|
|
got = _client().get("/api/m01/logic/doc", params={"key": DOC_KEY}).json() # 화면 꼴(수 = JSON)
|
|
real = doc_screen.layered
|
|
|
|
def slow(doc, key): # 두 저장이 겹치게 — 판 확인 · 쓰기 사이를 넓힘
|
|
time.sleep(0.3)
|
|
return real(doc, key)
|
|
|
|
monkeypatch.setattr(doc_screen, "layered", slow)
|
|
plain = got["문서"]
|
|
out: list[str] = []
|
|
|
|
def one(tail: str) -> None:
|
|
try:
|
|
doc_screen.save(_renamed(plain, tail), got["판"])
|
|
out.append("ok")
|
|
except doc_screen.store.StoreError as e:
|
|
out.append(str(e.status))
|
|
|
|
runs = [threading.Thread(target=one, args=(t,)) for t in ("sub5임시A", "sub5임시B")]
|
|
for t in runs:
|
|
t.start()
|
|
for t in runs:
|
|
t.join()
|
|
assert sorted(out) == ["409", "ok"]
|
|
|
|
|
|
def test_기계_목록_등록이_바꾼_문서를_옛_판_저장이_덮지_않는다(docs: Path) -> None:
|
|
from M01_MasterData import M01_MasterData_Doc as doc_screen
|
|
from M01_MasterData import M01_MasterData_Machine as machine
|
|
|
|
key = next(
|
|
k
|
|
for k, d in doc_screen.docs().items()
|
|
if any(
|
|
v.get("이름") == "기계" and (v.get("연결") or {}).get("갈래") == "선택"
|
|
for v in d.get("변수") or []
|
|
)
|
|
)
|
|
got = _client().get("/api/m01/logic/doc", params={"key": key}).json()
|
|
machine._register_docs({key: ["sub5임시EQ"]}, set())
|
|
after = doc_screen.read(key)
|
|
assert after["판"] != got["판"]
|
|
plain = got["문서"]
|
|
with pytest.raises(doc_screen.store.StoreError) as e:
|
|
doc_screen.save(_renamed(plain, "sub5임시"), got["판"])
|
|
assert e.value.status == 409
|
|
picks = next(v for v in doc_screen.read(key)["문서"]["변수"] if v.get("이름") == "기계")
|
|
assert "sub5임시EQ" in picks["연결"]["목록"]
|
|
|
|
|
|
def test_마스터만_사본인_시험은_정본_수식_문서를_안_쓴다(master: Path) -> None:
|
|
from M01_MasterData import M01_MasterData_Doc as doc_screen
|
|
|
|
path = doc_screen.folder() / "수식_자체.json" # 정본 폴더(사본으로 안 바꿈)
|
|
raw = path.read_bytes()
|
|
doc_screen.put(path, {"그룹": "수식문서", "로직파일": "", "줄": []})
|
|
assert path.read_bytes() == raw
|