"""M01 [로직 복사/생성] 이 수식 문서도 같이 지음(PLAN 854-2 · 요구 896) — 사본 폴더에서만. - 만들면 문서 있음 · 금액 = 본뜬 기초 문서 금액. - 지우면(`/logic/remove` · `/save` 지움) 문서도 없음 · [복사/생성] = 원본 문서 복제 + 기본정보(855 L3 ⓐ). """ from __future__ import annotations import shutil from decimal import Decimal from pathlib import Path import pytest from fastapi import FastAPI from fastapi.testclient import TestClient from M01_MasterData import M01_MasterData_Doc as doc_screen from M01_MasterData import M01_MasterData_Router as router_module from M01_MasterData import M01_MasterData_Store as store from M01_MasterData import M01_MasterData_Store_Make as make REAL = store.FOLDER SAMPLE = "GF000219" # 산림 13-4-1 메쌓기(인력) COSTS = ("노무비", "재료비", "경비", "계") @pytest.fixture def client(tmp_path: Path, monkeypatch) -> TestClient: 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 / "기계") shutil.copytree(REAL / "수식문서", tmp_path / "수식문서") monkeypatch.setattr(store, "FOLDER", tmp_path) monkeypatch.setattr(doc_screen.ml, "FOLDER", tmp_path / "수식문서") doc_screen._memo.clear() app = FastAPI() app.include_router(router_module.router) return TestClient(app) def _get(client: TestClient, url: str, **params) -> dict: res = client.get(url, params=params) assert res.status_code == 200, res.text return res.json() def _post(client: TestClient, url: str, body: dict) -> dict: res = client.post(url, json=body) assert res.status_code == 200, res.text return res.json() def _money(answer: dict) -> dict: return {k: Decimal(str(answer[k])) for k in COSTS} def test_복사로_만든_로직은_수식_문서가_있고_금액이_본뜬_기초와_같다(client: TestClient) -> None: made = _post(client, "/api/m01/logic/copy", {"key": SAMPLE, "이름": "sub5임시 본뜸"}) key = made["key"] assert key.startswith("GX") and made["수식문서"] is True got = _get(client, "/api/m01/logic/doc", key=key) assert got["새"] is False and got["파일"] == "수식_자체.json" and got["문서"]["식"] assert got["셈"]["셈됨"] is True source = _get(client, "/api/m01/logic/doc", key=SAMPLE) assert _money(got["셈"]) == _money(source["셈"]) and _money(source["셈"])["계"] > 0 # 지우면 문서도 같이 info = _get(client, "/api/m01/logic", key=key) _post( client, "/api/m01/logic/remove", {"key": key, "file": info["file"], "version": info["version"]}, ) assert _get(client, "/api/m01/logic/doc", key=SAMPLE)["새"] is False assert key not in doc_screen.docs() def test_기본정보를_고쳐_복사해도_문서가_있고_save_지움이면_문서도_없다(client: TestClient) -> None: more = {"이름": "sub7임시 복사", "원문번호": "13-4-9", "결과단위": "m3", "출처": "자체 시험"} made = _post(client, "/api/m01/logic/copy", {"key": SAMPLE, **more}) # 855 L3 ⓐ [복사/생성] key = made["key"] assert made["수식문서"] is True and key in doc_screen.docs() source = _money(_get(client, "/api/m01/logic/doc", key=SAMPLE)["셈"]) assert _money(_get(client, "/api/m01/logic/doc", key=key)["셈"]) == source head = _get(client, "/api/m01/logic", key=key)["logic"] assert {k: head[k] for k in more} == more and head["키"] == key body = {"file": make.OWN, "version": made["version"], "changes": [{"op": "delete", "key": key}]} _post(client, "/api/m01/save", {"files": [body]}) assert key not in doc_screen.docs()