Skip to content

Commit

Permalink
Merge pull request #43 from CABLE-LSM/41-change-tests-to-in-memory-files
Browse files Browse the repository at this point in the history
Changed upload logic and tests to require paths, rather than file obj…
  • Loading branch information
bschroeter authored Aug 8, 2024
2 parents 97369a2 + 47034d5 commit 4b36be0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
14 changes: 5 additions & 9 deletions meorg_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import meorg_client.constants as mcc
import meorg_client.endpoints as endpoints
import meorg_client.exceptions as mx
import meorg_client.utilities as mu
import mimetypes as mt
from pathlib import Path

Expand Down Expand Up @@ -217,14 +218,14 @@ def logout(self):

def upload_files(
self,
files,
files: Union[str, Path],
) -> Union[dict, requests.Response]:
"""Upload a file.
Parameters
----------
files : path-like, readable or list
Path to the file, readable object, or a list containing either.
files : path-like, list
Path to the file, or a list containing paths.
Returns
-------
Expand All @@ -240,8 +241,7 @@ def upload_files(
"""

# Cast as list for iterative upload
if not isinstance(files, list):
files = [files]
files = mu.ensure_list(files)

# Prepare the files
_files = list()
Expand All @@ -250,10 +250,6 @@ def upload_files(
if isinstance(f, (str, Path)) and os.path.isfile(f):
_files.append(open(f, "rb"))

# IO handle (i.e. open file or bytes)
elif f.readable() and hasattr(f, "name"):
_files.append(f)

# Bail out
else:
dtype = type(f)
Expand Down
9 changes: 6 additions & 3 deletions meorg_client/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def test_get_analysis_status(client):
assert client.success()


@pytest.mark.xfail(strict=False)
def test_upload_file_large(client):
"""Test the uploading of a large-ish file."""

Expand All @@ -132,11 +133,13 @@ def test_upload_file_large(client):
tmp.write(data)
tmp.seek(0)

# tmp files have no extension...
tmp.name = tmp.name + ".nc"
# tmp files have no extension, so we have to rename them
new_name = tmp.name + ".nc"
os.rename(tmp.name, new_name)
tmp.name = new_name

# Upload and ensure it worked
_ = client.upload_files(tmp)
_ = client.upload_files(new_name)

assert client.success()

Expand Down
28 changes: 28 additions & 0 deletions meorg_client/tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Test utility functions."""

import meorg_client.utilities as mu
from pathlib import Path


def test_ensure_list():
"""Test ensure_list."""

# Test null case
result = mu.ensure_list([1])
assert isinstance(result, list)

# Test casting
result = mu.ensure_list(1)
assert isinstance(result, list)


def test_get_user_data_filepath():
"""Test get_user_data_filepath."""
result = mu.get_user_data_filepath("test.txt")
assert result == Path.home() / ".meorg" / "test.txt"


def test_load_package_data():
"""Test load_package_data."""
result = mu.load_package_data("openapi.json")
assert isinstance(result, dict)
16 changes: 16 additions & 0 deletions meorg_client/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ def load_user_data(filename):
raw = open(filepath, "r").read()
ext = filename.split(".")[-1]
return PACKAGE_DATA_DECODERS[ext](raw)


def ensure_list(obj):
"""Ensure that obj is a list.
Parameters
----------
obj : mixed
Object of any type.
Returns
-------
list
The object as a list, if it is not already.
"""
return obj if isinstance(obj, list) else [obj]

0 comments on commit 4b36be0

Please sign in to comment.