Skip to content

Commit

Permalink
Fix tests for background task behaviour in Wagtail 6.4 (#255)
Browse files Browse the repository at this point in the history
In Wagtail 6.4 (wagtail/wagtail#12787), updating the search index and reference index after a database update are now handled as background tasks through the django-tasks library. In the default configuration, this happens at the end of the current transaction (if any), and since unit tests are typically wrapped in a transaction, we can no longer count on the results of database updates within tests being immediately visible in search and object usage queries. To ensure that these tasks are executed, we trigger the oncommit hooks via `self.captureOnCommitCallbacks(execute=True)`.
  • Loading branch information
gasman authored Jan 29, 2025
1 parent 7e3277a commit 00ef044
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
7 changes: 4 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
class ApiTestBase(TestCase):
@classmethod
def setUpTestData(cls):
cls.a_space_odyssey = create_video("2001: A Space Odyssey")
cls.tng = create_video("Star Trek: The Next Generation")
cls.pink_floyd_time = create_audio("Pink Floyd: Time")
with cls.captureOnCommitCallbacks(execute=True):
cls.a_space_odyssey = create_video("2001: A Space Odyssey")
cls.tng = create_video("Star Trek: The Next Generation")
cls.pink_floyd_time = create_audio("Pink Floyd: Time")

def tearDown(self) -> None:
for item in Media.objects.all():
Expand Down
13 changes: 8 additions & 5 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,17 @@ def test_duration_display_as_tenths(self):
class TestMediaQuerySet(TestCase):
def test_search_method(self):
# Make a test media
media = Media.objects.create(title="Test media file", duration=100)
with self.captureOnCommitCallbacks(execute=True):
media = Media.objects.create(title="Test media file", duration=100)

# Search for it
results = Media.objects.search("Test")
self.assertEqual(list(results), [media])

def test_operators(self):
aaa_media = Media.objects.create(title="AAA Test media", duration=100)
zzz_media = Media.objects.create(title="ZZZ Test media", duration=100)
with self.captureOnCommitCallbacks(execute=True):
aaa_media = Media.objects.create(title="AAA Test media", duration=100)
zzz_media = Media.objects.create(title="ZZZ Test media", duration=100)

results = Media.objects.search("aaa test", operator="and")
self.assertEqual(list(results), [aaa_media])
Expand All @@ -129,8 +131,9 @@ def test_operators(self):
self.assertEqual(sorted_results, [aaa_media, zzz_media])

def test_custom_ordering(self):
aaa_media = Media.objects.create(title="AAA Test media", duration=100)
zzz_media = Media.objects.create(title="ZZZ Test media", duration=100)
with self.captureOnCommitCallbacks(execute=True):
aaa_media = Media.objects.create(title="AAA Test media", duration=100)
zzz_media = Media.objects.create(title="ZZZ Test media", duration=100)

results = Media.objects.order_by("title").search(
"Test", order_by_relevance=False
Expand Down
37 changes: 20 additions & 17 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,15 +723,16 @@ def filter_media(media, request):
self.assertEqual(response.context["media_files"][0], media)

def test_construct_queryset_hook_search(self):
media = models.Media.objects.create(
title="Test media shown",
duration=100,
type="audio",
uploaded_by_user=self.user,
)
models.Media.objects.create(
title="Test media not shown", duration=100, type="audio"
)
with self.captureOnCommitCallbacks(execute=True):
media = models.Media.objects.create(
title="Test media shown",
duration=100,
type="audio",
uploaded_by_user=self.user,
)
models.Media.objects.create(
title="Test media not shown", duration=100, type="audio"
)

def filter_media(media, request):
return media.filter(uploaded_by_user=self.user)
Expand Down Expand Up @@ -1100,19 +1101,21 @@ def test_unused_media_usage_count(self):
def test_used_media_usage_count(self):
media = models.Media.objects.get(id=1)
page = EventPage.objects.get(id=3)
event_page_related_link = EventPageRelatedMedia()
event_page_related_link.page = page
event_page_related_link.link_media = media
event_page_related_link.save()
with self.captureOnCommitCallbacks(execute=True):
event_page_related_link = EventPageRelatedMedia()
event_page_related_link.page = page
event_page_related_link.link_media = media
event_page_related_link.save()
self.assertEqual(media.get_usage().count(), 1)

def test_usage_count_appears(self):
media = models.Media.objects.get(id=1)
page = EventPage.objects.get(id=3)
event_page_related_link = EventPageRelatedMedia()
event_page_related_link.page = page
event_page_related_link.link_media = media
event_page_related_link.save()
with self.captureOnCommitCallbacks(execute=True):
event_page_related_link = EventPageRelatedMedia()
event_page_related_link.page = page
event_page_related_link.link_media = media
event_page_related_link.save()
response = self.client.get(reverse("wagtailmedia:edit", args=(1,)))
self.assertContains(response, "Used 1 time")

Expand Down

0 comments on commit 00ef044

Please sign in to comment.