Skip to content

Commit 13818b4

Browse files
authored
Merge pull request #130 from labd/add-changeAttributeConstraint-action-testing-backend
Add changeAttributeConstraint action to testing backend
2 parents 8a325bd + 602c2c4 commit 13818b4

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/commercetools/testing/product_types.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from commercetools.platform import models
77
from commercetools.platform.models._schemas.product_type import (
88
AttributeDefinitionSchema,
9+
ProductTypeChangeAttributeConstraintActionSchema,
910
ProductTypeDraftSchema,
1011
ProductTypePagedQueryResponseSchema,
1112
ProductTypeSchema,
@@ -124,6 +125,19 @@ def updater(
124125
return updater
125126

126127

128+
def change_attribute_constraint_action(
129+
self, obj: typing.Dict, action: models.ProductTypeChangeAttributeConstraintAction
130+
):
131+
new = copy.deepcopy(obj)
132+
133+
for attribute in new["attributes"]:
134+
if attribute["name"] == action.attribute_name:
135+
attribute["attributeConstraint"] = action.new_value.value
136+
return new
137+
138+
raise InternalUpdateError("No attribute found with name %r" % action.attribute_name)
139+
140+
127141
class ProductTypesBackend(ServiceBackend):
128142
service_path = "product-types"
129143
model_class = ProductTypesModel
@@ -149,4 +163,5 @@ def urls(self):
149163
"changeLabel": change_label_action(),
150164
"changeLocalizedEnumValueLabel": change_localized_enum_value_label(),
151165
"addAttributeDefinition": add_attribute_definition_action(),
166+
"changeAttributeConstraint": change_attribute_constraint_action,
152167
}

tests/platform/test_service_product_types.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
import requests_mock
32
from requests.exceptions import HTTPError
43

54
from commercetools.platform import models
@@ -42,12 +41,12 @@ def test_product_types_get_by_key(old_client):
4241

4342

4443
def test_product_type_query(old_client):
45-
product_type = old_client.product_types.create(
44+
old_client.product_types.create(
4645
models.ProductTypeDraft(
4746
key="test-product-type1", name="test-1", description="something"
4847
)
4948
)
50-
product_type = old_client.product_types.create(
49+
old_client.product_types.create(
5150
models.ProductTypeDraft(
5251
key="test-product-type2", name="test-2", description="something"
5352
)
@@ -88,3 +87,47 @@ def test_product_update(old_client):
8887
key="test-product-type", version=product_type.version, actions=[]
8988
)
9089
assert product_type.key == "test-product-type"
90+
91+
92+
def test_product_update_attribute_constraint_change(old_client):
93+
attribute_name = "testConstraint"
94+
product_type = old_client.product_types.create(
95+
models.ProductTypeDraft(
96+
key="test-product-type",
97+
name="test",
98+
description="something",
99+
attributes=[
100+
models.AttributeDefinitionDraft(
101+
type=models.AttributeTextType(),
102+
name=attribute_name,
103+
label=models.LocalizedString({"en": "testConstraint"}),
104+
is_required=False,
105+
attribute_constraint=models.AttributeConstraintEnum.SAME_FOR_ALL,
106+
)
107+
],
108+
)
109+
)
110+
111+
assert product_type.id
112+
assert product_type.key == "test-product-type"
113+
assert (
114+
product_type.attributes[0].attribute_constraint
115+
== models.AttributeConstraintEnum.SAME_FOR_ALL
116+
)
117+
118+
product_type = old_client.product_types.update_by_id(
119+
id=product_type.id,
120+
version=product_type.version,
121+
actions=[
122+
models.ProductTypeChangeAttributeConstraintAction(
123+
attribute_name=attribute_name,
124+
new_value=models.AttributeConstraintEnumDraft.NONE,
125+
)
126+
],
127+
)
128+
129+
assert product_type.key == "test-product-type"
130+
assert (
131+
product_type.attributes[0].attribute_constraint
132+
== models.AttributeConstraintEnum.NONE
133+
)

0 commit comments

Comments
 (0)