Skip to content

Commit

Permalink
api: add __reversed__ for RevisionsIterator which allows to use rever…
Browse files Browse the repository at this point in the history
…sed operator

 *Allows for proper use pf reversed operator with new implementation of __getitem__ if there are gaps in revision numbers
  • Loading branch information
pazembrz authored and lnielsen committed May 27, 2020
1 parent 7f857d4 commit 54f8c94
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions invenio_records/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,8 @@ def __contains__(self, revision_id):
return True
except IndexError:
return False

def __reversed__(self):
"""Allows to use reversed operator."""
for version_index in range(self.model.versions.count()):
yield RecordRevision(self.model.versions[-(version_index+1)])
19 changes: 19 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,22 @@ def test_validate_partial(app, db):
record['a'] = 1
with pytest.raises(ValidationError) as exc_info:
record.commit(validator=PartialDraft4Validator)


def test_reversed_works_for_revisions(app, db):
record = Record.create({'title': 'test 1'})
db.session.commit()

record['title'] = "test 2"
record.commit()
record['title'] = "Test 3"
db.session.commit()

record['title'] = "test 4"
record.commit()
db.session.commit()

reversed_revisions = list(reversed(record.revisions))
reversed_revisions[0].revision_id == 3
reversed_revisions[1].revision_id == 1
reversed_revisions[2].revision_id == 0

0 comments on commit 54f8c94

Please sign in to comment.