Author: andrej
Date: Tue Mar 19 14:57:53 2013
New Revision: 1458325

URL: http://svn.apache.org/r1458325
Log:
changing bhsearch to ask for upgrade if index does not exist - towards #472 
(from Anze)

Modified:
    
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/search_resources/ticket_search.py
    
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/whoosh_backend.py
    incubator/bloodhound/trunk/bloodhound_search/bhsearch/whoosh_backend.py

Modified: 
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/search_resources/ticket_search.py
URL: 
http://svn.apache.org/viewvc/incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/search_resources/ticket_search.py?rev=1458325&r1=1458324&r2=1458325&view=diff
==============================================================================
--- 
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/search_resources/ticket_search.py
 (original)
+++ 
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/search_resources/ticket_search.py
 Tue Mar 19 14:57:53 2013
@@ -19,14 +19,17 @@
 #  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
-from trac.ticket.model import Component, Ticket
+from trac.ticket.model import Component
 
 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")

Modified: 
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/whoosh_backend.py
URL: 
http://svn.apache.org/viewvc/incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/whoosh_backend.py?rev=1458325&r1=1458324&r2=1458325&view=diff
==============================================================================
--- 
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/whoosh_backend.py 
(original)
+++ 
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/whoosh_backend.py 
Tue Mar 19 14:57:53 2013
@@ -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 @@ class WhooshBackendTestCase(BaseBloodhou
         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 @@ class WhooshBackendTestCase(BaseBloodhou
     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):

Modified: 
incubator/bloodhound/trunk/bloodhound_search/bhsearch/whoosh_backend.py
URL: 
http://svn.apache.org/viewvc/incubator/bloodhound/trunk/bloodhound_search/bhsearch/whoosh_backend.py?rev=1458325&r1=1458324&r2=1458325&view=diff
==============================================================================
--- incubator/bloodhound/trunk/bloodhound_search/bhsearch/whoosh_backend.py 
(original)
+++ incubator/bloodhound/trunk/bloodhound_search/bhsearch/whoosh_backend.py Tue 
Mar 19 14:57:53 2013
@@ -92,7 +92,10 @@ class WhooshBackend(Component):
         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 @@ class WhooshBackend(Component):
         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 @@ class WhooshBackend(Component):
         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,


Reply via email to