Files
dbx-main/tests/test_malaysia_stock.py
king c7cc94a09c fix: 2026-10-15 코드 검토 F01~F10 — 수집 실패 전파·트랜잭션 경계·저장 실패·공지 버전/확인 조건·테스트 체계
- F01 쿠팡: HTTP 200 비JSON·data 목록 없음은 수집 실패(기간 교체 안 함), 정상 빈 목록과 구분
- F02 회의록 [적용]: 업무 INSERT 와 줄 연결을 한 연결·한 트랜잭션(_insert_task), 확인은 잠그기 전·알림/메일은 커밋 뒤, 줄 바뀜 409
- F03 업무 등록 표: persist 실패 전파, 미저장이면 [적용] 중단, 초안 유지·다시 저장·이탈 경고
- F04 인증정보 변경 시 내부 토큰 삭제 + 네이버·토스 토큰을 발급 인증정보 지문(SHA-256)에 묶음, GS샵 방식 캐시도 키 지문 기준
- F05 쪽 수 상한·커서 반복 = 실패(쿠팡은 구간을 하루까지 나눠 재시도), 네이버·카카오·토스·GS샵 동일 기준
- F06 [중지] 확인과 저장 시작을 같은 잠금(begin_saving), 저장 시작 뒤 중지 불가
- F07/F08 긴급 공지: 내용 version·고친 공지 다시 그리기·확인에 version(409), 확인은 행 잠금 트랜잭션에서 수신자·표시 시각·취소 검증
- F09 쇼핑몰 연결 테스트도 clear 처리·저장 안 한 시험은 상태 유지(광고 테스트와 같은 계약)
- F10 DB 테스트 skip·가짜 app.main/time.sleep 파일 범위 fixture, 오래된 기대값 갱신, pytest.ini·requirements-dev·tests/js/package.json·docs/TESTING.md
- 결과·후속 과제: docs/CODE_REVIEW_2026-10-15_RESULT.md (마이그레이션·환경변수 변경 없음)

Python 440 passed/63 skipped(DB 없음), 503 passed(로컬 PostgreSQL), jsdom 7종·Edge 6종 통과

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 18:41:42 +09:00

