From 33a4b37cdad0ed839cf2631fb0847bd096e274a8 Mon Sep 17 00:00:00 2001 From: Jan Range Date: Thu, 9 May 2024 13:52:22 +0200 Subject: [PATCH 1/2] keep `file_name` if specified --- dvuploader/file.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dvuploader/file.py b/dvuploader/file.py index 85045ee..3dab308 100644 --- a/dvuploader/file.py +++ b/dvuploader/file.py @@ -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, From 12615311b19c5ed13df58c432bd0d9b26d07be55 Mon Sep 17 00:00:00 2001 From: Jan Range Date: Thu, 9 May 2024 13:52:32 +0200 Subject: [PATCH 2/2] add test for renamed file --- tests/integration/test_native_upload.py | 59 +++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/tests/integration/test_native_upload.py b/tests/integration/test_native_upload.py index 183cc54..5d2fb32 100644 --- a/tests/integration/test_native_upload.py +++ b/tests/integration/test_native_upload.py @@ -1,4 +1,5 @@ from io import BytesIO +import os import json import tempfile @@ -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, @@ -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, @@ -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 @@ -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)}"