From ce2c3658be17f51d754e5ab64af5923f70209c1c Mon Sep 17 00:00:00 2001 From: zerolab Date: Mon, 23 Dec 2024 11:23:55 +0100 Subject: [PATCH] Move some test data creation in `setUpTestData` --- .../tests/test_edit_translation.py | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/wagtail_localize/tests/test_edit_translation.py b/wagtail_localize/tests/test_edit_translation.py index 52f8acfb..7f89f6e9 100644 --- a/wagtail_localize/tests/test_edit_translation.py +++ b/wagtail_localize/tests/test_edit_translation.py @@ -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 @@ -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", @@ -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 ) @@ -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) @@ -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):