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
11 changes: 11 additions & 0 deletions mpt_api_client/resources/catalog/catalog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from mpt_api_client.http import AsyncHTTPClient, HTTPClient
from mpt_api_client.resources.catalog.items import AsyncItemsService, ItemsService
from mpt_api_client.resources.catalog.products import AsyncProductsService, ProductsService


Expand All @@ -13,6 +14,11 @@ def products(self) -> ProductsService:
"""Products service."""
return ProductsService(http_client=self.http_client)

@property
def items(self) -> ItemsService: # noqa: WPS110
"""Items service."""
return ItemsService(http_client=self.http_client)


class AsyncCatalog:
"""Catalog MPT API Module."""
Expand All @@ -24,3 +30,8 @@ def __init__(self, *, http_client: AsyncHTTPClient):
def products(self) -> AsyncProductsService:
"""Products service."""
return AsyncProductsService(http_client=self.http_client)

@property
def items(self) -> AsyncItemsService: # noqa: WPS110
"""Items service."""
return AsyncItemsService(http_client=self.http_client)
47 changes: 47 additions & 0 deletions mpt_api_client/resources/catalog/items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from mpt_api_client.http import AsyncService, CreateMixin, Service
from mpt_api_client.http.mixins import (
AsyncCreateMixin,
AsyncDeleteMixin,
AsyncUpdateMixin,
DeleteMixin,
UpdateMixin,
)
from mpt_api_client.models import Model
from mpt_api_client.resources.catalog.mixins import (
AsyncPublishableMixin,
PublishableMixin,
)


class Item(Model): # noqa: WPS110
"""Item resource."""


class ItemsServiceConfig:
"""Items service configuration."""

_endpoint = "/public/v1/catalog/items"
_model_class = Item
_collection_key = "data"


class ItemsService(
CreateMixin[Item],
DeleteMixin,
UpdateMixin[Item],
PublishableMixin[Item],
Service[Item],
ItemsServiceConfig,
):
"""Items service."""


class AsyncItemsService(
AsyncCreateMixin[Item],
AsyncDeleteMixin,
AsyncUpdateMixin[Item],
AsyncPublishableMixin[Item],
AsyncService[Item],
ItemsServiceConfig,
):
"""Items service."""
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extend-ignore =
per-file-ignores =
mpt_api_client/rql/query_builder.py: WPS110 WPS115 WPS210 WPS214
mpt_api_client/resources/catalog/products.py: WPS215
mpt_api_client/resources/catalog/items.py: WPS215
mpt_api_client/resources/catalog/products_parameter_groups.py: WPS215
tests/http/test_async_service.py: WPS204 WPS202
tests/http/test_service.py: WPS204 WPS202
Expand Down
77 changes: 43 additions & 34 deletions tests/resources/catalog/test_catalog.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,67 @@
from typing import Any

import pytest

from mpt_api_client.http import AsyncHTTPClient
from mpt_api_client.resources.catalog import AsyncCatalog, Catalog
from mpt_api_client.resources.catalog.catalog import AsyncCatalog, Catalog
from mpt_api_client.resources.catalog.items import AsyncItemsService, ItemsService
from mpt_api_client.resources.catalog.products import AsyncProductsService, ProductsService


def test_catalog_init(http_client):
catalog = Catalog(http_client=http_client)

assert isinstance(catalog, Catalog)
assert catalog.http_client is http_client


def test_catalog_products_multiple_calls(http_client):
catalog = Catalog(http_client=http_client)

products_service = catalog.products
products_service_additional = catalog.products

assert products_service is not products_service_additional
assert isinstance(products_service, ProductsService)
assert isinstance(products_service_additional, ProductsService)

@pytest.fixture
def catalog(http_client: Any) -> Catalog:
return Catalog(http_client=http_client)

def test_async_catalog_init(async_http_client: AsyncHTTPClient):
catalog = AsyncCatalog(http_client=async_http_client)

assert isinstance(catalog, AsyncCatalog)
assert catalog.http_client is async_http_client
@pytest.fixture
def async_catalog(async_http_client: Any) -> AsyncCatalog:
return AsyncCatalog(http_client=async_http_client)


@pytest.mark.parametrize(
("attr_name", "expected"),
("property_name", "expected_service_class"),
[
("products", ProductsService),
("items", ItemsService),
],
)
def test_catalog_properties(http_client, attr_name, expected):
catalog = Catalog(http_client=http_client)

service = getattr(catalog, attr_name)
def test_catalog_properties(
catalog: Catalog, property_name: str, expected_service_class: type
) -> None:
"""Test that Catalog properties return correct instances."""
service = getattr(catalog, property_name)

assert isinstance(service, expected)
assert isinstance(service, expected_service_class)
assert service.http_client is catalog.http_client


@pytest.mark.parametrize(
("attr_name", "expected"),
("property_name", "expected_service_class"),
[
("products", AsyncProductsService),
("items", AsyncItemsService),
],
)
def test_async_catalog_properties(http_client, attr_name, expected):
catalog = AsyncCatalog(http_client=http_client)
def test_async_catalog_properties(
async_catalog: AsyncCatalog, property_name: str, expected_service_class: type
) -> None:
"""Test that AsyncCatalog properties return correct instances."""
service = getattr(async_catalog, property_name)

assert isinstance(service, expected_service_class)
assert service.http_client is async_catalog.http_client


def test_catalog_initialization(http_client: Any) -> None:
"""Test that Catalog can be properly initialized with http_client."""
catalog = Catalog(http_client=http_client)

assert catalog.http_client is http_client
assert isinstance(catalog, Catalog)


service = getattr(catalog, attr_name)
def test_async_catalog_initialization(async_http_client: Any) -> None:
"""Test that AsyncCatalog can be properly initialized with http_client."""
async_catalog = AsyncCatalog(http_client=async_http_client)

assert isinstance(service, expected)
assert async_catalog.http_client is async_http_client
assert isinstance(async_catalog, AsyncCatalog)
29 changes: 29 additions & 0 deletions tests/resources/catalog/test_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import Any

import pytest

from mpt_api_client.resources.catalog.items import AsyncItemsService, ItemsService


@pytest.fixture
def items_service(http_client: Any) -> ItemsService:
return ItemsService(http_client=http_client)


@pytest.fixture
def async_items_service(async_http_client: Any) -> AsyncItemsService:
return AsyncItemsService(http_client=async_http_client)


@pytest.mark.parametrize(
"method", ["get", "create", "update", "delete", "review", "publish", "unpublish"]
)
def test_mixins_present(items_service: ItemsService, method: str) -> None:
assert hasattr(items_service, method)


@pytest.mark.parametrize(
"method", ["get", "create", "update", "delete", "review", "publish", "unpublish"]
)
def test_async_mixins_present(async_items_service: AsyncItemsService, method: str) -> None:
assert hasattr(async_items_service, method)