Skip to content

Commit d8cd2ac

Browse files
committed
MPT-16325 Publish product during the seeding process
1 parent 5665533 commit d8cd2ac

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

seed/catalog/product.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ async def seed_product() -> None:
3838
"""Seed product data."""
3939
logger.debug("Seeding catalog.product ...")
4040
await init_resource("catalog.product.id", create_product)
41+
await publish_product()
4142
await init_resource("catalog.unit.id", create_unit_of_measure)
4243
await init_resource("catalog.product.item_group.id", create_item_group)
4344
await init_resource("catalog.product.item.id", create_product_item)
@@ -50,6 +51,22 @@ async def seed_product() -> None:
5051
logger.debug("Seeded catalog.product completed.")
5152

5253

54+
async def publish_product(
55+
context: Context = Provide[Container.context],
56+
mpt_vendor: AsyncMPTClient = Provide[Container.mpt_vendor],
57+
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
58+
) -> None:
59+
"""Publish product."""
60+
product_id = require_context_id(context, "catalog.product.id", "publish product")
61+
product = await mpt_vendor.catalog.products.get(product_id)
62+
if product.status == "Draft":
63+
product = await mpt_vendor.catalog.products.review(product_id)
64+
if product.status in {"Pending", "Unpublished"}:
65+
product = await mpt_operations.catalog.products.publish(product_id)
66+
if product.status != "Published":
67+
raise RuntimeError(f"Product {product_id} is not published")
68+
69+
5370
@inject
5471
async def create_terms_variant(
5572
context: Context = Provide[Container.context],

seed/seed_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ async def seed_api(context: Context = Provide[Container.context]) -> None:
2020
try: # noqa: WPS229
2121
await seed_accounts()
2222
await seed_catalog()
23+
logger.info("Seeding completed successfully.")
2324
except Exception:
2425
logger.exception("Exception occurred during seeding.")
2526
finally:

tests/seed/catalog/test_product.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
create_terms,
1313
create_terms_variant,
1414
create_unit_of_measure,
15+
publish_product,
1516
)
1617
from seed.context import Context
1718

@@ -28,6 +29,22 @@ def context_with_product():
2829
return context
2930

3031

32+
async def test_publish_product(mocker, vendor_client, operations_client, context_with_product):
33+
product_draft = Product({"id": "prod-123", "status": "Draft"})
34+
product_pending = Product({"id": "prod-123", "status": "Pending"})
35+
product_published = Product({"id": "prod-123", "status": "Published"})
36+
37+
vendor_client.catalog.products.get = mocker.AsyncMock(return_value=product_draft)
38+
vendor_client.catalog.products.review = mocker.AsyncMock(return_value=product_pending)
39+
operations_client.catalog.products.publish = mocker.AsyncMock(return_value=product_published)
40+
41+
await publish_product(context_with_product, vendor_client, operations_client)
42+
43+
vendor_client.catalog.products.get.assert_called_once_with("prod-123")
44+
vendor_client.catalog.products.review.assert_called_once_with("prod-123")
45+
operations_client.catalog.products.publish.assert_called_once_with("prod-123")
46+
47+
3148
async def test_create_product(mocker, context: Context, vendor_client, product):
3249
mpt_vendor = vendor_client
3350
mpt_vendor.catalog.products.create = mocker.AsyncMock(return_value=product)

0 commit comments

Comments
 (0)