# ruff: noqa: F811 — 임시 층 fixture(world)를 test_m02_labor_ref 에서 받아 씀 """836-2 M01 → M02 단가산출 자동 따라옴(요구 854) — 임시 storage + M01 메모리 사본(정본 파일 안 고침). ㉠ 노임 · 재료 · 기계 값 바뀜 = 누르지 않고 새 금액(수식 길 열기 · 옛 셈 길 목록 · 일위대가 펼침 같은 답) ㉡ 부른 M01 수식 문서 바뀜 = 따라옴 ㉢ 고친 문서 + 그 문서 M01 바뀜 = 다시 복사 + 알림(전 · 후 · 내 고침) ㉣ 고침 + 다른 항목만 바뀜 = 고침 그대로 ㉤ 바꿨다 되돌림 = 안 고침(알림 없음) ㉥ 프로젝트 층 = 그대로 ㉦ 가져온 식(우리) = 값만 · 식 그대로 · 열기 = 파일 안 고침 · 알림 [확인] = 저장. """ from __future__ import annotations import copy import shutil import pytest from M02_MasterTemplete import M02_Doc_Copy as dc from M02_MasterTemplete import M02_Doc_Sk_Path as path from M02_MasterTemplete import M02_Labor_Ref as refs from M02_MasterTemplete import M02_MasterTemplete_Store as store from M02_MasterTemplete import M02_Sangeun_Calc as calc from resources.tester.test_822_1_copy_gap import ( DIG, MACHINE, MAN, PCT, _at, _live, _move, _sk, _stack, _sum, ) from resources.tester.test_m02_labor_ref import world # noqa: F401 — 같은 임시 층 TOOL = 2 # PCT 「공구손료 및 경장비」 금액 줄 차례 def _doc_move(monkeypatch, key: str, fn) -> None: """M01 수식 문서 하나 고침(메모리 · 폴더 그대로).""" live = dc.folder() new = copy.deepcopy(live[key]) fn(new) fake = {**live, key: new} monkeypatch.setattr(dc, "folder", lambda: fake) def _rate(text: str): def fn(d: dict) -> None: d["금액"][TOOL]["단가"] = text return fn def _edit(me, key: str, text: str) -> None: got = _at(me, path.read, key) doc = copy.deepcopy(got["문서"]) doc["금액"][TOOL]["단가"] = text _at(me, path.save, key, {"문서": doc, "판": got["판"]}) def _base(me) -> str: return _sk(me, 바탕={"로직": PCT}, 단위="㎡") def _labor_unit(me, key: str): """일위대가 줄이 SK 참조를 펼치는 길(`Expand` → `calc.unit`) 1 단위 계.""" from M02_MasterTemplete.M02_Labor_Expand import Expand with store.at(me): ref = {"층": "personal", "주인": "7/42", "키": key} whole = _live() got = calc.unit(refs.read_ref(ref), whole, Expand(whole), ()) return got["기준"]["계"] if got["기준"] else None def _list_unit(me, key: str): """목록 · 옛 셈 길(`calc_doc`) 1 단위 계.""" with store.at(me): got = calc.calc_doc(store.read("sangeun", key)["문서"]) return got["1단위"] @pytest.mark.parametrize("what", ["노임", "재료", "기계"]) def test_values_follow(world, monkeypatch, what): """㉠ 값 바뀜 = 묻지 않고 새 금액 · 세 길 같은 답 · 열기 = 파일 안 고침 · 알림 없음.""" me = world["personal"] key = _sk(me) res = {"노임": MAN, "재료": next(iter(_live().index["MT"])), "기계": DIG}[what] link = MACHINE if what == "기계" else {"갈래": "요소", "키": res} part = {"노임": "노무비", "재료": "재료비", "기계": "묶음"}[what] _stack(me, key, [("자원", link, part)]) was = _sum(me, key) file = me / "sangeun" / f"{key}.json" before = file.read_bytes() _move(monkeypatch, res) got = _at(me, path.read, key) assert got["셈"]["계"] > was assert got["사본"]["자동"] is True and got["사본"]["갱신"] == [] assert not got["사본"]["단가새값"] and not got["사본"]["M01새판"] unit = _list_unit(me, key) assert unit["계"] == pytest.approx(got["셈"]["계"]) assert _labor_unit(me, key) == pytest.approx(got["셈"]["계"]) assert file.read_bytes() == before # 읽기 = 작업본 안 씀 def test_logic_follows(world, monkeypatch): """㉡ 부른 M01 수식 문서(기계 시간당) 바뀜 = 따라옴 · 알림 없음.""" me = world["personal"] key = _sk(me) _stack(me, key, [("기계", MACHINE, "묶음")]) was = _sum(me, key) def twice(d: dict) -> None: for line in d["금액"]: line["수량"] = f"({line.get('수량') or 1}) × 2" _doc_move(monkeypatch, MACHINE["키"], twice) got = _at(me, path.read, key) assert got["셈"]["계"] > was * 1.5 # 율 줄은 누계를 받아 두 배 넘음 assert got["사본"]["갱신"] == [] and not got["사본"]["문서새판"] def test_edited_item_recopied(world, monkeypatch): """㉢ 고친 문서 + 그 M01 문서 바뀜 = 고침 지우고 다시 복사 + 알림(전 · 후 · 내 고침) · [확인](저장) 뒤 알림 없음 · 따로 아님.""" me = world["personal"] key = _base(me) plain = _sum(me, key) _edit(me, key, "노무비 × 5%") assert _sum(me, key) > plain _doc_move(monkeypatch, PCT, _rate("노무비 × 4%")) got = _at(me, path.read, key) labor = got["셈"]["노무비"] assert got["셈"]["경비"] == pytest.approx(int(labor * 0.04), abs=1) assert got["문서"]["금액"][TOOL]["단가"] == "노무비 × 4%" [note] = got["사본"]["갱신"] assert note["키"] == PCT and note["갈래"] == "문서" assert any("3%" in x and "4%" in x for x in note["M01"]) assert any("3%" in x and "5%" in x for x in note["내고침"]) _at(me, path.save, key, {"할일": "갱신확인", "판": got["판"]}) after = _at(me, path.read, key) assert after["사본"]["갱신"] == [] and after["사본"]["따로"] is False assert after["셈"]["계"] == got["셈"]["계"] def test_edit_kept_when_other_item_moves(world, monkeypatch): """㉣ 고침 + 다른 항목(노임 값)만 바뀜 = 고침 그대로 · 값은 따라옴.""" me = world["personal"] key = _base(me) _edit(me, key, "노무비 × 5%") was = _at(me, path.read, key)["셈"] _move(monkeypatch, MAN) got = _at(me, path.read, key) assert got["문서"]["금액"][TOOL]["단가"] == "노무비 × 5%" assert got["사본"]["따로"] is True and got["사본"]["갱신"] == [] assert got["셈"]["노무비"] > was["노무비"] def test_reverted_is_not_edit(world, monkeypatch): """㉤ 바꿨다 되돌려 저장 = 안 고침 — M01 바뀌면 조용히 따라옴(알림 없음).""" me = world["personal"] key = _base(me) _edit(me, key, "노무비 × 5%") _edit(me, key, "노무비 × 3%") _doc_move(monkeypatch, PCT, _rate("노무비 × 4%")) got = _at(me, path.read, key) assert got["사본"]["갱신"] == [] assert got["문서"]["금액"][TOOL]["단가"] == "노무비 × 4%" def test_project_layer_keeps(world, monkeypatch, tmp_path): """㉥ 프로젝트 층 사본 = 그대로(M01 바뀌어도 금액 같음 · 고침 그대로).""" me = world["personal"] key = _base(me) _edit(me, key, "노무비 × 5%") proj = me.parent / "p836" / "templates" (proj / "sangeun").mkdir(parents=True) shutil.copy(me / "sangeun" / f"{key}.json", proj / "sangeun" / f"{key}.json") with store.at(proj): assert refs.here()[0] == "project" was = _at(proj, path.read, key) _move(monkeypatch, MAN) _doc_move(monkeypatch, PCT, _rate("노무비 × 4%")) got = _at(proj, path.read, key) assert got["셈"]["계"] == was["셈"]["계"] and got["사본"]["자동"] is False assert got["문서"]["금액"][TOOL]["단가"] == "노무비 × 5%" and got["사본"]["갱신"] == [] def test_imported_values_only(world, monkeypatch): """㉦ 가져온 식 단가기준 우리 = 이음 노임만 따라옴 · 가져온 줄 식 그대로 · 엑셀 기준 = 엑셀 값.""" from M02_MasterTemplete import M02_Sangeun_Imported as imp me = world["personal"] man = {"종류": "인력", "이름": "보통인부", "단위": "인", "수량": "0.5", "비목": "노무비"} row = { "이름": "sub2임시 가져온", "결과단위": "원/㎡", "호표": [{**man, "단가": 165545, "이음": MAN}], } key = _at(me, lambda: imp.create(row, None, "sub2임시 가져온")["이름"]) excel = _sum(me, key) doc = _at(me, lambda: store.read("sangeun", key)["문서"]) ours = _at(me, lambda: imp.to_price(doc, "우리")["문서"]) _at(me, lambda: store.write("sangeun", key, store.read("sangeun", key)["판"], ours)) was = _at(me, path.read, key) _move(monkeypatch, MAN) got = _at(me, path.read, key) assert got["셈"]["계"] == pytest.approx(was["셈"]["계"] * 2, abs=2) assert got["문서"]["금액"] == was["문서"]["금액"] and got["사본"]["갱신"] == [] back = _at(me, lambda: imp.to_price(store.read("sangeun", key)["문서"], "엑셀")["문서"]) _at(me, lambda: store.write("sangeun", key, store.read("sangeun", key)["판"], back)) assert _sum(me, key) == excel def _cover(me, key: str, cover: dict) -> None: got = _at(me, path.read, key) doc = {**copy.deepcopy(got["문서"]), "기계덮음": cover} _at(me, path.save, key, {"문서": doc, "판": got["판"]}) def test_machine_cover_kept_when_machine_moves(world, monkeypatch): """㉢ 기계 덮음(836-1) + 그 칸 M01 바뀜 = 덮음 지킴(848-2 칸 단위) + 알림(전 · 후 · 내 값).""" me = world["personal"] key = _sk(me) _stack(me, key, [("기계", MACHINE, "묶음")]) no = MACHINE["입력"]["기계"].strip("'") _cover(me, key, {no: {"값": {"가격": 150000}}}) assert _at(me, path.read, key)["문서"]["기계덮음"] _move(monkeypatch, DIG) got = _at(me, path.read, key) assert got["문서"]["기계덮음"] == {no: {"값": {"가격": 150000}}} [note] = got["사본"]["갱신"] assert note["키"] == DIG and note["갈래"] == "기계" and note["처리"] == "지킴" assert any(x.startswith("가격 ") for x in note["M01"]) and note["내고침"] == ["가격 150000"] def test_machine_cover_kept_other_item(world, monkeypatch): """㉣ 기계 덮음 + 다른 항목(바탕 문서)만 바뀜 = 덮음 그대로(문서 다시 복사 뒤에도).""" me = world["personal"] key = _base(me) _stack(me, key, [("기계", MACHINE, "묶음")]) no = MACHINE["입력"]["기계"].strip("'") _cover(me, key, {no: {"값": {"가격": 150000}}}) _doc_move(monkeypatch, PCT, _rate("노무비 × 4%")) got = _at(me, path.read, key) assert got["문서"]["기계덮음"] == {no: {"값": {"가격": 150000}}} assert [n["갈래"] for n in got["사본"]["갱신"]] == ["문서"] # 기계 줄 붙인 문서 고침