Skip to content

Commit

Permalink
[store][s+]: implement search properly (ported from old code) and add…
Browse files Browse the repository at this point in the history
… uri attribute to model.Annotation (was there before but had been dropped).
  • Loading branch information
rgrp authored and nickstenning committed Feb 18, 2011
1 parent e2e7447 commit 89b3037
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions annotator/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def setup_in_memory():

class Annotation(Entity):
id = Field(Integer, primary_key=True)
uri = Field(UnicodeText)
text = Field(UnicodeText)
user = Field(UnicodeText)
extras = Field(UnicodeText, default=u'{}')
Expand Down
31 changes: 28 additions & 3 deletions annotator/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ def delete_annotation(id):
# Search
@store.route('/search')
def search_annotations():
# TODO: actually do some searching.
annotations = [a.to_dict() for a in Annotation.query.all()]
return jsonify({'results': annotations})
params = [
(k,v) for k,v in request.args.items() if k not in [ 'all_fields', 'offset', 'limit' ]
]
all_fields = request.args.get('all_fields', False)
all_fields = bool(all_fields)
offset = request.args.get('offset', 0)
limit = int(request.args.get('limit', 100))
if limit < 0:
limit = None

q = Annotation.query
for k,v in params:
kwargs = { k: unicode(v) }
q = q.filter_by(**kwargs)

total = q.count()
rows = q.offset(offset).limit(limit).all()
if all_fields:
rows = [ x.to_dict() for x in rows ]
else:
rows = [ {'id': x.id} for x in rows ]

qrows = {
'total': total,
'rows': rows
}
return jsonify(qrows)

55 changes: 55 additions & 0 deletions tests/annotator/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,61 @@ def test_delete_notfound(self):
response = self.app.delete('/annotations/123')
assert response.status_code == 404, "response should be 404 NOT FOUND"

def test_search(self):
uri1 = u'http://xyz.com'
uri2 = u'urn:uuid:xxxxx'
user = u'levin'
user2 = u'anna'
anno = Annotation(
uri=uri1,
text=uri1,
user=user,
)
anno2 = Annotation(
uri=uri1,
text=uri1 + uri1,
user=user2,
)
anno3 = Annotation(
uri=uri2,
text=uri2,
user=user
)
session.commit()
annoid = anno.id
anno2id = anno2.id
session.remove()

url = '/search'
res = self.app.get(url)
body = json.loads(res.data)
assert body['total'] == 3, body

url = '/search?limit=1'
res = self.app.get(url)
body = json.loads(res.data)
assert body['total'] == 3, body
assert len(body['rows']) == 1

url = '/search?uri=' + uri1 + '&all_fields=1'
res = self.app.get(url)
body = json.loads(res.data)
assert body['total'] == 2, body
out = body['rows']
assert len(out) == 2
assert out[0]['uri'] == uri1
assert out[0]['id'] in [ annoid, anno2id ]

url = '/search?uri=' + uri1
res = self.app.get(url)
body = json.loads(res.data)
assert body['rows'][0].keys() == ['id'], body['rows']

url = '/search?limit=-1'
res = self.app.get(url)
body = json.loads(res.data)
assert len(body['rows']) == 3, body

def test_cors_preflight(self):
response = self.app.open('/annotations', method="OPTIONS")

Expand Down

0 comments on commit 89b3037

Please sign in to comment.