diff --git a/lms/djangoapps/discussion_api/pagination.py b/lms/djangoapps/discussion_api/pagination.py index c355ee899d..0a705e8e88 100644 --- a/lms/djangoapps/discussion_api/pagination.py +++ b/lms/djangoapps/discussion_api/pagination.py @@ -47,7 +47,6 @@ class DiscussionAPIPagination(NamespacedPageNumberPagination): """ self.page = _Page(page_num, num_pages) self.base_url = request.build_absolute_uri() - self.num_pages = num_pages super(DiscussionAPIPagination, self).__init__() @@ -63,7 +62,7 @@ class DiscussionAPIPagination(NamespacedPageNumberPagination): """ Returns total number of pages the response is divided into """ - return self.num_pages + return self.page.num_pages def get_next_link(self): """ diff --git a/lms/djangoapps/discussion_api/tests/test_api.py b/lms/djangoapps/discussion_api/tests/test_api.py index c1e680f0cd..3a4373b548 100644 --- a/lms/djangoapps/discussion_api/tests/test_api.py +++ b/lms/djangoapps/discussion_api/tests/test_api.py @@ -695,7 +695,7 @@ class GetThreadListTest(CommentsServiceMockMixin, UrlResetMixin, SharedModuleSto ] expected_result = make_paginated_api_response( - expected_threads, 0, 1, None, None + results=expected_threads, count=0, num_pages=1, next_link=None, previous_link=None ) expected_result.update({"text_search_rewrite": None}) self.assertEqual( @@ -728,7 +728,9 @@ class GetThreadListTest(CommentsServiceMockMixin, UrlResetMixin, SharedModuleSto def test_pagination(self): # N.B. Empty thread list is not realistic but convenient for this test - expected_result = make_paginated_api_response([], 0, 3, "http://testserver/test_path?page=2", None) + expected_result = make_paginated_api_response( + results=[], count=0, num_pages=3, next_link="http://testserver/test_path?page=2", previous_link=None + ) expected_result.update({"text_search_rewrite": None}) self.assertEqual( self.get_thread_list([], page=1, num_pages=3).data, @@ -736,7 +738,11 @@ class GetThreadListTest(CommentsServiceMockMixin, UrlResetMixin, SharedModuleSto ) expected_result = make_paginated_api_response( - [], 0, 3, "http://testserver/test_path?page=3", "http://testserver/test_path?page=1" + results=[], + count=0, + num_pages=3, + next_link="http://testserver/test_path?page=3", + previous_link="http://testserver/test_path?page=1" ) expected_result.update({"text_search_rewrite": None}) self.assertEqual( @@ -745,7 +751,7 @@ class GetThreadListTest(CommentsServiceMockMixin, UrlResetMixin, SharedModuleSto ) expected_result = make_paginated_api_response( - [], 0, 3, None, "http://testserver/test_path?page=2" + results=[], count=0, num_pages=3, next_link=None, previous_link="http://testserver/test_path?page=2" ) expected_result.update({"text_search_rewrite": None}) self.assertEqual( @@ -761,7 +767,7 @@ class GetThreadListTest(CommentsServiceMockMixin, UrlResetMixin, SharedModuleSto @ddt.data(None, "rewritten search string") def test_text_search(self, text_search_rewrite): expected_result = make_paginated_api_response( - [], 0, 0, None, None + results=[], count=0, num_pages=0, next_link=None, previous_link=None ) expected_result.update({"text_search_rewrite": text_search_rewrite}) self.register_get_threads_search_response([], text_search_rewrite, num_pages=0) @@ -796,7 +802,9 @@ class GetThreadListTest(CommentsServiceMockMixin, UrlResetMixin, SharedModuleSto following=True, ).data - expected_result = make_paginated_api_response([], 0, 0, None, None) + expected_result = make_paginated_api_response( + results=[], count=0, num_pages=0, next_link=None, previous_link=None + ) expected_result.update({"text_search_rewrite": None}) self.assertEqual( result, @@ -827,7 +835,7 @@ class GetThreadListTest(CommentsServiceMockMixin, UrlResetMixin, SharedModuleSto ).data expected_result = make_paginated_api_response( - [], 0, 0, None, None + results=[], count=0, num_pages=0, next_link=None, previous_link=None ) expected_result.update({"text_search_rewrite": None}) self.assertEqual( @@ -872,7 +880,9 @@ class GetThreadListTest(CommentsServiceMockMixin, UrlResetMixin, SharedModuleSto order_by=http_query, ).data - expected_result = make_paginated_api_response([], 0, 0, None, None) + expected_result = make_paginated_api_response( + results=[], count=0, num_pages=0, next_link=None, previous_link=None + ) expected_result.update({"text_search_rewrite": None}) self.assertEqual(result, expected_result) self.assertEqual( @@ -900,7 +910,9 @@ class GetThreadListTest(CommentsServiceMockMixin, UrlResetMixin, SharedModuleSto order_direction=http_query, ).data - expected_result = make_paginated_api_response([], 0, 0, None, None) + expected_result = make_paginated_api_response( + results=[], count=0, num_pages=0, next_link=None, previous_link=None + ) expected_result.update({"text_search_rewrite": None}) self.assertEqual(result, expected_result) self.assertEqual( @@ -1065,7 +1077,7 @@ class GetCommentListTest(CommentsServiceMockMixin, SharedModuleStoreTestCase): ) self.assertEqual( self.get_comment_list(discussion_thread).data, - make_paginated_api_response([], 0, 1, None, None) + make_paginated_api_response(results=[], count=0, num_pages=1, next_link=None, previous_link=None) ) question_thread = self.make_minimal_cs_thread({ @@ -1076,11 +1088,11 @@ class GetCommentListTest(CommentsServiceMockMixin, SharedModuleStoreTestCase): }) self.assertEqual( self.get_comment_list(question_thread, endorsed=False).data, - make_paginated_api_response([], 0, 1, None, None) + make_paginated_api_response(results=[], count=0, num_pages=1, next_link=None, previous_link=None) ) self.assertEqual( self.get_comment_list(question_thread, endorsed=True).data, - make_paginated_api_response([], 0, 1, None, None) + make_paginated_api_response(results=[], count=0, num_pages=1, next_link=None, previous_link=None) ) def test_basic_query_params(self): diff --git a/lms/djangoapps/discussion_api/tests/test_pagination.py b/lms/djangoapps/discussion_api/tests/test_pagination.py index 4d632f47ff..d74d54e63b 100644 --- a/lms/djangoapps/discussion_api/tests/test_pagination.py +++ b/lms/djangoapps/discussion_api/tests/test_pagination.py @@ -23,31 +23,39 @@ class PaginationSerializerTest(TestCase): def test_empty(self): self.do_case( - [], 1, 0, make_paginated_api_response([], 0, 0, None, None) + [], 1, 0, make_paginated_api_response( + results=[], count=0, num_pages=0, next_link=None, previous_link=None + ) ) def test_only_page(self): self.do_case( - ["foo"], 1, 1, make_paginated_api_response(["foo"], 0, 1, None, None) + ["foo"], 1, 1, make_paginated_api_response( + results=["foo"], count=0, num_pages=1, next_link=None, previous_link=None + ) ) def test_first_of_many(self): self.do_case( ["foo"], 1, 3, make_paginated_api_response( - ["foo"], 0, 3, "http://testserver/test?page=2", None + results=["foo"], count=0, num_pages=3, next_link="http://testserver/test?page=2", previous_link=None ) ) def test_last_of_many(self): self.do_case( ["foo"], 3, 3, make_paginated_api_response( - ["foo"], 0, 3, None, "http://testserver/test?page=2" + results=["foo"], count=0, num_pages=3, next_link=None, previous_link="http://testserver/test?page=2" ) ) def test_middle_of_many(self): self.do_case( ["foo"], 2, 3, make_paginated_api_response( - ["foo"], 0, 3, "http://testserver/test?page=3", "http://testserver/test?page=1" + results=["foo"], + count=0, + num_pages=3, + next_link="http://testserver/test?page=3", + previous_link="http://testserver/test?page=1" ) ) diff --git a/lms/djangoapps/discussion_api/tests/test_views.py b/lms/djangoapps/discussion_api/tests/test_views.py index 0c325a75ee..a92181fd54 100644 --- a/lms/djangoapps/discussion_api/tests/test_views.py +++ b/lms/djangoapps/discussion_api/tests/test_views.py @@ -309,11 +309,11 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): self.register_get_threads_response(source_threads, page=1, num_pages=2) response = self.client.get(self.url, {"course_id": unicode(self.course.id), "following": ""}) expected_respoonse = make_paginated_api_response( - expected_threads, - 0, - 2, - "http://testserver/api/discussion/v1/threads/?course_id=x%2Fy%2Fz&page=2", - None + results=expected_threads, + count=0, + num_pages=2, + next_link="http://testserver/api/discussion/v1/threads/?course_id=x%2Fy%2Fz&page=2", + previous_link=None ) expected_respoonse.update({"text_search_rewrite": None}) self.assert_response_correct( @@ -384,7 +384,9 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): {"course_id": unicode(self.course.id), "text_search": "test search string"} ) - expected_response = make_paginated_api_response([], 0, 0, None, None) + expected_response = make_paginated_api_response( + results=[], count=0, num_pages=0, next_link=None, previous_link=None + ) expected_response.update({"text_search_rewrite": None}) self.assert_response_correct( response, @@ -414,7 +416,9 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): } ) - expected_response = make_paginated_api_response([], 0, 0, None, None) + expected_response = make_paginated_api_response( + results=[], count=0, num_pages=0, next_link=None, previous_link=None + ) expected_response.update({"text_search_rewrite": None}) self.assert_response_correct( response, @@ -949,7 +953,9 @@ class CommentViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): self.assert_response_correct( response, 200, - make_paginated_api_response(expected_comments, 0, 10, next_link, None) + make_paginated_api_response( + results=expected_comments, count=0, num_pages=10, next_link=next_link, previous_link=None + ) ) self.assert_query_params_equal( httpretty.httpretty.latest_requests[-2], diff --git a/lms/djangoapps/discussion_api/tests/utils.py b/lms/djangoapps/discussion_api/tests/utils.py index 76e88dcd87..b43310fe1d 100644 --- a/lms/djangoapps/discussion_api/tests/utils.py +++ b/lms/djangoapps/discussion_api/tests/utils.py @@ -373,7 +373,7 @@ def make_minimal_cs_comment(overrides=None): return ret -def make_paginated_api_response(results, count, num_pages, next_link, previous_link): +def make_paginated_api_response(results=[], count=0, num_pages=0, next_link=None, previous_link=None): """ Generates the response dictionary of paginated APIs with passed data """