Skip to content

Commit

Permalink
Fixed test failure caused by different NULL ordering between backends
Browse files Browse the repository at this point in the history
  • Loading branch information
akaariai committed Aug 20, 2013
1 parent 9054098 commit 8dc76c4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions django/db/backends/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -595,6 +595,9 @@ class BaseDatabaseFeatures(object):
# to remove any ordering? # to remove any ordering?
requires_explicit_null_ordering_when_grouping = False 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? # Is there a 1000 item limit on query parameters?
supports_1000_query_parameters = True supports_1000_query_parameters = True


Expand Down
1 change: 1 addition & 0 deletions django/db/backends/postgresql_psycopg2/base.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_tablespaces = True supports_tablespaces = True
supports_transactions = True supports_transactions = True
can_distinct_on_fields = True can_distinct_on_fields = True
nulls_order_largest = True




class DatabaseWrapper(BaseDatabaseWrapper): class DatabaseWrapper(BaseDatabaseWrapper):
Expand Down
4 changes: 4 additions & 0 deletions tests/queries/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -262,9 +262,13 @@ def __str__(self):
return self.name return self.name


# A simpler shared-foreign-key setup that can expose some problems. # A simpler shared-foreign-key setup that can expose some problems.
@python_2_unicode_compatible
class SharedConnection(models.Model): class SharedConnection(models.Model):
data = models.CharField(max_length=10) data = models.CharField(max_length=10)


def __str__(self):
return self.data

class PointerA(models.Model): class PointerA(models.Model):
connection = models.ForeignKey(SharedConnection) connection = models.ForeignKey(SharedConnection)


Expand Down
8 changes: 6 additions & 2 deletions tests/queries/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2984,7 +2984,11 @@ def test_ticket_14056(self):
s2 = SharedConnection.objects.create(data='s2') s2 = SharedConnection.objects.create(data='s2')
s3 = SharedConnection.objects.create(data='s3') s3 = SharedConnection.objects.create(data='s3')
PointerA.objects.create(connection=s2) PointerA.objects.create(connection=s2)
expected_ordering = (
[s1, s3, s2] if connection.features.nulls_order_largest
else [s2, s1, s3]
)
self.assertQuerysetEqual( self.assertQuerysetEqual(
SharedConnection.objects.order_by('pointera__connection', 'pk'), SharedConnection.objects.order_by('-pointera__connection', 'pk'),
[s1, s3, s2], lambda x: x expected_ordering, lambda x: x
) )

0 comments on commit 8dc76c4

Please sign in to comment.