Changing default order_by to ascending

This commit is contained in:
Andrew Melton 2013-04-08 17:44:08 -04:00
parent 32bef7714e
commit 81fe1c3255
2 changed files with 31 additions and 8 deletions

View File

@ -252,7 +252,7 @@ def _get_filter_args(klass, request, custom_filters=None):
return filter_args
def get_db_objects(klass, request, default_order_by, direction='asc',
def get_db_objects(klass, request, default_order_by, direction='desc',
custom_filters=None):
filter_args = _get_filter_args(klass, request,
custom_filters=custom_filters)
@ -271,6 +271,8 @@ def get_db_objects(klass, request, default_order_by, direction='asc',
direction = request.GET.get('direction', direction)
if direction == 'desc':
order_by = '-%s' % order_by
else:
order_by = '%s' % order_by
offset = request.GET.get('offset')
limit = request.GET.get('limit', DEFAULT_LIMIT)

View File

@ -139,7 +139,7 @@ class DBAPITestCase(unittest.TestCase):
dbapi._check_has_field(fake_model, 'id')
result = self.mox.CreateMockAnything()
fake_model.objects.all().AndReturn(result)
result.order_by('id').AndReturn(result)
result.order_by('-id').AndReturn(result)
s = slice(None, dbapi.DEFAULT_LIMIT, None)
result.__getitem__(s).AndReturn(result)
self.mox.ReplayAll()
@ -170,6 +170,27 @@ class DBAPITestCase(unittest.TestCase):
self.mox.VerifyAll()
def test_get_db_objects_asc(self):
fake_model = self.make_fake_model()
fake_request = self.mox.CreateMockAnything()
fake_request.GET = {'direction': 'asc'}
self.mox.StubOutWithMock(dbapi, '_get_filter_args')
dbapi._get_filter_args(fake_model, fake_request,
custom_filters=None).AndReturn({})
self.mox.StubOutWithMock(dbapi, '_check_has_field')
dbapi._check_has_field(fake_model, 'id')
result = self.mox.CreateMockAnything()
fake_model.objects.all().AndReturn(result)
result.order_by('id').AndReturn(result)
s = slice(None, dbapi.DEFAULT_LIMIT, None)
result.__getitem__(s).AndReturn(result)
self.mox.ReplayAll()
query_result = dbapi.get_db_objects(fake_model, fake_request, 'id')
self.assertEquals(query_result, result)
self.mox.VerifyAll()
def test_get_db_objects_limit(self):
fake_model = self.make_fake_model()
fake_request = self.mox.CreateMockAnything()
@ -181,7 +202,7 @@ class DBAPITestCase(unittest.TestCase):
dbapi._check_has_field(fake_model, 'id')
result = self.mox.CreateMockAnything()
fake_model.objects.all().AndReturn(result)
result.order_by('id').AndReturn(result)
result.order_by('-id').AndReturn(result)
result.__getitem__(slice(None, 1, None)).AndReturn(result)
self.mox.ReplayAll()
@ -201,7 +222,7 @@ class DBAPITestCase(unittest.TestCase):
dbapi._check_has_field(fake_model, 'id')
result = self.mox.CreateMockAnything()
fake_model.objects.all().AndReturn(result)
result.order_by('id').AndReturn(result)
result.order_by('-id').AndReturn(result)
s = slice(None, dbapi.HARD_LIMIT, None)
result.__getitem__(s).AndReturn(result)
self.mox.ReplayAll()
@ -222,7 +243,7 @@ class DBAPITestCase(unittest.TestCase):
dbapi._check_has_field(fake_model, 'id')
result = self.mox.CreateMockAnything()
fake_model.objects.all().AndReturn(result)
result.order_by('id').AndReturn(result)
result.order_by('-id').AndReturn(result)
result.__getslice__(1, dbapi.DEFAULT_LIMIT + 1).AndReturn(result)
self.mox.ReplayAll()
@ -242,7 +263,7 @@ class DBAPITestCase(unittest.TestCase):
dbapi._check_has_field(fake_model, 'id')
result = self.mox.CreateMockAnything()
fake_model.objects.all().AndReturn(result)
result.order_by('id').AndReturn(result)
result.order_by('-id').AndReturn(result)
result.__getslice__(2, 4).AndReturn(result)
self.mox.ReplayAll()
@ -263,7 +284,7 @@ class DBAPITestCase(unittest.TestCase):
dbapi._check_has_field(fake_model, 'id')
result = self.mox.CreateMockAnything()
fake_model.objects.filter(**filters).AndReturn(result)
result.order_by('id').AndReturn(result)
result.order_by('-id').AndReturn(result)
s = slice(None, dbapi.DEFAULT_LIMIT, None)
result.__getitem__(s).AndReturn(result)
self.mox.ReplayAll()
@ -289,7 +310,7 @@ class DBAPITestCase(unittest.TestCase):
all_filters.update(filters)
all_filters.update(custom_filters['raw'])
fake_model.objects.filter(**all_filters).AndReturn(result)
result.order_by('id').AndReturn(result)
result.order_by('-id').AndReturn(result)
s = slice(None, dbapi.DEFAULT_LIMIT, None)
result.__getitem__(s).AndReturn(result)
self.mox.ReplayAll()