|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 | from admin_api.test.helpers import CategoryGroupsAdminApi, OptionGroupsAdminApi, ProductsAdminApi, TagsAdminApi |
| 5 | +from django.urls import reverse |
| 6 | +from event.models import Event |
5 | 7 | from rest_framework.status import ( |
6 | 8 | HTTP_200_OK, |
7 | 9 | HTTP_201_CREATED, |
|
11 | 13 | ) |
12 | 14 | from shop.conftest import FAR_FUTURE, FAR_PAST |
13 | 15 | from shop.product.models import Category, CategoryGroup, OptionGroup, Product, Tag |
| 16 | +from user.models.organization import Organization |
| 17 | + |
| 18 | +PRODUCT_CHOICES_URL = reverse("v1:admin-shop-product-list") + "choices/" |
| 19 | +OPTION_GROUP_CHOICES_URL = reverse("v1:admin-shop-option-group-list") + "choices/" |
14 | 20 |
|
15 | 21 |
|
16 | 22 | @pytest.mark.parametrize("api_cls", [CategoryGroupsAdminApi, TagsAdminApi, ProductsAdminApi]) |
@@ -433,3 +439,41 @@ def test_admin_product_list_filters_by_status(api_client, products_by_status, st |
433 | 439 | response = ProductsAdminApi(http_client=api_client).list({"status": status.value}) |
434 | 440 | assert response.status_code == HTTP_200_OK |
435 | 441 | assert [p["id"] for p in response.json()] == [str(products_by_status[status].id)] |
| 442 | + |
| 443 | + |
| 444 | +@pytest.mark.django_db |
| 445 | +def test_admin_product_choices_include_category_meta(api_client, ticket_product): |
| 446 | + # Product choices 의 category FK 는 Category.get_choice_meta() 로 group/is_ticket/event 메타를 실어야 한다. |
| 447 | + org = Organization.objects.create(name="PSK") |
| 448 | + event = Event.objects.create( |
| 449 | + organization=org, name="PyCon Korea 2026", event_start_at=datetime(2026, 8, 1, tzinfo=timezone.utc) |
| 450 | + ) |
| 451 | + evented = Category.objects.create( |
| 452 | + group=CategoryGroup.objects.create(name="2026"), name="Conference", is_ticket=True, event=event |
| 453 | + ) |
| 454 | + |
| 455 | + response = api_client.get(PRODUCT_CHOICES_URL) |
| 456 | + assert response.status_code == HTTP_200_OK |
| 457 | + category_choices = {c["const"]: c for c in response.json()["category"]} |
| 458 | + |
| 459 | + # event 가 있는 카테고리 — str(event) 분기. |
| 460 | + evented_meta = category_choices[str(evented.id)]["meta"] |
| 461 | + assert evented_meta == {"group": "2026", "is_ticket": True, "event": str(event)} |
| 462 | + |
| 463 | + # event 가 없는 카테고리(fixture) — None 분기. |
| 464 | + plain_meta = category_choices[str(ticket_product.category.id)]["meta"] |
| 465 | + assert plain_meta == {"group": "기본", "is_ticket": True, "event": None} |
| 466 | + |
| 467 | + |
| 468 | +@pytest.mark.django_db |
| 469 | +def test_admin_option_group_choices_include_product_meta(api_client, ticket_product): |
| 470 | + # OptionGroup choices 의 product FK 는 Product.get_choice_meta() 로 category/price/stock/status 메타를 실어야 한다. |
| 471 | + response = api_client.get(OPTION_GROUP_CHOICES_URL) |
| 472 | + assert response.status_code == HTTP_200_OK |
| 473 | + product_choices = {c["const"]: c for c in response.json()["product"]} |
| 474 | + assert product_choices[str(ticket_product.id)]["meta"] == { |
| 475 | + "category": str(ticket_product.category), |
| 476 | + "price": ticket_product.price, |
| 477 | + "stock": ticket_product.stock, |
| 478 | + "status": Product.CurrentStatus.ACTIVE.label, |
| 479 | + } |
0 commit comments