~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/tests/test_search.py

  • Committer: Tarmac
  • Author(s): Brad Crittenden
  • Date: 2013-08-02 18:23:12 UTC
  • mfrom: (332.1.2 apisearch)
  • Revision ID: tarmac-20130802182312-ty9mt8k6h39pmr4h
[r=abentley][bug=][author=bac] Change api_search and related charms to ignore bundles for now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    Charm,
22
22
)
23
23
from charmworld.search import (
 
24
    BUNDLE,
24
25
    CHARM,
25
26
    ElasticSearchClient,
26
27
    exact_fields,
279
280
        self.assertAlmostEqual(by_id[charmers_id]['weight'],
280
281
                               by_id[jrandom_id]['weight'])
281
282
 
282
 
    def test_api_search_all(self):
283
 
        """With no parameters, all charms are returned."""
 
283
    def get_charm_bundle_test_data(self):
284
284
        one = self.makeCharm(name='name1', promulgated=True)
285
285
        maintainer2 = 'jrandom@example.com (J. Random Hacker)'
286
286
        provides2 = {'cookie-factory': {
315
315
                             requires=requires2, options=options2,
316
316
                             files=files2, subordinate=True)
317
317
        bundle = self.makeBundle(name='name3')
318
 
        result = self.get_charms_and_bundles()
 
318
        return one, two, bundle
 
319
 
 
320
    def test_api_search_all(self):
 
321
        """With no parameters, all charms are returned."""
 
322
        one, two, bundle = self.get_charm_bundle_test_data()
 
323
        result = self.get_charms_and_bundles(doctype=None)
319
324
        result = sorted(result, key=lambda x: x['name'])
320
325
        self.assertEqual(3, len(result))
321
326
        one_url = make_store_url(1, get_address(one, short=True))
326
331
        self.assertNotEqual(one_url, two_url)
327
332
        self.assertEqual(bundle, result[2])
328
333
 
 
334
    def test_api_search_charms_only(self):
 
335
        """With no parameters, all charms are returned."""
 
336
        one, two, bundle = self.get_charm_bundle_test_data()
 
337
        result = self.get_charms_and_bundles(doctype=CHARM)
 
338
        result = sorted(result, key=lambda x: x['name'])
 
339
        self.assertEqual(2, len(result))
 
340
        self.assertEqual(one, result[0])
 
341
        self.assertEqual(two, result[1])
 
342
        self.assertNotEqual(one['_id'], two['_id'])
 
343
 
 
344
    def test_api_search_bundles_only(self):
 
345
        """Passing doctype of 'bundles' retrieves only bundles."""
 
346
        one, two, bundle = self.get_charm_bundle_test_data()
 
347
        result = self.get_charms_and_bundles(doctype=BUNDLE)
 
348
        result = sorted(result, key=lambda x: x['name'])
 
349
        self.assertEqual(1, len(result))
 
350
        self.assertEqual(bundle, result[0])
 
351
 
329
352
    def test_autocomplete_matches_on_names(self):
330
353
        matching_charm = self.makeCharm(name='foo')
331
354
        differing_charm = self.makeCharm(name='bar')
348
371
               autocomplete=True)]
349
372
        self.assertItemsEqual([charm1['_id'], charm2['_id']], ids)
350
373
 
351
 
    def get_charms_and_bundles(self, text='', type_=None, kind=CHARM,
 
374
    def get_charms_and_bundles(self, text='', type_=None, doctype=CHARM,
352
375
                               *args, **kwargs):
353
376
        filters = dict((key, [value]) for key, value in kwargs.items())
354
377
        if type_ is not None and isinstance(type_, basestring):
355
378
            type_ = [type_]
356
 
        return self.index_client.api_search(text, filters, type_)
 
379
        return self.index_client.api_search(
 
380
            text, filters, type_, doctype=doctype)
357
381
 
358
382
    def _test_filter(self, filter_str, make_charm=None):
359
383
        if make_charm is None:
471
495
        self.makeCharm(name='evil-charm', charm_error=True)
472
496
        self.makeCharm(name='good-charm')
473
497
        self.makeBundle(name='great-bundle')
474
 
        result = self.index_client.api_search('', {}, None)
 
498
        result = self.index_client.api_search('', {}, None, doctype=None)
475
499
        self.assertEqual(2, len(result))
476
500
        names = set(charm['name'] for charm in result)
477
501
        self.assertEqual(set(('good-charm', 'great-bundle')), names)
479
503
        # if the parameter valid_only is set to False.  Bundles are
480
504
        # always included.
481
505
        result = self.index_client.api_search(
482
 
            '', {}, None, valid_only=False)
 
506
            '', {}, None, valid_only=False, doctype=None)
483
507
        self.assertEqual(3, len(result))
484
508
        names = set(charm['name'] for charm in result)
485
509
        self.assertEqual(
608
632
            client.put_mapping()
609
633
 
610
634
    def test_get_mapping_default(self):
611
 
        # Calling get_mapping with no kind returns charms.
 
635
        # Calling get_mapping with no doctype returns charms.
612
636
        client = self.index_client
613
637
        client.put_mapping()
614
638
        mapping = client.get_mapping()