|
| 1 | +from albums.models import Album |
| 2 | +from albums.serializers import AlbumSerializer |
| 3 | + |
| 4 | +from django.conf.urls import url |
| 5 | +from django.test.utils import override_settings |
| 6 | +from django.test import TestCase |
| 7 | + |
| 8 | +from rest_framework.generics import ListAPIView |
| 9 | +from rest_framework.test import ( |
| 10 | + APIClient, |
| 11 | +) |
| 12 | +from rest_framework_datatables.pagination import ( |
| 13 | + DatatablesLimitOffsetPagination, |
| 14 | +) |
| 15 | + |
| 16 | +class TestFilterTestCase(TestCase): |
| 17 | + class TestAPIView(ListAPIView): |
| 18 | + serializer_class = AlbumSerializer |
| 19 | + pagination_class = DatatablesLimitOffsetPagination |
| 20 | + _datatables_additional_order_by = 'year' |
| 21 | + |
| 22 | + def get_queryset(self): |
| 23 | + return Album.objects.all() |
| 24 | + |
| 25 | + fixtures = ['test_data'] |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + self.client = APIClient() |
| 29 | + |
| 30 | + @override_settings(ROOT_URLCONF=__name__) |
| 31 | + def test_additional_order_by(self): |
| 32 | + response = self.client.get('/api/additionalorderby/?format=datatables&draw=1&columns[0][data]=rank&columns[0][name]=&columns[0][searchable]=true&columns[0][orderable]=true&columns[0][search][value]=&columns[0][search][regex]=false&columns[1][data]=artist_name&columns[1][name]=artist.name&columns[1][searchable]=true&columns[1][orderable]=true&columns[1][search][value]=&columns[1][search][regex]=false&columns[2][data]=name&columns[2][name]=&columns[2][searchable]=true&columns[2][orderable]=true&columns[2][search][value]=&columns[2][search][regex]=false&order[0][column]=1&order[0][dir]=desc&start=4&length=1&search[value]=&search[regex]=false') |
| 33 | + # Would be "Sgt. Pepper's Lonely Hearts Club Band" without the additional order by |
| 34 | + expected = (15, 15, 'Rubber Soul') |
| 35 | + result = response.json() |
| 36 | + self.assertEquals((result['recordsFiltered'], result['recordsTotal'], result['data'][0]['name']), expected) |
| 37 | + |
| 38 | + |
| 39 | +urlpatterns = [ |
| 40 | + url('^api/additionalorderby', TestFilterTestCase.TestAPIView.as_view()), |
| 41 | +] |
0 commit comments