Skip to content

Commit 7a327d4

Browse files
authored
chore: update integrations test for impit with mocks for impit.Response.json (#526)
1 parent a08300a commit 7a327d4

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

tests/integration/test_dataset.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import json
34
from unittest import mock
45
from unittest.mock import Mock
56

@@ -80,7 +81,9 @@ def test_public_url(self, api_token: str, api_url: str, api_public_url: str) ->
8081
dataset = apify_client.dataset('someID')
8182

8283
# Mock the API call to return predefined response
83-
with mock.patch.object(apify_client.http_client, 'call', return_value=Mock(text=MOCKED_API_DATASET_RESPONSE)):
84+
mock_response = Mock()
85+
mock_response.json.return_value = json.loads(MOCKED_API_DATASET_RESPONSE)
86+
with mock.patch.object(apify_client.http_client, 'call', return_value=mock_response):
8487
public_url = dataset.create_items_public_url()
8588
assert public_url == (
8689
f'{(api_public_url or DEFAULT_API_URL).strip("/")}/v2/datasets/'
@@ -135,7 +138,9 @@ async def test_public_url(self, api_token: str, api_url: str, api_public_url: st
135138
dataset = apify_client.dataset('someID')
136139

137140
# Mock the API call to return predefined response
138-
with mock.patch.object(apify_client.http_client, 'call', return_value=Mock(text=MOCKED_API_DATASET_RESPONSE)):
141+
mock_response = Mock()
142+
mock_response.json.return_value = json.loads(MOCKED_API_DATASET_RESPONSE)
143+
with mock.patch.object(apify_client.http_client, 'call', return_value=mock_response):
139144
public_url = await dataset.create_items_public_url()
140145
assert public_url == (
141146
f'{(api_public_url or DEFAULT_API_URL).strip("/")}/v2/datasets/'

tests/integration/test_key_value_store.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import json
43
from unittest import mock
54
from unittest.mock import Mock
65

@@ -16,7 +15,7 @@
1615
MOCKED_ID = 'someID'
1716

1817

19-
def _get_mocked_api_kvs_response(signing_key: str | None = None) -> str:
18+
def _get_mocked_api_kvs_response(signing_key: str | None = None) -> Mock:
2019
response_data = {
2120
'data': {
2221
'id': MOCKED_ID,
@@ -37,7 +36,9 @@ def _get_mocked_api_kvs_response(signing_key: str | None = None) -> str:
3736
if signing_key:
3837
response_data['data']['urlSigningSecretKey'] = signing_key
3938

40-
return json.dumps(response_data)
39+
mock_response = Mock()
40+
mock_response.json.return_value = response_data
41+
return mock_response
4142

4243

4344
class TestKeyValueStoreSync:
@@ -87,7 +88,7 @@ def test_public_url(self, api_token: str, api_url: str, api_public_url: str, sig
8788
with mock.patch.object(
8889
apify_client.http_client,
8990
'call',
90-
return_value=Mock(text=_get_mocked_api_kvs_response(signing_key=signing_key)),
91+
return_value=_get_mocked_api_kvs_response(signing_key=signing_key),
9192
):
9293
public_url = kvs.create_keys_public_url()
9394
if signing_key:
@@ -112,7 +113,7 @@ def test_record_public_url(self, api_token: str, api_url: str, api_public_url: s
112113
with mock.patch.object(
113114
apify_client.http_client,
114115
'call',
115-
return_value=Mock(text=_get_mocked_api_kvs_response(signing_key=signing_key)),
116+
return_value=_get_mocked_api_kvs_response(signing_key=signing_key),
116117
):
117118
public_url = kvs.get_record_public_url(key=key)
118119
expected_signature = f'?signature={create_hmac_signature(signing_key, key)}' if signing_key else ''
@@ -170,13 +171,12 @@ async def test_key_value_store_should_create_public_keys_non_expiring_url(
170171
async def test_public_url(self, api_token: str, api_url: str, api_public_url: str, signing_key: str) -> None:
171172
apify_client = ApifyClientAsync(token=api_token, api_url=api_url, api_public_url=api_public_url)
172173
kvs = apify_client.key_value_store(MOCKED_ID)
173-
mocked_response = _get_mocked_api_kvs_response(signing_key=signing_key)
174174

175175
# Mock the API call to return predefined response
176176
with mock.patch.object(
177177
apify_client.http_client,
178178
'call',
179-
return_value=Mock(text=mocked_response),
179+
return_value=_get_mocked_api_kvs_response(signing_key=signing_key),
180180
):
181181
public_url = await kvs.create_keys_public_url()
182182
if signing_key:
@@ -201,7 +201,7 @@ async def test_record_public_url(self, api_token: str, api_url: str, api_public_
201201
with mock.patch.object(
202202
apify_client.http_client,
203203
'call',
204-
return_value=Mock(text=_get_mocked_api_kvs_response(signing_key=signing_key)),
204+
return_value=_get_mocked_api_kvs_response(signing_key=signing_key),
205205
):
206206
public_url = await kvs.get_record_public_url(key=key)
207207
expected_signature = f'?signature={create_hmac_signature(signing_key, key)}' if signing_key else ''

0 commit comments

Comments
 (0)