Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions seed/catalog/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async def seed_product() -> None:
"""Seed product data."""
logger.debug("Seeding catalog.product ...")
await init_resource("catalog.product.id", create_product)
await publish_product()
await init_resource("catalog.unit.id", create_unit_of_measure)
await init_resource("catalog.product.item_group.id", create_item_group)
await init_resource("catalog.product.item.id", create_product_item)
Expand All @@ -50,6 +51,23 @@ async def seed_product() -> None:
logger.debug("Seeded catalog.product completed.")


@inject
async def publish_product(
context: Context = Provide[Container.context],
mpt_vendor: AsyncMPTClient = Provide[Container.mpt_vendor],
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
) -> None:
"""Publish product."""
product_id = require_context_id(context, "catalog.product.id", "publish product")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use an ActionEnum instead of passing a str? It would avoid having different str for the same action, I can see different patterns like starting with uppercase and other with lowercase

product = await mpt_vendor.catalog.products.get(product_id)
if product.status == "Draft":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a Enum for those statuses?

product = await mpt_vendor.catalog.products.review(product_id)
if product.status in {"Pending", "Unpublished"}:
product = await mpt_operations.catalog.products.publish(product_id)
if product.status != "Published":
raise RuntimeError(f"Product {product_id} is not published")


@inject
async def create_terms_variant(
context: Context = Provide[Container.context],
Expand Down
1 change: 1 addition & 0 deletions seed/seed_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async def seed_api(context: Context = Provide[Container.context]) -> None:
try: # noqa: WPS229
await seed_accounts()
await seed_catalog()
logger.info("Seeding completed successfully.")
except Exception:
logger.exception("Exception occurred during seeding.")
finally:
Expand Down
17 changes: 17 additions & 0 deletions tests/seed/catalog/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
create_terms,
create_terms_variant,
create_unit_of_measure,
publish_product,
)
from seed.context import Context

Expand All @@ -28,6 +29,22 @@ def context_with_product():
return context


async def test_publish_product(mocker, vendor_client, operations_client, context_with_product):
product_draft = Product({"id": "prod-123", "status": "Draft"})
product_pending = Product({"id": "prod-123", "status": "Pending"})
product_published = Product({"id": "prod-123", "status": "Published"})

vendor_client.catalog.products.get = mocker.AsyncMock(return_value=product_draft)
vendor_client.catalog.products.review = mocker.AsyncMock(return_value=product_pending)
operations_client.catalog.products.publish = mocker.AsyncMock(return_value=product_published)

await publish_product(context_with_product, vendor_client, operations_client)

vendor_client.catalog.products.get.assert_called_once_with("prod-123")
vendor_client.catalog.products.review.assert_called_once_with("prod-123")
operations_client.catalog.products.publish.assert_called_once_with("prod-123")


async def test_create_product(mocker, context: Context, vendor_client, product):
mpt_vendor = vendor_client
mpt_vendor.catalog.products.create = mocker.AsyncMock(return_value=product)
Expand Down