Skip to content

Commit

Permalink
Add (skipped&failing) test for #27
Browse files Browse the repository at this point in the history
  • Loading branch information
jieter committed Jan 25, 2018
1 parent 7743708 commit e872887
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions tests/test_querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import print_function, unicode_literals

import pickle
from unittest import skipIf
from unittest import skipIf, skip

import django
from django.db import models
Expand Down Expand Up @@ -329,7 +329,7 @@ def test_order_by_i18n(self):
with override('nl'):
qs = Blog.objects.all().order_by('title_i18n')

self.assertEquals(key(qs, 'title_i18n'), 'A B C D H X Y Z'.split())
self.assertEquals(key(qs, 'title_i18n', sep=' '), 'A B C D H X Y Z')


class AnnotateTest(TestCase):
Expand Down Expand Up @@ -430,20 +430,21 @@ def setUpTestData(self):
mammals = Category.objects.create(name='Mammals', name_nl='Zoogdieren')

Blog.objects.bulk_create([
Blog(title='Falcon', category=birds),
Blog(title='Vulture', category=birds),
Blog(title='Falcon', title_nl='Valk', category=birds),
Blog(title='Vulture', title_nl='Gier', category=birds),

Blog(title='Bat', category=mammals),
Blog(title='Dolfin', category=mammals),
Blog(title='Zebra', category=mammals)
])

def test_order_by_related_field(self):
expected = 'Zebra Dolfin Bat Vulture Falcon'
qs = Blog.objects.filter(category__isnull=False).order_by('-category__name_i18n', '-title')
self.assertEquals(key(qs, 'title'), 'Zebra Dolfin Bat Vulture Falcon'.split())
self.assertEquals(key(qs, 'title', sep=' '), expected)

qs = Blog.objects.filter(category__isnull=False).order_by('-category__name_nl', '-title')
self.assertEquals(key(qs, 'title'), 'Zebra Dolfin Bat Vulture Falcon'.split())
self.assertEquals(key(qs, 'title', sep=' '), expected)

def test_order_by_lower(self):
'''
Expand Down Expand Up @@ -501,6 +502,33 @@ def test_order_by_annotation(self):
self.assertEquals(key(qs.order_by('num_blogs'), 'name', sep=' '), 'Birds Mammals')
self.assertEquals(key(qs.order_by('-num_blogs'), 'name', sep=' '), 'Mammals Birds')

@skip
def test_order_by_distinct(self):
'''
Postgres requires the distict expression to match the order_by expression.
This is because it needs to reliably choose the first row from a set of
duplicates, which is only possible if the results are also ordered by the
value that needs to be distinct.
https://github.com/zostera/django-modeltrans/issues/27
The english case works, as the queryset is sorted on `title_i18n`, which is
translated to `title`.
For the dutch case, this error message is raised, because `title_i18n`
translates to a COALESCE-expression, resulting in a database error:
SELECT DISTINCT ON expressions must match initial ORDER BY expressions.
'''

with override('en'):
qs = Blog.objects.filter(category__name_i18n='Birds').order_by('title_i18n').distinct('title')
self.assertEqual(key(qs, 'title_i18n', sep=' '), 'Falcon Vulture')

with override('nl'):
qs = Blog.objects.filter(category__name_i18n='Vogels').order_by('title_i18n').distinct('title_i18n')
self.assertEqual(key(qs, 'title_i18n', sep=' '), 'Valk Gier')


class FallbackOrderByTest(TestCase):

Expand Down Expand Up @@ -536,7 +564,7 @@ class Meta:
# should use the 'default' fallback chain
with override('nl'):
qs = TestObj.objects.all().order_by('title_i18n')
self.assertEquals(key(qs, 'title_i18n'), ['Gecko', 'Gerbil', 'Gier', 'Kikker', 'Valk', 'Vos'])
self.assertEquals(key(qs, 'title_i18n', sep=' '), 'Gecko Gerbil Gier Kikker Valk Vos')

# should use the 'fy' fallback chain
with override('fy'):
Expand All @@ -551,7 +579,7 @@ class Meta:
# should use the 'default' fallback chain
with override('fr'):
qs = TestObj.objects.all().order_by('title_i18n')
self.assertEquals(key(qs, 'title_i18n'), ['Falcon', 'Fox', 'Gecko', 'Gerbil', 'Grenouilles', 'Vautour'])
self.assertEquals(key(qs, 'title_i18n', sep=' '), 'Falcon Fox Gecko Gerbil Grenouilles Vautour')


class FilteredOrderByTest(TestCase):
Expand Down

0 comments on commit e872887

Please sign in to comment.