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.backend.add(testa) self.testa = testa testb = models.SearchTest() testb.title = "Hello" testb.live = True testb.save() self.backend.add(testb) self.testb = testb testc = models.SearchTestChild() testc.title = "Hello" testc.live = True testc.save() self.backend.add(testc) self.testc = testc testd = models.SearchTestChild() testd.title = "World" testd.save() self.backend.add(testd) self.testd = testd # Refresh the index self.backend.refresh_index()
def test_custom_ordering(self): # Reset the index self.reset_index() self.backend.add_type(models.SearchTest) # Add some test data # a is more relevant, but b is more recent a = models.SearchTest() a.title = "Hello Hello World" a.live = True a.published_date = datetime.date(2015, 10, 11) a.save() self.backend.add(a) b = models.SearchTest() b.title = "Hello World" b.live = True b.published_date = datetime.date(2015, 10, 12) b.save() self.backend.add(b) # Refresh the index self.refresh_index() # Do a search ordered by relevence results = self.backend.search("Hello", models.SearchTest.objects.all()) self.assertEqual(list(results), [a, b]) # Do a search ordered by published date results = self.backend.search( "Hello", models.SearchTest.objects.order_by('-published_date'), order_by_relevance=False ) self.assertEqual(list(results), [b, a])
def load_test_data(self): self.reset_index() # Create a test database testa = models.SearchTest() testa.title = "Hello World" testa.save() testa.subobjects.create(name='A subobject') self.backend.add(testa) self.testa = testa testb = models.SearchTest() testb.title = "Hello" testb.live = True testb.save() self.backend.add(testb) self.testb = testb testc = models.SearchTestChild() testc.title = "Hello" testc.live = True testc.content = "Hello" testc.subtitle = "Foo" testc.save() self.backend.add(testc) self.testc = testc testd = models.SearchTestChild() testd.title = "World" testd.subtitle = "Foo" testd.save() self.backend.add(testd) self.testd = testd self.refresh_index()
def test_search_with_hyphen(self): """ This tests that punctuation characters are treated the same way in both indexing and querying. See: https://github.com/torchbox/wagtail/issues/937 """ # Reset the index self.backend.reset_index() self.backend.add_type(models.SearchTest) self.backend.add_type(models.SearchTestChild) # Add some test data obj = models.SearchTest() obj.title = "Hello-World" obj.live = True obj.save() self.backend.add(obj) # Refresh the index self.backend.refresh_index() # Test search for "Hello-World" results = self.backend.search("Hello-World", models.SearchTest.objects.all()) # Should find the result self.assertEqual(len(results), 1)
def test_query_analyser(self): """ This is testing that fields that use edgengram_analyzer as their index analyser do not have it also as their query analyser """ # Reset the index self.backend.reset_index() self.backend.add_type(models.SearchTest) self.backend.add_type(models.SearchTestChild) # Add some test data obj = models.SearchTest() obj.title = "Hello" obj.live = True obj.save() self.backend.add(obj) # Refresh the index self.backend.refresh_index() # Test search for "Hello" results = self.backend.search("Hello", models.SearchTest.objects.all()) # Should find the result self.assertEqual(len(results), 1) # Test search for "Horse" results = self.backend.search("Horse", models.SearchTest.objects.all()) # Even though they both start with the letter "H". This should not be considered a match self.assertEqual(len(results), 0)
def setUp(self): # Create ES mapping self.es_mapping = Elasticsearch2SearchBackend.mapping_class(models.SearchTest) # Create ES document self.obj = models.SearchTest(title="Hello") self.obj.save() self.obj.tags.add("a tag")
def test_gets_instance(self): obj = models.SearchTest( title="Hello", live=True, ) obj.save() # Should just return the object indexed_instance = signal_handlers.get_indexed_instance(obj) self.assertEqual(indexed_instance, obj)
def setUp(self): # Import using a try-catch block to prevent crashes if the elasticsearch-py # module is not installed try: from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearchMapping from elasticsearch.serializer import JSONSerializer except ImportError: raise unittest.SkipTest("elasticsearch-py not installed") self.JSONSerializer = JSONSerializer # Create ES mapping self.es_mapping = ElasticSearchMapping(models.SearchTest) # Create ES document self.obj = models.SearchTest(title="Hello") self.obj.save()
def test_and_operator_with_single_field(self): # Testing for bug #1859 # Reset the index self.reset_index() self.backend.add_type(models.SearchTest) a = models.SearchTest() a.title = "Hello World" a.live = True a.published_date = datetime.date(2015, 10, 12) a.save() self.backend.add(a) # Refresh the index self.refresh_index() # Run query with "and" operator and single field results = self.backend.search("Hello World", models.SearchTest, operator='and', fields=['title']) self.assertEqual(list(results), [a])
def test_ascii_folding(self): # Reset the index self.backend.reset_index() self.backend.add_type(models.SearchTest) self.backend.add_type(models.SearchTestChild) # Add some test data obj = models.SearchTest() obj.title = "Ĥéllø" obj.live = True obj.save() self.backend.add(obj) # Refresh the index self.backend.refresh_index() # Search and check results = self.backend.search("Hello", models.SearchTest.objects.all()) self.assertEqual(len(results), 1) self.assertEqual(results[0].id, obj.id)
def test_doesnt_insert_unsaved_object(self, backend): obj = models.SearchTest(title="Test") index.insert_or_update_object(obj) self.assertFalse(backend().add.mock_calls)
def test_removes_unsaved_object(self, backend): obj = models.SearchTest(title="Test") index.remove_object(obj) backend().delete.assert_called_with(obj)