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
2 changes: 2 additions & 0 deletions e2e_config.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
"catalog.product.parameter.id": "PAR-7255-3950-0016",
"catalog.product.parameter_group.id": "PGR-7255-3950-0001",
"catalog.product.template.id": "TPL-7255-3950-0001",
"catalog.product.terms.id": "TCS-7255-3950-0001",
"catalog.product.terms.variant.id": "TCV-7255-3950-0001-0001",
"catalog.unit.id": "UNT-1229"
}
23 changes: 4 additions & 19 deletions mpt_api_client/resources/catalog/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ async def unpublish(self, resource_id: str, resource_data: ResourceData | None =
class AsyncCreateFileMixin[Model]:
"""Create file mixin."""

_upload_file_key = "file"
_upload_data_key = "document"

async def create(self, resource_data: ResourceData, file: FileTypes | None = None) -> Model:
"""Create document.

Expand All @@ -101,13 +98,13 @@ async def create(self, resource_data: ResourceData, file: FileTypes | None = Non
"""
files = {}
if file:
files[self._upload_file_key] = file
files[self._upload_file_key] = file # type: ignore[attr-defined]
response = await self.http_client.request( # type: ignore[attr-defined]
"post",
self.path, # type: ignore[attr-defined]
json=resource_data,
files=files,
json_file_key=self._upload_data_key,
json_file_key=self._upload_data_key, # type: ignore[attr-defined]
force_multipart=True,
)
return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]
Expand All @@ -124,9 +121,6 @@ class AsyncDocumentMixin[Model](
class CreateFileMixin[Model]:
"""Create file mixin."""

_upload_file_key = "file"
_upload_data_key = "document"

def create(self, resource_data: ResourceData, file: FileTypes | None = None) -> Model:
"""Create document.

Expand All @@ -141,13 +135,13 @@ def create(self, resource_data: ResourceData, file: FileTypes | None = None) ->
"""
files = {}
if file:
files[self._upload_file_key] = file
files[self._upload_file_key] = file # type: ignore[attr-defined]
response = self.http_client.request( # type: ignore[attr-defined]
"post",
self.path, # type: ignore[attr-defined]
json=resource_data,
files=files,
json_file_key=self._upload_data_key,
json_file_key=self._upload_data_key, # type: ignore[attr-defined]
force_multipart=True,
)
return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]
Expand All @@ -160,9 +154,6 @@ class DocumentMixin[Model](
):
"""Document mixin."""

_upload_file_key = "file"
_upload_data_key = "document"


