Skip to content

Commit 3729fac

Browse files
committed
Migrate tests
1 parent 2ebd75c commit 3729fac

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/apify_client/clients/resource_clients/request_queue.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ def unique_key_to_request_id(unique_key: str, *, request_id_length: int = 15) ->
5959
Returns:
6060
A URL-safe, truncated request ID based on the unique key.
6161
"""
62-
if not unique_key:
63-
raise ValueError('unique_key must not be empty')
64-
6562
# Encode the unique key and compute its SHA-256 hash
6663
hashed_key = sha256(unique_key.encode('utf-8')).digest()
6764

tests/integration/test_request_queue.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
from typing import TYPE_CHECKING
44

5+
import pytest
6+
57
from integration.integration_test_utils import random_resource_name, random_string
68

9+
from apify_client.clients.resource_clients.request_queue import unique_key_to_request_id
10+
711
if TYPE_CHECKING:
812
from apify_client import ApifyClient, ApifyClientAsync
913

@@ -113,3 +117,38 @@ async def test_request_batch_operations(self, apify_client_async: ApifyClientAsy
113117
assert len(requests_in_queue2['items']) == 25 - len(delete_response['processedRequests'])
114118

115119
await queue.delete()
120+
121+
122+
def test_unique_key_to_request_id_length() -> None:
123+
unique_key = 'exampleKey123'
124+
request_id = unique_key_to_request_id(unique_key, request_id_length=15)
125+
assert len(request_id) == 15, 'Request ID should have the correct length.'
126+
127+
128+
def test_unique_key_to_request_id_consistency() -> None:
129+
unique_key = 'consistentKey'
130+
request_id_1 = unique_key_to_request_id(unique_key)
131+
request_id_2 = unique_key_to_request_id(unique_key)
132+
assert request_id_1 == request_id_2, 'The same unique key should generate consistent request IDs.'
133+
134+
135+
@pytest.mark.parametrize(
136+
('unique_key', 'expected_request_id'),
137+
[
138+
('abc', 'ungWv48BzpBQUDe'),
139+
('uniqueKey', 'xiWPs083cree7mH'),
140+
('', '47DEQpj8HBSaTIm'),
141+
('测试中文', 'lKPdJkdvw8MXEUp'),
142+
('test+/=', 'XZRQjhoG0yjfnYD'),
143+
],
144+
ids=[
145+
'basic_abc',
146+
'keyword_uniqueKey',
147+
'empty_string',
148+
'non_ascii_characters',
149+
'url_unsafe_characters',
150+
],
151+
)
152+
def test_unique_key_to_request_id_matches_known_values(unique_key: str, expected_request_id: str) -> None:
153+
request_id = unique_key_to_request_id(unique_key)
154+
assert request_id == expected_request_id, f'Unique key "{unique_key}" should produce the expected request ID.'

0 commit comments

Comments
 (0)