inveniosoftware / invenio-oaiserver

Invenio module that adds more fun to the platform.
https://invenio-oaiserver.readthedocs.io
MIT License
3 stars 44 forks source link

OAISets: implement add and remove record #211

Open ppanero opened 2 years ago

ppanero commented 2 years ago

With the new dynamic implementation of sets, the old add_record and remove_records do not work. They can be implemented but would require to do update the search_pattern.

    def add_record(self, record):
        """Add a record to the OAISet.

        :param record: Record to be added.
        :type record: `invenio_records.api.Record` or derivative.
        """
        record.setdefault('_oai', {}).setdefault('sets', [])

        assert not self.has_record(record)

        record['_oai']['sets'].append(self.spec)

    def remove_record(self, record):
        """Remove a record from the OAISet.

        :param record: Record to be removed.
        :type record: `invenio_records.api.Record` or derivative.
        """
        assert self.has_record(record)

        record['_oai']['sets'] = [
            s for s in record['_oai']['sets'] if s != self.spec]

Old tests:

def test_oaiset_add_remove_record(app):
    """Test the API method for manual record adding."""
    with app.app_context():
        oaiset1 = OAISet(spec='abc')
        rec1 = Record.create({'title_statement': {'title': 'Test1'}})
        rec1.commit()
        # Adding a record to an OAIset should change the record's updated date
        dt1 = rec1.updated
        assert not oaiset1.has_record(rec1)
        oaiset1.add_record(rec1)
        assert 'abc' in rec1['_oai']['sets']
        assert oaiset1.has_record(rec1)
        rec1.commit()
        dt2 = rec1.updated
        assert dt2 > dt1

        oaiset1.remove_record(rec1)
        rec1.commit()
        dt3 = rec1.updated
        assert 'abc' not in rec1['_oai']['sets']
        assert not oaiset1.has_record(rec1)
        assert dt3 > dt2