Skip to content

Commit de805ef

Browse files
committed
test: Add an integration test for uploading a pre-compressed KVS record
1 parent a701e83 commit de805ef

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

‎tests/integration/test_key_value_store.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import gzip
56
import json
67
from collections.abc import AsyncIterator, Iterator
78
from datetime import timedelta
@@ -601,6 +602,42 @@ async def get_record() -> dict | None:
601602
await maybe_await(store_client.delete())
602603

603604

605+
async def test_key_value_store_set_and_get_pre_compressed_record(client: ApifyClient | ApifyClientAsync) -> None:
606+
"""A record uploaded with an explicit `content_encoding` is stored as sent and served back under that encoding."""
607+
store_name = get_random_resource_name('kvs')
608+
created_store = await maybe_await(client.key_value_stores().get_or_create(name=store_name))
609+
assert isinstance(created_store, KeyValueStore)
610+
store_client = client.key_value_store(created_store.id)
611+
612+
try:
613+
# Past the size threshold and of a compressible content type, so without the explicit encoding the client
614+
# would compress the body itself - a second time, on top of the gzip the caller already applied.
615+
payload = b'compressible payload ' * 500
616+
await maybe_await(
617+
store_client.set_record(
618+
'pre-compressed',
619+
gzip.compress(payload),
620+
content_type='text/plain',
621+
content_encoding='gzip',
622+
)
623+
)
624+
625+
# Poll until the record is visible (eventual consistency)
626+
async def get_record() -> dict | None:
627+
return await maybe_await(store_client.get_record_as_bytes('pre-compressed'))
628+
629+
record = await poll_until_condition(get_record, lambda record: record is not None)
630+
assert isinstance(record, dict)
631+
632+
# The record is served with the `Content-Encoding` it was stored under, so the transport decodes it back
633+
# to the original payload. A body compressed twice would arrive here as the gzip bytes instead, and one
634+
# stored without the encoding would not be decoded at all.
635+
assert record['value'] == payload
636+
assert 'text/plain' in record['content_type']
637+
finally:
638+
await maybe_await(store_client.delete())
639+
640+
604641
async def test_key_value_store_get_record_public_url(client: ApifyClient | ApifyClientAsync) -> None:
605642
"""Test get_record_public_url returns a working signed URL."""
606643
store_name = get_random_resource_name('kvs')

0 commit comments

Comments
 (0)