Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ async def upload_attachment(
file_name, mimetype, content_encoding = self._mime_and_content_type_from_path(posix_path)

if not mimetype:
raise ValueError(f"The MIME-type of '{posix_path}' could not be computed.")
extension = posix_path.suffix
if extension:
mimetype = f"application/x-{extension.lstrip('.')}"
else:
mimetype = "application/octet-stream" # fallback to generic 'binary data' MIME type

# Run the synchronous file upload in a thread pool to avoid blocking the event loop
loop = asyncio.get_event_loop()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,62 @@
"""Tests for UploadLowLevelClient functionality."""

from pathlib import Path
from unittest.mock import patch

import pytest

from sift_client._internal.low_level_wrappers.upload import UploadLowLevelClient


class TestUploadLowLevelClient:
class TestMimeAndContentTypeFromPath:
def test_parquet_file_extension(self):
_, mime, _ = UploadLowLevelClient._mime_and_content_type_from_path(Path("data.parquet"))
assert mime == "application/vnd.apache.parquet"
class TestUploadAttachment:
@pytest.mark.asyncio
async def test_known_mime_type(self, tmp_path):
test_file = tmp_path / "video.mp4"
test_file.write_bytes(b"fake data")

client = UploadLowLevelClient.__new__(UploadLowLevelClient)

with patch.object(client, "_upload_file_sync", return_value="remote-file-123") as mock:
await client.upload_attachment(path=test_file, entity_id="e1", entity_type="runs")

_, _, mimetype, *_ = mock.call_args[0]
assert mimetype == "video/mp4"

@pytest.mark.asyncio
async def test_unknown_extension_falls_back_to_application_x_ext(self, tmp_path):
test_file = tmp_path / "data.pcapng"
test_file.write_bytes(b"fake data")

client = UploadLowLevelClient.__new__(UploadLowLevelClient)

with patch.object(client, "_upload_file_sync", return_value="remote-file-123") as mock:
await client.upload_attachment(path=test_file, entity_id="e1", entity_type="runs")

_, _, mimetype, *_ = mock.call_args[0]
assert mimetype == "application/x-pcapng"

@pytest.mark.asyncio
async def test_no_extension_falls_back_to_octet_stream(self, tmp_path):
test_file = tmp_path / "README"
test_file.write_bytes(b"fake data")

client = UploadLowLevelClient.__new__(UploadLowLevelClient)

with patch.object(client, "_upload_file_sync", return_value="remote-file-123") as mock:
await client.upload_attachment(path=test_file, entity_id="e1", entity_type="runs")

_, _, mimetype, *_ = mock.call_args[0]
assert mimetype == "application/octet-stream"

@pytest.mark.asyncio
async def test_file_name_preserved(self, tmp_path):
test_file = tmp_path / "my_file.pcapng"
test_file.write_bytes(b"fake data")

client = UploadLowLevelClient.__new__(UploadLowLevelClient)

with patch.object(client, "_upload_file_sync", return_value="remote-file-123") as mock:
await client.upload_attachment(path=test_file, entity_id="e1", entity_type="runs")

def test_pqt_file_extension(self):
_, mime, _ = UploadLowLevelClient._mime_and_content_type_from_path(Path("data.pqt"))
assert mime == "application/vnd.apache.parquet"
file_name, *_ = mock.call_args[0]
assert file_name == Path(test_file)
Loading