Skip to content
Merged
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
4 changes: 3 additions & 1 deletion dvuploader/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def extract_file_name_hash_file(self):
self.directory_label = os.path.dirname(self.filepath)
self.handler.seek(0)

self.file_name = os.path.basename(self.filepath)
if self.file_name is None:
self.file_name = os.path.basename(self.filepath)

self.checksum = Checksum.from_file(
handler=self.handler,
hash_fun=hash_fun,
Expand Down
59 changes: 55 additions & 4 deletions tests/integration/test_native_upload.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from io import BytesIO
import os
import json
import tempfile

Expand Down Expand Up @@ -58,6 +59,53 @@ def test_native_upload(
assert len(files) == 3
assert sorted([file["label"] for file in files]) == sorted(expected_files)

def test_native_upload_renamed_file(
self,
credentials,
):

BASE_URL, API_TOKEN = credentials

with tempfile.TemporaryDirectory() as directory:
# Arrange
create_mock_file(directory, "will_be_renamed.txt", size=1)

# Add a file and rename it during upload
files = [
File(
filepath=os.path.join(directory, "will_be_renamed.txt"),
file_name="renamed_file.txt", # type: ignore
)
]

# Create Dataset
pid = create_dataset(
parent="Root",
server_url=BASE_URL,
api_token=API_TOKEN,
)

# Act
uploader = DVUploader(files=files)
uploader.upload(
persistent_id=pid,
api_token=API_TOKEN,
dataverse_url=BASE_URL,
n_parallel_uploads=1,
)

# Assert
files = retrieve_dataset_files(
dataverse_url=BASE_URL,
persistent_id=pid,
api_token=API_TOKEN,
)

expected_files = ["renamed_file.txt"]

assert len(files) == 1
assert sorted([file["label"] for file in files]) == sorted(expected_files)

def test_forced_native_upload(
self,
credentials,
Expand Down Expand Up @@ -106,7 +154,6 @@ def test_forced_native_upload(
assert len(files) == 3
assert sorted([file["label"] for file in files]) == sorted(expected_files)


def test_native_upload_by_handler(
self,
credentials,
Expand All @@ -117,7 +164,7 @@ def test_native_upload_by_handler(
byte_string = b"Hello, World!"
files = [
File(filepath="subdir/file.txt", handler=BytesIO(byte_string)),
File(filepath="biggerfile.txt", handler=BytesIO(byte_string*10000)),
File(filepath="biggerfile.txt", handler=BytesIO(byte_string * 10000)),
]

# Create Dataset
Expand Down Expand Up @@ -154,5 +201,9 @@ def test_native_upload_by_handler(

file = next(file for file in files if file["label"] == ex_f)

assert file["label"] == ex_f, f"File label does not match for file {json.dumps(file)}"
assert file.get("directoryLabel", "") == ex_dir, f"Directory label does not match for file {json.dumps(file)}"
assert (
file["label"] == ex_f
), f"File label does not match for file {json.dumps(file)}"
assert (
file.get("directoryLabel", "") == ex_dir
), f"Directory label does not match for file {json.dumps(file)}"