~juju-gui-bot/charmworld/trunk

« back to all changes in this revision

Viewing changes to charmworld/views/tests/test_api.py

  • Committer: Tarmac
  • Author(s): Brad Crittenden
  • Date: 2014-03-12 21:10:53 UTC
  • mfrom: (489.1.6 1287977)
  • Revision ID: tarmac-20140312211053-6ncl536f5u2kj4er
Filter charmworld searches by score.

This change introduces a new parameter to the API search called 'min_score'.
It defaults to a value of 1.0.  Searches will be filtered and only those
results with a score above min_score will be returned.

The min_score will not be used if the search is for all bundles or charms.

In addition, the number of results will be limited the smaller of 20 or the
limit provided.  Again, the limit is not applied if the search is for all
charms or bundles.

QA:

% bin/ingest --limit=20
% make run

In another window, interactively poke at it, e.g.

% http GET "http://127.0.0.1:2464/api/3/search?text=mysql&min_score=0"
% http GET "http://127.0.0.1:2464/api/3/search?text=mysql&min_score=2"
% http GET "http://127.0.0.1:2464/api/3/search?text=charmworld&min_score=0"

https://codereview.appspot.com/74900043/

R=benji. Fixes: https://bugs.launchpad.net/bugs/1287977.

Approved by Juju Gui Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
__metaclass__ = type
5
5
 
 
6
import collections
6
7
from datetime import (
7
8
    date,
8
9
    datetime,
143
144
        request.GET = MultiDict()
144
145
        for key, values in kwargs.iteritems():
145
146
            key = {'type_': 'type'}.get(key, key)
146
 
            if isinstance(values, basestring):
 
147
            if (isinstance(values, basestring) or
 
148
                not isinstance(values, collections.Iterable)):
147
149
                values = [values]
148
150
            for value in values:
149
151
                request.GET.add(key, value)
222
224
            resp.headerlist)
223
225
 
224
226
    def get_charms(self, *args, **kwargs):
225
 
        return self.get_response(
226
 
            self.search_endpoint, *args, **kwargs).json_body['result']
 
227
        response = self.get_response(self.search_endpoint, *args, **kwargs)
 
228
        return response.json_body['result']
227
229
 
228
230
    def _test_filter(self, filter_str, make_charm=None):
229
231
        if make_charm is None:
333
335
    def test_charms_text_search(self):
334
336
        abc_charm = self.makeCharm(summary='abc def')[1]
335
337
        self.makeCharm(summary='ghi jkl')[1]
336
 
        response = self.get_response(self.search_endpoint, text='def')
 
338
        response = self.get_response(
 
339
            self.search_endpoint, text='def', min_score=0)
337
340
        self.assertEqual(200, response.status_code)
338
341
        charms = response.json_body['result']
339
342
        self.assertEqual(1, len(charms))
345
348
        self.makeCharm()
346
349
        self.makeCharm(summary='abc def', owner='steve')
347
350
        self.makeCharm(owner='steve')
348
 
        result = self.get_charms(type_=['approved'], text='def')
 
351
        result = self.get_charms(type_=['approved'], text='def', min_score=0)
349
352
        self.assertEqual(
350
353
            [self.api_class._charm_result(Charm(official))],
351
354
            [charm for charm in result])