Permalink
Please sign in to comment.
Showing
with
240 additions
and 237 deletions.
| @@ -0,0 +1,9 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from h.api.search.core import index | ||
| from h.api.search.core import search | ||
| __all__ = ( | ||
| 'index', | ||
| 'search', | ||
| ) |
| @@ -0,0 +1,50 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import logging | ||
| from elasticsearch import helpers | ||
| import webob.multidict | ||
| from h.api import models | ||
| from h.api.search import query | ||
| log = logging.getLogger(__name__) | ||
| def search(request_params, user=None): | ||
| """ | ||
| Search with the given params and return the matching annotations. | ||
| :param request_params: the HTTP request params that were posted to the | ||
| h search API | ||
| :type request_params: webob.multidict.NestedMultiDict | ||
| :param user: the authorized user, or None | ||
| :type user: h.accounts.models.User or None | ||
| :returns: a dict with keys "rows" (the list of matching annotations, as | ||
| dicts) and "total" (the number of matching annotations, an int) | ||
| :rtype: dict | ||
| """ | ||
| userid = user.id if user else None | ||
| log.debug("Searching with user=%s, for uri=%s", | ||
| str(userid), request_params.get('uri')) | ||
| body = query.build(request_params, userid=userid) | ||
| results = models.Annotation.search_raw(body, user=user, raw_result=True) | ||
| total = results['hits']['total'] | ||
| docs = results['hits']['hits'] | ||
| rows = [models.Annotation(d['_source'], id=d['_id']) for d in docs] | ||
| return {"rows": rows, "total": total} | ||
| def index(user=None): | ||
| """ | ||
| Return the 20 most recent annotations, most-recent first. | ||
| Returns the 20 most recent annotations that are visible to the given user, | ||
| or that are public if user is None. | ||
| """ | ||
| return search(webob.multidict.NestedMultiDict({"limit": 20}), user=user) |
No changes.
| @@ -0,0 +1,50 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import mock | ||
| from webob import multidict | ||
| from h.api.search import core | ||
| @mock.patch("annotator.annotation.Annotation.search_raw") | ||
| @mock.patch("h.api.search.query.build") | ||
| def test_search_with_user_object(_, search_raw): | ||
| """If search() gets a user arg it passes it to search_raw(). | ||
| Note: This test is testing the function's user param. You can also | ||
| pass one or more user arguments in the request.params, those are | ||
| tested elsewhere. | ||
| """ | ||
| user = mock.MagicMock() | ||
| core.search(request_params=multidict.NestedMultiDict(), user=user) | ||
| first_call = search_raw.call_args_list[0] | ||
| assert first_call[1]["user"] == user | ||
| @mock.patch("annotator.annotation.Annotation.search_raw") | ||
| @mock.patch("h.api.search.query.build") | ||
| def test_search_does_not_pass_userid_to_build(build, _): | ||
| core.search(multidict.NestedMultiDict()) | ||
| assert build.call_args[1]["userid"] is None | ||
| @mock.patch("annotator.annotation.Annotation.search_raw") | ||
| @mock.patch("h.api.search.query.build") | ||
| def test_search_does_pass_userid_to_build(build, _): | ||
| user = mock.Mock(id="test_id") | ||
| core.search(multidict.NestedMultiDict(), user=user) | ||
| assert build.call_args[1]["userid"] == "test_id" | ||
| @mock.patch("h.api.search.core.search") | ||
| def test_index_limit_is_20(search_func): | ||
| """index() calls search with "limit": 20.""" | ||
| core.index() | ||
| first_call = search_func.call_args_list[0] | ||
| assert first_call[0][0]["limit"] == 20 |
Oops, something went wrong.
0 comments on commit
8d05ab5