Skip to content
Open
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
15 changes: 15 additions & 0 deletions databusclient/api/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ def _validate_graph_mode(graph_mode: str | None) -> None:
)


def _normalize_compression(compression: str | None) -> str | None:
# The CLI already does this with click.Choice, but workflow steps and
# direct API calls pass the value through as is.
if not compression:
return None
normalized = compression.lower()
if normalized != "none" and normalized not in COMPRESSION_EXTENSIONS:
raise ValueError(
f"Unsupported compression: {compression}. "
f"Supported: {sorted(COMPRESSION_EXTENSIONS) + ['none']}"
)
return normalized


def _write_graph_sidecars(
final_file_paths: list[str],
source_url: str,
Expand Down Expand Up @@ -1460,6 +1474,7 @@ def download(
the versioned databus-python-client default.
"""
_validate_graph_mode(graph_mode)
compression = _normalize_compression(compression)
for databusURI in databusURIs:
host, account, group, artifact, version, file = get_databus_id_parts_from_file_url(databusURI)

Expand Down
87 changes: 87 additions & 0 deletions tests/test_download_compression_option.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Tests for the compression value check at the start of download() (#129)."""

import gzip
import os

import pytest

from databusclient.api import download as dl

FILE_URL = "https://databus.example.org/acc/g/a/2024.01.01/data.ttl"
BODY = b"<a> <b> <c> .\n"


class FakeResponse:
status_code = 200

def __init__(self, body=b""):
self.headers = {"content-length": str(len(body))}
self._body = body

def raise_for_status(self):
pass

def iter_content(self, chunk_size):
yield self._body


@pytest.fixture
def fake_http(monkeypatch):
calls = []

def fake_head(url, *args, **kwargs):
calls.append(("HEAD", url))
return FakeResponse()

def fake_get(url, *args, **kwargs):
calls.append(("GET", url))
return FakeResponse(BODY)

monkeypatch.setattr(dl.requests, "head", fake_head)
monkeypatch.setattr(dl.requests, "get", fake_get)
return calls


def _files(root):
return sorted(f for _, _, names in os.walk(root) for f in names)


@pytest.mark.parametrize(
"value, expected",
[
(None, None),
("", None),
("gz", "gz"),
("GZ", "gz"),
("Bz2", "bz2"),
("xz", "xz"),
("NONE", "none"),
],
)
def test_normalize_compression(value, expected):
assert dl._normalize_compression(value) == expected


@pytest.mark.parametrize("value", ["zip", "gzip", "tar.gz"])
def test_normalize_compression_rejects_unknown(value):
with pytest.raises(ValueError, match="Unsupported compression"):
dl._normalize_compression(value)


def test_unknown_compression_fails_before_any_request(fake_http, tmp_path):
with pytest.raises(ValueError, match="Unsupported compression: zip"):
dl.download(str(tmp_path), None, [FILE_URL], compression="zip")

assert fake_http == []
assert _files(tmp_path) == []


def test_uppercase_compression_works_like_lowercase(fake_http, tmp_path):
dl.download(str(tmp_path), None, [FILE_URL], compression="GZ")

assert _files(tmp_path) == ["data.ttl.gz"]
gz_path = next(
os.path.join(root, f) for root, _, names in os.walk(tmp_path) for f in names
)
with gzip.open(gz_path, "rb") as f:
assert f.read() == BODY
Loading