def test_unmanaged_duplicated_doi(not_req_unmanaged_pid_cmp, minimal_record, identity_simple, location): component = not_req_unmanaged_pid_cmp pids = { "nrequman": { "identifier": "avalues", "provider": "nrequman", } } # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == pids # publish record = RDMRecord.publish(draft) component.publish(identity_simple, draft=draft, record=record) assert record.pids == pids # create a second record with the same unmanaged DOI data = minimal_record.copy() data["pids"] = pids draft = RDMDraft.create(data) with pytest.raises(ValidationError): component.create(identity_simple, data=data, record=draft)
def test_unmanaged_no_required_no_partial_value(not_req_unmanaged_pid_cmp, minimal_record, identity_simple, location): component = not_req_unmanaged_pid_cmp # if no name provided, one of the configured must be required # since is not required we need to pass the provider name pids = { "nrequman": { "provider": "nrequman", } } # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == pids # publish # NOTE: Better fail than strip and not tell the user. record = RDMRecord.publish(draft) with pytest.raises(ValidationError): component.publish(identity_simple, draft=draft, record=record)
def test_unmanaged_required_pid_value(req_pid_unmanaged_cmp, minimal_record, identity_simple, location): component = req_pid_unmanaged_cmp pids = { "requman": { "identifier": "value", "provider": "requman", } } # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == pids # publish record = RDMRecord.publish(draft) component.publish(identity_simple, draft=draft, record=record) assert record.pids == pids provider = RequiredUnmanagedPIDProvider("requir") pid = provider.get_by_record(record.id, pid_value="value") assert pid.is_registered()
def test_create_managed_doi_empty_pids(req_managed_pid_cmp, minimal_record, identity_simple, mocker, location): client = mocker.patch( "invenio_rdm_records.services.pids.providers.datacite." "DOIDataCiteClient") mocker.patch("invenio_rdm_records.services.config.DOIDataCiteClient") mocker.patch("invenio_rdm_records.services.pids.providers.datacite." + "DataCiteRESTClient") component = req_managed_pid_cmp pids = {} # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == pids # publish record = RDMRecord.publish(draft) component.publish(identity_simple, draft=draft, record=record) doi = record.pids.get("doi") assert doi assert doi["identifier"] assert doi["provider"] == "datacite" assert doi["client"] == "datacite" # default provider = DOIDataCitePIDProvider(client) assert provider.get_by_record(record.id, pid_value=doi["identifier"])
def test_create_managed_doi_with_no_value(req_managed_pid_cmp, minimal_record, identity_simple, mocker, location): client = mocker.patch( "invenio_rdm_records.services.pids.providers.datacite." "DOIDataCiteClient") mocker.patch("invenio_rdm_records.services.pids.providers.datacite." + "DataCiteRESTClient") mocker.patch("invenio_rdm_records.services.pids.providers.datacite." + "DataCite43JSONSerializer") component = req_managed_pid_cmp pids = {"doi": {"provider": "datacite", "client": "rdm"}} # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == pids # publish record = RDMRecord.publish(draft) component.publish(identity_simple, draft=draft, record=record) doi = record.pids.get("doi") assert doi assert doi["identifier"] assert doi["provider"] == "datacite" assert doi["client"] == "rdm" # tests non-default client works provider = DOIDataCitePIDProvider(client) assert provider.get(doi["identifier"]).is_registered()
def test_create_managed_doi_with_value(req_managed_pid_cmp, minimal_record, identity_simple, mocker, location): client = mocker.patch( "invenio_rdm_records.services.pids.providers.datacite." "DOIDataCiteClient") mocker.patch("invenio_rdm_records.services.pids.providers.datacite." + "DataCiteRESTClient") component = req_managed_pid_cmp # create a minimal draft data = minimal_record.copy() draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == {} # when provider is managed and required, and the identifier is in the # payload, it means that it was reserved via the `reserve` endpoint provider = DOIDataCitePIDProvider(client) pid = provider.create(draft) provider.reserve(pid, draft) pids = { "doi": { "identifier": pid.pid_value, "provider": "datacite", "client": "rdm" } } draft["pids"] = pids # publish record = RDMRecord.publish(draft) component.publish(identity_simple, draft=draft, record=record) assert record.pids == pids assert provider.get(pid.pid_value).is_registered()
def test_doi_publish_versions(app, location, minimal_record, identity_simple, mocker): client = mocker.patch( "invenio_rdm_records.services.pids.providers.datacite." "DOIDataCiteClient") mocker.patch("invenio_rdm_records.services.pids.providers.datacite." + "DataCiteRESTClient") mocker.patch("invenio_rdm_records.services.pids.providers.datacite." + "DataCite43JSONSerializer") component = ExternalPIDsComponent(current_rdm_records.records_service) doi_provider = DOIDataCitePIDProvider(client) data = minimal_record.copy() # make sure `pids` field is added on create del data["pids"] # create a minimal draft draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == {} # publish record = RDMRecord.publish(draft) # NOTE: simulate metadata component required by datacite serialization record["metadata"] = draft["metadata"] component.publish(identity_simple, draft=draft, record=record) doi_v1 = record.pids["doi"] # DOI assert doi_v1 assert doi_v1["identifier"] assert doi_v1["provider"] == "datacite" assert doi_v1["client"] == "datacite" # Default since no values given doi_pid = doi_provider.get(doi_v1["identifier"]) assert doi_pid.object_uuid == record.id # create a new version (v2) of the record draft_v2 = RDMDraft.new_version(record) component.new_version(identity_simple, draft=draft_v2, record=record) assert "pids" in draft and draft.pids == {} # publish v2 record_v2 = RDMRecord.publish(draft_v2) # NOTE: simulate metadata component required by datacite serialization draft_v2["metadata"] = minimal_record["metadata"] component.publish(identity_simple, draft=draft_v2, record=record_v2) doi_v2 = record_v2.pids["doi"] # DOI assert doi_v2 assert doi_v2["identifier"] assert doi_v2["provider"] == "datacite" assert doi_v2["client"] == "datacite" # Default since no values given assert doi_v1 != doi_v2 doi_v2_pid = doi_provider.get(doi_v2["identifier"]) assert doi_v2_pid.object_uuid == record_v2.id assert doi_v1 != doi_v2
def test_create_no_required_pids(pids, no_required_pids_cmp, minimal_record, identity_simple, location): component = no_required_pids_cmp # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == pids
def _create_record(): """Create a record.""" # create draft draft = RDMDraft.create(minimal_record) draft.commit() db.session.commit() # publish and get record record = RDMRecord.publish(draft) record.commit() db.session.commit() return record
def test_update_external_to_empty(no_required_pids_cmp, minimal_record, superuser_identity, location): component = no_required_pids_cmp data = minimal_record.copy() data["pids"] = {"test": {"identifier": "1234", "provider": "external"}} # create a minimal draft draft = RDMDraft.create(data) assert draft.pids["test"]["identifier"] == "1234" data["pids"] = {} # run the delete hook and check the pid is not in the system anymore component.update_draft(superuser_identity, data=data, record=draft) assert draft.pids == {}
def test_create_with_incomplete_payload(pids, expected_errors, no_required_pids_cmp, minimal_record, identity_simple, location): component = no_required_pids_cmp # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) errors = [] component.create(identity_simple, data=data, record=draft, errors=errors) assert errors == expected_errors assert "pids" in draft and draft.pids == pids
def test_publish_no_pids(no_pids_cmp, minimal_record, identity_simple, location): component = no_pids_cmp # empty pids field pids = {} # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) # no validation needed since /publish gets no payload (new data) record = RDMRecord.publish(draft) component.publish(identity_simple, draft=draft, record=record) assert record.get("pids") == {}
def test_create_pid_type_not_supported(no_pids_cmp, minimal_record, identity_simple, location): component = no_pids_cmp # managed pid pids = {"test": {"identifier": "1234", "provider": "managed"}} # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) # note that something like {"test": {}} should be caught at marshmallow # level, which is tested at service level with pytest.raises(PIDSchemeNotSupportedError): component.create(identity_simple, data=data, record=draft)
def test_non_configured_provider(not_req_managed_pid_cmp, minimal_record, identity_simple, location): component = not_req_managed_pid_cmp pids = { "noconfig": { "provider": "nrequman", } } # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) with pytest.raises(Exception): component.create(identity_simple, data=data, record=draft)
def test_publish_non_existing_required_external(required_external_pids_cmp, minimal_record, identity_simple, location): # no need to test for existing (i.e. already in the payload) required pids # since they go over the flow of the test above # test_publish_no_required_pids component = required_external_pids_cmp # make sure `pids` field is added data = minimal_record.copy() data["pids"] = {} # create a minimal draft draft = RDMDraft.create(data) # no validation needed since /publish gets no payload (new data) record = RDMRecord.publish(draft) with pytest.raises(ValidationError): component.publish(identity_simple, draft=draft, record=record)
def test_unmanaged_no_required_no_pids(not_req_unmanaged_pid_cmp, minimal_record, identity_simple): # NOTE: Since is {} should simply be ignored component = not_req_unmanaged_pid_cmp pids = {} # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == pids # publish record = RDMRecord.publish(draft) component.publish(identity_simple, draft=draft, record=record)
def test_delete_pids_from_draft(no_required_pids_cmp, minimal_record, identity_simple, location): # a draft that gets updated with a pid saved to pidstore via reserve component = no_required_pids_cmp data = minimal_record.copy() pid_value = "1" data["pids"] = {"test": {"identifier": pid_value, "provider": "managed"}} # create a minimal draft draft = RDMDraft.create(data) # create pid, simulates a query to the reserve endpoint (/:scheme) provider = TestManagedPIDProvider("managed", pid_type="test") pid = _create_managed_pid(draft, pid_value, provider) # run the delete hook and check the pid is not in the system anymore component.delete_draft(identity_simple, draft=draft) with pytest.raises(PIDDoesNotExistError): pid = provider.get(pid_value, pid_provider="managed")
def test_update_managed_to_empty(no_required_pids_cmp, minimal_record, superuser_identity, location): component = no_required_pids_cmp data = minimal_record.copy() pid_value = "1" data["pids"] = {"test": {"identifier": pid_value, "provider": "managed"}} # create a minimal draft draft = RDMDraft.create(data) assert draft.pids["test"]["identifier"] == "1" # create pid, simulates a query to the reserve endpoint (/:scheme) provider = TestManagedPIDProvider("managed", pid_type="test") pid = _create_managed_pid(draft, pid_value, provider) data["pids"] = {} # run the delete hook and check the pid is not in the system anymore component.update_draft(superuser_identity, data=data, record=draft) assert draft.pids == {}
def test_update_external_to_managed(no_required_pids_cmp, minimal_record, superuser_identity, location): component = no_required_pids_cmp data = minimal_record.copy() data["pids"] = {"test": {"identifier": "1234", "provider": "external"}} # create a minimal draft draft = RDMDraft.create(data) assert draft.pids["test"]["identifier"] == "1234" # create pid, simulates a query to the reserve endpoint (/:scheme) pid_value = "1" provider = TestManagedPIDProvider("managed", pid_type="test") pid = _create_managed_pid(draft, pid_value, provider) ext_pids = {"test": {"identifier": pid_value, "provider": "managed"}} data["pids"] = ext_pids # run the delete hook and check the pid is not in the system anymore component.update_draft(superuser_identity, data=data, record=draft) assert draft.pids == ext_pids pid = provider.get(pid_value, pid_provider="managed") assert pid.pid_value == pid_value assert pid.status == PIDStatus.NEW
def test_publish_no_required_pids(pids, no_required_pids_service, no_required_pids_cmp, minimal_record, identity_simple, location): # a managed pid is not accepted in this case since it would need to be # created/reserved before hand. that flow is tested at service level. component = no_required_pids_cmp # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) # no validation needed since /publish gets no payload (new data) record = RDMRecord.publish(draft) component.publish(identity_simple, draft=draft, record=record) assert record.get("pids") == pids for schema, pid in pids.items(): provider = no_required_pids_service.pids.pid_manager._get_provider( schema, pid["provider"]) pid = provider.get(pid.get("identifier")) assert pid.is_reserved() # cannot test registration because is async
def test_unmanaged_no_required_no_pids(not_req_unmanaged_pid_cmp, minimal_record, identity_simple, location): # NOTE: Since is {} should simply be ignored component = not_req_unmanaged_pid_cmp pids = {} # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == pids # publish record = RDMRecord.publish(draft) component.publish(identity_simple, draft=draft, record=record) with pytest.raises(PIDDoesNotExistError): provider = NotRequiredUnmanagedPIDProvider("notreq") assert provider.get_by_record(record.id, pid_value="value")
def test_unmanaged_required_no_pid_value(req_pid_unmanaged_cmp, minimal_record, identity_simple, location): component = req_pid_unmanaged_cmp pids = { "requman": { "provider": "requman", } } # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == pids # publish, fail because is required an no value given record = RDMRecord.publish(draft) with pytest.raises(ValidationError): component.publish(identity_simple, draft=draft, record=record)
def test_unmanaged_no_required_pid_value(not_req_unmanaged_pid_cmp, minimal_record, identity_simple): component = not_req_unmanaged_pid_cmp pids = { "nrequman": { "identifier": "value", "provider": "nrequman", } } # make sure `pids` field is added data = minimal_record.copy() data["pids"] = pids # create a minimal draft draft = RDMDraft.create(data) component.create(identity_simple, data=data, record=draft) assert "pids" in draft and draft.pids == pids # publish record = RDMRecord.publish(draft) component.publish(identity_simple, draft=draft, record=record) assert record.pids == pids
def test_publish_non_existing_required_managed(required_managed_pids_cmp, minimal_record, identity_simple, location): # no need to test for existing (i.e. already in the payload) required pids # since they go over the flow of the test above # test_publish_no_required_pids component = required_managed_pids_cmp # make sure `pids` field is added data = minimal_record.copy() data["pids"] = {} # create a minimal draft draft = RDMDraft.create(data) # no validation needed since /publish gets no payload (new data) record = RDMRecord.publish(draft) component.publish(identity_simple, draft=draft, record=record) assert "pids" in record assert record.pids == {"test": {"identifier": "1", "provider": "managed"}} provider = TestManagedPIDProvider("managed", pid_type="test") pid = provider.get(record.pids["test"]["identifier"]) assert pid.is_reserved() # cannot test registration because is async
def record(location): """Creates an empty record.""" draft = RDMDraft.create({}) record = RDMRecord.publish(draft) return record