def setUp(self): super().setUp() self.paginator = NamespacedPageNumberPagination() self.users = [ self.TestUser(f'user_{idx}', f'user_{idx}@example.com') for idx in range(25) ] self.request_factory = RequestFactory()
def setUp(self): super(NamespacedPaginationTestCase, self).setUp() self.paginator = NamespacedPageNumberPagination() self.users = [ self.TestUser('user_{}'.format(idx), 'user_{}@example.com'.format(idx)) for idx in range(25) ] self.request_factory = RequestFactory()
def get(self, request, course_key_string, cohort_id=None): """ Endpoint to get either one or all cohorts. """ course_key, course = _get_course_with_access(request, course_key_string) if not cohort_id: all_cohorts = cohorts.get_course_cohorts(course) paginator = NamespacedPageNumberPagination() paginator.max_page_size = MAX_PAGE_SIZE page = paginator.paginate_queryset(all_cohorts, request) response = [_get_cohort_representation(c, course) for c in page] return Response(response, status=status.HTTP_200_OK) else: cohort = cohorts.get_cohort_by_id(course_key, cohort_id) return _get_cohort_response(cohort, course)
class NamespacedPaginationTestCase(TestCase): """ Test behavior of `NamespacedPageNumberPagination` """ TestUser = namedtuple('TestUser', ['username', 'email']) class TestUserSerializer(serializers.Serializer): # pylint: disable=abstract-method """ Simple serializer to paginate results from """ username = serializers.CharField() email = serializers.CharField() expected_data = { 'results': [ {'username': '******', 'email': '*****@*****.**'}, {'username': '******', 'email': '*****@*****.**'}, {'username': '******', 'email': '*****@*****.**'}, {'username': '******', 'email': '*****@*****.**'}, {'username': '******', 'email': '*****@*****.**'}, ], 'pagination': { 'next': 'http://testserver/endpoint?page=3&page_size=5', 'previous': 'http://testserver/endpoint?page_size=5', 'count': 25, 'num_pages': 5, } } def setUp(self): super(NamespacedPaginationTestCase, self).setUp() self.paginator = NamespacedPageNumberPagination() self.users = [self.TestUser('user_{}'.format(idx), 'user_{}@example.com'.format(idx)) for idx in range(25)] self.request_factory = RequestFactory() def test_basic_pagination(self): request = self.request_factory.get('/endpoint', data={'page': 2, 'page_size': 5}) request.query_params = {'page': 2, 'page_size': 5} paged_users = self.paginator.paginate_queryset(self.users, request) results = self.TestUserSerializer(paged_users, many=True).data self.assertEqual(self.expected_data, self.paginator.get_paginated_response(results).data)