Skip to content

Commit

Permalink
Merge pull request #172 from SciCatProject/preserve-meta
Browse files Browse the repository at this point in the history
Preserve Dataset.meta
  • Loading branch information
jl-wynen authored Oct 25, 2023
2 parents b75cca4 + 0330337 commit f48bf42
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Bugfixes

* :class:`scitacean.Client` no longer requires a file transfer during upload if the dataset has no files that need to be uploaded.
* The HTML representation of datasets no longer flags read-only fields with a value as broken.
* Preserve ``meta`` in ``Dataset.replace`` and allow replacing it.
This also affects ``Dataset.replace_files`` and ``Client.download_files``.

Documentation
~~~~~~~~~~~~~
Expand Down
3 changes: 3 additions & 0 deletions src/scitacean/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,15 @@ def get_val(source: Dict[str, Any], name: str) -> Any:
},
}
attachments = get_val(replacements, "attachments")
meta = get_val(replacements, "meta")

if replacements or _read_only:
raise TypeError(
f"Invalid arguments: {', '.join((*replacements, *_read_only))}"
)
dset = Dataset(
**kwargs,
meta=meta,
)
dset._orig_datablocks.extend(
_orig_datablocks if _orig_datablocks is not None else self._orig_datablocks
Expand Down
29 changes: 29 additions & 0 deletions tests/dataset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,35 @@ def test_replace_does_not_change_files_with_input_files(initial):
assert list(replaced.files) == list(initial.files)


@given(sst.datasets())
@settings(max_examples=1)
def test_replace_preserves_meta(initial):
initial.meta["key"] = "val"
replaced = initial.replace(owner="a-new-owner")
assert replaced.meta == {"key": "val"}


@given(sst.datasets())
@settings(max_examples=1)
def test_replace_meta(initial):
initial.meta["key"] = {"value": 2, "unit": "m"}
initial.meta["old-key"] = "old-val"
replaced = initial.replace(
owner="a-new-owner",
meta={"key": {"value": 3, "unit": "m"}, "new-key": "new-val"},
)
assert replaced.meta == {"key": {"value": 3, "unit": "m"}, "new-key": "new-val"}


@given(sst.datasets())
@settings(max_examples=1)
def test_replace_remove_meta(initial):
initial.meta["key"] = {"value": 2, "unit": "m"}
initial.meta["old-key"] = "old-val"
replaced = initial.replace(owner="a-new-owner", meta=None)
assert replaced.meta == {}


@pytest.mark.parametrize(
"attachments",
(None, [], [model.Attachment(caption="Attachment 1", owner_group="owner")]),
Expand Down
12 changes: 12 additions & 0 deletions tests/download_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hashlib
import re
from contextlib import contextmanager
from copy import deepcopy
from pathlib import Path
from typing import Union

Expand Down Expand Up @@ -56,6 +57,10 @@ def dataset_and_files(data_files):
size=sum(f.size for f in data_files[0]),
sourceFolder=RemotePath("/src/stibbons/774"),
type=DatasetType.RAW,
scientificMetadata={
"height": {"value": 0.3, "unit": "m"},
"mass": "hefty",
},
)
block = DownloadOrigDatablock(
chkAlg="md5",
Expand Down Expand Up @@ -188,11 +193,18 @@ def test_download_files_creates_local_files_select_one_by_predicate(

def test_download_files_returns_updated_dataset(fs, dataset_and_files):
dataset, contents = dataset_and_files
original = deepcopy(dataset)
client = Client.without_login(
url="/", file_transfer=FakeFileTransfer(fs=fs, files=contents)
)
finalized = client.download_files(dataset, target="./download")

assert dataset == original
assert all(
getattr(finalized, field.name) == getattr(original, field.name)
for field in original.fields()
)
assert finalized.meta == original.meta
for f in finalized.files:
assert f.is_on_local
for f in dataset.files:
Expand Down

0 comments on commit f48bf42

Please sign in to comment.