|
1 | | -from datetime import datetime |
| 1 | +from datetime import date, datetime |
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 | from core.const.datetime import KST |
@@ -197,6 +197,51 @@ def test_sales_trend_missing_date_range_is_400(api_client): |
197 | 197 | assert "date_range" in {e["attr"] for e in resp.json()["errors"]} |
198 | 198 |
|
199 | 199 |
|
| 200 | +# --- 이벤트별 통계 기간 프리셋 --- |
| 201 | +def _attach_event(ticket_product, **stats): |
| 202 | + event = baker.make("event.Event", name="파이콘 한국 2026", **stats) |
| 203 | + ticket_product.category.event = event |
| 204 | + ticket_product.category.save() |
| 205 | + return event |
| 206 | + |
| 207 | + |
| 208 | +def test_time_series_defaults_to_event_stats_period(api_client, ticket_product): |
| 209 | + """date_range 미전송 + 통계 기간이 설정된 이벤트 선택 → 그 기간(inclusive)을 기본값으로 사용.""" |
| 210 | + event = _attach_event(ticket_product, stats_start_date=date(2026, 8, 14), stats_end_date=date(2026, 8, 16)) |
| 211 | + body = _chart_data(api_client, "line-sales-trend", {"event_id": str(event.id), "granularity": "day"}).json() |
| 212 | + assert [d["label"] for d in body["data"]] == ["2026-08-14", "2026-08-15", "2026-08-16"] |
| 213 | + |
| 214 | + |
| 215 | +def test_explicit_date_range_overrides_event_preset(api_client, ticket_product): |
| 216 | + event = _attach_event(ticket_product, stats_start_date=date(2026, 8, 1), stats_end_date=date(2026, 8, 31)) |
| 217 | + body = _chart_data( |
| 218 | + api_client, |
| 219 | + "line-sales-trend", |
| 220 | + { |
| 221 | + "event_id": str(event.id), |
| 222 | + "date_range": {"date_from": "2026-08-14", "date_to": "2026-08-15"}, |
| 223 | + "granularity": "day", |
| 224 | + }, |
| 225 | + ).json() |
| 226 | + assert [d["label"] for d in body["data"]] == ["2026-08-14", "2026-08-15"] |
| 227 | + |
| 228 | + |
| 229 | +def test_event_without_stats_period_still_requires_date_range(api_client, ticket_product): |
| 230 | + event = _attach_event(ticket_product) # 통계 기간 미설정 |
| 231 | + resp = _chart_data(api_client, "line-sales-trend", {"event_id": str(event.id), "granularity": "day"}) |
| 232 | + assert resp.status_code == status.HTTP_400_BAD_REQUEST |
| 233 | + assert "date_range" in {e["attr"] for e in resp.json()["errors"]} |
| 234 | + |
| 235 | + |
| 236 | +def test_event_option_carries_stats_period(api_client, ticket_product): |
| 237 | + """프론트 프리필용: event 옵션이 통계 기본 기간을 동봉.""" |
| 238 | + event = _attach_event(ticket_product, stats_start_date=date(2026, 8, 14), stats_end_date=date(2026, 8, 16)) |
| 239 | + params = next(c for c in api_client.get(CHARTS_URL).json() if c["id"] == "line-sales-trend")["params"] |
| 240 | + opt = next(o for p in params if p["key"] == "event_id" for o in p["options"] if o["value"] == str(event.id)) |
| 241 | + assert opt["date_from"] == "2026-08-14" |
| 242 | + assert opt["date_to"] == "2026-08-16" |
| 243 | + |
| 244 | + |
200 | 245 | def test_sales_trend_invalid_granularity_is_400(api_client): |
201 | 246 | resp = _chart_data( |
202 | 247 | api_client, |
|
0 commit comments