Skip to content

Commit

Permalink
api: fix revisions resolving
Browse files Browse the repository at this point in the history
 *Fixes revisions resolving when there are gaps in revisions numbers

*INSPIR-3486
  • Loading branch information
pazembrz committed May 26, 2020
1 parent 3b0a8d2 commit bf9f15a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
17 changes: 15 additions & 2 deletions invenio_records/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from invenio_db import db
from jsonpatch import apply_patch
from sqlalchemy.orm.attributes import flag_modified
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.local import LocalProxy

from .errors import MissingModelError
Expand Down Expand Up @@ -398,8 +399,20 @@ def __next__(self):
return RecordRevision(next(self._it))

def __getitem__(self, revision_id):
"""Get a specific revision."""
return RecordRevision(self.model.versions[revision_id])
"""Get a specific revision.
Revision id is always smaller by 1 from version_id.
"""
if revision_id < 0:
return RecordRevision(self.model.versions[revision_id])
try:
return RecordRevision(
self.model.versions.filter_by(
version_id=revision_id + 1
).one()
)
except NoResultFound:
raise IndexError

def __contains__(self, revision_id):
"""Test if revision exists."""
Expand Down
40 changes: 40 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,46 @@ def test_revisions(app, db):
assert 5 not in record.revisions


def test_retrieve_proper_revision(app, db):
"""Check accessing revision with gaps
Test checks if it's possible to access proper revision
when revision numbers have 'gaps'
"""
record = Record.create({'title': 'test 1'})
db.session.commit()
record['title'] = "test 2"
record.commit()
record['title'] = "test 3"
record.commit()
db.session.commit()
record['title'] = "test 4"
record.commit()
db.session.commit()
record['title'] = "test 5"
record.commit()
db.session.commit()

assert len(record.revisions) == 4
revs = list(record.revisions)
assert revs[0]['title'] == "test 1"
assert revs[0].revision_id == 0
assert revs[1]['title'] == "test 3"
assert revs[1].revision_id == 2
assert revs[2]['title'] == "test 4"
assert revs[2].revision_id == 3
assert revs[3]['title'] == "test 5"
assert revs[3].revision_id == 4

# Access revision by revision_id
rev_2 = record.revisions[2]
assert rev_2['title'] == "test 3"
assert rev_2.revision_id == 2

# Access revision by negative list index
assert rev_2 == record.revisions[-3]


def test_record_update_mutable(app, db):
"""Test updating mutables in a record."""
recid = uuid.UUID('262d2748-ba41-456f-a844-4d043a419a6f')
Expand Down

0 comments on commit bf9f15a

Please sign in to comment.