feat(performance): 광고비 파일 여러 개 업로드(같은 날짜는 최신 파일) + 끝나면 창 닫기

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-14 17:32:01 +09:00
parent 15a5eddf20
commit e3bc9d518d
9 changed files with 150 additions and 25 deletions
@@ -303,3 +303,63 @@ def test_naver_upload_and_details_endpoints(monkeypatch):
r = client.post("/performance/api/ad-spend/upload?channel=naver", files={"file": ("n.csv", NAVER_CSV.encode("utf-8"))})
assert r.status_code == 503 and "006" in r.json()["detail"]
assert "006" in client.get("/performance/api/ad-spend/naver/details?from=2026-09-01&to=2026-09-30").json()["detail"]
def test_merge_results_newest_file_wins():
"""여러 파일 — 같은 날짜는 파일 날짜(수정 시각)가 최신인 파일 값, 같으면 나중 파일. ROAS·상세도 그 파일 것."""
a = ads.ParseResult(entries=[(date(2026, 9, 1), 100), (date(2026, 9, 2), 200)],
roas={date(2026, 9, 2): Decimal("10")}, metrics={date(2026, 9, 2): {"노출수": 1}}, columns=["노출수"])
b = ads.ParseResult(entries=[(date(2026, 9, 2), 999), (date(2026, 9, 3), 300)],
roas={date(2026, 9, 2): Decimal("99")}, metrics={date(2026, 9, 2): {"노출수": 9}}, columns=["노출수", "클릭수"])
m, overlap = ads.merge_results([("new.csv", 2000.0, a), ("old.csv", 1000.0, b)]) # a 가 최신
assert m.entries == [(date(2026, 9, 1), 100), (date(2026, 9, 2), 200), (date(2026, 9, 3), 300)]
assert m.roas[date(2026, 9, 2)] == Decimal("10") and m.metrics[date(2026, 9, 2)] == {"노출수": 1}
assert overlap == [date(2026, 9, 2)] and m.columns == ["노출수", "클릭수"]
m2, _ = ads.merge_results([("a.csv", 1000.0, a), ("b.csv", 1000.0, b)]) # 같은 시각 → 나중 파일
assert dict(m2.entries)[date(2026, 9, 2)] == 999
bad = ads.ParseResult(errors=["3행: 오류"])
m3, _ = ads.merge_results([("a.csv", 1.0, a), ("bad.csv", 2.0, bad)])
assert m3.errors == ["[bad.csv] 3행: 오류"]
def test_upload_multiple_files_endpoint(monkeypatch):
import importlib
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.modules.performance import common
perf_router = importlib.import_module("app.modules.performance.router")
class Store:
saved: list = []
def set_ad_spend_bulk(self, code, entries, *, source="manual", user="", roas=None, metrics=None):
self.saved = list(entries)
self.roas = roas
return len(entries)
store = Store()
monkeypatch.setattr(common, "require_store", lambda request, admin=False: (store, {"email": "king@x"}))
monkeypatch.setattr(perf_router, "today_kst", lambda: TODAY)
app = FastAPI()
app.include_router(perf_router.router)
client = TestClient(app)
old = NAVER_CSV
new = NAVER_CSV.replace("2026.09.23.,300672", "2026.09.23.,111111").replace(",67.58", ",12.34")
files = [("files", ("new.csv", new.encode("utf-8"))), ("files", ("old.csv", old.encode("utf-8")))]
r = client.post("/performance/api/ad-spend/upload?channel=naver", files=files, data={"modified": ["2000", "1000"]})
assert r.status_code == 200, r.text
body = r.json()
assert body["files"] == 2 and body["overlap_days"] == 5 and body["saved"] == 5
assert dict(store.saved)[date(2026, 9, 23)] == 111111 and str(store.roas[date(2026, 9, 23)]) == "12.34" # 최신 파일
r = client.post("/performance/api/ad-spend/upload?channel=naver", files=files, data={"modified": ["1000", "2000"]})
assert dict(store.saved)[date(2026, 9, 23)] == 300672 # 이번엔 old 가 최신
bad = NAVER_CSV + "0_미라네주방,38461,2026.09.20.,abc,0,0,0,0,0,0,0,1\n"
r = client.post("/performance/api/ad-spend/upload?channel=naver",
files=[("files", ("ok.csv", old.encode("utf-8"))), ("files", ("bad.csv", bad.encode("utf-8")))])
assert r.status_code == 400 and r.json()["errors"][0].startswith("[bad.csv]")
r = client.post("/performance/api/ad-spend/upload?channel=naver", files={"file": ("one.csv", old.encode("utf-8"))})
assert r.status_code == 200 and r.json()["files"] == 1 # 예전 file 한 개도
assert client.post("/performance/api/ad-spend/upload?channel=naver").status_code in (400, 422)