Skip to content

Commit

Permalink
Add test cases for SelectRelatedTranslationsQuerySetMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
shun-liang committed Mar 11, 2018
1 parent ad65fd8 commit f5d1c12
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
43 changes: 39 additions & 4 deletions parler/tests/test_querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
import django
from django.utils import translation
from .utils import AppTestCase
from .testapp.models import SimpleModel, SimpleLightModel
from .testapp.models import SimpleModel, SimpleLightModel, SimpleRelatedModel, TranslatedSimpleRelatedModel, \
AnotherRelatedModel


class QuerySetsTests(AppTestCase):
def setUp(self):
super(QuerySetsTests, self).setUp()
self.title = 'TITLE_XX'
self.id = SimpleModel.objects.create(tr_title=self.title).pk


@skipIf(django.VERSION < (1, 8), 'Test for django ver > 1.7')
class TranslatableQuerySetTests(QuerySetsTests):
def setUp(self):
super(TranslatableQuerySetTests, self).setUp()
self.light_model_id = SimpleLightModel.objects.create(tr_title=self.title).pk

def assertNumTranslatedQueries(self, num, qs):
Expand All @@ -19,7 +26,6 @@ def test_qs():
str(obj.tr_title)
self.assertNumQueries(num, test_qs)

@skipIf(django.VERSION < (1, 8), 'Test for django ver > 1.7')
def test_select_related_light_model(self):
with translation.override('ca-fr'):
qs = SimpleLightModel.objects.select_related('translations').filter(pk=self.light_model_id)
Expand All @@ -33,7 +39,6 @@ def test_select_related_light_model(self):
x = SimpleLightModel.objects.select_related('translations_active').get(pk=self.light_model_id)
self.assertEqual(x.tr_title, self.title)

@skipIf(django.VERSION < (1, 8), 'Test for django ver > 1.7')
def test_select_related_force_model(self):
with translation.override('ca-fr'):
qs = SimpleModel.objects.select_related('translations').filter(pk=self.id)
Expand All @@ -47,7 +52,6 @@ def test_select_related_force_model(self):
x = SimpleModel.objects.select_related('translations_active').get(pk=self.id)
self.assertEqual(x.tr_title, self.title)

@skipIf(django.VERSION < (1, 8), 'Test for django ver > 1.7')
def test_only(self):
with translation.override('ca-fr'):
qs = SimpleModel.objects.all().only('id')
Expand All @@ -67,3 +71,34 @@ def test_only(self):

x = SimpleModel.objects.all().only('id', 'tr_title').get(pk=self.id)
self.assertEqual(x.tr_title, self.title)


@skipIf(django.VERSION < (1, 8), 'Test for django ver > 1.7')
class SelectRelatedTranslationsQuerySetMixinTest(QuerySetsTests):
def setUp(self):
super(SelectRelatedTranslationsQuerySetMixinTest, self).setUp()
self.related_model_id = SimpleRelatedModel.objects.create(
some_attribute='Test', some_reference_id=self.id
).pk
self.translated_related_model_id = TranslatedSimpleRelatedModel.objects.create(
tr_attribute='Test', some_reference_id=self.id
).pk
self.another_related_model_id = AnotherRelatedModel.objects.create(
another_attribute='AnotherTest', another_reference_id=self.translated_related_model_id
).pk

def test_select_related__one_degree_relation(self):
with translation.override('ca-fr'):
qs = SimpleRelatedModel.objects.select_related('some_reference')
self.assertEqual(qs.query.select_related, {
'some_reference': {'translations_active': {}, 'translations_default': {}}
})

def test_select_related__two_degree_relation(self):
with translation.override('ca-fr'):
qs = AnotherRelatedModel.objects.select_related(
'another_reference__some_reference'
)
self.assertEqual(qs.query.select_related, {
'another_reference': {'some_reference': {'translations_active': {}, 'translations_default': {}}}
})
27 changes: 26 additions & 1 deletion parler/tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from parler.fields import TranslatedField
from parler.managers import TranslatableLightSelectRelatedManager
from parler.managers import TranslatableLightSelectRelatedManager, SelectRelatedTranslationsQuerySetMixin
from parler.models import TranslatableModel, TranslatedFields, TranslatedFieldsModel
from parler.utils.context import switch_language

Expand Down Expand Up @@ -48,6 +48,31 @@ def __str__(self):
return self.tr_title


class SimpleRelatedModelManager(SelectRelatedTranslationsQuerySetMixin, models.Manager):
pass


class SimpleRelatedModel(models.Model):
some_attribute = models.CharField(max_length=200, default='')
some_reference = models.ForeignKey(SimpleModel, on_delete=models.CASCADE)

objects = SimpleRelatedModelManager()


class TranslatedSimpleRelatedModel(TranslatableModel):
translations = TranslatedFields(
tr_attribute = models.CharField("Translated Attribute", max_length=200)
)
some_reference = models.ForeignKey(SimpleModel, on_delete=models.CASCADE)


class AnotherRelatedModel(models.Model):
another_attribute = models.CharField(max_length=200, default='')
another_reference = models.OneToOneField(TranslatedSimpleRelatedModel, on_delete=models.CASCADE)

objects = SimpleRelatedModelManager()


class CleanCharField(models.CharField):

def clean(self, value, model_instance):
Expand Down

0 comments on commit f5d1c12

Please sign in to comment.