Commit 7fe9f70a by Miles Steele

fix test mocking

parent a15c2d73
...@@ -8,7 +8,7 @@ from urllib import quote ...@@ -8,7 +8,7 @@ from urllib import quote
from django.conf import settings from django.conf import settings
from django.test import TestCase from django.test import TestCase
from nose.tools import raises from nose.tools import raises
from mock import Mock from mock import Mock, patch
from django.test.utils import override_settings from django.test.utils import override_settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
...@@ -23,6 +23,9 @@ from student.tests.factories import UserFactory, AdminFactory ...@@ -23,6 +23,9 @@ from student.tests.factories import UserFactory, AdminFactory
from student.models import CourseEnrollment from student.models import CourseEnrollment
from courseware.models import StudentModule from courseware.models import StudentModule
# modules which are mocked in test cases.
import instructor_task.api
from instructor.access import allow_access from instructor.access import allow_access
import instructor.views.api import instructor.views.api
from instructor.views.api import ( from instructor.views.api import (
...@@ -572,13 +575,10 @@ class TestInstructorAPIRegradeTask(ModuleStoreTestCase, LoginEnrollmentTestCase) ...@@ -572,13 +575,10 @@ class TestInstructorAPIRegradeTask(ModuleStoreTestCase, LoginEnrollmentTestCase)
0 0
) )
def test_reset_student_attempts_all(self): # mock out the function which should be called to execute the action.
@patch.object(instructor_task.api, 'submit_reset_problem_attempts_for_all_students')
def test_reset_student_attempts_all(self, act):
""" Test reset all student attempts. """ """ Test reset all student attempts. """
# mock out the function which should be called to execute the action.
import instructor_task.api
act = Mock(return_value=None)
instructor_task.api.submit_reset_problem_attempts_for_all_students = act
url = reverse('reset_student_attempts', kwargs={'course_id': self.course.id}) url = reverse('reset_student_attempts', kwargs={'course_id': self.course.id})
response = self.client.get(url, { response = self.client.get(url, {
'problem_to_reset': self.problem_urlname, 'problem_to_reset': self.problem_urlname,
...@@ -629,12 +629,9 @@ class TestInstructorAPIRegradeTask(ModuleStoreTestCase, LoginEnrollmentTestCase) ...@@ -629,12 +629,9 @@ class TestInstructorAPIRegradeTask(ModuleStoreTestCase, LoginEnrollmentTestCase)
print response.content print response.content
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)
def test_rescore_problem_single(self): @patch.object(instructor_task.api, 'submit_rescore_problem_for_student')
def test_rescore_problem_single(self, act):
""" Test rescoring of a single student. """ """ Test rescoring of a single student. """
import instructor_task.api
act = Mock(return_value=None)
instructor_task.api.submit_rescore_problem_for_student = act
url = reverse('rescore_problem', kwargs={'course_id': self.course.id}) url = reverse('rescore_problem', kwargs={'course_id': self.course.id})
response = self.client.get(url, { response = self.client.get(url, {
'problem_to_reset': self.problem_urlname, 'problem_to_reset': self.problem_urlname,
...@@ -644,12 +641,9 @@ class TestInstructorAPIRegradeTask(ModuleStoreTestCase, LoginEnrollmentTestCase) ...@@ -644,12 +641,9 @@ class TestInstructorAPIRegradeTask(ModuleStoreTestCase, LoginEnrollmentTestCase)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTrue(act.called) self.assertTrue(act.called)
def test_rescore_problem_all(self): @patch.object(instructor_task.api, 'submit_rescore_problem_for_all_students')
def test_rescore_problem_all(self, act):
""" Test rescoring for all students. """ """ Test rescoring for all students. """
import instructor_task.api
act = Mock(return_value=None)
instructor_task.api.submit_rescore_problem_for_all_students = act
url = reverse('rescore_problem', kwargs={'course_id': self.course.id}) url = reverse('rescore_problem', kwargs={'course_id': self.course.id})
response = self.client.get(url, { response = self.client.get(url, {
'problem_to_reset': self.problem_urlname, 'problem_to_reset': self.problem_urlname,
...@@ -699,12 +693,10 @@ class TestInstructorAPITaskLists(ModuleStoreTestCase, LoginEnrollmentTestCase): ...@@ -699,12 +693,10 @@ class TestInstructorAPITaskLists(ModuleStoreTestCase, LoginEnrollmentTestCase):
self.tasks = [self.FakeTask() for _ in xrange(6)] self.tasks = [self.FakeTask() for _ in xrange(6)]
def test_list_instructor_tasks_running(self): @patch.object(instructor_task.api, 'get_running_instructor_tasks')
def test_list_instructor_tasks_running(self, act):
""" Test list of all running tasks. """ """ Test list of all running tasks. """
import instructor_task.api act.return_value = self.tasks
act = Mock(return_value=self.tasks)
instructor_task.api.get_running_instructor_tasks = act
url = reverse('list_instructor_tasks', kwargs={'course_id': self.course.id}) url = reverse('list_instructor_tasks', kwargs={'course_id': self.course.id})
response = self.client.get(url, {}) response = self.client.get(url, {})
print response.content print response.content
...@@ -716,12 +708,10 @@ class TestInstructorAPITaskLists(ModuleStoreTestCase, LoginEnrollmentTestCase): ...@@ -716,12 +708,10 @@ class TestInstructorAPITaskLists(ModuleStoreTestCase, LoginEnrollmentTestCase):
expected_res = {'tasks': expected_tasks} expected_res = {'tasks': expected_tasks}
self.assertEqual(json.loads(response.content), expected_res) self.assertEqual(json.loads(response.content), expected_res)
def test_list_instructor_tasks_problem(self): @patch.object(instructor_task.api, 'get_instructor_task_history')
def test_list_instructor_tasks_problem(self, act):
""" Test list task history for problem. """ """ Test list task history for problem. """
import instructor_task.api act.return_value = self.tasks
act = Mock(return_value=self.tasks)
instructor_task.api.get_instructor_task_history = act
url = reverse('list_instructor_tasks', kwargs={'course_id': self.course.id}) url = reverse('list_instructor_tasks', kwargs={'course_id': self.course.id})
response = self.client.get(url, { response = self.client.get(url, {
'problem_urlname': self.problem_urlname, 'problem_urlname': self.problem_urlname,
...@@ -735,12 +725,10 @@ class TestInstructorAPITaskLists(ModuleStoreTestCase, LoginEnrollmentTestCase): ...@@ -735,12 +725,10 @@ class TestInstructorAPITaskLists(ModuleStoreTestCase, LoginEnrollmentTestCase):
expected_res = {'tasks': expected_tasks} expected_res = {'tasks': expected_tasks}
self.assertEqual(json.loads(response.content), expected_res) self.assertEqual(json.loads(response.content), expected_res)
def test_list_instructor_tasks_problem_student(self): @patch.object(instructor_task.api, 'get_instructor_task_history')
def test_list_instructor_tasks_problem_student(self, act):
""" Test list task history for problem AND student. """ """ Test list task history for problem AND student. """
import instructor_task.api act.return_value = self.tasks
act = Mock(return_value=self.tasks)
instructor_task.api.get_instructor_task_history = act
url = reverse('list_instructor_tasks', kwargs={'course_id': self.course.id}) url = reverse('list_instructor_tasks', kwargs={'course_id': self.course.id})
response = self.client.get(url, { response = self.client.get(url, {
'problem_urlname': self.problem_urlname, 'problem_urlname': self.problem_urlname,
...@@ -781,10 +769,10 @@ class TestInstructorAPIAnalyticsProxy(ModuleStoreTestCase, LoginEnrollmentTestCa ...@@ -781,10 +769,10 @@ class TestInstructorAPIAnalyticsProxy(ModuleStoreTestCase, LoginEnrollmentTestCa
self.course = CourseFactory.create() self.course = CourseFactory.create()
self.client.login(username=self.instructor.username, password='test') self.client.login(username=self.instructor.username, password='test')
def test_analytics_proxy_url(self): @patch.object(instructor.views.api.requests, 'get')
def test_analytics_proxy_url(self, act):
""" Test legacy analytics proxy url generation. """ """ Test legacy analytics proxy url generation. """
act = Mock(return_value=self.FakeProxyResponse()) act.return_value = self.FakeProxyResponse()
instructor.views.api.requests.get = act
url = reverse('proxy_legacy_analytics', kwargs={'course_id': self.course.id}) url = reverse('proxy_legacy_analytics', kwargs={'course_id': self.course.id})
response = self.client.get(url, { response = self.client.get(url, {
...@@ -802,12 +790,12 @@ class TestInstructorAPIAnalyticsProxy(ModuleStoreTestCase, LoginEnrollmentTestCa ...@@ -802,12 +790,12 @@ class TestInstructorAPIAnalyticsProxy(ModuleStoreTestCase, LoginEnrollmentTestCa
) )
act.assert_called_once_with(expected_url) act.assert_called_once_with(expected_url)
def test_analytics_proxy(self): @patch.object(instructor.views.api.requests, 'get')
def test_analytics_proxy(self, act):
""" """
Test legacy analytics content proxying. Test legacy analytics content proxyin, actg.
""" """
act = Mock(return_value=self.FakeProxyResponse()) act.return_value = self.FakeProxyResponse()
instructor.views.api.requests.get = act
url = reverse('proxy_legacy_analytics', kwargs={'course_id': self.course.id}) url = reverse('proxy_legacy_analytics', kwargs={'course_id': self.course.id})
response = self.client.get(url, { response = self.client.get(url, {
...@@ -821,10 +809,10 @@ class TestInstructorAPIAnalyticsProxy(ModuleStoreTestCase, LoginEnrollmentTestCa ...@@ -821,10 +809,10 @@ class TestInstructorAPIAnalyticsProxy(ModuleStoreTestCase, LoginEnrollmentTestCa
expected_res = {'test_content': "robot test content"} expected_res = {'test_content': "robot test content"}
self.assertEqual(json.loads(response.content), expected_res) self.assertEqual(json.loads(response.content), expected_res)
def test_analytics_proxy_reqfailed(self): @patch.object(instructor.views.api.requests, 'get')
def test_analytics_proxy_reqfailed(self, act):
""" Test proxy when server reponds with failure. """ """ Test proxy when server reponds with failure. """
act = Mock(return_value=self.FakeBadProxyResponse()) act.return_value = self.FakeBadProxyResponse()
instructor.views.api.requests.get = act
url = reverse('proxy_legacy_analytics', kwargs={'course_id': self.course.id}) url = reverse('proxy_legacy_analytics', kwargs={'course_id': self.course.id})
response = self.client.get(url, { response = self.client.get(url, {
...@@ -833,10 +821,10 @@ class TestInstructorAPIAnalyticsProxy(ModuleStoreTestCase, LoginEnrollmentTestCa ...@@ -833,10 +821,10 @@ class TestInstructorAPIAnalyticsProxy(ModuleStoreTestCase, LoginEnrollmentTestCa
print response.content print response.content
self.assertEqual(response.status_code, 500) self.assertEqual(response.status_code, 500)
def test_analytics_proxy_missing_param(self): @patch.object(instructor.views.api.requests, 'get')
def test_analytics_proxy_missing_param(self, act):
""" Test proxy when missing the aname query parameter. """ """ Test proxy when missing the aname query parameter. """
act = Mock(return_value=self.FakeProxyResponse()) act.return_value = self.FakeProxyResponse()
instructor.views.api.requests.get = act
url = reverse('proxy_legacy_analytics', kwargs={'course_id': self.course.id}) url = reverse('proxy_legacy_analytics', kwargs={'course_id': self.course.id})
response = self.client.get(url, {}) response = self.client.get(url, {})
......
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