Skip to content

Commit

Permalink
Move some test data creation in setUpTestData
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Dec 23, 2024
1 parent 81bfd17 commit ce2c365
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions wagtail_localize/tests/test_edit_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import polib

from django.contrib.admin.utils import quote
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.contrib.messages import get_messages
Expand Down Expand Up @@ -113,31 +112,33 @@ def patched_translate_html(html):
class EditTranslationTestData(WagtailTestUtils):
@classmethod
def setUpTestData(cls):
cls.user = cls.create_test_user()
cls.moderators_group = Group.objects.get(name="Moderators")

avatar_suffix = "mp" if WAGTAIL_VERSION >= (6, 4, 0, "alpha", 0) else "mm"
cls.avatar_url = f"//www.gravatar.com/avatar/93942e96f5acd83e2e047ad8fe03114d?s=50&d={ avatar_suffix }"

def setUp(self):
self.login()
self.user = get_user_model().objects.get()

# Convert the user into an editor
for permission in Permission.objects.filter(
content_type=ContentType.objects.get_for_model(TestSnippet)
):
self.moderators_group.permissions.add(permission)
cls.moderators_group.permissions.add(permission)
for permission in Permission.objects.filter(
content_type=ContentType.objects.get_for_model(NonTranslatableSnippet)
):
self.moderators_group.permissions.add(permission)
self.user.is_superuser = False
self.user.groups.add(self.moderators_group)
self.user.save()
cls.moderators_group.permissions.add(permission)
cls.user.groups.add(cls.moderators_group)
cls.user.is_superuser = False
cls.user.save(update_fields=["is_superuser"])

cls.home_page = Page.objects.get(depth=2)
cls.fr_locale = Locale.objects.create(language_code="fr")

avatar_suffix = "mp" if WAGTAIL_VERSION >= (6, 4, 0, "alpha", 0) else "mm"
cls.avatar_url = f"//www.gravatar.com/avatar/93942e96f5acd83e2e047ad8fe03114d?s=50&d={ avatar_suffix }"

def setUp(self):
self.login(username=self.user.username)

# Create page
self.snippet = TestSnippet.objects.create(field="Test snippet")
self.home_page = Page.objects.get(depth=2)
self.page = self.home_page.add_child(
instance=TestPage(
title="The title",
Expand All @@ -159,8 +160,6 @@ def setUp(self):
)

# Create translations
self.fr_locale = Locale.objects.create(language_code="fr")

self.snippet_source, created = TranslationSource.get_or_create_from_instance(
self.snippet
)
Expand Down Expand Up @@ -3726,9 +3725,13 @@ class TestEditAlias(WagtailTestUtils, TestCase):
that have a different locale to their original.
"""

@classmethod
def setUpTestData(cls):
cls.user = cls.create_test_user()
cls.fr_locale = Locale.objects.create(language_code="fr")

def setUp(self):
self.login()
self.user = get_user_model().objects.get()
self.login(username=self.user.username)

# Create a test page
self.home_page = Page.objects.get(depth=2)
Expand All @@ -3740,7 +3743,6 @@ def setUp(self):
)

# Set up French locale
self.fr_locale = Locale.objects.create(language_code="fr")
self.fr_home_page = self.home_page.copy_for_translation(self.fr_locale)

def test_edit_translatable_alias(self):
Expand Down

0 comments on commit ce2c365

Please sign in to comment.