Skip to content

Commit 6751911

Browse files
committed
Merge branch 'metadata-native-zips' into sdc-dev
2 parents 2aa7581 + 927bffe commit 6751911

4 files changed

Lines changed: 77 additions & 2 deletions

File tree

dvuploader/file.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class File(BaseModel):
3030
Private Attributes:
3131
_size (int): Size of the file in bytes.
3232
_unchanged_data (bool): Indicates if the file data has not changed since last upload.
33+
_is_inside_zip (bool): Indicates if the file is packaged inside a zip archive.
3334
3435
Methods:
3536
extract_file_name(): Extracts filename from filepath and initializes file handler.
@@ -59,6 +60,7 @@ class File(BaseModel):
5960

6061
_size: int = PrivateAttr(default=0)
6162
_unchanged_data: bool = PrivateAttr(default=False)
63+
_is_inside_zip: bool = PrivateAttr(default=False)
6264

6365
def extract_file_name(self):
6466
"""

dvuploader/nativeupload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ async def _update_metadata(
364364
try:
365365
if _tab_extension(dv_path) in file_mapping:
366366
file_id = file_mapping[_tab_extension(dv_path)]
367-
elif file.file_name and _is_zip(file.file_name):
368-
# When the file is a zip it will be unpacked and thus
367+
elif file.file_name and _is_zip(file.file_name) and not file._is_inside_zip:
368+
# When the file is a zip package it will be unpacked and thus
369369
# the expected file name of the zip will not be in the
370370
# dataset, since it has been unpacked.
371371
continue

dvuploader/packaging.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def zip_files(
9999
zinfo_or_arcname=_create_arcname(file),
100100
)
101101

102+
file._is_inside_zip = True
103+
102104
return path
103105

104106

tests/integration/test_native_upload.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def test_native_upload_with_proxy(
157157
assert len(files) == 3
158158
assert sorted([file["label"] for file in files]) == sorted(expected_files)
159159

160+
@pytest.mark.xfail(reason="See discussion in #34")
160161
def test_native_upload_by_handler(
161162
self,
162163
credentials,
@@ -460,6 +461,76 @@ def test_zipzip_file_upload(
460461

461462
assert sorted([file["label"] for file in files]) == sorted(expected_files)
462463

464+
def test_metadata_with_zip_files_in_package(self, credentials):
465+
BASE_URL, API_TOKEN = credentials
466+
467+
# Create Dataset
468+
pid = create_dataset(
469+
parent="Root",
470+
server_url=BASE_URL,
471+
api_token=API_TOKEN,
472+
)
473+
474+
# Arrange
475+
files = [
476+
File(filepath="tests/fixtures/archive.zip",
477+
dv_dir="subdir2",
478+
description="This file should not be unzipped",
479+
categories=["Test file"]
480+
),
481+
File(filepath="tests/fixtures/add_dir_files/somefile.txt",
482+
dv_dir="subdir",
483+
description="A simple text file",
484+
categories=["Test file"]
485+
),
486+
]
487+
488+
# Act
489+
uploader = DVUploader(files=files)
490+
uploader.upload(
491+
persistent_id=pid,
492+
api_token=API_TOKEN,
493+
dataverse_url=BASE_URL,
494+
n_parallel_uploads=10,
495+
)
496+
497+
# Assert
498+
files = retrieve_dataset_files(
499+
dataverse_url=BASE_URL,
500+
persistent_id=pid,
501+
api_token=API_TOKEN,
502+
)
503+
504+
assert len(files) == 2, f"Expected 2 files, got {len(files)}"
505+
506+
expected_files = [
507+
{
508+
"label": "archive.zip",
509+
"description": "This file should not be unzipped",
510+
"categories": ["Test file"]
511+
},
512+
{
513+
"label": "somefile.txt",
514+
"description": "A simple text file",
515+
"categories": ["Test file"]
516+
},
517+
]
518+
519+
files_as_expected = sorted(
520+
[
521+
{
522+
k: (f[k] if k in f else None)
523+
for k in expected_files[0].keys()
524+
}
525+
for f in files
526+
],
527+
key=lambda x: x["label"]
528+
)
529+
assert files_as_expected == expected_files, (
530+
f"File metadata not as expected: {json.dumps(files, indent=2)}"
531+
)
532+
533+
463534
def test_too_many_zip_files(
464535
self,
465536
credentials,

0 commit comments

Comments
 (0)