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 f3c758b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
34 changes: 33 additions & 1 deletion parler/tests/test_querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import django
from django.utils import translation
from .utils import AppTestCase
from .testapp.models import SimpleModel, SimpleLightModel
from .testapp.models import SimpleModel, SimpleLightModel, SimpleRelatedModel, AnotherSimpleRelatedModel


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


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 Down Expand Up @@ -67,3 +72,30 @@ def test_only(self):

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


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.another_related_model_id = AnotherSimpleRelatedModel.objects.create(
another_attribute='AnotherTest', another_reference_id=self.related_model_id
)

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 = AnotherSimpleRelatedModel.objects.select_related(
'another_reference__some_reference'
)
self.assertEqual(qs.query.select_related, {
'another_reference': {'some_reference': {'translations_active': {}, 'translations_default': {}}}
})
20 changes: 19 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,24 @@ 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 AnotherSimpleRelatedModel(models.Model):
another_attribute = models.CharField(max_length=200, default='')
another_reference = models.OneToOneField(SimpleRelatedModel, on_delete=models.CASCADE)

objects = SimpleRelatedModelManager()


class CleanCharField(models.CharField):

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

0 comments on commit f3c758b

Please sign in to comment.