def load_test_data(self): # Reset the index self.backend.reset_index() self.backend.add_type(models.SearchTest) self.backend.add_type(models.SearchTestChild) # Create a test database testa = models.SearchTest() testa.title = "Hello World" testa.save() self.testa = testa testb = models.SearchTest() testb.title = "Hello" testb.live = True testb.save() testc = models.SearchTestChild() testc.title = "Hello" testc.live = True testc.save() testd = models.SearchTestChild() testd.title = "World" testd.save() # Refresh the index self.backend.refresh_index()
def setUp(self): s = get_search_backend() # Stick some documents into the index testa = models.SearchTest() testa.title = "Hello World" testa.save() s.add(testa) testb = models.SearchTest() testb.title = "Hello" testb.save() s.add(testb) testc = models.SearchTestChild() testc.title = "Hello" testc.save() s.add(testc)
def test_search(self, backend=None): # Don't run this test directly (this will be called from other tests) if backend is None: raise unittest.SkipTest() # Don't test the same backend more than once! if backend in self.backends_tested: return self.backends_tested.append(backend) # Get search backend and reset the index s = get_search_backend(backend=backend) s.reset_index() # Create a couple of objects and add them to the index testa = models.SearchTest() testa.title = "Hello World" testa.save() s.add(testa) testb = models.SearchTest() testb.title = "Hello" testb.save() s.add(testb) testc = models.SearchTestChild() testc.title = "Hello" testc.save() s.add(testc) # Refresh index s.refresh_index() # Ordinary search results = s.search("Hello", models.SearchTest) self.assertEqual(len(results), 3) # Retrieve single result self.assertIsInstance(results[0], models.SearchTest) # Retrieve results through iteration iterations = 0 for result in results: self.assertIsInstance(result, models.SearchTest) iterations += 1 self.assertEqual(iterations, 3) # Retrieve results through slice iterations = 0 for result in results[:]: self.assertIsInstance(result, models.SearchTest) iterations += 1 self.assertEqual(iterations, 3) # Ordinary search on "World" results = s.search("World", models.SearchTest) self.assertEqual(len(results), 1) # Searcher search results = models.SearchTest.title_search("Hello", backend=backend) self.assertEqual(len(results), 3) # Ordinary search on child results = s.search("Hello", models.SearchTestChild) self.assertEqual(len(results), 1) # Searcher search on child results = models.SearchTestChild.title_search("Hello", backend=backend) self.assertEqual(len(results), 1) # Delete a record testc.delete() results = s.search("Hello", models.SearchTest) # TODO: FIXME Deleting records doesn't seem to be deleting them from the index! (but they still get deleted on update_index) #self.assertEqual(len(results), 2) # Try to index something that shouldn't be indexed # TODO: This currently fails on the DB backend if not isinstance(s, DBSearch): testd = models.SearchTest() testd.title = "Don't index me!" testd.save() s.add(testd) results = s.search("Don't", models.SearchTest) self.assertEqual(len(results), 0) # Reset the index, this should clear out the index (but doesn't have to!) s.reset_index() # Run update_index command management.call_command('update_index', backend, interactive=False, stdout=StringIO()) # Should have results again now results = s.search("Hello", models.SearchTest) self.assertEqual(len(results), 2)