Commit 8c94fdcf by Ehtesham

[MA-1862] updating views tests

parent 73aceb5d
...@@ -6,6 +6,7 @@ from unittest import TestCase ...@@ -6,6 +6,7 @@ from unittest import TestCase
from django.test import RequestFactory from django.test import RequestFactory
from discussion_api.pagination import DiscussionAPIPagination from discussion_api.pagination import DiscussionAPIPagination
from discussion_api.tests.utils import make_paginated_api_response
class PaginationSerializerTest(TestCase): class PaginationSerializerTest(TestCase):
...@@ -20,47 +21,33 @@ class PaginationSerializerTest(TestCase): ...@@ -20,47 +21,33 @@ class PaginationSerializerTest(TestCase):
actual = paginator.get_paginated_response(objects) actual = paginator.get_paginated_response(objects)
self.assertEqual(actual.data, expected) 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, self.get_expected_response([], 0, 0, None, None) [], 1, 0, make_paginated_api_response([], 0, 0, None, None)
) )
def test_only_page(self): def test_only_page(self):
self.do_case( self.do_case(
["foo"], 1, 1, self.get_expected_response(["foo"], 0, 1, None, None) ["foo"], 1, 1, make_paginated_api_response(["foo"], 0, 1, None, None)
) )
def test_first_of_many(self): def test_first_of_many(self):
self.do_case( self.do_case(
["foo"], 1, 3, self.get_expected_response( ["foo"], 1, 3, make_paginated_api_response(
["foo"], 0, 3, "http://testserver/test?page=2", None ["foo"], 0, 3, "http://testserver/test?page=2", None
) )
) )
def test_last_of_many(self): def test_last_of_many(self):
self.do_case( self.do_case(
["foo"], 3, 3, self.get_expected_response( ["foo"], 3, 3, make_paginated_api_response(
["foo"], 0, 3, None, "http://testserver/test?page=2" ["foo"], 0, 3, None, "http://testserver/test?page=2"
) )
) )
def test_middle_of_many(self): def test_middle_of_many(self):
self.do_case( self.do_case(
["foo"], 2, 3, self.get_expected_response( ["foo"], 2, 3, make_paginated_api_response(
["foo"], 0, 3, "http://testserver/test?page=3", "http://testserver/test?page=1" ["foo"], 0, 3, "http://testserver/test?page=3", "http://testserver/test?page=1"
) )
) )
...@@ -23,6 +23,7 @@ from discussion_api.tests.utils import ( ...@@ -23,6 +23,7 @@ from discussion_api.tests.utils import (
CommentsServiceMockMixin, CommentsServiceMockMixin,
make_minimal_cs_comment, make_minimal_cs_comment,
make_minimal_cs_thread, make_minimal_cs_thread,
make_paginated_api_response
) )
from student.tests.factories import CourseEnrollmentFactory, UserFactory from student.tests.factories import CourseEnrollmentFactory, UserFactory
from util.testing import UrlResetMixin, PatchMediaTypeMixin from util.testing import UrlResetMixin, PatchMediaTypeMixin
...@@ -307,15 +308,18 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -307,15 +308,18 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
}] }]
self.register_get_threads_response(source_threads, page=1, num_pages=2) 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": ""}) 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
)
expected_respoonse.update({"text_search_rewrite": None})
self.assert_response_correct( self.assert_response_correct(
response, response,
200, 200,
{ expected_respoonse
"results": expected_threads,
"next": "http://testserver/api/discussion/v1/threads/?course_id=x%2Fy%2Fz&page=2",
"previous": None,
"text_search_rewrite": None,
}
) )
self.assert_last_query_params({ self.assert_last_query_params({
"user_id": [unicode(self.user.id)], "user_id": [unicode(self.user.id)],
...@@ -374,15 +378,18 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -374,15 +378,18 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
def test_text_search(self): def test_text_search(self):
self.register_get_user_response(self.user) self.register_get_user_response(self.user)
self.register_get_threads_search_response([], None) self.register_get_threads_search_response([], None, num_pages=0)
response = self.client.get( response = self.client.get(
self.url, self.url,
{"course_id": unicode(self.course.id), "text_search": "test search string"} {"course_id": unicode(self.course.id), "text_search": "test search string"}
) )
expected_response = make_paginated_api_response([], 0, 0, None, None)
expected_response.update({"text_search_rewrite": None})
self.assert_response_correct( self.assert_response_correct(
response, response,
200, 200,
{"results": [], "next": None, "previous": None, "text_search_rewrite": None} expected_response
) )
self.assert_last_query_params({ self.assert_last_query_params({
"user_id": [unicode(self.user.id)], "user_id": [unicode(self.user.id)],
...@@ -398,7 +405,7 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -398,7 +405,7 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
@ddt.data(True, "true", "1") @ddt.data(True, "true", "1")
def test_following_true(self, following): def test_following_true(self, following):
self.register_get_user_response(self.user) self.register_get_user_response(self.user)
self.register_subscribed_threads_response(self.user, [], page=1, num_pages=1) self.register_subscribed_threads_response(self.user, [], page=1, num_pages=0)
response = self.client.get( response = self.client.get(
self.url, self.url,
{ {
...@@ -406,10 +413,13 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -406,10 +413,13 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
"following": following, "following": following,
} }
) )
expected_response = make_paginated_api_response([], 0, 0, None, None)
expected_response.update({"text_search_rewrite": None})
self.assert_response_correct( self.assert_response_correct(
response, response,
200, 200,
{"results": [], "next": None, "previous": None, "text_search_rewrite": None} expected_response
) )
self.assertEqual( self.assertEqual(
urlparse(httpretty.last_request().path).path, urlparse(httpretty.last_request().path).path,
...@@ -933,16 +943,13 @@ class CommentViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -933,16 +943,13 @@ class CommentViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
"resp_total": 100, "resp_total": 100,
}) })
response = self.client.get(self.url, {"thread_id": self.thread_id}) response = self.client.get(self.url, {"thread_id": self.thread_id})
next_link = "http://testserver/api/discussion/v1/comments/?page=2&thread_id={}".format(
self.thread_id
)
self.assert_response_correct( self.assert_response_correct(
response, response,
200, 200,
{ make_paginated_api_response(expected_comments, 0, 10, next_link, None)
"results": expected_comments,
"next": "http://testserver/api/discussion/v1/comments/?page=2&thread_id={}".format(
self.thread_id
),
"previous": None,
}
) )
self.assert_query_params_equal( self.assert_query_params_equal(
httpretty.httpretty.latest_requests[-2], httpretty.httpretty.latest_requests[-2],
......
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