f02742497a
- cupang_products.weight_g (마이그레이션 007, NULL = 미입력, 0~1,000,000 CHECK) - 초기 무게 39건 시드(cupang_products_weight_seed.sql) — 무게가 비어 있는 행만 채움, 실행 전 값 백업 - 수기 추가에 포장무게(g) 칸(1,070·1070g 허용, 비우면 기존 무게 유지), 수정 팝업에서 수정·지우기 - 등록 목록에 포장무게 열(1,070g, 숫자 정렬, 무게 없는 줄은 맨 뒤) - 007 미적용이면 무게 칸·열을 숨기고 저장 SQL 에서도 뺌(기존 동작 그대로) - tests/test_cupang_weight.py: 입력 읽기 + 무게 칸 유무별 등록·수정 SQL Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
4.1 KiB
Python
124 lines
4.1 KiB
Python
"""쿠팡 밀크런 — 제품 포장무게(g) (마이그레이션 007).
|
|
|
|
DB 없이 돈다: 입력 읽기(parse_weight_g)와, 가짜 연결로 등록·수정 SQL 이 무게 칸 유무에 맞게
|
|
만들어지는지(열·자리표시자·값 개수) 확인한다.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from app.modules.cupang.db import CupangDBStore
|
|
from app.modules.cupang.store import parse_weight_g
|
|
|
|
|
|
def test_parse_weight_g():
|
|
assert parse_weight_g("1,070") == 1070
|
|
assert parse_weight_g(" 1,070 ") == 1070
|
|
assert parse_weight_g("1070g") == 1070
|
|
assert parse_weight_g("1070 G") == 1070
|
|
assert parse_weight_g("1070.0") == 1070
|
|
assert parse_weight_g(450) == 450
|
|
assert parse_weight_g("0") == 0
|
|
for empty in ("", " ", None):
|
|
assert parse_weight_g(empty) is None
|
|
for bad in ("abc", "-5", "0.5", "1,000,001", "nan"):
|
|
with pytest.raises(ValueError):
|
|
parse_weight_g(bad)
|
|
|
|
|
|
class _Cur:
|
|
def __init__(self, row):
|
|
self._row = row
|
|
self.rowcount = 1
|
|
|
|
def fetchone(self):
|
|
return self._row
|
|
|
|
|
|
class _Conn:
|
|
def __init__(self, log, row):
|
|
self.log, self.row = log, row
|
|
|
|
def execute(self, sql, params=None):
|
|
self.log.append((sql, params))
|
|
if "information_schema" in sql:
|
|
return _Cur(None)
|
|
if sql.lstrip().startswith("SELECT 1 FROM cupang"):
|
|
return _Cur(None) # 제품코드 중복·상자 규칙 없음
|
|
return _Cur(dict(self.row))
|
|
|
|
def transaction(self):
|
|
return self
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *a):
|
|
return False
|
|
|
|
|
|
class _Pool:
|
|
def __init__(self, row):
|
|
self.log: list[tuple[str, Any]] = []
|
|
self.row = row
|
|
|
|
def connection(self):
|
|
return _Conn(self.log, self.row)
|
|
|
|
|
|
ROW = {"id": 7, "product_code": "MS-9809", "product_name": "미라클통", "coupang_item_code": "",
|
|
"active": True, "sort_order": 0, "weight_g": 1070}
|
|
|
|
|
|
def _store(weight: bool) -> tuple[CupangDBStore, _Pool]:
|
|
st = CupangDBStore.__new__(CupangDBStore)
|
|
pool = _Pool(ROW if weight else {k: v for k, v in ROW.items() if k != "weight_g"})
|
|
st._pool = pool
|
|
st._weight_col = weight or False
|
|
return st, pool
|
|
|
|
|
|
def _main_sql(pool: _Pool, verb: str) -> tuple[str, Any]:
|
|
return next((q, p) for q, p in pool.log if q.lstrip().startswith(verb))
|
|
|
|
|
|
def test_upsert_with_weight():
|
|
st, pool = _store(True)
|
|
out = st.upsert_product(product_code="MS-9809", product_name="미라클통", weight_g=1070)
|
|
sql, params = _main_sql(pool, "INSERT")
|
|
assert "weight_g)" in sql and "%(w)s)" in sql
|
|
assert "weight_g = COALESCE(%(w)s, cupang_products.weight_g)" in sql # 비워 두면 기존 무게 유지
|
|
assert params["w"] == 1070
|
|
assert set(re.findall(r"%\((\w+)\)s", sql)) <= set(params)
|
|
assert out["weight_g"] == 1070
|
|
|
|
|
|
def test_upsert_without_column_ignores_weight():
|
|
st, pool = _store(False)
|
|
out = st.upsert_product(product_code="MS-9809", product_name="미라클통", weight_g=1070)
|
|
sql, _ = _main_sql(pool, "INSERT")
|
|
assert "weight_g" not in sql
|
|
assert out["weight_g"] is None
|
|
|
|
|
|
def test_update_weight_set_clear_keep():
|
|
for weight_arg, expect_col, expect_val in ((950, True, 950), (None, True, None), (..., False, None)):
|
|
st, pool = _store(True)
|
|
st.update_product(product_id=7, product_name="미라클통", product_code="MS-9809",
|
|
weight_g=weight_arg)
|
|
sql, params = _main_sql(pool, "UPDATE cupang_products")
|
|
assert ("weight_g = %s" in sql) is expect_col, weight_arg
|
|
assert sql.count("%s") == len(params) # 자리표시자 = 값 개수
|
|
if expect_col:
|
|
assert params[-2] == expect_val and params[-1] == 7
|
|
|
|
|
|
def test_update_without_column_ignores_weight():
|
|
st, pool = _store(False)
|
|
st.update_product(product_id=7, product_name="미라클통", product_code="MS-9809", weight_g=950)
|
|
sql, params = _main_sql(pool, "UPDATE cupang_products")
|
|
assert "weight_g" not in sql and sql.count("%s") == len(params) == 4
|