-
Notifications
You must be signed in to change notification settings - Fork 11
feat(rdm.migration): Configurable auto-adding secondary community #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
53f4ac6
33494bf
5d13d98
149196f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |||||||||||||||||||||
| # the terms of the MIT License; see LICENSE file for more details. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| """CDS-RDM transform step module.""" | ||||||||||||||||||||||
| import datetime | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import logging | ||||||||||||||||||||||
| from collections import OrderedDict | ||||||||||||||||||||||
| from copy import deepcopy | ||||||||||||||||||||||
|
|
@@ -32,6 +32,7 @@ | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| from cds_migrator_kit.errors import ( | ||||||||||||||||||||||
| ManualImportRequired, | ||||||||||||||||||||||
| MissingConfiguration, | ||||||||||||||||||||||
| MissingRequiredField, | ||||||||||||||||||||||
| MultipleModelsMatched, | ||||||||||||||||||||||
| RecordFlaggedCuration, | ||||||||||||||||||||||
|
|
@@ -43,6 +44,7 @@ | |||||||||||||||||||||
| VOCABULARIES_NAMES_SCHEMES, | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| from cds_migrator_kit.rdm.records.transform.config import ( | ||||||||||||||||||||||
| CERN_SCIENTIFIC_RESOURCE_TYPES, | ||||||||||||||||||||||
| EXPERIMENT_ALIASES, | ||||||||||||||||||||||
| FILE_SUBFORMATS_TO_DROP, | ||||||||||||||||||||||
| IDENTIFIERS_SCHEMES_TO_DROP, | ||||||||||||||||||||||
|
|
@@ -857,9 +859,42 @@ def __init__( | |||||||||||||||||||||
| self.db_state = {"affiliations": CDSMigrationAffiliationMapping} | ||||||||||||||||||||||
| super().__init__(workers, throw) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def _should_add_scientific_community(self, entry, record): | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| Determine if the scientific community should be added to the record. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| The scientific community is added if the following conditions are met: | ||||||||||||||||||||||
| - The record is public | ||||||||||||||||||||||
| - The files are public | ||||||||||||||||||||||
| - The record has a resource type in the CERN Scientific resource types | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| if self.restricted or record.get("access") != "public": | ||||||||||||||||||||||
|
sakshamarora1 marked this conversation as resolved.
|
||||||||||||||||||||||
| return False | ||||||||||||||||||||||
| if any(file.get("status") for file in entry.get("files", [])): | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are you sure that this covers everything? I would also check if the output json in access.files has it set to restricted or not
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested all the possible scenarios in the test cases. Could you also check that and let me know if something is missing? There is no access.files populated in this flow. Do you mean here? It only sets enabled key cds-migrator-kit/cds_migrator_kit/rdm/records/transform/transform.py Lines 781 to 785 in 059313b
cds-migrator-kit/cds_migrator_kit/rdm/records/transform/transform.py Lines 240 to 244 in 059313b
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for checking this. One more case comes to mind: we can set the whole collection to restricted, in this case we shouldn't add the scientific community either
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||
| return False | ||||||||||||||||||||||
| resource_type_id = ( | ||||||||||||||||||||||
| record.get("json", {}) | ||||||||||||||||||||||
| .get("metadata", {}) | ||||||||||||||||||||||
| .get("resource_type", {}) | ||||||||||||||||||||||
| .get("id") | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| return resource_type_id in CERN_SCIENTIFIC_RESOURCE_TYPES | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def _communities_ids(self, entry, record): | ||||||||||||||||||||||
| communities = record.get("communities", []) | ||||||||||||||||||||||
| communities = self.communities_ids + [slug for slug in communities] | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| scientific_community = current_app.config.get( | ||||||||||||||||||||||
|
sakshamarora1 marked this conversation as resolved.
|
||||||||||||||||||||||
| "CDS_CERN_SCIENTIFIC_COMMUNITY_ID" | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| if not scientific_community: | ||||||||||||||||||||||
|
sakshamarora1 marked this conversation as resolved.
|
||||||||||||||||||||||
| raise MissingConfiguration( | ||||||||||||||||||||||
| "CDS_CERN_SCIENTIFIC_COMMUNITY_ID is not configured" | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| if self._should_add_scientific_community(entry, record): | ||||||||||||||||||||||
| if scientific_community not in communities: | ||||||||||||||||||||||
| communities.append(scientific_community) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if communities: | ||||||||||||||||||||||
| return {"ids": communities, "default": self.communities_ids[0]} | ||||||||||||||||||||||
| return {} | ||||||||||||||||||||||
|
|
@@ -913,6 +948,7 @@ def _transform(self, entry): | |||||||||||||||||||||
| ManualImportRequired, | ||||||||||||||||||||||
| MissingRequiredField, | ||||||||||||||||||||||
| MultipleModelsMatched, | ||||||||||||||||||||||
| MissingConfiguration, | ||||||||||||||||||||||
| ) as e: | ||||||||||||||||||||||
| migration_logger.add_log(e, record=entry) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # Copyright (C) 2026 CERN. | ||
| # | ||
| # CDS-RDM is free software; you can redistribute it and/or modify it under | ||
| # the terms of the MIT License; see LICENSE file for more details. | ||
|
|
||
| """Tests for auto-inclusion in the CERN Research community.""" | ||
|
|
||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from cds_migrator_kit.errors import MissingConfiguration | ||
| from cds_migrator_kit.rdm.records.transform.config import ( | ||
| CERN_SCIENTIFIC_RESOURCE_TYPES, | ||
| ) | ||
| from cds_migrator_kit.rdm.records.transform.transform import CDSToRDMRecordTransform | ||
|
|
||
|
|
||
| def _test_record( | ||
| access="public", | ||
| resource_type="publication-preprint", | ||
| communities=[], | ||
| recid="123456", | ||
| ): | ||
| """Build a minimal CDSToRDMRecordEntry.transform() output for community tests.""" | ||
| metadata = { | ||
| "title": "Test record", | ||
| "publication_date": "2020-01-01", | ||
| } | ||
| if resource_type is not None: | ||
| metadata["resource_type"] = {"id": resource_type} | ||
|
|
||
| return { | ||
| "recid": recid, | ||
| "access": access, | ||
| "communities": communities, | ||
| "json": { | ||
| "files": {"enabled": False}, | ||
| "metadata": metadata, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| def _test_entry(files_restricted=False): | ||
| """Build a minimal raw dump entry for community tests.""" | ||
| status = "restricted" if files_restricted else "" | ||
| return {"files": [{"status": status}]} | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def transform(tmp_path, community): | ||
| """Transform instance with a collection community configured.""" | ||
| return CDSToRDMRecordTransform( | ||
| files_dump_dir=tmp_path, | ||
| missing_users=tmp_path, | ||
| communities_ids=[str(community.id)], | ||
| migration_logger=MagicMock(), | ||
| ) | ||
|
|
||
|
|
||
| class TestCommunitiesIds: | ||
| """Test CDSToRDMRecordTransform._communities_ids().""" | ||
|
|
||
| def test_adds_scientific_community_for_public_research_test_record( | ||
| self, transform, community, cern_scientific_community | ||
| ): | ||
| """Public research records are included in the CERN Scientific community.""" | ||
| record = _test_record() | ||
| record["json"]["files"] = {"enabled": True} | ||
| result = transform._communities_ids( | ||
| _test_entry(), | ||
| record, | ||
| ) | ||
|
|
||
| assert result == { | ||
| "ids": [str(community.id), str(cern_scientific_community.id)], | ||
| "default": str(community.id), | ||
| } | ||
|
|
||
| def test_keep_collection_community_as_default( | ||
| self, transform, community, cern_scientific_community | ||
| ): | ||
| """Collection community remains the default when CERN Scientific community is added.""" | ||
| result = transform._communities_ids( | ||
| _test_entry(), | ||
| _test_record(communities=["test-community"]), | ||
| ) | ||
|
|
||
| assert result["default"] == str(community.id) | ||
| assert result["ids"] == [ | ||
| str(community.id), | ||
| "test-community", | ||
| str(cern_scientific_community.id), | ||
| ] | ||
|
|
||
| @pytest.mark.parametrize("resource_type", CERN_SCIENTIFIC_RESOURCE_TYPES) | ||
| def test_research_resource_types( | ||
| self, transform, cern_scientific_community, resource_type | ||
| ): | ||
| """All configured public research resource types trigger inclusion.""" | ||
| result = transform._communities_ids( | ||
| _test_entry(), _test_record(resource_type=resource_type) | ||
| ) | ||
|
|
||
| assert str(cern_scientific_community.id) in result["ids"] | ||
| assert cern_scientific_community.id != result["default"] | ||
|
|
||
| def test_skip_restricted_test_record( | ||
| self, transform, community, cern_scientific_community | ||
| ): | ||
| """Restricted records are not included in the CERN Research community.""" | ||
| result = transform._communities_ids( | ||
| _test_entry(), _test_record(access="restricted") | ||
| ) | ||
|
|
||
| assert result == { | ||
| "ids": [str(community.id)], | ||
| "default": str(community.id), | ||
| } | ||
|
|
||
| def test_skip_restricted_files( | ||
| self, transform, community, cern_scientific_community | ||
| ): | ||
| """Records with restricted files are not included in the CERN Scientific community.""" | ||
| result = transform._communities_ids( | ||
| _test_entry(files_restricted=True), _test_record() | ||
| ) | ||
|
|
||
| assert result == { | ||
| "ids": [str(community.id)], | ||
| "default": str(community.id), | ||
| } | ||
|
|
||
| def test_skip_non_research_resource_type( | ||
| self, transform, community, cern_scientific_community | ||
| ): | ||
| """Non-research resource types are not included in the CERN Scientific community.""" | ||
| result = transform._communities_ids( | ||
| _test_entry(), _test_record(resource_type="other") | ||
| ) | ||
|
|
||
| assert result == { | ||
| "ids": [str(community.id)], | ||
| "default": str(community.id), | ||
| } | ||
|
|
||
| def test_skip_when_stream_is_restricted( | ||
| self, tmp_path, community, cern_scientific_community | ||
| ): | ||
| """Records on restricted migration streams are not included in the CERN Scientific community.""" | ||
| transform = CDSToRDMRecordTransform( | ||
| files_dump_dir=tmp_path, | ||
| missing_users=tmp_path, | ||
| communities_ids=[str(community.id)], | ||
| restricted=True, | ||
| migration_logger=MagicMock(), | ||
| ) | ||
|
|
||
| result = transform._communities_ids(_test_entry(), _test_record()) | ||
|
|
||
| assert result == { | ||
| "ids": [str(community.id)], | ||
| "default": str(community.id), | ||
| } | ||
|
|
||
| def test_raise_when_cern_scientific_community_not_configured( | ||
| self, test_app, transform, community, monkeypatch | ||
| ): | ||
| """No CERN Scientific community is added when config is unset.""" | ||
| monkeypatch.setitem(test_app.config, "CDS_CERN_SCIENTIFIC_COMMUNITY_ID", None) | ||
|
|
||
| with pytest.raises(MissingConfiguration): | ||
| transform._communities_ids(_test_entry(), _test_record()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to add: 'publication-conferencenote', added recently