"""기간별 발송 수량 엑셀(export.aggregate_shipped / build_period_xlsx) 테스트. DB 없이 집계·엑셀 생성만 검증한다. python -m app.modules.dispatch.tests.test_period_export 또는 pytest 로 실행 가능. """ from __future__ import annotations import io from app.modules.dispatch import export _ROWS = [ {"dispatch_date": "2026-10-02", "seller_sku": "MT-0320", "product_name": "A", "qty": 2}, {"dispatch_date": "2026-10-01", "seller_sku": "MT-0320_3", "product_name": "A", "qty": 1}, {"dispatch_date": "2026-10-01", "seller_sku": "mt-0320", "product_name": "A", "qty": 1}, {"dispatch_date": "2026-10-01", "seller_sku": "MY-0001", "product_name": "Set", "qty": 4}, {"dispatch_date": "2026-10-02", "seller_sku": "", "product_name": "", "qty": 9}, ] def test_aggregate_multiplier_and_merge(): """_N 배수 반영 + 대소문자/접미사 다른 같은 코드 합산, 빈 코드 제외(BOM 없으면 세트 그대로).""" dates, names, qty = export.aggregate_shipped(_ROWS, {"MT-0320": "상품A"}) assert dates == ["2026-10-01", "2026-10-02"], dates assert qty[("2026-10-01", "MT-0320")] == 4 # 1×3 + 1 assert qty[("2026-10-02", "MT-0320")] == 2 assert qty[("2026-10-01", "MY-0001")] == 4 assert names["MT-0320"] == "상품A" # itemcode 이름 우선 assert names["MY-0001"] == "Set" # 없으면 파싱된 이름 _BOM = {"MY-0001": [ {"component_code": "MT-0320", "component_qty": 1}, {"component_code": "mt-0550", "component_qty": 2}, ]} def test_aggregate_explodes_sets(): """세트는 BOM 으로 낱개 분해해 같은 날 같은 낱개 코드에 합산.""" dates, names, qty = export.aggregate_shipped(_ROWS, {"MT-0550": "상품B"}, _BOM) assert ("2026-10-01", "MY-0001") not in qty assert qty[("2026-10-01", "MT-0320")] == 8 # 1×3 + 1 + 세트 4×1 assert qty[("2026-10-01", "MT-0550")] == 8 # 세트 4×2 assert names["MT-0550"] == "상품B" assert export.missing_set_boms(_ROWS, _BOM) == [] assert export.missing_set_boms(_ROWS, {}) == ["MY-0001"] def test_build_period_xlsx_sheets(): from openpyxl import load_workbook data = export.build_period_xlsx( rows=_ROWS, date_from="2026-10-01", date_to="2026-10-02", name_map={}, set_bom=_BOM ) wb = load_workbook(io.BytesIO(data)) assert wb.sheetnames == ["날짜별", "아이템별 요약", "세트별 요약"] ws = wb["날짜별"] rows = list(ws.iter_rows(values_only=True)) assert rows[0] == ("발송 날짜", "아이템 코드", "상품이름", "수량") assert rows[1] == ("2026-10-01", "MT-0320", "A", 8) assert rows[2] == ("2026-10-01", "MT-0550", None, 8) assert rows[3] == ("2026-10-02", "MT-0320", "A", 2) assert rows[-1][0] == "합계" and rows[-1][3] == 18 ws2 = wb["아이템별 요약"] rows2 = list(ws2.iter_rows(values_only=True)) assert rows2[0] == ("아이템 코드", "상품이름", "2026-10-01", "2026-10-02", "합계") assert rows2[1] == ("MT-0320", "A", 8, 2, 10) assert rows2[2] == ("MT-0550", None, 8, None, 8) assert rows2[3] == ("합계", None, 16, 2, 18) # 세트는 낱개로 풀기 전 세트 단위 수량, 날짜 열은 전체 발송 날짜와 같다. rows3 = list(wb["세트별 요약"].iter_rows(values_only=True)) assert rows3[0] == ("세트 코드", "상품이름", "2026-10-01", "2026-10-02", "합계") assert rows3[1] == ("MY-0001", "Set", 4, None, 4) assert rows3[2] == ("합계", None, 4, 0, 4) def test_period_filename(): assert export.period_filename("2026-10-01", "2026-10-28") == "2026.10.01-2026.10.28_shipped_qty.xlsx" if __name__ == "__main__": test_aggregate_multiplier_and_merge() test_aggregate_explodes_sets() test_build_period_xlsx_sheets() test_period_filename() print("OK")