Exemplo n.º 1
0
    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()
Exemplo n.º 2
0
    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])
Exemplo n.º 3
0
    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/tuiuiu/tuiuiu/issues/937
        """
        # Reset the index
        self.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.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)
Exemplo n.º 4
0
    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.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.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)
Exemplo n.º 5
0
    def test_doesnt_insert_unsaved_object(self, backend):
        obj = models.SearchTest(title="Test")
        backend().reset_mock()

        index.insert_or_update_object(obj)

        self.assertFalse(backend().add.mock_calls)
Exemplo n.º 6
0
    def test_removes_unsaved_object(self, backend):
        obj = models.SearchTest(title="Test")
        backend().reset_mock()

        index.remove_object(obj)

        backend().delete.assert_called_with(obj)
Exemplo n.º 7
0
    def setUp(self):
        # Create ES mapping
        self.es_mapping = ElasticsearchSearchBackend.mapping_class(models.SearchTest)

        # Create ES document
        self.obj = models.SearchTest(title="Hello")
        self.obj.save()
        self.obj.tags.add("a tag")
        self.obj.subobjects.create(name="A subobject")
Exemplo n.º 8
0
    def test_gets_instance(self):
        obj = models.SearchTest(
            title="Hello",
            live=True,
        )
        obj.save()

        # Should just return the object
        indexed_instance = index.get_indexed_instance(obj)
        self.assertEqual(indexed_instance, obj)
Exemplo n.º 9
0
    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])
Exemplo n.º 10
0
    def test_ascii_folding(self):
        # Reset the index
        self.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.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)