11from __future__ import annotations
22
3- import json
43from unittest import mock
54from unittest .mock import Mock
65
1615MOCKED_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
4344class 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