def test_many_redirections_to_the_same_pid(inspire_app): pid_1 = PersistentIdentifier.create( pid_type="a", pid_value="1", status=PIDStatus.REGISTERED, object_uuid=uuid.uuid4(), ) pid_2 = PersistentIdentifier.create( pid_type="a", pid_value="2", status=PIDStatus.REGISTERED, object_uuid=uuid.uuid4(), ) pid_3 = PersistentIdentifier.create( pid_type="a", pid_value="3", status=PIDStatus.REGISTERED, object_uuid=uuid.uuid4(), ) InspireRedirect.redirect(pid_1, pid_2) InspireRedirect.redirect(pid_3, pid_2) assert pid_1.status == PIDStatus.REDIRECTED assert pid_3.status == PIDStatus.REDIRECTED assert len(pid_2.redirected_pids) == 2
def test_get_redirected_pid(inspire_app): pid_1 = PersistentIdentifier.create(pid_type="a", pid_value="1", status=PIDStatus.REGISTERED) pid_2 = PersistentIdentifier.create(pid_type="a", pid_value="2", status=PIDStatus.REGISTERED) InspireRedirect.redirect(pid_1, pid_2) assert InspireRedirect.get_redirect(pid_1) == pid_2
def test_redirection_invalidates_correctly_pid_objects(inspire_app): pid_1 = PersistentIdentifier.create( pid_type="a", pid_value="1", status=PIDStatus.REGISTERED, object_uuid=uuid.uuid4(), ) pid_2 = PersistentIdentifier.create( pid_type="a", pid_value="2", status=PIDStatus.REGISTERED, object_uuid=uuid.uuid4(), ) pid_3 = PersistentIdentifier.create( pid_type="a", pid_value="3", status=PIDStatus.REGISTERED, object_uuid=uuid.uuid4(), ) InspireRedirect.redirect(pid_1, pid_2) redirection = InspireRedirect.get(pid_1) assert redirection.original_pid == pid_1 assert redirection.new_pid == pid_2 assert pid_1.status == PIDStatus.REDIRECTED assert pid_1.redirection == redirection assert pid_2.redirected_pids == [redirection] assert pid_3.redirected_pids == [] pid_2.status = PIDStatus.DELETED InspireRedirect.redirect(pid_1, pid_3) assert redirection.original_pid == pid_1 assert redirection.new_pid == pid_3 assert pid_1.status == PIDStatus.REDIRECTED assert pid_1.redirection == redirection assert pid_2.redirected_pids == [] assert pid_3.redirected_pids == [redirection] assert pid_2.status == PIDStatus.DELETED redirection.delete() assert pid_1.status == PIDStatus.DELETED assert pid_1.redirection is None assert len(pid_2.redirected_pids) == 0 with pytest.raises(PidRedirectionMissing): InspireRedirect.get(pid_1)
def test_delete_redirected_record_is_not_deleting_redirected_pid(inspire_app): redirected_record = create_record("lit") new_record = create_record("lit") redirected_pid = redirected_record.control_number_pid new_pid = new_record.control_number_pid InspireRedirect.redirect(redirected_pid, new_pid) redirected_record.delete() record_from_redirection = LiteratureRecord.get_record_by_pid_value( redirected_record.control_number) assert record_from_redirection.id == new_record.id
def test_redirect_pid_to_many_pids_overwrites_previous_redirection( inspire_app): pid_1 = PersistentIdentifier.create(pid_type="a", pid_value="1", status=PIDStatus.REGISTERED) pid_2 = PersistentIdentifier.create(pid_type="a", pid_value="2", status=PIDStatus.REGISTERED) pid_3 = PersistentIdentifier.create(pid_type="a", pid_value="3", status=PIDStatus.REGISTERED) InspireRedirect.redirect(pid_1, pid_2) InspireRedirect.redirect(pid_1, pid_3) assert InspireRedirect.get_redirect(pid_1) == pid_3
def test_get_redirection_object(inspire_app): pid_1 = PersistentIdentifier.create(pid_type="a", pid_value="1", status=PIDStatus.REGISTERED) pid_2 = PersistentIdentifier.create(pid_type="a", pid_value="2", status=PIDStatus.REGISTERED) InspireRedirect.redirect(pid_1, pid_2) redirection = InspireRedirect.get(pid_1) assert redirection.original_pid.pid_type == pid_1.pid_type assert redirection.original_pid.pid_value == pid_1.pid_value assert redirection.new_pid.pid_type == pid_2.pid_type assert redirection.new_pid.pid_value == pid_2.pid_value with pytest.raises(PidRedirectionMissing): InspireRedirect.get(pid_2)
def test_deleting_redirection_object(inspire_app): pid_1 = PersistentIdentifier.create(pid_type="a", pid_value="1", status=PIDStatus.REGISTERED) pid_2 = PersistentIdentifier.create(pid_type="a", pid_value="2", status=PIDStatus.REGISTERED) InspireRedirect.redirect(pid_1, pid_2) assert pid_1.status == PIDStatus.REDIRECTED redirection = InspireRedirect.get(pid_1) redirection.delete() assert pid_1.status == PIDStatus.DELETED with pytest.raises(PidRedirectionMissing): InspireRedirect.get(pid_1)
def redirect_pid(self, pid_type, pid_value): try: old_pid = PersistentIdentifier.get(pid_type, pid_value) except PIDDoesNotExistError: LOGGER.warning( "Cannot redirect non existent PID", pid_to_redirect=pid_value, redirecting_pid=self.control_number, pid_type=self.pid_type, ) return old_pid_object_uuid = str(old_pid.object_uuid) new_pid = self.control_number_pid InspireRedirect.redirect(old_pid, new_pid) old_record = self.get_record(old_pid_object_uuid, with_deleted=True) old_record["new_record"] = get_ref_from_pid(self.pid_type, self.control_number) if not old_record.get("deleted"): old_record.delete() else: old_record.update(dict(old_record))