~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/jobs/tests/test_index.py

  • Committer: Tarmac
  • Author(s): Aaron Bentley
  • Date: 2013-03-28 18:56:12 UTC
  • mfrom: (179.3.4 remove-xappy)
  • Revision ID: tarmac-20130328185612-6f635k1h2wxh4eaw
[r=sinzui][bug=1099856][author=abentley] Remove Xapian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2013 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
 
from charmworld.jobs.ingest import (
5
 
    IndexIngestJob,
6
 
    XappyClient,
 
4
from charmworld.jobs.ingest import IndexIngestJob
 
5
from charmworld.testing import factory
 
6
from charmworld.testing import (
 
7
    JobTestBase,
 
8
    temp_index_client,
7
9
)
8
 
from charmworld.testing import factory
9
 
from charmworld.testing import JobTestBase
10
 
 
11
 
 
12
 
class TestIndexer(object):
13
 
 
14
 
    def __init__(self):
15
 
        self.collection = []
16
 
        self.flushed = False
17
 
 
18
 
    def flush(self):
19
 
        self.flushed = True
20
 
 
21
 
    def replace(self, doc):
22
 
        self.collection.append(doc)
23
10
 
24
11
 
25
12
class IndexIngestTest(JobTestBase):
26
13
 
27
 
    def test_charmers_weighted(self):
28
 
        job = IndexIngestJob()
29
 
        index_client = XappyClient(TestIndexer())
30
 
        job.setup(index_client=index_client)
31
 
        ignore, charmer_charm = factory.makeCharm(self.db, owner='charmers')
32
 
        ignore, noncharmer_charm = factory.makeCharm(self.db, owner='foo')
33
 
        job.run(charmer_charm)
34
 
        job.run(noncharmer_charm)
35
 
        charmer_doc, noncharmer_doc = index_client.indexer.collection
36
 
        self.assertEqual(1, noncharmer_doc.fields[0].weight)
37
 
        self.assertEqual(10, charmer_doc.fields[0].weight)
38
 
 
39
14
    def test_basic_index(self):
40
15
        job = IndexIngestJob()
41
 
        index_client = XappyClient(TestIndexer())
42
 
        job.setup(index_client=index_client)
43
 
        ignore, charm = factory.makeCharm(self.db)
44
 
        job.run(charm)
45
 
        doc = index_client.indexer.collection[0]
46
 
        fields = ['name', 'description', 'summary']
47
 
        for field in [f for f in doc.fields if f.name in fields]:
48
 
            self.assertEqual(field.value, charm[field.name])
 
16
        with temp_index_client() as index_client:
 
17
            job.setup(index_client=index_client)
 
18
            ignore, charm = factory.makeCharm(self.db)
 
19
            job.run(charm)
 
20
            result = index_client._client.search(
 
21
                {}, index=index_client.index_name)
 
22
            doc = result['hits']['hits'][0]['_source']
 
23
            fields = ['name', 'description', 'summary']
 
24
            for field in [f for f in doc.keys() if f in fields]:
 
25
                self.assertEqual(doc[field], charm[field])