Files
edx-platform/lms/djangoapps/grades/tests/integration/test_access.py
David Ormsbee 2051c90924 Test Speedup: Isolate Modulestore Signals
There are a number of Django Signals that are on the modulestore's
SignalHandler class, such as SignalHandler.course_published. These
signals can trigger very expensive processes to occur, such as course
overview or block structures generation. Most of the time, the test
author doesn't care about these side-effects.

This commit does a few things:

* Converts the signals on SignalHandler to be instances of a new
  SwitchedSignal class, that allows signal sending to be disabled.

* Creates a SignalIsolationMixin helper similar in spirit to the
  CacheIsolationMixin, and adds it to the ModuleStoreIsolationMixin
  (and thus to ModuleStoreTestCase and SharedModuleStoreTestCase).

* Converts our various tests to use this new mechanism. In some cases,
  this means adjusting query counts downwards because they no longer
  have to account for publishing listener actions.

Modulestore generated signals are now muted by default during test runs.
Calls to send() them will result in no-ops. You can choose to enable
specific signals for a given subclass of ModuleStoreTestCase or
SharedModuleStoreTestCase by specifying an ENABLED_SIGNALS class
attribute, like the following example:

    from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase

    class MyPublishTestCase(ModuleStoreTestCase):
        ENABLED_SIGNALS = ['course_published', 'pre_publish']

You should take great care when disabling signals outside of a
ModuleStoreTestCase or SharedModuleStoreTestCase, since they can leak
out into other tests. Be sure to always clean up, and never disable
signals outside of testing. Because signals are essentially process
globals, it can have a lot of unpleasant side-effects if we start
mucking around with them during live requests.

Overall, this change has cut the total test execution time for
edx-platform by a bit over a third, though we still spend a lot in
pre-test setup during our test builds.

[PERF-413]
2017-02-23 10:31:16 -05:00

111 lines
4.5 KiB
Python

"""
Test grading with access changes.
"""
# pylint: disable=protected-access
from capa.tests.response_xml_factory import MultipleChoiceResponseXMLFactory
from lms.djangoapps.course_blocks.api import get_course_blocks
from openedx.core.djangolib.testing.utils import get_mock_request
from xmodule.modulestore.django import modulestore
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
from courseware.tests.test_submitting_problems import ProblemSubmissionTestMixin
from student.models import CourseEnrollment
from student.tests.factories import UserFactory
from xmodule.modulestore import ModuleStoreEnum
from ...new.subsection_grade import SubsectionGradeFactory
class GradesAccessIntegrationTest(ProblemSubmissionTestMixin, SharedModuleStoreTestCase):
"""
Tests integration between grading and block access.
"""
ENABLED_SIGNALS = ['course_published']
@classmethod
def setUpClass(cls):
super(GradesAccessIntegrationTest, cls).setUpClass()
cls.store = modulestore()
cls.course = CourseFactory.create()
cls.chapter = ItemFactory.create(
parent=cls.course,
category="chapter",
display_name="Test Chapter"
)
cls.sequence = ItemFactory.create(
parent=cls.chapter,
category='sequential',
display_name="Test Sequential 1",
graded=True,
format="Homework"
)
cls.vertical = ItemFactory.create(
parent=cls.sequence,
category='vertical',
display_name='Test Vertical 1'
)
problem_xml = MultipleChoiceResponseXMLFactory().build_xml(
question_text='The correct answer is Choice 2',
choices=[False, False, True, False],
choice_names=['choice_0', 'choice_1', 'choice_2', 'choice_3']
)
cls.problem = ItemFactory.create(
parent=cls.vertical,
category="problem",
display_name="p1",
data=problem_xml,
metadata={'weight': 2}
)
cls.problem_2 = ItemFactory.create(
parent=cls.vertical,
category="problem",
display_name="p2",
data=problem_xml,
metadata={'weight': 2}
)
def setUp(self):
super(GradesAccessIntegrationTest, self).setUp()
self.request = get_mock_request(UserFactory())
self.student = self.request.user
self.client.login(username=self.student.username, password="test")
CourseEnrollment.enroll(self.student, self.course.id)
self.instructor = UserFactory.create(is_staff=True, username=u'test_instructor', password=u'test')
self.refresh_course()
def test_subsection_access_changed(self):
"""
Tests retrieving a subsection grade before and after losing access
to a block in the subsection.
"""
# submit answers
self.submit_question_answer('p1', {'2_1': 'choice_choice_2'})
self.submit_question_answer('p2', {'2_1': 'choice_choice_2'})
# check initial subsection grade
course_structure = get_course_blocks(self.request.user, self.course.location)
subsection_grade_factory = SubsectionGradeFactory(self.request.user, self.course, course_structure)
grade = subsection_grade_factory.create(self.sequence, read_only=True)
self.assertEqual(grade.graded_total.earned, 4.0)
self.assertEqual(grade.graded_total.possible, 4.0)
# set a block in the subsection to be visible to staff only
with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred):
problem_2 = self.store.get_item(self.problem_2.location)
problem_2.visible_to_staff_only = True
self.store.update_item(problem_2, self.instructor.id)
self.store.publish(self.course.location, self.instructor.id)
course_structure = get_course_blocks(self.student, self.course.location)
# ensure that problem_2 is not accessible for the student
self.assertNotIn(problem_2.location, course_structure)
# make sure we can still get the subsection grade
subsection_grade_factory = SubsectionGradeFactory(self.student, self.course, course_structure)
grade = subsection_grade_factory.create(self.sequence, read_only=True)
self.assertEqual(grade.graded_total.earned, 4.0)
self.assertEqual(grade.graded_total.possible, 4.0)