Skip to content

Commit b6e9985

Browse files
authored
Add missing API actions to testing backend
2 parents 6e97a44 + 61fa7c8 commit b6e9985

File tree

7 files changed

+237
-2
lines changed

7 files changed

+237
-2
lines changed

src/commercetools/testing/cart_discounts.py

Lines changed: 8 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
@@ -11,7 +12,11 @@
1112
)
1213
from commercetools.testing import utils
1314
from commercetools.testing.abstract import BaseModel, ServiceBackend
14-
from commercetools.testing.utils import update_attribute, update_enum_attribute
15+
from commercetools.testing.utils import (
16+
update_attribute,
17+
update_datetime_attribute,
18+
update_enum_attribute,
19+
)
1520

1621

1722
class CartDiscountsModel(BaseModel):
@@ -72,4 +77,6 @@ def urls(self):
7277
"setName": update_attribute("name", "name"),
7378
"setDescription": update_attribute("description", "description"),
7479
"setCartPredicate": update_attribute("cartPredicate", "cart_predicate"),
80+
"setValidFrom": update_datetime_attribute("validFrom", "valid_from"),
81+
"setValidUntil": update_datetime_attribute("validUntil", "valid_until"),
7582
}

src/commercetools/testing/discount_codes.py

Lines changed: 11 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
@@ -11,7 +12,11 @@
1112
)
1213
from commercetools.testing import utils
1314
from commercetools.testing.abstract import BaseModel, ServiceBackend
14-
from commercetools.testing.utils import update_attribute
15+
from commercetools.testing.utils import (
16+
update_attribute,
17+
update_datetime_attribute,
18+
update_nested_object_attribute,
19+
)
1520

1621

1722
class DiscountCodesModel(BaseModel):
@@ -67,4 +72,9 @@ def urls(self):
6772
"setMaxApplicationsPerCustomer": update_attribute(
6873
"maxApplicationsPerCustomer", "max_applications_per_customer"
6974
),
75+
"setValidFrom": update_datetime_attribute("validFrom", "valid_from"),
76+
"setValidUntil": update_datetime_attribute("validUntil", "valid_until"),
77+
"changeCartDiscounts": update_nested_object_attribute(
78+
"cartDiscounts", "cart_discounts"
79+
),
7080
}

src/commercetools/testing/extensions.py

Lines changed: 6 additions & 0 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
@@ -10,6 +11,7 @@
1011
ExtensionUpdateSchema,
1112
)
1213
from commercetools.testing.abstract import BaseModel, ServiceBackend
14+
from commercetools.testing.utils import update_attribute, update_nested_object_attribute
1315

1416

1517
class ExtensionsModel(BaseModel):
@@ -49,3 +51,7 @@ def urls(self):
4951
("^(?P<id>[^/]+)$", "DELETE", self.delete_by_id),
5052
("^key=(?P<key>[^/]+)$", "DELETE", self.delete_by_key),
5153
]
54+
55+
_actions = {
56+
"changeTriggers": update_nested_object_attribute("triggers", "triggers"),
57+
}

src/commercetools/testing/utils.py

Lines changed: 35 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,40 @@ 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.isoformat():
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, list):
138+
raise TypeError(f"Unsupported nested object type: f{type(values)}")
139+
140+
items = [item.serialize() for item in values]
141+
if items != obj.get(dst):
142+
new = copy.deepcopy(obj)
143+
new[dst] = items
144+
return new
145+
146+
return obj
147+
148+
return updater
149+
150+
116151
def update_enum_attribute(dst: str, src: str):
117152
def updater(self, obj, action):
118153
value = getattr(action, src).value

tests/platform/test_service_cart_discounts.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from datetime import datetime
2+
13
import pytest
4+
from freezegun import freeze_time
25
from requests.exceptions import HTTPError
36

47
from commercetools.platform import models
@@ -90,3 +93,51 @@ def test_cart_discount_update(old_client):
9093
)
9194

9295
assert cart_discount.is_active is False
96+
97+
98+
@freeze_time("2021-03-01 12:34:56")
99+
def test_cart_discount_set_valid_from(old_client):
100+
cart_discount = old_client.cart_discounts.create(
101+
models.CartDiscountDraft(
102+
name=models.LocalizedString(en="en-cart_discount"),
103+
value=models.CartDiscountValueRelative(permyriad=10),
104+
is_active=True,
105+
cart_predicate="",
106+
sort_order="",
107+
requires_discount_code=False,
108+
)
109+
)
110+
assert cart_discount.id
111+
assert cart_discount.valid_from is None
112+
113+
cart_discount = old_client.cart_discounts.update_by_id(
114+
id=cart_discount.id,
115+
version=cart_discount.version,
116+
actions=[models.CartDiscountSetValidFromAction(valid_from=datetime.now())],
117+
)
118+
119+
assert cart_discount.valid_from == datetime.now()
120+
121+
122+
@freeze_time("2021-03-01 12:34:56")
123+
def test_cart_discount_set_valid_until(old_client):
124+
cart_discount = old_client.cart_discounts.create(
125+
models.CartDiscountDraft(
126+
name=models.LocalizedString(en="en-cart_discount"),
127+
value=models.CartDiscountValueRelative(permyriad=10),
128+
is_active=True,
129+
cart_predicate="",
130+
sort_order="",
131+
requires_discount_code=False,
132+
)
133+
)
134+
assert cart_discount.id
135+
assert cart_discount.valid_until is None
136+
137+
cart_discount = old_client.cart_discounts.update_by_id(
138+
id=cart_discount.id,
139+
version=cart_discount.version,
140+
actions=[models.CartDiscountSetValidUntilAction(valid_until=datetime.now())],
141+
)
142+
143+
assert cart_discount.valid_until == datetime.now()

