Skip to content

Commit

Permalink
tests for file download
Browse files Browse the repository at this point in the history
  • Loading branch information
vedina committed Sep 13, 2024
1 parent b8c3c82 commit 6f5312a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
18 changes: 9 additions & 9 deletions src/rcapi/api/convertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ async def convert_get(
FigureCanvas(fig).print_png(output)
return Response(content=output.getvalue(), media_type='image/png')

if what in ["dict","thumbnail","b64png","image"]: #solr query
if what == "dict":
prm = dict(request.query_params)
prm["what"] = None
fig = dict2figure(prm, figsize)
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(content=output.getvalue(), media_type='image/png')
else:

if what == "dict":
prm = dict(request.query_params)
prm["what"] = None
fig = dict2figure(prm, figsize)
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(content=output.getvalue(), media_type='image/png')
elif what in ["thumbnail","b64png","image"]: #solr query
async with inject_api_key_into_httpx(api_key):
fig = await solr2image(solr_url, domain, figsize, extra)
output = io.BytesIO()
Expand Down
49 changes: 43 additions & 6 deletions tests/test_api_download.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from fastapi.testclient import TestClient
from rcapi.main import app
import pytest
import h5pyd
import h5py, h5pyd
import tempfile
from os import remove

client = TestClient(app)

Expand All @@ -14,17 +16,52 @@ def domain():
return _domain



def test_access_domain(domain):
print(h5pyd.__version__)
print(domain)
with h5pyd.File(domain) as top_group:
for index,key in enumerate(top_group):
print(index,key)

def test_download_domain(domain):
print(domain)
params = { "domain" : domain , "what" : "h5"}
response = client.get("/download",params=params)
assert response.status_code == 200
print(response)

def test_download_domain_h5(domain):
print(domain)
params = { "domain" : domain , "what" : "h5"}
response = client.get("/download",params=params)
assert response.status_code == 200, f"Expected status code 200 but got {response.status_code}"
assert response.headers["Content-Type"] == "application/x-hdf5", "Response is not an HDF5 file but {}".format(response.headers["Content-Type"])
# Check if the response content starts with HDF5 file signature
hdf5_signature = b'\x89HDF\r\n\x1a\n'
assert response.content.startswith(hdf5_signature), "Response content is not a valid HDF5 file"
# Create a temporary file to store the HDF5 content
with tempfile.NamedTemporaryFile(suffix=".h5", delete=False) as tmp_file:
# Write the response content to the temporary file
tmp_file.write(response.content)
tmp_file_path = tmp_file.name
# Open the saved file using h5py
with h5py.File(tmp_file_path, 'r') as hdf:
# Print and inspect the HDF5 file structure (e.g., datasets, attributes)
print(f"Contents of the HDF5 file {tmp_file_path}:")
print(list(hdf.keys())) # This will print out the datasets/groups in the file
# Perform assertions based on the file structure
assert len(hdf.keys()) > 0, "HDF5 file is empty"
# Clean up the temporary file after the test
remove(tmp_file_path)

def test_access_domain(domain):
print(h5pyd.__version__)
def test_download_domain_image(domain):
print(domain)
with h5pyd.File(domain) as top_group:
for index,key in enumerate(top_group):
print(index,key)
params = { "domain" : domain , "what" : "thumbnail"}
response = client.get("/download",params=params)
assert response.status_code == 200, f"Expected status code 200 but got {response.status_code}"
# Assert that the Content-Type is "image/png"
assert response.headers["Content-Type"] == "image/png", "Response is not a PNG image"
# Check if the response content starts with PNG header bytes
png_signature = b'\x89PNG\r\n\x1a\n'
assert response.content.startswith(png_signature), "Response content is not a valid PNG image"

0 comments on commit 6f5312a

Please sign in to comment.