Skip to content

Commit d81659f

Browse files
committed
Fix serialize errors
1 parent 46e7aa1 commit d81659f

File tree

5 files changed

+98
-10
lines changed

5 files changed

+98
-10
lines changed

src/commercetools/testing/cart_discounts.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import datetime
23
import typing
34
import uuid
@@ -64,6 +65,22 @@ def urls(self):
6465
("^(?P<id>[^/]+)$", "DELETE", self.delete_by_id),
6566
]
6667

68+
def set_valid_from(
69+
self, obj, action: models.CartDiscountSetValidFromAction
70+
):
71+
# real API always increments version, so always apply new value.
72+
new = copy.deepcopy(obj)
73+
new["valid_from"] = action.valid_from.isoformat()
74+
return new
75+
76+
def set_valid_until(
77+
self, obj, action: models.CartDiscountSetValidUntilAction
78+
):
79+
# real API always increments version, so always apply new value.
80+
new = copy.deepcopy(obj)
81+
new["valid_until"] = action.valid_until.isoformat()
82+
return new
83+
6784
_actions = {
6885
"setKey": update_attribute("key", "key"),
6986
"changeSortOrder": update_attribute("sortOrder", "sort_order"),
@@ -72,6 +89,6 @@ def urls(self):
7289
"setName": update_attribute("name", "name"),
7390
"setDescription": update_attribute("description", "description"),
7491
"setCartPredicate": update_attribute("cartPredicate", "cart_predicate"),
75-
"setValidFrom": update_attribute("valid_from", "valid_from"),
76-
"setValidUntil": update_attribute("valid_until", "valid_until")
92+
"setValidFrom": set_valid_from,
93+
"setValidUntil": set_valid_until
7794
}

src/commercetools/testing/discount_codes.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import datetime
23
import typing
34
import uuid
@@ -58,6 +59,30 @@ def urls(self):
5859
("^(?P<id>[^/]+)$", "POST", self.update_by_id),
5960
]
6061

62+
def set_valid_from(
63+
self, obj, action: models.DiscountCodeSetValidFromAction
64+
):
65+
# real API always increments version, so always apply new value.
66+
new = copy.deepcopy(obj)
67+
new["valid_from"] = action.valid_from.isoformat()
68+
return new
69+
70+
def set_valid_until(
71+
self, obj, action: models.DiscountCodeSetValidUntilAction
72+
):
73+
# real API always increments version, so always apply new value.
74+
new = copy.deepcopy(obj)
75+
new["valid_until"] = action.valid_until.isoformat()
76+
return new
77+
78+
def change_cart_discounts(
79+
self, obj, action: models.DiscountCodeChangeCartDiscountsAction
80+
):
81+
# real API always increments version, so always apply new value.
82+
new = copy.deepcopy(obj)
83+
new["cart_discounts"] = [cart_discount.serialize() for cart_discount in action.cart_discounts]
84+
return new
85+
6186
_actions = {
6287
"changeIsActive": update_attribute("isActive", "is_active"),
6388
"setName": update_attribute("name", "name"),
@@ -67,6 +92,7 @@ def urls(self):
6792
"setMaxApplicationsPerCustomer": update_attribute(
6893
"maxApplicationsPerCustomer", "max_applications_per_customer"
6994
),
70-
"setValidFrom": update_attribute("valid_from", "valid_from"),
71-
"setValidUntil": update_attribute("valid_until", "valid_until")
95+
"setValidFrom": set_valid_from,
96+
"setValidUntil": set_valid_until,
97+
"changeCartDiscounts": change_cart_discounts
7298
}

src/commercetools/testing/extensions.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import datetime
23
import typing
34
import uuid
@@ -51,6 +52,14 @@ def urls(self):
5152
("^key=(?P<key>[^/]+)$", "DELETE", self.delete_by_key),
5253
]
5354

55+
def change_triggers(
56+
self, obj, action: models.ExtensionChangeTriggersAction
57+
):
58+
# real API always increments version, so always apply new value.
59+
new = copy.deepcopy(obj)
60+
new["triggers"] = [trigger.serialize() for trigger in action.triggers]
61+
return new
62+
5463
_actions = {
55-
"changeTriggers": update_attribute("triggers", "triggers"),
64+
"changeTriggers": change_triggers,
5665
}

tests/platform/test_service_cart_discounts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def test_cart_discount_set_valid_from(old_client):
114114
actions=[models.CartDiscountSetValidFromAction(valid_from=datetime.now())],
115115
)
116116

117-
assert cart_discount.valid_from == datetime.now()
117+
assert cart_discount.valid_from == datetime.now().isoformat()
118118

119119

120120
@pytest.mark.freeze_time("2021-03-01 12:34:56")
@@ -137,4 +137,4 @@ def test_cart_discount_set_valid_until(old_client):
137137
actions=[models.CartDiscountSetValidUntilAction(valid_until=datetime.now())],
138138
)
139139

140-
assert cart_discount.valid_until == datetime.now()
140+
assert cart_discount.valid_until == datetime.now().isoformat()

tests/platform/test_service_discount_codes.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_discount_code_update(old_client):
7373
assert discount_code.is_active is False
7474

7575

76-
@pytest.mark.freeze_time("2021-03-01 12:34:56")
76+
@pytest.mark.freeze_time("2021-03-01")
7777
def test_discount_code_set_valid_from(old_client):
7878
discount_code = old_client.discount_codes.create(
7979
models.DiscountCodeDraft(
@@ -95,7 +95,7 @@ def test_discount_code_set_valid_from(old_client):
9595
assert discount_code.valid_from == datetime.now()
9696

9797

98-
@pytest.mark.freeze_time("2021-03-01 12:34:56")
98+
@pytest.mark.freeze_time("2021-03-01")
9999
def test_discount_code_set_valid_until(old_client):
100100
discount_code = old_client.discount_codes.create(
101101
models.DiscountCodeDraft(
@@ -114,4 +114,40 @@ def test_discount_code_set_valid_until(old_client):
114114
actions=[models.DiscountCodeSetValidUntilAction(valid_until=datetime.now())],
115115
)
116116

117-
assert discount_code.valid_until == datetime.now()
117+
assert discount_code.version == 2
118+
assert discount_code.valid_until == datetime.now().isoformat()
119+
120+
121+
def test_discount_code_change_cart_discounts(old_client):
122+
discount_code = old_client.discount_codes.create(
123+
models.DiscountCodeDraft(
124+
name=models.LocalizedString(en="en-discount_code"),
125+
code="1337",
126+
is_active=True,
127+
cart_discounts=[],
128+
)
129+
)
130+
assert discount_code.id
131+
assert discount_code.cart_discounts == []
132+
133+
cart_discount = old_client.cart_discounts.create(
134+
models.CartDiscountDraft(
135+
name=models.LocalizedString(en="cart-discount-test"),
136+
value=models.CartDiscountValueDraft(type="absolute"),
137+
cart_predicate="sku",
138+
sort_order="1",
139+
requires_discount_code=True,
140+
)
141+
)
142+
assert cart_discount.id
143+
144+
discount_code = old_client.discount_codes.update_by_id(
145+
id=discount_code.id,
146+
version=discount_code.version,
147+
actions=[models.DiscountCodeChangeCartDiscountsAction(
148+
cart_discounts=[models.CartDiscountResourceIdentifier(id=cart_discount.id)]
149+
)],
150+
)
151+
152+
assert discount_code.version == 2
153+
assert discount_code.cart_discounts == cart_discount

0 commit comments

Comments
 (0)