incubator-bloodhound-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anze Staric <anze.sta...@gmail.com>
Subject bhsearch - Ask for upgrade if no index exists.
Date Mon, 18 Mar 2013 13:11:13 GMT
BHsearch support IEnvironmentSetupParticipant interface, but does not
currently ask for upgrade, if the index is missing.

The following patch fixes this behaviour by preventing WhooshBackend
to automatically create index if it does not exist.


Please review and commit, if it is ok.

Anze




Index: bloodhound_search/bhsearch/tests/search_resources/ticket_search.py
===================================================================
--- bloodhound_search/bhsearch/tests/search_resources/ticket_search.py
(revision 1457645)
+++ bloodhound_search/bhsearch/tests/search_resources/ticket_search.py
(working copy)
@@ -19,6 +19,7 @@
 #  under the License.
 import unittest
 from bhsearch.api import BloodhoundSearchApi
+from bhsearch.whoosh_backend import WhooshBackend

 from bhsearch.tests.base import BaseBloodhoundSearchTest
 from bhsearch.search_resources.ticket_search import TicketIndexer
@@ -26,6 +27,8 @@
 class TicketIndexerTestCase(BaseBloodhoundSearchTest):
     def setUp(self):
         super(TicketIndexerTestCase, self).setUp()
+        self.whoosh_backend = WhooshBackend(self.env)
+        self.whoosh_backend.recreate_index()
         self.ticket_indexer = TicketIndexer(self.env)
         self.search_api = BloodhoundSearchApi(self.env)
         self.env.config.set('bhsearch', 'silence_on_error', "False")
Index: bloodhound_search/bhsearch/tests/whoosh_backend.py
===================================================================
--- bloodhound_search/bhsearch/tests/whoosh_backend.py (revision 1457645)
+++ bloodhound_search/bhsearch/tests/whoosh_backend.py (working copy)
@@ -18,7 +18,7 @@
 #  specific language governing permissions and limitations
 #  under the License.
 from datetime import datetime
-
+import os
 import unittest
 import tempfile
 import shutil
@@ -41,10 +41,6 @@
         self.whoosh_backend.recreate_index()
         self.parser = DefaultQueryParser(self.env)

-    def tearDown(self):
-        shutil.rmtree(self.env.path)
-        self.env.reset_db()
-
     def test_can_retrieve_docs(self):
         self.whoosh_backend.add_doc(dict(id="1", type="ticket"))
         self.whoosh_backend.add_doc(dict(id="2", type="ticket"))
@@ -416,19 +412,33 @@
     def _highlighted(self, term):
         return '<em>%s</em>' % term

+
+class WhooshIndexCreationTests(BaseBloodhoundSearchTest):
+    def setUp(self):
+        super(WhooshIndexCreationTests, self).setUp()
+        self.index_dir = os.path.join(self.env.path, 'whoosh_index')
+        if not os.path.exists(self.index_dir):
+            os.mkdir(self.index_dir)
+
+    def test_does_not_automatically_create_index(self):
+        whoosh_backend = WhooshBackend(self.env)
+
+        self.assertIs(whoosh_backend.index, None)
+        self.assertEqual(whoosh_backend.is_index_outdated(), True)
+
+        whoosh_backend.recreate_index()
+        self.assertEqual(whoosh_backend.is_index_outdated(), False)
+        self.assertIsNot(whoosh_backend.index, None)
+
     def test_detects_that_index_needs_upgrade(self):
-        index_dir = self.whoosh_backend.index.storage.folder
         wrong_schema = Schema(content=TEXT())
-        ix = index.create_in(index_dir, schema=wrong_schema)
+        index.create_in(self.index_dir, schema=wrong_schema)

-        self.assertEqual(self.whoosh_backend.is_index_outdated(), False)
+        whoosh_backend = WhooshBackend(self.env)
+        self.assertEqual(whoosh_backend.is_index_outdated(), True)

-        # Inform WhooshBackend about the new index
-        self.whoosh_backend.index = ix
-        self.assertEqual(self.whoosh_backend.is_index_outdated(), True)
-        # Recreate index
-        self.whoosh_backend.recreate_index()
-        self.assertEqual(self.whoosh_backend.is_index_outdated(), False)
+        whoosh_backend.recreate_index()
+        self.assertEqual(whoosh_backend.is_index_outdated(), False)


 class WhooshFunctionalityTestCase(unittest.TestCase):
Index: bloodhound_search/bhsearch/whoosh_backend.py
===================================================================
--- bloodhound_search/bhsearch/whoosh_backend.py (revision 1457645)
+++ bloodhound_search/bhsearch/whoosh_backend.py (working copy)
@@ -92,7 +92,10 @@
         self.index_dir = self.index_dir_setting
         if not os.path.isabs(self.index_dir):
             self.index_dir = os.path.join(self.env.path, self.index_dir)
-        self.index = self._open_or_create_index_if_missing()
+        if index.exists_in(self.index_dir):
+            self.index = index.open_dir(self.index_dir)
+        else:
+            self.index = None

     #ISearchBackend methods
     def start_operation(self):
@@ -162,7 +165,7 @@
         writer.commit(optimize=True)

     def is_index_outdated(self):
-        return not self.index.schema == self.SCHEMA
+        return self.index is None or not self.index.schema == self.SCHEMA

     def recreate_index(self):
         self.log.info('Creating Whoosh index in %s' % self.index_dir)
@@ -170,12 +173,6 @@
         self.index = index.create_in(self.index_dir, schema=self.SCHEMA)
         return self.index

-    def _open_or_create_index_if_missing(self):
-        if index.exists_in(self.index_dir):
-            return index.open_dir(self.index_dir)
-        else:
-            return self.recreate_index()
-
     def query(self,
               query,
               sort = None,

Mime
View raw message