From ced3e6b17d18174216cdbe1ec7c24cc1819db787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Tue, 20 Aug 2013 11:33:44 +0300 Subject: [PATCH] Fixed test failure caused by different NULL ordering between backends --- django/db/backends/__init__.py | 3 +++ django/db/backends/postgresql_psycopg2/base.py | 1 + tests/queries/models.py | 4 ++++ tests/queries/tests.py | 10 +++++++--- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 771b9af59c1..8d3c09ab1ad 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -599,6 +599,9 @@ class BaseDatabaseFeatures(object): # to remove any ordering? requires_explicit_null_ordering_when_grouping = False + # Does the backend order NULL values as largest or smallest? + nulls_order_largest = False + # Is there a 1000 item limit on query parameters? supports_1000_query_parameters = True diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index f283476429a..76b2935a1f4 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -58,6 +58,7 @@ class DatabaseFeatures(BaseDatabaseFeatures): can_distinct_on_fields = True can_rollback_ddl = True supports_combined_alters = True + nulls_order_largest = True class DatabaseWrapper(BaseDatabaseWrapper): diff --git a/tests/queries/models.py b/tests/queries/models.py index 71346d8be93..3a638b28676 100644 --- a/tests/queries/models.py +++ b/tests/queries/models.py @@ -262,9 +262,13 @@ class ReservedName(models.Model): return self.name # A simpler shared-foreign-key setup that can expose some problems. +@python_2_unicode_compatible class SharedConnection(models.Model): data = models.CharField(max_length=10) + def __str__(self): + return self.data + class PointerA(models.Model): connection = models.ForeignKey(SharedConnection) diff --git a/tests/queries/tests.py b/tests/queries/tests.py index bb4e9eee8fb..91d4b17d0be 100644 --- a/tests/queries/tests.py +++ b/tests/queries/tests.py @@ -2984,7 +2984,11 @@ class Ticket14056Tests(TestCase): s2 = SharedConnection.objects.create(data='s2') s3 = SharedConnection.objects.create(data='s3') PointerA.objects.create(connection=s2) - self.assertQuerysetEqual( - SharedConnection.objects.order_by('pointera__connection', 'pk'), - [s1, s3, s2], lambda x: x + expected_ordering = ( + [s1, s3, s2] if connection.features.nulls_order_largest + else [s2, s1, s3] + ) + self.assertQuerysetEqual( + SharedConnection.objects.order_by('-pointera__connection', 'pk'), + expected_ordering, lambda x: x )