Skip to content

Commit 7e1f86b

Browse files
committed
Rewrite tests to use the new-style API
1 parent d2e7b1f commit 7e1f86b

33 files changed

Lines changed: 1978 additions & 1066 deletions

src/commercetools/base_client.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(
5252
) -> None:
5353
# Use environment variables as fallback
5454
config = {
55-
"project_key": project_key,
55+
"project_key": project_key or "example-project",
5656
"client_id": client_id,
5757
"client_secret": client_secret,
5858
"url": url,
@@ -230,9 +230,3 @@ def _get_user_agent(self):
230230
sys.platform,
231231
arch,
232232
)
233-
234-
235-
class Client(BaseClient):
236-
def __init__(self, *args, **kwargs):
237-
super().__init__(*args, **kwargs)
238-
self._base_url = f"{self._config['url']}/{self._config['project_key']}/"

src/commercetools/contrib/pytest.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import pytest
66
import requests
77

8-
from commercetools import Client
98
from commercetools.platform.client import Client as PlatformClient
109
from commercetools.testing import backend_mocker
1110
from commercetools.testing.server import Server
@@ -25,7 +24,9 @@ def commercetools_api():
2524

2625

2726
@pytest.fixture
28-
def ct_platform_client(commercetools_api) -> typing.Generator[Client, None, None]:
27+
def ct_platform_client(
28+
commercetools_api,
29+
) -> typing.Generator[PlatformClient, None, None]:
2930
yield PlatformClient(
3031
client_id="client-id",
3132
client_secret="client-secret",
@@ -35,18 +36,6 @@ def ct_platform_client(commercetools_api) -> typing.Generator[Client, None, None
3536
)
3637

3738

38-
@pytest.fixture
39-
def commercetools_client(commercetools_api) -> typing.Generator[Client, None, None]:
40-
yield Client(
41-
project_key="unittest",
42-
client_id="client-id",
43-
client_secret="client-secret",
44-
scope=[],
45-
url="https://api.europe-west1.gcp.commercetools.com",
46-
token_url="https://auth.europe-west1.gcp.commercetools.com/oauth/token",
47-
)
48-
49-
5039
@pytest.fixture()
5140
def commercetools_http_server(commercetools_api):
5241
is_running = threading.Event()

src/commercetools/testing/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def introspect(self, request):
9191
if token in stored_tokens:
9292
status = {
9393
"active": True,
94-
"scope": "manage_project:todo",
94+
"scope": "manage_project:example-project",
9595
"exp": self._expire_time,
9696
}
9797
else:

src/commercetools/testing/product_projections.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import typing
22

33
from marshmallow import fields
4+
45
from commercetools.platform import models
56
from commercetools.platform.models._schemas.product import (
67
ProductProjectionPagedQueryResponseSchema,
@@ -11,6 +12,7 @@
1112
from commercetools.testing.abstract import ServiceBackend
1213
from commercetools.testing.utils import create_commercetools_response
1314

15+
1416
class _ProductProjectionQuerySchema(
1517
traits.ExpandableSchema,
1618
traits.SortableSchema,
@@ -21,7 +23,6 @@ class _ProductProjectionQuerySchema(
2123
staged = fields.Bool(required=False, missing=False)
2224

2325

24-
2526
class ProductProjectionsBackend(ServiceBackend):
2627
service_path = "product-projections"
2728
_schema_query_params = _ProductProjectionQuerySchema

tests/platform/test_client.py

Lines changed: 93 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from requests_mock.adapter import Adapter
66

77
from commercetools import CommercetoolsError
8-
from commercetools.client import Client
98
from commercetools.platform import models
9+
from commercetools.platform.client import Client
1010

1111

1212
@pytest.fixture()
@@ -34,14 +34,13 @@ def test_auto_refresh(commercetools_api):
3434
client = Client(
3535
client_id="unittest",
3636
client_secret="mysecret",
37-
project_key="test",
3837
url="https://api.europe-west1.gcp.commercetools.com",
3938
token_url="https://auth.europe-west1.gcp.commercetools.com/oauth/token",
40-
)
41-
client.products.query()
39+
).with_project_key("unittest")
40+
client.products().get()
4241
time.sleep(1)
43-
client.products.query()
44-
client.products.query()
42+
client.products().get()
43+
client.products().get()
4544

4645
auth_headers = set()
4746
for request in commercetools_api.requests_mock.request_history:
@@ -56,7 +55,7 @@ def test_cache_token(commercetools_api):
5655
Client(
5756
client_id="unittest",
5857
client_secret="none",
59-
project_key="test",
58+
scope=["manage_project:test"],
6059
url="https://api.europe-west1.gcp.commercetools.com",
6160
token_url="https://auth.europe-west1.gcp.commercetools.com/oauth/token",
6261
)
@@ -90,90 +89,135 @@ def test_allows_passing_custom_http_adapter():
9089
assert len(my_adapter.request_history) == 1
9190

9291

93-
def test_resource_update_conflict(old_client):
92+
def test_resource_update_conflict(ct_platform_client: Client):
9493
"""Test the return value of the update methods.
9594
9695
It doesn't test the actual update itself.
9796
TODO: See if this is worth testing since we're using a mocking backend
9897
"""
99-
product = old_client.products.create(
100-
models.ProductDraft(
101-
key="test-product",
102-
product_type=models.ProductTypeResourceIdentifier(key="dummy"),
103-
name={"en": "my-product"},
104-
slug={"en": "foo-bar"},
98+
product = (
99+
ct_platform_client.with_project_key("unittest")
100+
.products()
101+
.post(
102+
models.ProductDraft(
103+
key="test-product",
104+
product_type=models.ProductTypeResourceIdentifier(key="dummy"),
105+
name={"en": "my-product"},
106+
slug={"en": "foo-bar"},
107+
)
105108
)
106109
)
107110

108111
assert product.version == 1
109112
assert uuid.UUID(product.id)
110113
assert product.key == "test-product"
111114

112-
product = old_client.products.update_by_id(
113-
id=product.id,
114-
version=product.version,
115-
actions=[
116-
models.ProductChangeSlugAction(slug=models.LocalizedString(nl="nl-slug2"))
117-
],
115+
product = (
116+
ct_platform_client.with_project_key("unittest")
117+
.products()
118+
.with_id(product.id)
119+
.post(
120+
models.ProductUpdate(
121+
version=product.version,
122+
actions=[
123+
models.ProductChangeSlugAction(
124+
slug=models.LocalizedString(nl="nl-slug2")
125+
)
126+
],
127+
)
128+
)
118129
)
119130
assert product.key == "test-product"
120131
assert product.version == 2
121132

122133
# This should raise a version conflict error
123134
with pytest.raises(CommercetoolsError) as exc:
124-
product = old_client.products.update_by_id(
125-
id=product.id,
126-
version=1,
127-
actions=[
128-
models.ProductChangeSlugAction(
129-
slug=models.LocalizedString(nl="nl-slug3")
135+
product = (
136+
ct_platform_client.with_project_key("unittest")
137+
.products()
138+
.with_id(product.id)
139+
.post(
140+
models.ProductUpdate(
141+
version=1,
142+
actions=[
143+
models.ProductChangeSlugAction(
144+
slug=models.LocalizedString(nl="nl-slug3")
145+
)
146+
],
130147
)
131-
],
148+
)
132149
)
133150
assert exc.value.response.status_code == 409
134151
assert exc.value.response.errors[0].current_version == 2
135152

136153
# Force it
137-
product = old_client.products.update_by_id(
138-
id=product.id,
139-
version=1,
140-
actions=[
141-
models.ProductChangeSlugAction(slug=models.LocalizedString(nl="nl-slug2"))
142-
],
143-
force_update=True,
154+
product = (
155+
ct_platform_client.with_project_key("unittest")
156+
.products()
157+
.with_id(product.id)
158+
.post(
159+
models.ProductUpdate(
160+
version=1,
161+
actions=[
162+
models.ProductChangeSlugAction(
163+
slug=models.LocalizedString(nl="nl-slug2")
164+
)
165+
],
166+
),
167+
options={"force_version": True},
168+
)
144169
)
145170

146171

147-
def test_resource_delete_conflict(old_client):
172+
def test_resource_delete_conflict(ct_platform_client: Client):
148173
"""Test the return value of the delete methods.
149174
150175
It doesn't test the actual delete itself.
151176
TODO: See if this is worth testing since we're using a mocking backend
152177
"""
153-
product = old_client.products.create(
154-
models.ProductDraft(
155-
key="test-product",
156-
product_type=models.ProductTypeResourceIdentifier(key="dummy"),
157-
name={"en": "my-product"},
158-
slug={"en": "foo-bar"},
178+
product = (
179+
ct_platform_client.with_project_key("unittest")
180+
.products()
181+
.post(
182+
models.ProductDraft(
183+
key="test-product",
184+
product_type=models.ProductTypeResourceIdentifier(key="dummy"),
185+
name={"en": "my-product"},
186+
slug={"en": "foo-bar"},
187+
)
159188
)
160189
)
161190

162-
product = old_client.products.update_by_id(
163-
id=product.id,
164-
version=product.version,
165-
actions=[
166-
models.ProductChangeSlugAction(slug=models.LocalizedString(nl="nl-slug2"))
167-
],
191+
product = (
192+
ct_platform_client.with_project_key("unittest")
193+
.products()
194+
.with_id(product.id)
195+
.post(
196+
models.ProductUpdate(
197+
version=product.version,
198+
actions=[
199+
models.ProductChangeSlugAction(
200+
slug=models.LocalizedString(nl="nl-slug2")
201+
)
202+
],
203+
),
204+
)
168205
)
169206
assert product.version == 2
170207

171208
# This should raise a version conflict error
172209
with pytest.raises(CommercetoolsError) as exc:
173-
product = old_client.products.delete_by_id(id=product.id, version=1)
210+
product = (
211+
ct_platform_client.with_project_key("unittest")
212+
.products()
213+
.with_id(product.id)
214+
.delete(version=1)
215+
)
174216

175217
assert exc.value.response.status_code == 409
176218
assert exc.value.response.errors[0].current_version == 2
177219

178220
# Force it
179-
old_client.products.delete_by_id(id=product.id, version=1, force_delete=True)
221+
ct_platform_client.with_project_key("unittest").products().with_id(
222+
product.id
223+
).delete(version=1, options={"force_version": True})

0 commit comments

Comments
 (0)