Skip to content

Commit bc89b1c

Browse files
committed
Add setLineItemCustomField action for cart and order
1 parent 93e1d99 commit bc89b1c

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- Testing: Add `datetime` and `list` updater utils
3434
- Testing: Add `setLocalizedDescription` action for shipping_method
3535
- Testing: Add `changeAttributeConstraint` action for product_type
36+
- Testing: Add `setLineItemCustomField` action for cart and order
3637

3738

3839
## Notes on code generation

src/commercetools/testing/carts.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,6 @@ def urls(self):
198198
("^(?P<id>[^/]+)$", "DELETE", self.delete_by_id),
199199
]
200200

201-
def set_custom_field(self, obj, action: models.CartSetCustomFieldAction):
202-
if not obj["custom"]:
203-
raise ValueError(
204-
"This resource has no custom type set - please use "
205-
"setCustomType first to set the type of the custom fields"
206-
)
207-
208-
name = action.name
209-
value = action.value
210-
211-
# real API always increments version, so always apply new value.
212-
new = copy.deepcopy(obj)
213-
new["custom"]["fields"][name] = value
214-
return new
215-
216201
def set_custom_type(self, obj, action: models.CartSetCustomTypeAction):
217202
custom_type = self.model._storage.get_by_resource_identifier(action.type)
218203

@@ -226,6 +211,7 @@ def set_custom_type(self, obj, action: models.CartSetCustomTypeAction):
226211

227212
_actions = {
228213
"addPayment": add_payment(),
229-
"setCustomField": set_custom_field,
214+
"setCustomField": utils.set_custom_field(),
215+
"setLineItemCustomField": utils.set_line_item_custom_field(),
230216
"setCustomType": set_custom_type,
231217
}

src/commercetools/testing/orders.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from commercetools.testing.abstract import BaseModel, ServiceBackend
1818
from commercetools.testing.utils import (
1919
set_custom_field,
20+
set_line_item_custom_field,
2021
update_attribute,
2122
update_enum_attribute,
2223
)
@@ -154,5 +155,6 @@ def urls(self):
154155
"setBillingAddress": update_attribute("billingAddress", "address"),
155156
"setCustomerEmail": update_attribute("customerEmail", "email"),
156157
"setCustomField": set_custom_field(),
158+
"setLineItemCustomField": set_line_item_custom_field(),
157159
"addPayment": add_payment(),
158160
}

src/commercetools/testing/utils.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
)
1818
from commercetools.platform.models._abstract import _BaseType
1919
from commercetools.platform.models._schemas.product import ProductSchema
20+
from commercetools.platform.models.cart import Cart, CartSetLineItemCustomFieldAction
21+
from commercetools.platform.models.order import Order, OrderSetLineItemCustomFieldAction
2022

2123

2224
class InternalUpdateError(ValueError):
@@ -223,6 +225,12 @@ def updater(
223225
obj,
224226
action: typing.Union[OrderSetCustomFieldAction, CartSetCustomFieldAction],
225227
):
228+
if not obj["custom"]:
229+
raise ValueError(
230+
"This resource has no custom type set - please use "
231+
"setCustomType first to set the type of the custom fields"
232+
)
233+
226234
name = action.name
227235
value = action.value
228236

@@ -236,6 +244,36 @@ def updater(
236244
return updater
237245

238246

247+
def set_line_item_custom_field():
248+
"""Set custom field of a line item.
249+
Note it always sets the type now, instead of type checking the custom field type!
250+
"""
251+
252+
def updater(
253+
self,
254+
obj,
255+
action: typing.Union[
256+
OrderSetLineItemCustomFieldAction, CartSetLineItemCustomFieldAction
257+
],
258+
):
259+
line_item_id = action.line_item_id
260+
name = action.name
261+
value = action.value
262+
263+
# real API always increments version, so always apply new value.
264+
new = copy.deepcopy(obj)
265+
for line in new["lineItems"]:
266+
if line["id"] != line_item_id:
267+
continue
268+
269+
if not line["custom"]:
270+
line["custom"] = {"fields": {}}
271+
line["custom"]["fields"][name] = value
272+
return new
273+
274+
return updater
275+
276+
239277
def get_product_from_storage(
240278
storage: "commercetools.testing.Storage",
241279
product_id: uuid.UUID = None,

0 commit comments

Comments
 (0)