"""M01 재료 기본 검색어 · 고름 · 단위 환산 · 단위 경고 — PLAN 8-1 · 8-2 계약. 재료 줄 단가 = `master_pick.priced`(문서 셈 · M02 가 같이 씀) · 기계 입력 · 품목 칸 = 수식 문서 셈. 옛 M01 `/calc` 길(견본 로직 줄 `row`)과 엔진은 855-E 2-E4 에서 걷음. 정본 폴더는 읽기만. """ from __future__ import annotations from decimal import Decimal import helper_855_engine as h import pytest from fastapi import FastAPI from fastapi.testclient import TestClient from M01_MasterData import M01_MasterData_Router as router_module from M01_MasterData import M01_MasterData_Store as store from M02_MasterTemplete import M02_Doc_Copy as dc mf = store.cm.mf mp = mf.mp @pytest.fixture(scope="module") def client() -> TestClient: app = FastAPI() app.include_router(router_module.router) return TestClient(app) def _line(term: str, unit: str, picks: dict | None = None) -> dict: """검색어 재료 줄 하나(수량 1) — {단가, 출처, 까닭, 단위경고?, 품목?}.""" token = mp.CHOSEN.set(picks or {}) try: price, source, why, extra = mp.priced( store.loaded()[1], {"단위": unit}, {"검색어": term}, {}, 0 ) finally: mp.CHOSEN.reset(token) return {"단가": price, "출처": source or "", "까닭": why, **extra} def _auto(key: str) -> dict: whole = h.world()[0] return dc.auto_of(dc.doc_of(whole, key), whole) def test_검색어로_찾고_지역_낱말이_맞는_줄이_먼저() -> None: seoul = _line("합판(내수) 12t 서울", "㎡") assert seoul["출처"].startswith("MT000018") and seoul["단가"] == 11354 # 값 열 가운데 낮은 값 busan = _line("합판(내수) 12t 부산", "㎡") assert busan["출처"].startswith("MT000020") and busan["단가"] == 11488 assert "단위경고" not in seoul def test_고름은_그_줄만_그_품목으로_셈() -> None: got = _line("합판(내수) 12t 서울", "㎡", {"0": "MT000019"}) assert got["출처"].startswith("MT000019") and got["단가"] == 11556 assert _line("합판(내수) 12t 서울", "㎡")["단가"] == 11354 # 요청마다 — 저장 안 함 def test_같은_성격_단위는_줄_단위로_환산() -> None: got = _line("어닐링철선 서울 4.0mm", "톤") # 자재 kg → 줄 톤 assert got["단가"] == 1610000 and "단위경고" not in got got = _line("어닐링철선 서울 4.0mm", "g") assert got["단가"] == pytest.approx(Decimal("1.61")) def test_성격이_다르면_계산은_하고_단위경고() -> None: got = _line("어닐링철선 서울 4.0mm", "m") assert got["단가"] == 1610 and got["단위경고"] == "줄 m · 자재 kg" def test_검색어_후보가_없으면_그_줄만_비움() -> None: got = _line("없는물건zzz", "개") assert got["단가"] is None and "후보 없음" in got["까닭"] def test_기계_입력은_목록_밖_원문번호도_받음() -> None: key = "GC000267" inputs = {**_auto(key), "기계": "0101-0007"} # 고르기 목록 밖 · 기계 마스터엔 있음 assert h.calc(key, inputs)["셈됨"] assert not h.calc(key, {**inputs, "기계": "9999-9999"})["셈됨"] # 마스터에 없는 번호 = 막힘 def test_단위_환산_표() -> None: assert mp.convert(Decimal(1610), "kg", "톤") == 1610000 assert mp.convert(Decimal(5), "Ton", "kg") == Decimal("0.005") assert mp.convert(Decimal(100), "㎡", "㎠") == Decimal("0.01") assert mp.convert(Decimal(7), "kg", "m") == 7 # 성격이 다르면 그대로 assert mp.same_kind("ℓ", "㎥") and not mp.same_kind("kg", "m") def test_check_master_검색어_후보0_단위경고() -> None: whole = store.loaded()[1] assert mf.check_pick(whole, "줄", {"검색어": "합판(내수) 12t 서울"}) == [] assert "후보 0" in mf.check_pick(whole, "줄", {"검색어": "없는물건zzz"})[0] assert mf.check_unit(whole, "줄", {"검색어": "어닐링철선 서울 4.0mm"}, "kg") == [] assert "성격이 다름" in mf.check_unit(whole, "줄", {"검색어": "어닐링철선 서울 4.0mm"}, "m")[0] def test_기계_입력은_EQ_키도_원문번호로_풀어_받음() -> None: key = "GC000267" number = "0101-0007" # 고르기 목록 밖 eq = next(k for k, r in store.loaded()[1].index["EQ"].items() if r.get("원문번호") == number) base = _auto(key) by_number = h.calc(key, {**base, "기계": number}) by_key = h.calc(key, {**base, "기계": eq}) assert by_key["셈됨"], by_key assert [by_key[k] for k in h.KEYS] == [by_number[k] for k in h.KEYS] # EQ 키 = 원문번호 assert not h.calc(key, {**base, "기계": "EQ999999"})["셈됨"] def test_재료_줄마다_품목_칸() -> None: got = h.traced("GF000160") first = next(x["품목"] for x in got["줄"] if (x.get("품목") or {}).get("이름") == "합판(내수)") assert mf.mp.mm.mmerge.where(first) == "서울" and first["키"].startswith("MT") # 830-10 지역 칸 assert {"키", "이름", "규격", "단위"} <= set(first) def test_목록_검색은_띄어쓰기를_무시(client) -> None: for q in ("함석", "골 함 석", "골함석"): got = client.get("/api/m01/rows", params={"file": "재료_자재품목.json", "q": q}).json() assert any(r["이름"] == "골 함 석" for r in got["rows"]), q