248 lines
11 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""말레이시아 재고관리 — 순수 로직 테스트 (DB 불필요).
실행:
cd /opt/www/main (또는 개발 PC 프로젝트 루트)
python -m pytest tests/test_malaysia_stock.py -v
pytest 미설치 시:
python tests/test_malaysia_stock.py # __main__ 폴백으로 assert 실행
"""
from __future__ import annotations
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
try:
import pytest
except ModuleNotFoundError: # pytest 미설치 → 최소 shim 으로 standalone 실행 지원
class _PytestShim:
class mark:
@staticmethod
def parametrize(argnames, argvalues):
def deco(fn):
fn._params = (argnames, list(argvalues))
return fn
return deco
class raises:
def __init__(self, exc):
self.exc = exc
def __enter__(self):
return self
def __exit__(self, etype, evalue, tb):
if etype is None:
raise AssertionError(f"{self.exc.__name__} 가 발생하지 않음")
return issubclass(etype, self.exc)
pytest = _PytestShim() # type: ignore
from app.modules.malaysia import store
# ────────────────────────────────────────────────────────────
# 코드 분류
# ────────────────────────────────────────────────────────────
def test_classify_code():
assert store.classify_code("MT-0320") == "individual"
assert store.classify_code("MX-0001") == "individual"
assert store.classify_code("MZ-0002") == "individual"
assert store.classify_code("MY-0002") == "set"
assert store.classify_code("MD-0000") == "blocked"
assert store.classify_code("ZZ-9999") == "unknown"
# 소문자/공백 정규화
assert store.classify_code(" mt-0320 ") == "individual"
# ────────────────────────────────────────────────────────────
# movement 코드 검증 — MT/MX/MZ 허용, MY/MD 차단
# ────────────────────────────────────────────────────────────
def test_validate_movement_code_ok():
assert store.validate_movement_code("mt-0320") == "MT-0320"
@pytest.mark.parametrize("bad", ["MY-0002", "MD-0000", "ZZ-1", ""])
def test_validate_movement_code_blocks(bad):
with pytest.raises(ValueError):
store.validate_movement_code(bad)
# ────────────────────────────────────────────────────────────
# 재고조사 코드 검증 — MT/MX/MZ/MY 허용, MD 차단
# ────────────────────────────────────────────────────────────
def test_validate_stocktake_code():
assert store.validate_stocktake_code("MY-0002") == "MY-0002"
assert store.validate_stocktake_code("MT-0320") == "MT-0320"
with pytest.raises(ValueError):
store.validate_stocktake_code("MD-0001")
# ────────────────────────────────────────────────────────────
# set_bom 검증 — set 은 MY-, component 는 MT/MX/MZ (중첩/MD 금지)
# ────────────────────────────────────────────────────────────
def test_validate_bom():
assert store.validate_bom_set_code("MY-0001") == "MY-0001"
assert store.validate_bom_component_code("MT-0320") == "MT-0320"
with pytest.raises(ValueError):
store.validate_bom_set_code("MT-0320") # set 자리에 낱개
with pytest.raises(ValueError):
store.validate_bom_component_code("MY-0002") # 세트 중첩 금지
with pytest.raises(ValueError):
store.validate_bom_component_code("MD-0000") # 뚜껑 금지
def test_validate_qty():
assert store.validate_qty(5) == 5
assert store.validate_qty(0) == 0
assert store.validate_qty(-3, allow_negative=True) == -3
with pytest.raises(ValueError):
store.validate_qty(-1)
with pytest.raises(ValueError):
store.validate_qty(0, allow_zero=False)
# ────────────────────────────────────────────────────────────
# 세트 분해
# ────────────────────────────────────────────────────────────
def _bom_map():
# MY-0002 Starter Set = MT-0320 x1 + MT-0550 x2
return {
"MY-0002": [
{"component_code": "MT-0320", "component_qty": 1},
{"component_code": "MT-0550", "component_qty": 2},
],
"MY-0003": [
{"component_code": "MT-0320", "component_qty": 1},
],
}
def test_explode_set():
out = store.explode_set("MY-0002", 20, _bom_map())
assert out == {"MT-0320": 20, "MT-0550": 40}
def test_explode_set_missing_returns_empty():
assert store.explode_set("MY-9999", 5, _bom_map()) == {}
# ────────────────────────────────────────────────────────────
# 재고조사 집계 — 요구사항 예시 검증
# MT-0320 낱개 100 + MY-0002 20세트(MT-0320 1개 포함) → 120
# ────────────────────────────────────────────────────────────
def test_explode_stocktake_example():
lines = [
{"sku_code": "MT-0320", "qty": 100},
{"sku_code": "MY-0002", "qty": 20},
]
res = store.explode_stocktake(lines, _bom_map())
assert res["MT-0320"]["direct_qty"] == 100
assert res["MT-0320"]["from_set_qty"] == 20 # 20세트 x 1
assert res["MT-0320"]["total_qty"] == 120
# MT-0550 은 낱개 직접 입력 없음, 세트에서만 40
assert res["MT-0550"]["direct_qty"] == 0
assert res["MT-0550"]["from_set_qty"] == 40
assert res["MT-0550"]["total_qty"] == 40
def test_explode_stocktake_multiple_sets_same_component():
# MY-0002(MT-0320 x1) 10세트 + MY-0003(MT-0320 x1) 5세트 → from_set 15
lines = [
{"sku_code": "MT-0320", "qty": 0},
{"sku_code": "MY-0002", "qty": 10},
{"sku_code": "MY-0003", "qty": 5},
]
res = store.explode_stocktake(lines, _bom_map())
assert res["MT-0320"]["from_set_qty"] == 15
assert res["MT-0320"]["total_qty"] == 15
def test_missing_bom_sets():
lines = [
{"sku_code": "MY-0002", "qty": 1}, # BOM 있음
{"sku_code": "MY-0005", "qty": 1}, # BOM 없음 → 경고
]
missing = store.missing_bom_sets(lines, _bom_map())
assert missing == ["MY-0005"]
# ────────────────────────────────────────────────────────────
# 랙 입력 — 레이아웃 / 집계
# ────────────────────────────────────────────────────────────
def test_rack_cell_codes_count_and_format():
codes = store.rack_cell_codes()
# A/B 2면 × 3행 × 3열 × 2분할 = 36칸 + Side B 아래 바닥(Ground) 파렛트 6칸(G1~G6) = 42칸
# (Ground 추가 후 기대값 갱신 — 2026-10-15 검토 F10)
assert len(codes) == 42
assert len(set(codes)) == 42
assert "A3-1-1" in codes
assert "B1-3-2" in codes
# 그림과 동일: 첫 칸은 A면 3행 1열 1분할, 랙 36칸 뒤에 바닥 G1~G6
assert codes[0] == "A3-1-1"
assert codes[36:] == ["G1", "G2", "G3", "G4", "G5", "G6"]
def test_rack_layout_structure():
layout = store.rack_layout()
assert [s["side"] for s in layout] == ["A", "B", "Ground"]
side_a = layout[0]
assert [r["row"] for r in side_a["rows"]] == [3, 2, 1]
# 각 행 3열, 각 열 2분할
first_row = side_a["rows"][0]
assert len(first_row["cols"]) == 3
assert first_row["cols"][0] == ["A3-1-1", "A3-1-2"]
# 바닥(Ground) — 한 줄에 분할 없는 파렛트 6칸
ground = layout[2]
assert len(ground["rows"]) == 1 and ground["rows"][0]["cols"] == [["G1"], ["G2"], ["G3"], ["G4"], ["G5"], ["G6"]]
def test_aggregate_rack_sums_units_times_boxes():
entries = [
{"sku_code": "MT-0320", "units_per_box": 24, "box_count": 3}, # 72
{"sku_code": "MT-0320", "units_per_box": 24, "box_count": 1}, # +24 (다른 칸)
{"sku_code": "MY-0002", "units_per_box": 6, "box_count": 2}, # 12 (콤보도 집계)
]
out = store.aggregate_rack(entries)
assert out == {"MT-0320": 96, "MY-0002": 12}
def test_aggregate_rack_skips_incomplete():
entries = [
{"sku_code": "", "units_per_box": 10, "box_count": 2}, # 코드 없음
{"sku_code": "MT-0550", "units_per_box": 0, "box_count": 5}, # 입수량 0
{"sku_code": "MT-0750", "units_per_box": 5, "box_count": 0}, # 박스 0
{"sku_code": "MT-0950", "units_per_box": "x", "box_count": 2}, # 정수 아님
]
assert store.aggregate_rack(entries) == {}
# pytest 미설치 환경 폴백
if __name__ == "__main__":
import traceback
fns = [v for k, v in sorted(globals().items()) if k.startswith("test_") and callable(v)]
total = 0
failed = 0
for fn in fns:
params = getattr(fn, "_params", None)
cases: list[tuple] = [()]
if params:
argnames, argvalues = params
cases = [(v if isinstance(v, tuple) else (v,)) for v in argvalues]
for case in cases:
total += 1
label = f"{fn.__name__}{case if case else ''}"
try:
fn(*case)
print(f"PASS: {label}")
except Exception: # noqa: BLE001
failed += 1
print(f"FAIL: {label}")
traceback.print_exc()
print(f"\n{total} tests, {failed} failed")
sys.exit(1 if failed else 0)