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
10 changes: 9 additions & 1 deletion mpt_api_client/resources/catalog/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
DeleteMixin,
UpdateMixin,
)
from mpt_api_client.models import Model
from mpt_api_client.models import Model, ResourceData
from mpt_api_client.resources.catalog.mixins import (
AsyncPublishableMixin,
PublishableMixin,
Expand Down Expand Up @@ -103,6 +103,10 @@ def terms(self, product_id: str) -> TermService:
"""Return terms service."""
return TermService(http_client=self.http_client, endpoint_params={"product_id": product_id})

def update_settings(self, product_id: str, settings: ResourceData) -> Product:
"""Update product settings."""
return self._resource_action(product_id, "PUT", "settings", settings)


class AsyncProductsService(
AsyncCreateMixin[Product],
Expand Down Expand Up @@ -155,3 +159,7 @@ def terms(self, product_id: str) -> AsyncTermService:
return AsyncTermService(
http_client=self.http_client, endpoint_params={"product_id": product_id}
)

async def update_settings(self, product_id: str, settings: ResourceData) -> Product:
"""Update product settings."""
return await self._resource_action(product_id, "PUT", "settings", settings)
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extend-ignore =

per-file-ignores =
mpt_api_client/rql/query_builder.py: WPS110 WPS115 WPS210 WPS214
mpt_api_client/resources/catalog/products.py: WPS204 WPS215
mpt_api_client/resources/catalog/products.py: WPS204 WPS214 WPS215
mpt_api_client/resources/catalog/items.py: WPS215
mpt_api_client/resources/catalog/products_item_groups.py: WPS215
mpt_api_client/resources/catalog/products_parameter_groups.py: WPS215
Expand All @@ -48,6 +48,7 @@ per-file-ignores =
tests/http/test_async_service.py: WPS204 WPS202
tests/http/test_service.py: WPS204 WPS202
tests/http/test_mixins.py: WPS204 WPS202
tests/resources/catalog/test_products.py: WPS202 WPS210

tests/*:
# Allow magic strings.
Expand Down
48 changes: 46 additions & 2 deletions tests/resources/catalog/test_products.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import httpx
import pytest
import respx

from mpt_api_client.resources.catalog.product_terms import (
AsyncTermService,
Expand Down Expand Up @@ -42,14 +44,16 @@ def async_products_service(async_http_client):


@pytest.mark.parametrize(
"method", ["get", "create", "update", "delete", "review", "publish", "unpublish"]
"method",
["get", "create", "update", "delete", "review", "publish", "unpublish", "update_settings"],
)
def test_mixins_present(products_service, method):
assert hasattr(products_service, method)


@pytest.mark.parametrize(
"method", ["get", "create", "update", "delete", "review", "publish", "unpublish"]
"method",
["get", "create", "update", "delete", "review", "publish", "unpublish", "update_settings"],
)
def test_async_mixins_present(async_products_service, method):
assert hasattr(async_products_service, method)
Expand Down Expand Up @@ -91,3 +95,43 @@ def test_async_property_services(async_products_service, service_method, expecte

assert isinstance(property_service, expected_service_class)
assert property_service.endpoint_params == {"product_id": "PRD-001"}


def test_update_settings(products_service):
"""Test updating product settings."""
product_id = "PRD-001"
settings_data = {"visibility": "public", "searchable": True, "featured": False}
expected_response = {"id": product_id, "name": "Test Product", "settings": settings_data}

with respx.mock:
mock_route = respx.put(
f"https://api.example.com/public/v1/catalog/products/{product_id}/settings"
).mock(return_value=httpx.Response(httpx.codes.OK, json=expected_response))

product = products_service.update_settings(product_id, settings_data)

assert mock_route.call_count == 1
request = mock_route.calls[0].request
assert request.method == "PUT"
assert request.url.path == f"/public/v1/catalog/products/{product_id}/settings"
assert product.to_dict() == expected_response


async def test_async_update_settings(async_products_service):
"""Test updating product settings asynchronously."""
product_id = "PRD-002"
settings_data = {"visibility": "private", "searchable": False, "featured": True}
expected_response = {"id": product_id, "name": "Test Product Async", "settings": settings_data}

with respx.mock:
mock_route = respx.put(
f"https://api.example.com/public/v1/catalog/products/{product_id}/settings"
).mock(return_value=httpx.Response(httpx.codes.OK, json=expected_response))

product = await async_products_service.update_settings(product_id, settings_data)

assert mock_route.call_count == 1
request = mock_route.calls[0].request
assert request.method == "PUT"
assert request.url.path == f"/public/v1/catalog/products/{product_id}/settings"
assert product.to_dict() == expected_response