Skip to content

Commit

Permalink
add test for renamed file
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed May 9, 2024
1 parent 33a4b37 commit 1261531
Showing 1 changed file with 55 additions and 4 deletions.
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)}"

0 comments on commit 1261531

Please sign in to comment.