def test_it_only_returns_top_level_annotations( self, pyramid_request, search, TopLevelAnnotationsFilter ): execute(pyramid_request, MultiDict(), self.PAGE_SIZE) TopLevelAnnotationsFilter.assert_called_once_with() search.append_modifier.assert_any_call(TopLevelAnnotationsFilter.return_value)
def test_it_fetches_the_annotations_from_the_database( self, fetch_annotations, pyramid_request, search): execute(pyramid_request, MultiDict(), self.PAGE_SIZE) fetch_annotations.assert_called_once_with( pyramid_request.db, search.run.return_value.annotation_ids, search.run.return_value.reply_ids)
def test_it_adds_a_tags_aggregation_to_the_search_query( self, pyramid_request, search, TagsAggregation): execute(pyramid_request, MultiDict(), self.PAGE_SIZE) TagsAggregation.assert_called_once_with(limit=50) search.append_aggregation.assert_called_with( TagsAggregation.return_value)
def test_it_passes_the_given_query_params_to_the_search( self, pyramid_request, search): query = MultiDict(foo='bar') execute(pyramid_request, query, self.PAGE_SIZE) assert search.run.call_args[0][0]['foo'] == 'bar'
def test_it_fetches_the_groups_from_the_database(self, _fetch_groups, group_pubids, matchers, pyramid_request): execute(pyramid_request, MultiDict(), self.PAGE_SIZE) _fetch_groups.assert_called_once_with( pyramid_request.db, matchers.unordered_list(group_pubids))
def test_it_adds_a_tags_aggregation_to_the_search_query( self, pyramid_request, search, TagsAggregation ): execute(pyramid_request, MultiDict(), self.PAGE_SIZE) TagsAggregation.assert_called_once_with(limit=50) search.append_aggregation.assert_called_with(TagsAggregation.return_value)
def test_it_only_shows_annotations_from_default_authority( self, pyramid_request, search, AuthorityFilter ): execute(pyramid_request, MultiDict(), self.PAGE_SIZE) AuthorityFilter.assert_called_once_with(pyramid_request.default_authority) search.append_modifier.assert_any_call(AuthorityFilter.return_value)
def test_it_does_not_add_a_users_aggregation( self, pyramid_request, search, UsersAggregation ): """On non-group pages there's no users aggregations.""" execute(pyramid_request, MultiDict(), self.PAGE_SIZE) assert not UsersAggregation.called
def test_it_creates_a_search_query(self, pyramid_request, Search): execute(pyramid_request, MultiDict(), self.PAGE_SIZE) Search.assert_called_once_with( pyramid_request, separate_wildcard_uri_keys=False, )
def test_it_creates_a_search_query(self, pyramid_request, Search): pyramid_request.stats = mock.Mock() execute(pyramid_request, MultiDict(), self.PAGE_SIZE) Search.assert_called_once_with(pyramid_request, separate_replies=True, stats=pyramid_request.stats)
def test_it_limits_the_search_results_to_one_pages_worth( self, pyramid_request, search): query = MultiDict() execute(pyramid_request, query, self.PAGE_SIZE) query = search.run.call_args[0][0] assert query['limit'] == self.PAGE_SIZE
def test_it_fetches_the_groups_from_the_database( self, _fetch_groups, group_pubids, matchers, pyramid_request ): execute(pyramid_request, MultiDict(), self.PAGE_SIZE) _fetch_groups.assert_called_once_with( pyramid_request.db, matchers.UnorderedList(group_pubids) )
def test_it_fetches_the_annotations_from_the_database( self, fetch_annotations, pyramid_request, search ): execute(pyramid_request, MultiDict(), self.PAGE_SIZE) fetch_annotations.assert_called_once_with( pyramid_request.db, search.run.return_value.annotation_ids )
def test_it_fetches_the_groups_from_the_database( self, _fetch_groups, group_pubids, pyramid_request ): execute(pyramid_request, MultiDict(), self.PAGE_SIZE) _fetch_groups.assert_called_once_with( pyramid_request.db, Any.iterable.containing(group_pubids).only() )
def test_it_passes_the_given_query_params_to_the_search( self, pyramid_request, search ): query = MultiDict(foo="bar") execute(pyramid_request, query, self.PAGE_SIZE) assert search.run.call_args[0][0]["foo"] == "bar"
def test_it_gets_the_first_page_of_results_if_page_arg_is_negative( self, pyramid_request, search, page): query = MultiDict() pyramid_request.params["page"] = page execute(pyramid_request, query, self.PAGE_SIZE) query = search.run.call_args[0][0] assert query["offset"] == 0
def test_it_limits_the_search_results_to_one_pages_worth( self, pyramid_request, search ): query = MultiDict() execute(pyramid_request, query, self.PAGE_SIZE) query = search.run.call_args[0][0] assert query["limit"] == self.PAGE_SIZE
def test_it_creates_a_search_query(self, pyramid_request, Search): pyramid_request.stats = mock.Mock() execute(pyramid_request, MultiDict(), self.PAGE_SIZE) Search.assert_called_once_with( pyramid_request, stats=pyramid_request.stats, separate_wildcard_uri_keys=False, )
def test_it_gets_the_first_page_of_results_if_page_arg_is_not_an_int( self, pyramid_request, search, page): query = MultiDict() pyramid_request.params['page'] = page execute(pyramid_request, query, self.PAGE_SIZE) query = search.run.call_args[0][0] assert query['offset'] == 0
def test_it_gets_the_first_page_of_results_if_no_page_arg( self, pyramid_request, search): query = MultiDict() assert 'page' not in pyramid_request.params execute(pyramid_request, query, self.PAGE_SIZE) query = search.run.call_args[0][0] assert query['offset'] == 0
def test_it_gets_the_first_page_of_results_if_no_page_arg( self, pyramid_request, search ): query = MultiDict() assert "page" not in pyramid_request.params execute(pyramid_request, query, self.PAGE_SIZE) query = search.run.call_args[0][0] assert query["offset"] == 0
def test_on_group_pages_it_adds_a_users_aggregation_to_the_search_query( self, pyramid_request, search, UsersAggregation): """If there's a single group facet it adds a users aggregation.""" query = MultiDict(group='foo') execute(pyramid_request, query, self.PAGE_SIZE) UsersAggregation.assert_called_once_with(limit=50) search.append_aggregation.assert_called_with( UsersAggregation.return_value)
def test_it_gets_the_first_page_of_results_if_page_arg_is_negative( self, pyramid_request, search, page ): query = MultiDict() pyramid_request.params["page"] = page execute(pyramid_request, query, self.PAGE_SIZE) query = search.run.call_args[0][0] assert query["offset"] == 0
def test_on_group_pages_it_adds_a_users_aggregation_to_the_search_query( self, pyramid_request, search, UsersAggregation ): """If there's a single group facet it adds a users aggregation.""" query = MultiDict(group="foo") execute(pyramid_request, query, self.PAGE_SIZE) UsersAggregation.assert_called_once_with(limit=50) search.append_aggregation.assert_called_with(UsersAggregation.return_value)
def test_it_returns_each_annotations_html_link( self, annotations, links, pyramid_request ): def html_link(request, annotation): assert ( request == pyramid_request ), "It should always pass the request to html_link" # Return a predictable per-annotation value for the html link. return "html_link_for_annotation_{id}".format(id=annotation.id) links.html_link.side_effect = html_link result = execute(pyramid_request, MultiDict(), self.PAGE_SIZE) presented_annotations = [] for timeframe in result.timeframes: for bucket in timeframe.document_buckets.values(): presented_annotations.extend(bucket.presented_annotations) for presented_annotation in presented_annotations: assert presented_annotation["html_link"] == ( "html_link_for_annotation_{id}".format( id=presented_annotation["annotation"].annotation.id ) )
def test_it_buckets_the_annotations(self, fetch_annotations, bucketing, pyramid_request, search): result = execute(pyramid_request, MultiDict(), self.PAGE_SIZE) bucketing.bucket.assert_called_once_with( fetch_annotations.return_value) assert result.timeframes == bucketing.bucket.return_value
def test_it_buckets_the_annotations( self, fetch_annotations, bucketing, pyramid_request, search ): result = execute(pyramid_request, MultiDict(), self.PAGE_SIZE) bucketing.bucket.assert_called_once_with(fetch_annotations.return_value) assert result.timeframes == bucketing.bucket.return_value
def test_it_recognizes_wildcards_in_uri_url(self, pyramid_request, search, UriFilter, UriCombinedWildcardFilter, enable_flag): pyramid_request.feature.flags['wildcard_search_on_activity_pages'] = enable_flag execute(pyramid_request, MultiDict(), self.PAGE_SIZE) if enable_flag: UriCombinedWildcardFilter.assert_called_once_with(pyramid_request) search.append_modifier.assert_any_call(UriCombinedWildcardFilter.return_value) else: UriFilter.assert_called_once_with(pyramid_request) search.append_modifier.assert_any_call(UriFilter.return_value)
def test_it_returns_the_search_result_if_there_are_no_matches( self, pyramid_request, search): search.run.return_value.total = 0 search.run.return_value.annotation_ids = [] result = execute(pyramid_request, MultiDict(), self.PAGE_SIZE) # This is what execute()'s result should look like if there are no # annotations that match the given search query. assert result.total == 0 assert result.aggregations == mock.sentinel.aggregations assert result.timeframes == []
def search(self): # Make a copy of the query params to be consumed by search. query_params = self.parsed_query_params.copy() # Check whether a redirect is required. query.check_url(self.request, query_params) page_size = self.request.params.get("page_size", PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, query_params, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({ "name": group.name, "pubid": group.pubid }) def tag_link(tag): tag = parser.unparse({"tag": tag}) return self.request.route_url("activity.search", _query=[("q", tag)]) def username_from_id(userid): parts = split_user(userid) return parts["username"] def user_link(userid): username = username_from_id(userid) return self.request.route_url("activity.user_search", username=username) return { "search_results": results, "groups_suggestions": groups_suggestions, "page": paginate(self.request, results.total, page_size=page_size), "pretty_link": pretty_link, "q": self.request.params.get("q", ""), "tag_link": tag_link, "user_link": user_link, "username_from_id": username_from_id, # The message that is shown (only) if there's no search results. "zero_message": _("No annotations matched your search."), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) page_size = request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results result = query.execute(request, q, page_size=page_size) groups_suggestions = [] if request.authenticated_user: for group in request.authenticated_user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) def tag_link(tag): q = parser.unparse({'tag': tag}) return request.route_url('activity.search', _query=[('q', q)]) def username_from_id(userid): parts = split_user(userid) return parts['username'] def user_link(userid): username=username_from_id(userid) return request.route_url('activity.user_search', username=username) return { 'aggregations': result.aggregations, 'groups_suggestions': groups_suggestions, 'page': paginate(request, result.total, page_size=page_size), 'pretty_link': pretty_link, 'q': request.params.get('q', ''), 'tag_link': tag_link, 'timeframes': result.timeframes, 'total': result.total, 'user_link': user_link, 'username_from_id': username_from_id, }
def test_it_returns_each_annotation_presented(self, annotations, pyramid_request): result = execute(pyramid_request, MultiDict(), self.PAGE_SIZE) presented_annotations = [] for timeframe in result.timeframes: for bucket in timeframe.document_buckets.values(): presented_annotations.extend(bucket.presented_annotations) for annotation in annotations: for presented_annotation in presented_annotations: if presented_annotation["annotation"].annotation == annotation: break else: assert False
def search(self): # Make a copy of the query params to be consumed by search. q = self.parsed_query_params.copy() # Check whether a redirect is required. query.check_url(self.request, q) page_size = self.request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, q, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) def tag_link(tag): q = parser.unparse({'tag': tag}) return self.request.route_url('activity.search', _query=[('q', q)]) def username_from_id(userid): parts = split_user(userid) return parts['username'] def user_link(userid): username = username_from_id(userid) return self.request.route_url('activity.user_search', username=username) return { 'search_results': results, 'groups_suggestions': groups_suggestions, 'page': paginate(self.request, results.total, page_size=page_size), 'pretty_link': pretty_link, 'q': self.request.params.get('q', ''), 'tag_link': tag_link, 'user_link': user_link, 'username_from_id': username_from_id, # The message that is shown (only) if there's no search results. 'zero_message': _('No annotations matched your search.'), }
def search(self): q = query.extract(self.request) # Check whether a redirect is required. query.check_url(self.request, q) page_size = self.request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, q, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) def tag_link(tag): q = parser.unparse({'tag': tag}) return self.request.route_url('activity.search', _query=[('q', q)]) def username_from_id(userid): parts = split_user(userid) return parts['username'] def user_link(userid): username = username_from_id(userid) return self.request.route_url('activity.user_search', username=username) return { 'search_results': results, 'groups_suggestions': groups_suggestions, 'page': paginate(self.request, results.total, page_size=page_size), 'pretty_link': pretty_link, 'q': self.request.params.get('q', ''), 'tag_link': tag_link, 'user_link': user_link, 'username_from_id': username_from_id, # The message that is shown (only) if there's no search results. 'zero_message': _('No annotations matched your search.'), }
def test_it_returns_each_annotations_group(self, _fetch_groups, pyramid_request): result = execute(pyramid_request, MultiDict(), self.PAGE_SIZE) presented_annotations = [] for timeframe in result.timeframes: for bucket in timeframe.document_buckets.values(): presented_annotations.extend(bucket.presented_annotations) for group in _fetch_groups.return_value: for presented_annotation in presented_annotations: if presented_annotation["group"] == group: break else: assert False
def test_it_returns_each_annotations_group( self, _fetch_groups, annotations, group_pubids, pyramid_request ): result = execute(pyramid_request, MultiDict(), self.PAGE_SIZE) presented_annotations = [] for timeframe in result.timeframes: for bucket in timeframe.document_buckets.values(): presented_annotations.extend(bucket.presented_annotations) for group in _fetch_groups.return_value: for presented_annotation in presented_annotations: if presented_annotation["group"] == group: break else: assert False
def search(self): # Make a copy of the query params to be consumed by search. q = self.parsed_query_params.copy() # Check whether a redirect is required. query.check_url(self.request, q) page_size = self.request.params.get("page_size", PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, q, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({"name": group.name, "pubid": group.pubid}) def tag_link(tag): q = parser.unparse({"tag": tag}) return self.request.route_url("activity.search", _query=[("q", q)]) def username_from_id(userid): parts = split_user(userid) return parts["username"] def user_link(userid): username = username_from_id(userid) return self.request.route_url("activity.user_search", username=username) return { "search_results": results, "groups_suggestions": groups_suggestions, "page": paginate(self.request, results.total, page_size=page_size), "pretty_link": pretty_link, "q": self.request.params.get("q", ""), "tag_link": tag_link, "user_link": user_link, "username_from_id": username_from_id, # The message that is shown (only) if there's no search results. "zero_message": _("No annotations matched your search."), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) # Fetch results result = query.execute(request, q, page_size=PAGE_SIZE) return { 'total': result.total, 'aggregations': result.aggregations, 'timeframes': result.timeframes, 'page': paginate(request, result.total, page_size=PAGE_SIZE), }
def test_it_returns_each_annotations_incontext_link( self, links, pyramid_request): def incontext_link(request, annotation): assert (request == pyramid_request ), "It should always pass the request to incontext_link" # Return a predictable per-annotation value for the incontext link. return f"incontext_link_for_annotation_{annotation.id}" links.incontext_link.side_effect = incontext_link result = execute(pyramid_request, MultiDict(), self.PAGE_SIZE) presented_annotations = [] for timeframe in result.timeframes: for bucket in timeframe.document_buckets.values(): presented_annotations.extend(bucket.presented_annotations) for presented_annotation in presented_annotations: assert presented_annotation["incontext_link"] == ( f"incontext_link_for_annotation_{presented_annotation['annotation'].annotation.id}" )
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) if q is None: return {} # Check whether a redirect is required query.check_url(request, q) # Fetch results result = query.execute(request, q) return { 'q': request.params['q'], 'total': result.total, 'aggregations': result.aggregations, 'timeframes': result.timeframes, 'page': paginate(request, result.total), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) page_size = request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results result = query.execute(request, q, page_size=page_size) for user in result.aggregations.get('users', []): user['username'] = util.user.split_user(user['user'])['username'] user['userid'] = user['user'] user['faceted_by'] = _faceted_by_user(request, user['username'], q) del user['user'] groups_suggestions = [] if request.authenticated_user: for group in request.authenticated_user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) return { 'total': result.total, 'aggregations': result.aggregations, 'groups_suggestions': groups_suggestions, 'timeframes': result.timeframes, 'page': paginate(request, result.total, page_size=page_size), }
def test_it_returns_each_annotations_incontext_link(self, annotations, links, pyramid_request): def incontext_link(request, annotation): assert request == pyramid_request, ( "It should always pass the request to incontext_link") # Return a predictable per-annotation value for the incontext link. return 'incontext_link_for_annotation_{id}'.format(id=annotation.id) links.incontext_link.side_effect = incontext_link result = execute(pyramid_request, MultiDict(), self.PAGE_SIZE) presented_annotations = [] for timeframe in result.timeframes: for bucket in timeframe.document_buckets.values(): presented_annotations.extend(bucket.presented_annotations) for presented_annotation in presented_annotations: assert presented_annotation['incontext_link'] == ( 'incontext_link_for_annotation_{id}'.format(id=presented_annotation['annotation'].annotation.id))
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) page_size = request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results result = query.execute(request, q, page_size=page_size) return { 'total': result.total, 'aggregations': result.aggregations, 'timeframes': result.timeframes, 'page': paginate(request, result.total, page_size=page_size), }
def test_it_returns_the_aggregations(self, pyramid_request): result = execute(pyramid_request, MultiDict(), self.PAGE_SIZE) assert result.aggregations == mock.sentinel.aggregations
def test_it_returns_the_total_number_of_matching_annotations(self, pyramid_request): assert execute(pyramid_request, MultiDict(), self.PAGE_SIZE).total == 20
def test_it_creates_a_search_query(self, pyramid_request, Search): execute(pyramid_request, MultiDict(), self.PAGE_SIZE) Search.assert_called_once_with(pyramid_request, separate_replies=True)
def test_it_creates_a_search_query(self, pyramid_request, Search): pyramid_request.stats = mock.Mock() execute(pyramid_request, MultiDict(), self.PAGE_SIZE) Search.assert_called_once_with(pyramid_request, stats=pyramid_request.stats)
def test_it_returns_the_total_number_of_matching_annotations( self, pyramid_request): assert execute(pyramid_request, MultiDict(), self.PAGE_SIZE).total == 20