Skip to content

Commit 7776927

Browse files
committed
Fix tests to account for search indexing happening as a background task
As of wagtail/wagtail#12787 in Wagtail 6.4, search indexing ordinarily happens at the end of the transaction where the objects are created/updated. Since tests are wrapped in transactions, we need to explicitly invoke the oncommit callbacks via `self.captureOnCommitCallbacks(execute=True)` to ensure that the changes we make to the database are reflected in search results.
1 parent 4e472b9 commit 7776927

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

tests/tests.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ def test_search(self):
100100
)
101101

102102
homepage = Page.objects.get(depth=2)
103-
red_page = homepage.add_child(title='A red page')
104-
another_red_page = homepage.add_child(title='Another red page')
105-
green_page = homepage.add_child(title='A green page')
103+
with self.captureOnCommitCallbacks(execute=True):
104+
red_page = homepage.add_child(title='A red page')
105+
another_red_page = homepage.add_child(title='Another red page')
106+
green_page = homepage.add_child(title='A green page')
106107

107108
response = self.client.get('/admin/page-chooser/')
108109
self.assertEqual(response.status_code, 200)
@@ -420,9 +421,10 @@ def test_pagination(self):
420421

421422
def test_search(self):
422423
homepage = Page.objects.get(depth=2)
423-
red_page = homepage.add_child(title='A red page')
424-
another_red_page = homepage.add_child(title='Another red page')
425-
green_page = homepage.add_child(title='A green page')
424+
with self.captureOnCommitCallbacks(execute=True):
425+
red_page = homepage.add_child(title='A red page')
426+
another_red_page = homepage.add_child(title='Another red page')
427+
green_page = homepage.add_child(title='A green page')
426428

427429
response = self.client.get('/admin/api-page-chooser/')
428430
self.assertEqual(response.status_code, 200)

0 commit comments

Comments
 (0)