tests/platform/test_service_discount_codes.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from datetime import datetime
2+
13
import pytest
4+
from freezegun import freeze_time
25
from requests.exceptions import HTTPError
36

47
from commercetools.platform import models
@@ -69,3 +72,89 @@ def test_discount_code_update(old_client):
6972
)
7073

7174
assert discount_code.is_active is False
75+
76+
77+
@freeze_time("2021-03-01 12:34:56")
78+
def test_discount_code_set_valid_from(old_client):
79+
discount_code = old_client.discount_codes.create(
80+
models.DiscountCodeDraft(
81+
name=models.LocalizedString(en="en-discount_code"),
82+
code="1337",
83+
is_active=True,
84+
cart_discounts=[],
85+
)
86+
)
87+
assert discount_code.id
88+
assert discount_code.valid_from is None
89+
90+
discount_code = old_client.discount_codes.update_by_id(
91+
id=discount_code.id,
92+
version=discount_code.version,
93+
actions=[models.DiscountCodeSetValidFromAction(valid_from=datetime.now())],
94+
)
95+
96+
assert discount_code.valid_from == datetime.now()
97+
98+
99+
@freeze_time("2021-03-01 12:34:56")
100+
def test_discount_code_set_valid_until(old_client):
101+
discount_code = old_client.discount_codes.create(
102+
models.DiscountCodeDraft(
103+
name=models.LocalizedString(en="en-discount_code"),
104+
code="1337",
105+
is_active=True,
106+
cart_discounts=[],
107+
)
108+
)
109+
assert discount_code.id
110+
assert discount_code.valid_until is None
111+
112+
discount_code = old_client.discount_codes.update_by_id(
113+
id=discount_code.id,
114+
version=discount_code.version,
115+
actions=[models.DiscountCodeSetValidUntilAction(valid_until=datetime.now())],
116+
)
117+
118+
assert discount_code.version == 2
119+
assert discount_code.valid_until == datetime.now()
120+
121+
122+
def test_discount_code_change_cart_discounts(old_client):
123+
discount_code = old_client.discount_codes.create(
124+
models.DiscountCodeDraft(
125+
name=models.LocalizedString(en="en-discount_code"),
126+
code="1337",
127+
is_active=True,
128+
cart_discounts=[],
129+
)
130+
)
131+
assert discount_code.id
132+
assert discount_code.cart_discounts == []
133+
134+
cart_discount = old_client.cart_discounts.create(
135+
models.CartDiscountDraft(
136+
name=models.LocalizedString(en="cart-discount-test"),
137+
value=models.CartDiscountValueDraft(type="absolute"),
138+
cart_predicate="sku",
139+
sort_order="1",
140+
requires_discount_code=True,
141+
)
142+
)
143+
assert cart_discount.id
144+
145+
discount_code = old_client.discount_codes.update_by_id(
146+
id=discount_code.id,
147+
version=discount_code.version,
148+
actions=[
149+
models.DiscountCodeChangeCartDiscountsAction(
150+
cart_discounts=[
151+
models.CartDiscountResourceIdentifier(id=cart_discount.id)
152+
]
153+
)
154+
],
155+
)
156+
157+
assert discount_code.version == 2
158+
assert discount_code.cart_discounts == [
159+
models.CartDiscountReference(id=cart_discount.id)
160+
]

tests/platform/test_service_extensions.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,40 @@ def test_extension_get_by_id(old_client):
2626

2727
extension = old_client.extensions.get_by_id(extension.id)
2828
assert extension.id
29+
30+
31+
def test_extension_update_change_triggers(old_client):
32+
extension = old_client.extensions.create(
33+
models.ExtensionDraft(
34+
destination=models.ExtensionAWSLambdaDestination(
35+
arn="arn:", access_key="access", access_secret="secret"
36+
),
37+
triggers=[],
38+
)
39+
)
40+
assert extension.id
41+
assert extension.triggers == []
42+
43+
extension = old_client.extensions.update_by_id(
44+
id=extension.id,
45+
version=extension.version,
46+
actions=[
47+
models.ExtensionChangeTriggersAction(
48+
triggers=[
49+
models.ExtensionTrigger(
50+
resource_type_id=models.ExtensionResourceTypeId.CART,
51+
actions=[
52+
models.ExtensionAction.CREATE,
53+
models.ExtensionAction.UPDATE,
54+
],
55+
)
56+
]
57+
)
58+
],
59+
)
60+
assert extension.triggers == [
61+
models.ExtensionTrigger(
62+
resource_type_id=models.ExtensionResourceTypeId.CART,
63+
actions=[models.ExtensionAction.CREATE, models.ExtensionAction.UPDATE],
64+
)
65+
]

0 commit comments

Comments
 (0)