From f9eca4a8663454c66d0c42bd3e66896af77af056 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Fri, 13 Jan 2017 17:26:39 +0000 Subject: [PATCH] Add tests for search indexing signal handlers --- .../tests/test_index_functions.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/wagtail/wagtailsearch/tests/test_index_functions.py b/wagtail/wagtailsearch/tests/test_index_functions.py index fc5c70999a..9d1b9989ba 100644 --- a/wagtail/wagtailsearch/tests/test_index_functions.py +++ b/wagtail/wagtailsearch/tests/test_index_functions.py @@ -135,3 +135,34 @@ class TestRemoveObject(TestCase, WagtailTestUtils): self.assertIn("Exception raised while deleting from the 'default' search backend", cm.output[0]) self.assertIn("Traceback (most recent call last):", cm.output[0]) self.assertIn("ValueError: Test", cm.output[0]) + + +@mock.patch('wagtail.wagtailsearch.tests.DummySearchBackend', create=True) +@override_settings(WAGTAILSEARCH_BACKENDS={ + 'default': { + 'BACKEND': 'wagtail.wagtailsearch.tests.DummySearchBackend' + } +}) +class TestSignalHandlers(TestCase, WagtailTestUtils): + def test_index_on_create(self, backend): + backend().reset_mock() + obj = models.SearchTest.objects.create(title="Test", content="This is the content") + backend().add.assert_called_with(obj) + + def test_index_on_update(self, backend): + obj = models.SearchTest.objects.create(title="Test", content="This is the content") + + backend().reset_mock() + obj.title = "Updated test" + obj.save() + + backend().add.assert_called_once() + indexed_object = backend().add.call_args[0][0] + self.assertEqual(indexed_object.title, "Updated test") + + def test_index_on_delete(self, backend): + obj = models.SearchTest.objects.create(title="Test", content="This is the content") + + backend().reset_mock() + obj.delete() + backend().delete.assert_called_with(obj)