Skip to content

Commit 44e2d5d

Browse files
committed
Change to generic update functions
1 parent 0b644ae commit 44e2d5d

File tree

4 files changed

+43
-43
lines changed

4 files changed

+43
-43
lines changed

src/commercetools/testing/cart_discounts.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
)
1313
from commercetools.testing import utils
1414
from commercetools.testing.abstract import BaseModel, ServiceBackend
15-
from commercetools.testing.utils import update_attribute, update_enum_attribute
15+
from commercetools.testing.utils import update_attribute, update_enum_attribute, \
16+
update_datetime_attribute
1617

1718

1819
class CartDiscountsModel(BaseModel):
@@ -65,18 +66,6 @@ def urls(self):
6566
("^(?P<id>[^/]+)$", "DELETE", self.delete_by_id),
6667
]
6768

68-
def set_valid_from(self, obj, action: models.CartDiscountSetValidFromAction):
69-
# real API always increments version, so always apply new value.
70-
new = copy.deepcopy(obj)
71-
new["validFrom"] = action.valid_from.isoformat()
72-
return new
73-
74-
def set_valid_until(self, obj, action: models.CartDiscountSetValidUntilAction):
75-
# real API always increments version, so always apply new value.
76-
new = copy.deepcopy(obj)
77-
new["validUntil"] = action.valid_until.isoformat()
78-
return new
79-
8069
_actions = {
8170
"setKey": update_attribute("key", "key"),
8271
"changeSortOrder": update_attribute("sortOrder", "sort_order"),
@@ -85,6 +74,6 @@ def set_valid_until(self, obj, action: models.CartDiscountSetValidUntilAction):
8574
"setName": update_attribute("name", "name"),
8675
"setDescription": update_attribute("description", "description"),
8776
"setCartPredicate": update_attribute("cartPredicate", "cart_predicate"),
88-
"setValidFrom": set_valid_from,
89-
"setValidUntil": set_valid_until,
77+
"setValidFrom": update_datetime_attribute("validFrom", "valid_from"),
78+
"setValidUntil": update_datetime_attribute("validUntil", "valid_until"),
9079
}

src/commercetools/testing/discount_codes.py

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
)
1313
from commercetools.testing import utils
1414
from commercetools.testing.abstract import BaseModel, ServiceBackend
15-
from commercetools.testing.utils import update_attribute
15+
from commercetools.testing.utils import update_attribute, update_datetime_attribute, \
16+
update_nested_object_attribute
1617

1718

1819
class DiscountCodesModel(BaseModel):
@@ -59,28 +60,6 @@ def urls(self):
5960
("^(?P<id>[^/]+)$", "POST", self.update_by_id),
6061
]
6162

62-
def set_valid_from(self, obj, action: models.DiscountCodeSetValidFromAction):
63-
# real API always increments version, so always apply new value.
64-
new = copy.deepcopy(obj)
65-
new["validFrom"] = action.valid_from.isoformat()
66-
return new
67-
68-
def set_valid_until(self, obj, action: models.DiscountCodeSetValidUntilAction):
69-
# real API always increments version, so always apply new value.
70-
new = copy.deepcopy(obj)
71-
new["validUntil"] = action.valid_until.isoformat()
72-
return new
73-
74-
def change_cart_discounts(
75-
self, obj, action: models.DiscountCodeChangeCartDiscountsAction
76-
):
77-
# real API always increments version, so always apply new value.
78-
new = copy.deepcopy(obj)
79-
new["cartDiscounts"] = [
80-
cart_discount.serialize() for cart_discount in action.cart_discounts
81-
]
82-
return new
83-
8463
_actions = {
8564
"changeIsActive": update_attribute("isActive", "is_active"),
8665
"setName": update_attribute("name", "name"),
@@ -90,7 +69,7 @@ def change_cart_discounts(
9069
"setMaxApplicationsPerCustomer": update_attribute(
9170
"maxApplicationsPerCustomer", "max_applications_per_customer"
9271
),
93-
"setValidFrom": set_valid_from,
94-
"setValidUntil": set_valid_until,
95-
"changeCartDiscounts": change_cart_discounts,
72+
"setValidFrom": update_datetime_attribute("validFrom", "valid_from"),
73+
"setValidUntil": update_datetime_attribute("validUntil", "valid_until"),
74+
"changeCartDiscounts": update_nested_object_attribute("cartDiscounts", "cart_discounts"),
9675
}

src/commercetools/testing/extensions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
ExtensionUpdateSchema,
1212
)
1313
from commercetools.testing.abstract import BaseModel, ServiceBackend
14-
from commercetools.testing.utils import update_attribute
14+
from commercetools.testing.utils import update_attribute, update_nested_object_attribute
1515

1616

1717
class ExtensionsModel(BaseModel):
@@ -59,5 +59,5 @@ def change_triggers(self, obj, action: models.ExtensionChangeTriggersAction):
5959
return new
6060

6161
_actions = {
62-
"changeTriggers": change_triggers,
62+
"changeTriggers": update_nested_object_attribute("triggers", "triggers"),
6363
}

src/commercetools/testing/utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import typing
55
import uuid
6+
from datetime import datetime
67

78
from marshmallow import Schema
89
from requests_mock import create_response
@@ -113,6 +114,37 @@ def updater(self, obj, action):
113114
return updater
114115

115116

117+
def update_datetime_attribute(dst: str, src: str):
118+
def updater(self, obj, action):
119+
value = getattr(action, src)
120+
121+
if not isinstance(value, datetime):
122+
raise TypeError(f"Unsupported datetime object type: f{type(value)}")
123+
124+
if obj.get(dst) != value:
125+
new = copy.deepcopy(obj)
126+
new[dst] = value.isoformat()
127+
return new
128+
return obj
129+
130+
return updater
131+
132+
133+
def update_nested_object_attribute(dst: str, src: str):
134+
def updater(self, obj, action):
135+
values = getattr(action, src)
136+
137+
if not isinstance(values, typing.List):
138+
raise TypeError(f"Unsupported nested object type: f{type(values)}")
139+
140+
if obj.get(dst) != values:
141+
new = copy.deepcopy(obj)
142+
new[dst] = [item.serialize() for item in values]
143+
return new
144+
return obj
145+
146+
return updater
147+
116148
def update_enum_attribute(dst: str, src: str):
117149
def updater(self, obj, action):
118150
value = getattr(action, src).value

0 commit comments

Comments
 (0)