From e6dd999cada5fc301ad369df8f0b8008a7d1b4a6 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Wynen Date: Mon, 22 Jan 2024 16:29:17 +0100 Subject: [PATCH] Update for backend behaviour changes --- src/scitacean/datablock.py | 9 ++++++-- src/scitacean/testing/client.py | 10 ++++----- tests/client/datablock_client_test.py | 6 +++--- tests/client/sample_client_test.py | 31 +++++++++++++++++++++++++-- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/scitacean/datablock.py b/src/scitacean/datablock.py index ed532076..c2299c44 100644 --- a/src/scitacean/datablock.py +++ b/src/scitacean/datablock.py @@ -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 @@ -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, @@ -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.""" diff --git a/src/scitacean/testing/client.py b/src/scitacean/testing/client.py index c9677a59..950ef5ea 100644 --- a/src/scitacean/testing/client.py +++ b/src/scitacean/testing/client.py @@ -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 @@ -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 @@ -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, @@ -356,8 +358,6 @@ def _process_orig_datablock( updatedAt=created_at, **fields, ) - if dblock.datasetId is not None: - processed.datasetId = dblock.datasetId return processed diff --git a/tests/client/datablock_client_test.py b/tests/client/datablock_client_test.py index 361e496f..74946947 100644 --- a/tests/client/datablock_client_test.py +++ b/tests/client/datablock_client_test.py @@ -52,7 +52,6 @@ def orig_datablock(scicat_access): ], datasetId="PLACEHOLDER", ownerGroup=scicat_access.user.group, - accessGroups=["group1", "2nd_group"], ) @@ -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}" diff --git a/tests/client/sample_client_test.py b/tests/client/sample_client_test.py index c05201ed..3becfac7 100644 --- a/tests/client/sample_client_test.py +++ b/tests/client/sample_client_test.py @@ -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() @@ -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}, )