Commit e9622f0d by Ehtesham

[MA-1862] fixing pagination tests

parent a13bf52d
...@@ -5,7 +5,7 @@ from unittest import TestCase ...@@ -5,7 +5,7 @@ from unittest import TestCase
from django.test import RequestFactory from django.test import RequestFactory
from discussion_api.pagination import get_paginated_data from discussion_api.pagination import DiscussionAPIPagination
class PaginationSerializerTest(TestCase): class PaginationSerializerTest(TestCase):
...@@ -16,55 +16,51 @@ class PaginationSerializerTest(TestCase): ...@@ -16,55 +16,51 @@ class PaginationSerializerTest(TestCase):
parameters returns the expected result parameters returns the expected result
""" """
request = RequestFactory().get("/test") request = RequestFactory().get("/test")
actual = get_paginated_data(request, objects, page_num, num_pages) paginator = DiscussionAPIPagination(request, page_num, num_pages)
self.assertEqual(actual, expected) actual = paginator.get_paginated_response(objects)
self.assertEqual(actual.data, expected)
def get_expected_response(self, results, count, num_pages, next, previous):
"""
Generates the response dictionary with passed data
"""
return {
"pagination": {
"next": next,
"previous": previous,
"count": count,
"num_pages": num_pages,
},
"results": results
}
def test_empty(self): def test_empty(self):
self.do_case( self.do_case(
[], 1, 0, [], 1, 0, self.get_expected_response([], 0, 0, None, None)
{
"next": None,
"previous": None,
"results": [],
}
) )
def test_only_page(self): def test_only_page(self):
self.do_case( self.do_case(
["foo"], 1, 1, ["foo"], 1, 1, self.get_expected_response(["foo"], 0, 1, None, None)
{
"next": None,
"previous": None,
"results": ["foo"],
}
) )
def test_first_of_many(self): def test_first_of_many(self):
self.do_case( self.do_case(
["foo"], 1, 3, ["foo"], 1, 3, self.get_expected_response(
{ ["foo"], 0, 3, "http://testserver/test?page=2", None
"next": "http://testserver/test?page=2", )
"previous": None,
"results": ["foo"],
}
) )
def test_last_of_many(self): def test_last_of_many(self):
self.do_case( self.do_case(
["foo"], 3, 3, ["foo"], 3, 3, self.get_expected_response(
{ ["foo"], 0, 3, None, "http://testserver/test?page=2"
"next": None, )
"previous": "http://testserver/test?page=2",
"results": ["foo"],
}
) )
def test_middle_of_many(self): def test_middle_of_many(self):
self.do_case( self.do_case(
["foo"], 2, 3, ["foo"], 2, 3, self.get_expected_response(
{ ["foo"], 0, 3, "http://testserver/test?page=3", "http://testserver/test?page=1"
"next": "http://testserver/test?page=3", )
"previous": "http://testserver/test?page=1",
"results": ["foo"],
}
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment