Skip to content

Commit

Permalink
Raise 422 errors when requesting scalar datasets with tiff or csv format
Browse files Browse the repository at this point in the history
  • Loading branch information
loichuder committed Aug 26, 2024
1 parent 9923d51 commit 443d060
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
21 changes: 13 additions & 8 deletions h5grove/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,26 @@ def encode(content: Any, encoding: Optional[str] = "json") -> Response:
f"Unsupported encoding {encoding} for non-numeric content"
)

if encoding == "csv":
if encoding == "npy":
return Response(
csv_encode(content_array),
npy_encode(content_array),
headers={
"Content-Type": "text/csv",
"Content-Disposition": 'attachment; filename="data.csv"',
"Content-Type": "application/octet-stream",
"Content-Disposition": 'attachment; filename="data.npy"',
},
)

if encoding == "npy":
if content_array.ndim == 0:
raise QueryArgumentError(
f"Unsupported encoding {encoding} for empty and scalar datasets"
)

if encoding == "csv":
return Response(
npy_encode(content_array),
csv_encode(content_array),
headers={
"Content-Type": "application/octet-stream",
"Content-Disposition": 'attachment; filename="data.npy"',
"Content-Type": "text/csv",
"Content-Disposition": 'attachment; filename="data.csv"',
},
)

Expand Down
18 changes: 17 additions & 1 deletion test/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def test_data_on_array_with_format(self, server, format_arg):

assert np.array_equal(retrieved_data, data)

# TODO: What should we do for csv, tiff
@pytest.mark.parametrize("format_arg", ("json", "bin", "npy"))
def test_data_on_scalar_with_format(self, server, format_arg):
"""Test /data/ endpoint on scalar dataset"""
Expand Down Expand Up @@ -596,3 +595,20 @@ def test_422_on_invalid_query_arg(self, server):
f"/meta/?file={filename}&path={path}&resolve_links={invalid_link_resolution}",
422,
)

@pytest.mark.parametrize("format_arg", ("csv", "tiff"))
def test_422_on_format_incompatible_with_empty_or_scalar_datasets(
self, server, format_arg
):
filename = "test.h5"

with h5py.File(server.served_directory / filename, mode="w") as h5file:
h5file["scalar"] = 55
h5file["empty"] = h5py.Empty(dtype="<4f")

server.assert_error_code(
f"/data/?file={filename}&path=/scalar&format={format_arg}", 422
)
server.assert_error_code(
f"/data/?file={filename}&path=/empty&format={format_arg}", 422
)

0 comments on commit 443d060

Please sign in to comment.