Add upgrade note about fixing tests for background tasks (#12794)

* Add upgrade note about fixing tests for background tasks

Addition to the upgrade note added in https://github.com/wagtail/wagtail/pull/12787 to cover how to update tests that make database changes and expect to see the results reflected immediately in search queries and other processes that now only happen at the end of a transaction.

* Update docs/releases/6.4.md

Co-authored-by: sag​e <laymonage@gmail.com>

---------

Co-authored-by: sag​e <laymonage@gmail.com>
pull/12727/merge
Matt Westcott 2025-01-20 16:08:47 +00:00 zatwierdzone przez GitHub
rodzic 6a7792e910
commit ab7547c6cb
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 13 dodań i 0 usunięć

Wyświetl plik

@ -206,6 +206,19 @@ TASKS = {
}
```
Django's test framework typically runs tests inside transactions, and so this situation is also likely to arise in tests that perform database updates and then - within the same test - expect these changes to be immediately reflected in search queries, object usage counts, and other processes that are now handled with background tasks. This can be addressed by setting `ENQUEUE_ON_COMMIT` to `False` as above in the test settings, or by wrapping the database updates in `with self.captureOnCommitCallbacks(execute=True)` to ensure that these tasks are completed before the test continues:
```python
def test_search(self):
home_page = Page.objects.get(slug="home")
with self.captureOnCommitCallbacks(execute=True): # Added
home_page.add_child(instance=EventPage(title="Christmas party"))
response = self.client.get("/search/?q=Christmas")
self.assertContains(response, "Christmas party")
```
## Upgrade considerations - deprecation of old functionality
## Upgrade considerations - changes affecting Wagtail customizations