~reedobrien/charmworld/027test-fails

« back to all changes in this revision

Viewing changes to charmworld/migrations/versions/tests/test_migrations.py

  • Committer: Tarmac
  • Author(s): Reed O'Brien
  • Date: 2014-05-20 14:46:39 UTC
  • mfrom: (511.1.6 es-migration)
  • Revision ID: tarmac-20140520144639-2wypg2q2m2qwvjch
This makes the ngram work migrate cleanly. Fixes: https://bugs.launchpad.net/bugs/1314239.

Approved by Juju Gui Bot, Brad Crittenden.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2012, 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.models import (
5
 
    CharmSource,
6
 
)
7
4
from charmworld.testing import (
8
5
    factory,
9
6
    MongoTestBase,
10
7
)
11
8
 
12
9
from charmworld.migrations import migrate
13
 
from charmworld.migrations.migrate import Versions
 
10
from charmworld.migrations.migrate import (
 
11
    DataStore,
 
12
    Versions
 
13
)
 
14
 
 
15
from charmworld.migrations.versions.tests.data import migration_027 as mdata
 
16
 
 
17
from charmworld.models import CharmSource
 
18
 
 
19
from pyelasticsearch.exceptions import ElasticHttpError
14
20
 
15
21
import logging
16
22
import mock
126
132
    def test_exodus_restrictions(self):
127
133
        """Ensure that deploy-from-scratch does not violate exodus rules."""
128
134
        self.versions._check_exodus(self.versions.list_pending(0))
 
135
 
 
136
 
 
137
class TestUseNgrams027(MigrationTestBase):
 
138
 
 
139
    version = 27
 
140
    module_name = '027_use_ngrams.py'
 
141
 
 
142
    def setUp(self):
 
143
        super(TestUseNgrams027, self).setUp()
 
144
        self.use_index_client()
 
145
        self.data_store = DataStore(self.db)
 
146
        self.charm_source = CharmSource(self.db, self.index_client)
 
147
 
 
148
    def exists_in_index(self, id_):
 
149
        return self.index_client.get(id_) is not None
 
150
 
 
151
    def sync_index(self):
 
152
        charm_ids = self.charm_source.sync_index()
 
153
        while True:
 
154
            try:
 
155
                next(charm_ids)
 
156
            except StopIteration:
 
157
                break
 
158
 
 
159
    def put_old_mapping(self):
 
160
        # Delete the temp_index.
 
161
        self.index_client.delete_index()
 
162
        # Create an empty index.
 
163
        self.index_client._client.create_index(self.index_client.index_name)
 
164
        # Put up the old mappings.
 
165
        for type_ in ('charm', 'bundle'):
 
166
            self.index_client._client.put_mapping(
 
167
                self.index_client.index_name,
 
168
                type_,
 
169
                getattr(mdata, 'old_{}_mapping'.format(type_)))
 
170
 
 
171
    def test_index_updated(self):
 
172
        """Ensure that running upgrade reindexes charms."""
 
173
        self.put_old_mapping()
 
174
        self.versions.ensure_initialized(self.data_store, True)
 
175
        ## Pretend upgrades through 26 are complete.
 
176
        self.data_store.update_version(26)
 
177
        charm = factory.get_charm_json()
 
178
        self.db.charms.insert(charm)
 
179
        self.assertFalse(self.exists_in_index(charm['_id']))
 
180
        self.sync_index()
 
181
        self.assertTrue(self.exists_in_index(charm['_id']))
 
182
        self.versions.upgrade(self.data_store, self.index_client, True)
 
183
        self.assertEquals(self.data_store.current_version, self.version)
 
184
        self.assertTrue(self.exists_in_index(charm['_id']))
 
185
 
 
186
    def test_exception_raised(self):
 
187
        """Putting new mapping without creating the filter and analyzer
 
188
        raises the expected exception.
 
189
        """
 
190
        self.versions.ensure_initialized(self.data_store, True)
 
191
        ## Pretend upgrades through 26 are complete.
 
192
        self.data_store.update_version(26)
 
193
        self.put_old_mapping()
 
194
        charm = factory.get_charm_json()
 
195
        self.assertFalse(self.exists_in_index(charm['_id']))
 
196
        self.index_client.index_charm(charm)
 
197
        self.assertTrue(self.exists_in_index(charm['_id']))
 
198
        with self.assertRaises(ElasticHttpError) as e:
 
199
            self.index_client.put_mapping()
 
200
            self.assertEquals(e.status_code, 400)
 
201
            self.assertEquals(e.error,
 
202
                              u'MapperParsingException[Analyzer [n3_20grams] '
 
203
                              u'not found for field [ngrams]]')