Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Translation Object Duplicates #756

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions wagtail_localize/tests/test_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
ACK1D marked this conversation as resolved.
Show resolved Hide resolved
from django.test import TestCase
from wagtail.models import Locale, Page

from wagtail_localize.models import Translation, TranslationSource
from wagtail_localize.operations import TranslationCreator
from wagtail_localize.segments import RelatedObjectSegmentValue
from wagtail_localize.test.models import TestPage


def create_test_page(**kwargs):
parent = kwargs.pop("parent", None) or Page.objects.get(id=1)
page = parent.add_child(instance=TestPage(**kwargs))
page_revision = page.save_revision()
page_revision.publish()
page.refresh_from_db()

source, created = TranslationSource.get_or_create_from_instance(page)
prepare_source(source)

return page


def prepare_source(source):
# Recurse into any related objects
for segment in source.relatedobjectsegment_set.all():
if isinstance(segment, RelatedObjectSegmentValue):
related_source, created = TranslationSource.get_or_create_from_instance(
segment.get_instance(source.locale)
)
prepare_source(related_source)


class TranslationOperationsTest(TestCase):
def setUp(self):
# Create a Belarusian locale for testing
self.be_locale = Locale.objects.create(language_code="be")

# Create a test page
self.page = create_test_page(
title="Test page",
slug="test-page",
test_charfield="This is some test content",
)

# Create a user
self.user = get_user_model().objects.create(username="testuser")

# Instantiate TranslationCreator with the default and Belarusian locales for target_locales
self.target_locales = [self.page.locale, self.be_locale]
self.translation_creator = TranslationCreator(
user=self.user, target_locales=self.target_locales
)

# Check if TranslationSource already exists
source, created = TranslationSource.objects.get_or_create(
object_id=self.page.translation_key,
specific_content_type=ContentType.objects.get_for_model(TestPage),
locale=self.page.locale,
)

# If not created, prepare the source
if created:
prepare_source(source)
zerolab marked this conversation as resolved.
Show resolved Hide resolved

def test_create_translations_skips_duplicate(self):
# Call create_translations() to check that only one translation has been created
self.translation_creator.create_translations(self.page)

# Assert statements for better readability
self.assertEqual(
TranslationSource.objects.filter(
object_id=self.page.translation_key
).count(),
1,
"Only one TranslationSource should be created for the source page",
)

self.assertEqual(
Translation.objects.filter(
source__object_id=self.page.translation_key,
target_locale=self.be_locale,
).count(),
1,
"Only one Translation object should be created for the Belarusian locale",
)

self.assertEqual(
Translation.objects.filter(
source__object_id=self.page.translation_key,
target_locale=self.page.locale,
).count(),
0,
"No Translation object should be created for the default locale",
)