Skip to content

Commit

Permalink
feat(latest_version): allow filter by has_version (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
philloooo authored May 7, 2018
1 parent f3cec9b commit 63f6874
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
4 changes: 3 additions & 1 deletion indexd/index/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ def get_latest_index_record_versions(record):
'''
Get the latest record version
'''
ret = blueprint.index_driver.get_latest_version(record)
has_version = flask.request.args.get('has_version', '').lower() == 'true'
ret = blueprint.index_driver.get_latest_version(
record, has_version=has_version)

return flask.jsonify(ret), 200

Expand Down
2 changes: 1 addition & 1 deletion indexd/index/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def get_all_versions(self, did):
raise NotImplementedError('TODO')

@abc.abstractmethod
def get_latest_version(self, did):
def get_latest_version(self, did, has_version=None):
'''
Get the lattest record version given did
'''
Expand Down
11 changes: 6 additions & 5 deletions indexd/index/drivers/alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,11 +703,10 @@ def get_all_versions(self, did):

return ret

def get_latest_version(self, did):
def get_latest_version(self, did, has_version=None):
"""
Get the lattest record version given did
"""
ret = {}
with self.session as session:
query = session.query(IndexRecord)
query = query.filter(IndexRecord.did == did)
Expand All @@ -721,9 +720,11 @@ def get_latest_version(self, did):
raise MultipleRecordsFound('multiple records found')

query = session.query(IndexRecord)
record = query.filter(IndexRecord.baseid == baseid) \
.order_by(IndexRecord.created_date.desc()).first()

query = query.filter(IndexRecord.baseid == baseid) \
.order_by(IndexRecord.created_date.desc())
if has_version:
query = query.filter(IndexRecord.version.isnot(None))
record = query.first()
if (not record):
raise NoRecordFound('no record found')

Expand Down
5 changes: 5 additions & 0 deletions openapis/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ paths:
description: entry id
required: true
type: string
- name: has_version
in: query
description: filter by latest doc that has version value populated
required: false
type: boolean
responses:
'200':
description: successful operation
Expand Down
12 changes: 10 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from indexd.index.blueprint import ACCEPTABLE_HASHES


def get_doc(has_metadata=True, has_baseid=False, has_urls_metadata=False):
def get_doc(
has_metadata=True, has_baseid=False,
has_urls_metadata=False, has_version=False):
doc = {
'form': 'object',
'size': 123,
Expand All @@ -20,6 +22,8 @@ def get_doc(has_metadata=True, has_baseid=False, has_urls_metadata=False):
if has_urls_metadata:
doc['urls_metadata'] = {
's3://endpointurl/bucket/key': {'file_state': 'uploaded'}}
if has_version:
doc['version'] = '1'
return doc


Expand Down Expand Up @@ -296,17 +300,21 @@ def test_create_index_version(swg_index_client):


def test_get_latest_version(swg_index_client):
data = get_doc(has_metadata=False, has_baseid=False)
data = get_doc(has_metadata=False, has_baseid=False, has_version=True)
r = swg_index_client.add_entry(data)
assert r.did

data = get_doc(has_metadata=False, has_baseid=False, has_version=False)
r2 = swg_index_client.add_new_version(r.did, body=data)
r3 = swg_index_client.get_latest_version(r.did)
assert r3.did == r2.did

r4 = swg_index_client.get_latest_version(r.baseid)
assert r4.did == r2.did

r5 = swg_index_client.get_latest_version(r.baseid, has_version=True)
assert r5.did == r.did


def test_get_all_versions(swg_index_client):
data = get_doc(has_metadata=False, has_baseid=False)
Expand Down

0 comments on commit 63f6874

Please sign in to comment.