|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +from unittest import mock |
| 4 | +from unittest.mock import Mock |
| 5 | + |
3 | 6 | import impit
|
4 | 7 |
|
5 | 8 | from integration.conftest import parametrized_api_urls
|
|
8 | 11 | from apify_client import ApifyClient, ApifyClientAsync
|
9 | 12 | from apify_client.client import DEFAULT_API_URL
|
10 | 13 |
|
| 14 | +MOCKED_API_KVS_RESPONSE = """{ |
| 15 | + "data": { |
| 16 | + "id": "someID", |
| 17 | + "name": "name", |
| 18 | + "userId": "userId", |
| 19 | + "createdAt": "2025-09-11T08:48:51.806Z", |
| 20 | + "modifiedAt": "2025-09-11T08:48:51.806Z", |
| 21 | + "accessedAt": "2025-09-11T08:48:51.806Z", |
| 22 | + "actId": null, |
| 23 | + "actRunId": null, |
| 24 | + "schema": null, |
| 25 | + "stats": { |
| 26 | + "readCount": 0, |
| 27 | + "writeCount": 0, |
| 28 | + "deleteCount": 0, |
| 29 | + "listCount": 0, |
| 30 | + "storageBytes": 0 |
| 31 | + }, |
| 32 | + "consoleUrl": "https://console.apify.com/storage/key-value-stores/someID", |
| 33 | + "keysPublicUrl": "https://api.apify.com/v2/key-value-stores/someID/keys", |
| 34 | + "generalAccess": "FOLLOW_USER_SETTING", |
| 35 | + "urlSigningSecretKey": "urlSigningSecretKey" |
| 36 | + } |
| 37 | +}""" |
| 38 | + |
11 | 39 |
|
12 | 40 | class TestKeyValueStoreSync:
|
13 | 41 | def test_key_value_store_should_create_expiring_keys_public_url_with_params(
|
@@ -49,24 +77,15 @@ def test_key_value_store_should_create_public_keys_non_expiring_url(self, apify_
|
49 | 77 | @parametrized_api_urls
|
50 | 78 | def test_public_url(self, api_token: str, api_url: str, api_public_url: str) -> None:
|
51 | 79 | apify_client = ApifyClient(token=api_token, api_url=api_url, api_public_url=api_public_url)
|
52 |
| - created_store = apify_client.key_value_stores().get_or_create(name=random_resource_name('key-value-store')) |
53 |
| - kvs = apify_client.key_value_store(created_store['id']) |
54 |
| - try: |
| 80 | + kvs = apify_client.key_value_store('someID') |
| 81 | + |
| 82 | + # Mock the API call to return predefined response |
| 83 | + with mock.patch.object(apify_client.http_client, 'call', return_value=Mock(text=MOCKED_API_KVS_RESPONSE)): |
55 | 84 | public_url = kvs.create_keys_public_url()
|
56 | 85 | assert public_url == (
|
57 | 86 | f'{(api_public_url or DEFAULT_API_URL).strip("/")}/v2/key-value-stores/'
|
58 |
| - f'{created_store["id"]}/keys?signature={public_url.split("signature=")[1]}' |
| 87 | + f'someID/keys?signature={public_url.split("signature=")[1]}' |
59 | 88 | )
|
60 |
| - finally: |
61 |
| - kvs.delete() |
62 |
| - |
63 |
| - def test_public_url_nonexistent_host(self, api_token: str) -> None: |
64 |
| - kvs_name = 'whatever' |
65 |
| - non_existent_url = 'http://10.0.88.214:8010' |
66 |
| - apify_client = ApifyClient(token=api_token, api_url=non_existent_url) |
67 |
| - kvs_client = apify_client.key_value_store(key_value_store_id=kvs_name) |
68 |
| - assert kvs_client._url() == f'{non_existent_url}/v2/key-value-stores/{kvs_name}' |
69 |
| - assert kvs_client._url(public=True) == f'{DEFAULT_API_URL}/v2/key-value-stores/{kvs_name}' |
70 | 89 |
|
71 | 90 |
|
72 | 91 | class TestKeyValueStoreAsync:
|
@@ -115,23 +134,12 @@ async def test_key_value_store_should_create_public_keys_non_expiring_url(
|
115 | 134 | @parametrized_api_urls
|
116 | 135 | async def test_public_url(self, api_token: str, api_url: str, api_public_url: str) -> None:
|
117 | 136 | apify_client = ApifyClientAsync(token=api_token, api_url=api_url, api_public_url=api_public_url)
|
118 |
| - created_store = await apify_client.key_value_stores().get_or_create( |
119 |
| - name=random_resource_name('key-value-store') |
120 |
| - ) |
121 |
| - kvs = apify_client.key_value_store(created_store['id']) |
122 |
| - try: |
| 137 | + kvs = apify_client.key_value_store('someID') |
| 138 | + |
| 139 | + # Mock the API call to return predefined response |
| 140 | + with mock.patch.object(apify_client.http_client, 'call', return_value=Mock(text=MOCKED_API_KVS_RESPONSE)): |
123 | 141 | public_url = await kvs.create_keys_public_url()
|
124 | 142 | assert public_url == (
|
125 | 143 | f'{(api_public_url or DEFAULT_API_URL).strip("/")}/v2/key-value-stores/'
|
126 |
| - f'{created_store["id"]}/keys?signature={public_url.split("signature=")[1]}' |
| 144 | + f'someID/keys?signature={public_url.split("signature=")[1]}' |
127 | 145 | )
|
128 |
| - finally: |
129 |
| - await kvs.delete() |
130 |
| - |
131 |
| - async def test_public_url_nonexistent_host(self, api_token: str) -> None: |
132 |
| - kvs_name = 'whatever' |
133 |
| - non_existent_url = 'http://10.0.88.214:8010' |
134 |
| - apify_client = ApifyClientAsync(token=api_token, api_url=non_existent_url) |
135 |
| - kvs_client = apify_client.key_value_store(key_value_store_id=kvs_name) |
136 |
| - assert kvs_client._url() == f'{non_existent_url}/v2/key-value-stores/{kvs_name}' |
137 |
| - assert kvs_client._url(public=True) == f'{DEFAULT_API_URL}/v2/key-value-stores/{kvs_name}' |
|
0 commit comments