Skip to content

Commit 9853460

Browse files
davidweteringsmvantellingen
authored andcommitted
Add testing backend for CustomObject query by container
1 parent 5fcf673 commit 9853460

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/commercetools/testing/custom_objects.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,18 @@ def urls(self):
4646
("^$", "GET", self.query),
4747
("^$", "POST", self.create),
4848
("^(?P<container>[^/]+)/(?P<key>[^/]+)$", "GET", self.get_by_container_key),
49+
("^(?P<container>[^/]+)$", "GET", self.query_by_container),
4950
]
5051

52+
def query_by_container(self, request, container: str):
53+
# container is not a valid predicate filter, but we can abuse it internally.
54+
if "where" not in request.qs:
55+
request.qs["where"] = f'container = "{container}"'
56+
else:
57+
request.qs["where"] = f'container = "{container}" AND {request.qs["where"]}'
58+
59+
return self.query(request)
60+
5161
def get_by_container_key(self, request, container: str, key: str):
5262
item = next(
5363
(

tests/test_service_custom_objects.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,21 @@ def test_custom_object_update(client):
5959
types.CustomObjectDraft(container="unittest", key="test-object-1", value=2345)
6060
)
6161
assert custom_object.key == "test-object-1"
62+
63+
64+
def test_custom_object_query_by_container(client):
65+
"""Test filtering by container."""
66+
client.custom_objects.create_or_update(
67+
types.CustomObjectDraft(container="unittest", key="test-object-1", value=1234)
68+
)
69+
client.custom_objects.create_or_update(
70+
types.CustomObjectDraft(container="unittest", key="test-object-2", value=1234)
71+
)
72+
client.custom_objects.create_or_update(
73+
types.CustomObjectDraft(container="unittest2", key="test-object-1", value=1234)
74+
)
75+
76+
result = client.custom_objects.query_by_container("unittest")
77+
assert len(result.results) == 2
78+
assert result.total == 2
79+

0 commit comments

Comments
 (0)