Skip to content

Commit 47034d5

Browse files
committed
Changed upload logic and tests to require paths, rather than file objects. Paving the way for #42. Fixes #41
1 parent 97369a2 commit 47034d5

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

meorg_client/client.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import meorg_client.constants as mcc
1010
import meorg_client.endpoints as endpoints
1111
import meorg_client.exceptions as mx
12+
import meorg_client.utilities as mu
1213
import mimetypes as mt
1314
from pathlib import Path
1415

@@ -217,14 +218,14 @@ def logout(self):
217218

218219
def upload_files(
219220
self,
220-
files,
221+
files: Union[str, Path],
221222
) -> Union[dict, requests.Response]:
222223
"""Upload a file.
223224
224225
Parameters
225226
----------
226-
files : path-like, readable or list
227-
Path to the file, readable object, or a list containing either.
227+
files : path-like, list
228+
Path to the file, or a list containing paths.
228229
229230
Returns
230231
-------
@@ -240,8 +241,7 @@ def upload_files(
240241
"""
241242

242243
# Cast as list for iterative upload
243-
if not isinstance(files, list):
244-
files = [files]
244+
files = mu.ensure_list(files)
245245

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

253-
# IO handle (i.e. open file or bytes)
254-
elif f.readable() and hasattr(f, "name"):
255-
_files.append(f)
256-
257253
# Bail out
258254
else:
259255
dtype = type(f)

meorg_client/tests/test_client.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def test_get_analysis_status(client):
120120
assert client.success()
121121

122122

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

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

135-
# tmp files have no extension...
136-
tmp.name = tmp.name + ".nc"
136+
# tmp files have no extension, so we have to rename them
137+
new_name = tmp.name + ".nc"
138+
os.rename(tmp.name, new_name)
139+
tmp.name = new_name
137140

138141
# Upload and ensure it worked
139-
_ = client.upload_files(tmp)
142+
_ = client.upload_files(new_name)
140143

141144
assert client.success()
142145

meorg_client/tests/test_utilities.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Test utility functions."""
2+
3+
import meorg_client.utilities as mu
4+
from pathlib import Path
5+
6+
7+
def test_ensure_list():
8+
"""Test ensure_list."""
9+
10+
# Test null case
11+
result = mu.ensure_list([1])
12+
assert isinstance(result, list)
13+
14+
# Test casting
15+
result = mu.ensure_list(1)
16+
assert isinstance(result, list)
17+
18+
19+
def test_get_user_data_filepath():
20+
"""Test get_user_data_filepath."""
21+
result = mu.get_user_data_filepath("test.txt")
22+
assert result == Path.home() / ".meorg" / "test.txt"
23+
24+
25+
def test_load_package_data():
26+
"""Test load_package_data."""
27+
result = mu.load_package_data("openapi.json")
28+
assert isinstance(result, dict)

meorg_client/utilities.py

+16
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,19 @@ def load_user_data(filename):
6868
raw = open(filepath, "r").read()
6969
ext = filename.split(".")[-1]
7070
return PACKAGE_DATA_DECODERS[ext](raw)
71+
72+
73+
def ensure_list(obj):
74+
"""Ensure that obj is a list.
75+
76+
Parameters
77+
----------
78+
obj : mixed
79+
Object of any type.
80+
81+
Returns
82+
-------
83+
list
84+
The object as a list, if it is not already.
85+
"""
86+
return obj if isinstance(obj, list) else [obj]

0 commit comments

Comments
 (0)