Skip to content

Commit

Permalink
Update for backend behaviour changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jl-wynen committed Jan 22, 2024
1 parent 8e33e60 commit e6dd999
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
9 changes: 7 additions & 2 deletions src/scitacean/datablock.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class OrigDatablock:

_files: List[File] = dataclasses.field(init=False)
checksum_algorithm: Optional[str] = None
access_groups: Optional[List[str]] = None
instrument_group: Optional[str] = None
owner_group: Optional[str] = None
init_files: dataclasses.InitVar[Optional[Iterable[File]]] = None
_access_groups: Optional[List[str]] = None
_created_at: Optional[datetime] = None
_created_by: Optional[str] = None
_dataset_id: Optional[PID] = None
Expand Down Expand Up @@ -67,8 +67,8 @@ def from_download_model(
return OrigDatablock(
checksum_algorithm=dblock.chkAlg,
owner_group=dblock.ownerGroup,
access_groups=dblock.accessGroups,
instrument_group=dblock.instrumentGroup,
_access_groups=dblock.accessGroups,
_created_at=dblock.createdAt,
_created_by=dblock.createdBy,
_dataset_id=orig_datablock_model.datasetId,
Expand All @@ -92,6 +92,11 @@ def size(self) -> int:
"""Total size of all files."""
return sum(file.size for file in self.files)

@property
def access_groups(self) -> Optional[List[str]]:
"""Access groups for this datablock."""
return self._access_groups

@property
def created_at(self) -> Optional[datetime]:
"""Creation time of this orig datablock."""
Expand Down
10 changes: 5 additions & 5 deletions src/scitacean/testing/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ def create_orig_datablock(
self, dblock: model.UploadOrigDatablock
) -> model.DownloadOrigDatablock:
"""Create a new orig datablock in SciCat."""
ingested = _process_orig_datablock(dblock)
dataset_id = ingested.datasetId
if dataset_id not in self.main.datasets:
dataset_id = dblock.datasetId
if (dset := self.main.datasets.get(dataset_id)) is None:
raise ScicatCommError(f"No dataset with id {dataset_id}")
ingested = _process_orig_datablock(dblock, dset)
self.main.orig_datablocks.setdefault(dataset_id, []).append(ingested)
return ingested

Expand Down Expand Up @@ -338,6 +338,7 @@ def _process_dataset(

def _process_orig_datablock(
dblock: model.UploadOrigDatablock,
dset: model.DownloadDataset,
) -> model.DownloadOrigDatablock:
created_at = datetime.datetime.now(tz=datetime.timezone.utc)
# TODO use user login if possible
Expand All @@ -347,6 +348,7 @@ def _process_orig_datablock(
fields = _model_dict(dblock)
if "dataFileList" in fields:
fields["dataFileList"] = list(map(_process_data_file, fields["dataFileList"]))
fields["accessGroups"] = dset.accessGroups
processed = model.construct(
model.DownloadOrigDatablock,
_strict_validation=False,
Expand All @@ -356,8 +358,6 @@ def _process_orig_datablock(
updatedAt=created_at,
**fields,
)
if dblock.datasetId is not None:
processed.datasetId = dblock.datasetId
return processed


Expand Down
6 changes: 3 additions & 3 deletions tests/client/datablock_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def orig_datablock(scicat_access):
],
datasetId="PLACEHOLDER",
ownerGroup=scicat_access.user.group,
accessGroups=["group1", "2nd_group"],
)


Expand All @@ -74,9 +73,10 @@ def test_create_first_orig_datablock(scicat_client, derived_dataset, orig_databl
# The database populates a number of fields that are orig_datablock in dset.
# But we don't want to test those here as we don't want to test the database.
if expected is not None and key != "dataFileList":
assert expected == dict(downloaded)[key], f"key = {key}"
assert dict(downloaded)[key] == expected, f"key = {key}"
assert downloaded.accessGroups == derived_dataset.accessGroups
for i in range(len(orig_datablock.dataFileList)):
for key, expected in orig_datablock.dataFileList[i]:
assert (
expected == dict(downloaded.dataFileList[i])[key]
dict(downloaded.dataFileList[i])[key] == expected
), f"i = {i}, key = {key}"
31 changes: 29 additions & 2 deletions tests/client/sample_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@
from scitacean.model import (
Sample,
)
from scitacean.testing.backend import config as backend_config
from scitacean.testing.backend import skip_if_not_backend


# Creating samples requires at least ingestor permissions.
@pytest.fixture()
def ingestor_access(
scicat_access: backend_config.SciCatAccess,
) -> backend_config.SciCatAccess:
return backend_config.SciCatAccess(
url=scicat_access.url,
user=backend_config.USERS["ingestor"],
)


@pytest.fixture()
def real_client(
request: pytest.FixtureRequest,
ingestor_access: backend_config.SciCatAccess,
scicat_backend: bool,
) -> Client:
skip_if_not_backend(request)
return Client.from_credentials(
url=ingestor_access.url,
**ingestor_access.user.credentials,
)


@pytest.fixture()
Expand All @@ -18,12 +44,13 @@ def scicat_client(client: Client) -> ScicatClient:


@pytest.fixture()
def sample(scicat_access):
def sample(ingestor_access):
scicat_access = ingestor_access
return Sample(
owner_group=scicat_access.user.group,
access_groups=["group1", "2nd_group"],
description="A test sample for Scitacean",
owner="ridcully",
owner=scicat_access.user.username,
sample_characteristics={"layers": ["H2O", "EtOH"], "mass": 2},
)

Expand Down

0 comments on commit e6dd999

Please sign in to comment.