Skip to content

Commit a28026c

Browse files
committed
Refactor the tests to avoid real API calls
1 parent 77c5f20 commit a28026c

File tree

3 files changed

+78
-58
lines changed

3 files changed

+78
-58
lines changed

tests/integration/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
('https://api.apify.com', 'https://custom-public-url.com'),
1616
('https://api.apify.com', 'https://custom-public-url.com/with/custom/path'),
1717
('https://api.apify.com', 'https://custom-public-url.com/with/custom/path/'),
18+
('http://10.0.88.214:8010', 'https://api.apify.com'),
1819
],
1920
)
2021

tests/integration/test_dataset.py

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

3+
from unittest import mock
4+
from unittest.mock import Mock
5+
36
import impit
47

58
from integration.conftest import parametrized_api_urls
@@ -8,6 +11,32 @@
811
from apify_client import ApifyClient, ApifyClientAsync
912
from apify_client.client import DEFAULT_API_URL
1013

14+
MOCKED_API_DATASET_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+
"fields": [],
33+
"consoleUrl": "https://console.apify.com/storage/datasets/someID",
34+
"itemsPublicUrl": "https://api.apify.com/v2/datasets/someID/items",
35+
"generalAccess": "FOLLOW_USER_SETTING",
36+
"urlSigningSecretKey": "urlSigningSecretKey"
37+
}
38+
}"""
39+
1140

1241
class TestDatasetSync:
1342
def test_dataset_should_create_public_items_expiring_url_with_params(self, apify_client: ApifyClient) -> None:
@@ -49,24 +78,15 @@ def test_dataset_should_create_public_items_non_expiring_url(self, apify_client:
4978
@parametrized_api_urls
5079
def test_public_url(self, api_token: str, api_url: str, api_public_url: str) -> None:
5180
apify_client = ApifyClient(token=api_token, api_url=api_url, api_public_url=api_public_url)
52-
created_store = apify_client.datasets().get_or_create(name=random_resource_name('key-value-store'))
53-
dataset = apify_client.dataset(created_store['id'])
54-
try:
81+
dataset = apify_client.dataset('someID')
82+
83+
# Mock the API call to return predefined response
84+
with mock.patch.object(apify_client.http_client, 'call', return_value=Mock(text=MOCKED_API_DATASET_RESPONSE)):
5585
public_url = dataset.create_items_public_url()
5686
assert public_url == (
5787
f'{(api_public_url or DEFAULT_API_URL).strip("/")}/v2/datasets/'
58-
f'{created_store["id"]}/items?signature={public_url.split("signature=")[1]}'
88+
f'someID/items?signature={public_url.split("signature=")[1]}'
5989
)
60-
finally:
61-
dataset.delete()
62-
63-
def test_public_url_nonexistent_host(self, api_token: str) -> None:
64-
dataset_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.dataset(dataset_id=dataset_name)
68-
assert kvs_client._url() == f'{non_existent_url}/v2/datasets/{dataset_name}'
69-
assert kvs_client._url(public=True) == f'{DEFAULT_API_URL}/v2/datasets/{dataset_name}'
7090

7191

7292
class TestDatasetAsync:
@@ -113,21 +133,12 @@ async def test_dataset_should_create_public_items_non_expiring_url(
113133
@parametrized_api_urls
114134
async def test_public_url(self, api_token: str, api_url: str, api_public_url: str) -> None:
115135
apify_client = ApifyClientAsync(token=api_token, api_url=api_url, api_public_url=api_public_url)
116-
created_store = await apify_client.datasets().get_or_create(name=random_resource_name('key-value-store'))
117-
dataset = apify_client.dataset(created_store['id'])
118-
try:
136+
dataset = apify_client.dataset('someID')
137+
138+
# Mock the API call to return predefined response
139+
with mock.patch.object(apify_client.http_client, 'call', return_value=Mock(text=MOCKED_API_DATASET_RESPONSE)):
119140
public_url = await dataset.create_items_public_url()
120141
assert public_url == (
121142
f'{(api_public_url or DEFAULT_API_URL).strip("/")}/v2/datasets/'
122-
f'{created_store["id"]}/items?signature={public_url.split("signature=")[1]}'
143+
f'someID/items?signature={public_url.split("signature=")[1]}'
123144
)
124-
finally:
125-
await dataset.delete()
126-
127-
def test_public_url_nonexistent_host(self, api_token: str) -> None:
128-
dataset_name = 'whatever'
129-
non_existent_url = 'http://10.0.88.214:8010'
130-
apify_client = ApifyClientAsync(token=api_token, api_url=non_existent_url)
131-
kvs_client = apify_client.dataset(dataset_id=dataset_name)
132-
assert kvs_client._url() == f'{non_existent_url}/v2/datasets/{dataset_name}'
133-
assert kvs_client._url(public=True) == f'{DEFAULT_API_URL}/v2/datasets/{dataset_name}'

tests/integration/test_key_value_store.py

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

3+
from unittest import mock
4+
from unittest.mock import Mock
5+
36
import impit
47

58
from integration.conftest import parametrized_api_urls
@@ -8,6 +11,31 @@
811
from apify_client import ApifyClient, ApifyClientAsync
912
from apify_client.client import DEFAULT_API_URL
1013

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+
1139

1240
class TestKeyValueStoreSync:
1341
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_
4977
@parametrized_api_urls
5078
def test_public_url(self, api_token: str, api_url: str, api_public_url: str) -> None:
5179
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)):
5584
public_url = kvs.create_keys_public_url()
5685
assert public_url == (
5786
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]}'
5988
)
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}'
7089

7190

7291
class TestKeyValueStoreAsync:
@@ -115,23 +134,12 @@ async def test_key_value_store_should_create_public_keys_non_expiring_url(
115134
@parametrized_api_urls
116135
async def test_public_url(self, api_token: str, api_url: str, api_public_url: str) -> None:
117136
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)):
123141
public_url = await kvs.create_keys_public_url()
124142
assert public_url == (
125143
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]}'
127145
)
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

Comments
 (0)