class MediaMixin[Model](
CreateFileMixin[Model],
Expand All @@ -171,9 +162,6 @@ class MediaMixin[Model](
):
"""Media mixin."""

_upload_file_key = "file"
_upload_data_key = "media"


class ActivatableMixin[Model]:
"""Activatable mixin adds the ability to activate and deactivate."""
Expand Down Expand Up @@ -235,6 +223,3 @@ class AsyncMediaMixin[Model](
AsyncPublishableMixin[Model],
):
"""Media mixin."""

_upload_file_key = "file"
_upload_data_key = "media"
16 changes: 11 additions & 5 deletions mpt_api_client/resources/catalog/product_term_variants.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from mpt_api_client.http import AsyncService, Service
from mpt_api_client.http.mixins import (
AsyncCollectionMixin,
AsyncFilesOperationsMixin,
AsyncDownloadFileMixin,
AsyncModifiableResourceMixin,
CollectionMixin,
FilesOperationsMixin,
DownloadFileMixin,
ModifiableResourceMixin,
)
from mpt_api_client.models import Model
from mpt_api_client.resources.catalog.mixins import (
AsyncCreateFileMixin,
AsyncPublishableMixin,
CreateFileMixin,
PublishableMixin,
)

Expand All @@ -21,13 +23,16 @@ class TermVariant(Model):
class TermVariantServiceConfig:
"""Term variant service configuration."""

_endpoint = "/public/v1/catalog/products/terms/{term_id}/variants"
_endpoint = "/public/v1/catalog/products/{product_id}/terms/{term_id}/variants"
_model_class = TermVariant
_collection_key = "data"
_upload_file_key = "file"
_upload_data_key = "variant"


class TermVariantService(
FilesOperationsMixin[TermVariant],
CreateFileMixin[TermVariant],
DownloadFileMixin[TermVariant],
ModifiableResourceMixin[TermVariant],
PublishableMixin[TermVariant],
CollectionMixin[TermVariant],
Expand All @@ -38,7 +43,8 @@ class TermVariantService(


class AsyncTermVariantService(
AsyncFilesOperationsMixin[TermVariant],
AsyncCreateFileMixin[TermVariant],
AsyncDownloadFileMixin[TermVariant],
AsyncModifiableResourceMixin[TermVariant],
AsyncPublishableMixin[TermVariant],
AsyncCollectionMixin[TermVariant],
Expand Down
4 changes: 2 additions & 2 deletions mpt_api_client/resources/catalog/product_terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def variants(self, term_id: str) -> TermVariantService:
"""Access term variants service."""
return TermVariantService(
http_client=self.http_client,
endpoint_params={"term_id": term_id},
endpoint_params={"product_id": self.endpoint_params["product_id"], "term_id": term_id},
)


Expand All @@ -55,5 +55,5 @@ def variants(self, term_id: str) -> AsyncTermVariantService:
"""Access async term variants service."""
return AsyncTermVariantService(
http_client=self.http_client,
endpoint_params={"term_id": term_id},
endpoint_params={"product_id": self.endpoint_params["product_id"], "term_id": term_id},
)
2 changes: 2 additions & 0 deletions mpt_api_client/resources/catalog/products_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class DocumentServiceConfig:
_endpoint = "/public/v1/catalog/products/{product_id}/documents"
_model_class = Document
_collection_key = "data"
_upload_file_key = "file"
_upload_data_key = "document"


class DocumentService(
Expand Down
2 changes: 2 additions & 0 deletions mpt_api_client/resources/catalog/products_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class MediaServiceConfig:
_endpoint = "/public/v1/catalog/products/{product_id}/media"
_model_class = Media
_collection_key = "data"
_upload_file_key = "file"
_upload_data_key = "media"


class MediaService(
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/catalog/product/documents/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def document_data():
"name": "e2e test document - please delete",
"description": "E2E test document for automated testing",
"language": "en-gb",
"url": "",
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mpt_api_client.exceptions import MPTAPIError
from mpt_api_client.rql.query_builder import RQLQuery

pytestmark = [pytest.mark.flaky, pytest.mark.asyncio]
pytestmark = [pytest.mark.flaky]


@pytest.fixture
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/catalog/product/documents/test_sync_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def created_document_from_file(logger, vendor_document_service, document_data, p


@pytest.fixture
def created_document_from_link(logger, vendor_document_service, document_data, pdf_url):
def created_document_from_url(logger, vendor_document_service, document_data, pdf_url):
document_data["url"] = pdf_url
document = vendor_document_service.create(document_data)
yield document
Expand All @@ -33,9 +33,9 @@ def test_create_document(created_document_from_file, document_data):
assert created_document_from_file.description == document_data["description"]


def test_create_from_link(created_document_from_link, pdf_url, document_data):
assert created_document_from_link.name == document_data["name"]
assert created_document_from_link.description == document_data["description"]
def test_create_document_from_url(created_document_from_url, document_data):
assert created_document_from_url.name == document_data["name"]
assert created_document_from_url.description == document_data["description"]


def test_update_document(vendor_document_service, created_document_from_file):
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/catalog/product/media/test_async_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from mpt_api_client.exceptions import MPTAPIError

pytestmark = [pytest.mark.flaky, pytest.mark.asyncio]
pytestmark = [pytest.mark.flaky]


@pytest.fixture
Expand Down
11 changes: 11 additions & 0 deletions tests/e2e/catalog/product/terms/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest


@pytest.fixture
def term_data():
return {"name": "e2e - please delete", "description": "Test term description"}


@pytest.fixture
def term_id(e2e_config):
return e2e_config["catalog.product.terms.id"]
67 changes: 67 additions & 0 deletions tests/e2e/catalog/product/terms/test_async_terms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pytest

from mpt_api_client.exceptions import MPTAPIError
from mpt_api_client.rql.query_builder import RQLQuery

pytestmark = [pytest.mark.flaky]


@pytest.fixture
def async_vendor_terms_service(async_mpt_vendor, product_id):
return async_mpt_vendor.catalog.products.terms(product_id)


@pytest.fixture
async def async_created_term(async_vendor_terms_service, term_data):
service = async_vendor_terms_service
term = await service.create(term_data)
yield term
try:
await service.delete(term.id)
except MPTAPIError as error:
print(f"TEARDOWN - Unable to delete term {term.id}: {error.title}")


def test_create_term(async_created_term):
term = async_created_term
assert term.name == "e2e - please delete"


async def test_update_term(async_vendor_terms_service, async_created_term):
service = async_vendor_terms_service
update_data = {"name": "e2e - delete me (updated)"}
term = await service.update(async_created_term.id, update_data)
assert term.name == "e2e - delete me (updated)"


async def test_get_term(async_vendor_terms_service, term_id):
service = async_vendor_terms_service
term = await service.get(term_id)
assert term.id == term_id


async def test_get_term_by_id(async_vendor_terms_service, term_id):
service = async_vendor_terms_service
term = await service.get(term_id)
assert term.id == term_id


async def test_iterate_terms(async_vendor_terms_service, async_created_term):
service = async_vendor_terms_service
terms = [term async for term in service.iterate()]
assert any(term.id == async_created_term.id for term in terms)


async def test_filter_terms(async_vendor_terms_service, term_id):
select_fields = ["-description"]
filtered_terms = async_vendor_terms_service.filter(RQLQuery(id=term_id)).select(*select_fields)
terms = [term async for term in filtered_terms.iterate()]
assert len(terms) == 1
assert terms[0].id == term_id


async def test_delete_term(async_vendor_terms_service, async_created_term):
service = async_vendor_terms_service
await service.delete(async_created_term.id)
with pytest.raises(MPTAPIError):
await service.get(async_created_term.id)
79 changes: 79 additions & 0 deletions tests/e2e/catalog/product/terms/test_sync_terms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pytest

from mpt_api_client.exceptions import MPTAPIError
from mpt_api_client.rql.query_builder import RQLQuery

pytestmark = [pytest.mark.flaky]


@pytest.fixture
def vendor_terms_service(mpt_vendor, product_id):
return mpt_vendor.catalog.products.terms(product_id)


@pytest.fixture
def created_term(logger, vendor_terms_service, term_data):
service = vendor_terms_service
term = service.create(term_data)
yield term
try:
service.delete(term.id)
except MPTAPIError as error:
print(f"TEARDOWN - Unable to delete term {term.id}: {error.title}")


@pytest.fixture
def created_term_from_url(logger, vendor_terms_service, term_data, pdf_url):
term_data["url"] = pdf_url
service = vendor_terms_service
term = service.create(term_data)
yield term
try:
service.delete(term.id)
except MPTAPIError as error:
print(f"TEARDOWN - Unable to delete term {term.id}: {error.title}")


def test_create_term(created_term):
term = created_term
assert term.name == "e2e - please delete"


def test_update_term(vendor_terms_service, created_term):
service = vendor_terms_service
update_data = {"name": "e2e - delete me (updated)"}
term = service.update(created_term.id, update_data)
assert term.name == "e2e - delete me (updated)"


def test_get_term(vendor_terms_service, term_id):
service = vendor_terms_service
term = service.get(term_id)
assert term.id == term_id


def test_get_term_by_id(vendor_terms_service, term_id):
service = vendor_terms_service
term = service.get(term_id)
assert term.id == term_id


def test_iterate_terms(vendor_terms_service, created_term):
service = vendor_terms_service
terms = list(service.iterate())
assert any(term.id == created_term.id for term in terms)


def test_filter_terms(vendor_terms_service, term_id):
select_fields = ["-description"]
filtered_terms = vendor_terms_service.filter(RQLQuery(id=term_id)).select(*select_fields)
terms = list(filtered_terms.iterate())
assert len(terms) == 1
assert terms[0].id == term_id


def test_delete_term(vendor_terms_service, created_term):
service = vendor_terms_service
service.delete(created_term.id)
with pytest.raises(MPTAPIError):
service.get(created_term.id)
17 changes: 17 additions & 0 deletions tests/e2e/catalog/product/terms/variants/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest


@pytest.fixture
def variant_data():
return {
"name": "e2e - please delete",
"description": "Test variant description",
"languageCode": "en-gb",
"type": "File",
"assetUrl": "",
}


@pytest.fixture
def variant_id(e2e_config):
return e2e_config["catalog.product.terms.variant.id"]
Loading