Skip to content

Commit 056acff

Browse files
authored
fix: update problem block tests (#37136)
1 parent 7ca1014 commit 056acff

3 files changed

Lines changed: 74 additions & 22 deletions

File tree

lms/djangoapps/grades/tests/integration/test_problems.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import ddt
66
import pytz
77
from crum import set_current_request
8+
from django.test.utils import override_settings
9+
from xmodule.capa_block import reset_class
810
from xmodule.graders import ProblemScore
911
from xmodule.modulestore import ModuleStoreEnum
1012
from xmodule.modulestore.tests.django_utils import (
@@ -25,23 +27,26 @@
2527

2628

2729
@ddt.ddt
28-
class TestMultipleProblemTypesSubsectionScores(SharedModuleStoreTestCase):
30+
class _TestMultipleProblemTypesSubsectionScoresBase(SharedModuleStoreTestCase):
2931
"""
3032
Test grading of different problem types.
3133
"""
34+
__test__ = False
3235
MODULESTORE = TEST_DATA_SPLIT_MODULESTORE
3336

3437
SCORED_BLOCK_COUNT = 7
3538
ACTUAL_TOTAL_POSSIBLE = 17.0
3639

3740
@classmethod
3841
def setUpClass(cls):
42+
reset_class()
3943
super().setUpClass()
4044
cls.load_scoreable_course()
4145
chapter1 = cls.course.get_children()[0]
4246
cls.seq1 = chapter1.get_children()[0]
4347

4448
def setUp(self):
49+
reset_class()
4550
super().setUp()
4651
self.student = UserFactory.create(is_staff=False, username='test_student', password=self.TEST_PASSWORD)
4752
self.client.login(username=self.student.username, password=self.TEST_PASSWORD)
@@ -98,12 +103,23 @@ def test_score_submission_for_all_problems(self):
98103
assert score.all_total.possible == (possible_per_block * block_count)
99104

100105

106+
@override_settings(USE_EXTRACTED_PROBLEM_BLOCK=True)
107+
class ExtractedTestMultipleProblemTypesSubsectionScores(_TestMultipleProblemTypesSubsectionScoresBase):
108+
__test__ = True
109+
110+
111+
@override_settings(USE_EXTRACTED_PROBLEM_BLOCK=False)
112+
class BuiltInTestMultipleProblemTypesSubsectionScores(_TestMultipleProblemTypesSubsectionScoresBase):
113+
__test__ = True
114+
115+
101116
@ddt.ddt
102-
class TestVariedMetadata(ProblemSubmissionTestMixin, ModuleStoreTestCase):
117+
class _TestVariedMetadataBase(ProblemSubmissionTestMixin, ModuleStoreTestCase):
103118
"""
104119
Test that changing the metadata on a block has the desired effect on the
105120
persisted score.
106121
"""
122+
__test__ = False
107123
MODULESTORE = TEST_DATA_SPLIT_MODULESTORE
108124

109125
default_problem_metadata = {
@@ -113,6 +129,7 @@ class TestVariedMetadata(ProblemSubmissionTestMixin, ModuleStoreTestCase):
113129
}
114130

115131
def setUp(self):
132+
reset_class()
116133
super().setUp()
117134
self.course = CourseFactory.create()
118135
with self.store.bulk_operations(self.course.id):
@@ -209,15 +226,27 @@ def test_graded_metadata_alterations(self, alterations, expected_earned, expecte
209226
assert score.graded_total.possible == expected_possible
210227

211228

229+
@override_settings(USE_EXTRACTED_PROBLEM_BLOCK=True)
230+
class ExtractedTestVariedMetadata(_TestVariedMetadataBase):
231+
__test__ = True
232+
233+
234+
@override_settings(USE_EXTRACTED_PROBLEM_BLOCK=False)
235+
class BuiltInTestVariedMetadata(_TestVariedMetadataBase):
236+
__test__ = True
237+
238+
212239
@ddt.ddt
213-
class TestWeightedProblems(SharedModuleStoreTestCase):
240+
class _TestWeightedProblemsBase(SharedModuleStoreTestCase):
214241
"""
215242
Test scores and grades with various problem weight values.
216243
"""
244+
__test__ = False
217245
MODULESTORE = TEST_DATA_SPLIT_MODULESTORE
218246

219247
@classmethod
220248
def setUpClass(cls):
249+
reset_class()
221250
super().setUpClass()
222251
cls.course = CourseFactory.create()
223252
with cls.store.bulk_operations(cls.course.id):
@@ -237,6 +266,7 @@ def setUpClass(cls):
237266
)
238267

239268
def setUp(self):
269+
reset_class()
240270
super().setUp()
241271
self.user = UserFactory()
242272
self.addCleanup(set_current_request, None)
@@ -315,3 +345,13 @@ def test_problem_weight(self, raw_earned, raw_possible, weight):
315345
first_attempted=datetime.datetime(2010, 1, 1),
316346
)
317347
self._verify_grades(raw_earned, raw_possible, weight, expected_score)
348+
349+
350+
@override_settings(USE_EXTRACTED_PROBLEM_BLOCK=True)
351+
class ExtractedTestWeightedProblems(_TestWeightedProblemsBase):
352+
__test__ = True
353+
354+
355+
@override_settings(USE_EXTRACTED_PROBLEM_BLOCK=False)
356+
class BuiltInTestWeightedProblems(_TestWeightedProblemsBase):
357+
__test__ = True

xmodule/capa_block.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2465,5 +2465,17 @@ def randomization_bin(seed, problem_id):
24652465
return int(r_hash.hexdigest()[:7], 16) % NUM_RANDOMIZATION_BINS
24662466

24672467

2468-
ProblemBlock = _ExtractedProblemBlock if settings.USE_EXTRACTED_PROBLEM_BLOCK else _BuiltInProblemBlock
2468+
ProblemBlock = None
2469+
2470+
2471+
def reset_class():
2472+
"""Reset class as per django settings flag"""
2473+
global ProblemBlock
2474+
ProblemBlock = (
2475+
_ExtractedProblemBlock if settings.USE_EXTRACTED_PROBLEM_BLOCK else _BuiltInProblemBlock
2476+
)
2477+
return ProblemBlock
2478+
2479+
2480+
reset_class()
24692481
ProblemBlock.__name__ = "ProblemBlock"

xmodule/tests/test_capa_block.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
)
3939
from xblocks_contrib.problem.capa.tests.test_util import UseUnsafeCodejail
4040
from xblocks_contrib.problem.capa.xqueue_interface import XQueueInterface
41-
from xmodule.capa_block import ComplexEncoder, ProblemBlock
41+
from xmodule.capa_block import ComplexEncoder, _BuiltInProblemBlock as ProblemBlock
4242
from xmodule.tests import DATA_DIR
4343

4444
from ..capa_block import RANDOMIZATION, SHOWANSWER
@@ -813,7 +813,7 @@ def test_submit_problem_correct(self):
813813
# what the input is, by patching CorrectMap.is_correct()
814814
# Also simulate rendering the HTML
815815
with patch("xblocks_contrib.problem.capa.correctmap.CorrectMap.is_correct") as mock_is_correct:
816-
with patch("xmodule.capa_block.ProblemBlock.get_problem_html") as mock_html:
816+
with patch("xmodule.capa_block._BuiltInProblemBlock.get_problem_html") as mock_html:
817817
mock_is_correct.return_value = True
818818
mock_html.return_value = "Test HTML"
819819

@@ -833,7 +833,7 @@ def test_submit_problem_correct(self):
833833
assert block.lcp.context["attempt"] == 2
834834

835835
@patch("xblocks_contrib.problem.capa.correctmap.CorrectMap.is_correct")
836-
@patch("xmodule.capa_block.ProblemBlock.get_problem_html")
836+
@patch("xmodule.capa_block._BuiltInProblemBlock.get_problem_html")
837837
def test_submit_problem_with_grading_method_disable(self, mock_html: Mock, mock_is_correct: Mock):
838838
"""
839839
Test that without a specific grading method, the score behaves as
@@ -873,7 +873,7 @@ def test_submit_problem_with_grading_method_disable(self, mock_html: Mock, mock_
873873
assert block.score == Score(raw_earned=1, raw_possible=1)
874874

875875
@patch("xblocks_contrib.problem.capa.correctmap.CorrectMap.is_correct")
876-
@patch("xmodule.capa_block.ProblemBlock.get_problem_html")
876+
@patch("xmodule.capa_block._BuiltInProblemBlock.get_problem_html")
877877
def test_submit_problem_with_grading_method_enable(self, mock_html: Mock, mock_is_correct: Mock):
878878
"""
879879
Test that the grading method is enabled when submit a problem.
@@ -895,7 +895,7 @@ def test_submit_problem_with_grading_method_enable(self, mock_html: Mock, mock_i
895895
mock_get_score.assert_called()
896896

897897
@patch("xblocks_contrib.problem.capa.correctmap.CorrectMap.is_correct")
898-
@patch("xmodule.capa_block.ProblemBlock.get_problem_html")
898+
@patch("xmodule.capa_block._BuiltInProblemBlock.get_problem_html")
899899
def test_submit_problem_grading_method_always_enabled(self, mock_html: Mock, mock_is_correct: Mock):
900900
"""
901901
Test problem submission when grading method is always enabled by default.
@@ -948,7 +948,7 @@ def test_submit_problem_grading_method_always_enabled(self, mock_html: Mock, moc
948948
assert block.score == Score(raw_earned=1, raw_possible=1)
949949

950950
@patch("xblocks_contrib.problem.capa.correctmap.CorrectMap.is_correct")
951-
@patch("xmodule.capa_block.ProblemBlock.get_problem_html")
951+
@patch("xmodule.capa_block._BuiltInProblemBlock.get_problem_html")
952952
def test_submit_problem_grading_method_always_enabled_highest_score(self, mock_html: Mock, mock_is_correct: Mock):
953953
"""
954954
Test problem submission when grading method is always enabled by default
@@ -1001,7 +1001,7 @@ def test_submit_problem_grading_method_always_enabled_highest_score(self, mock_h
10011001
assert block.score == Score(raw_earned=1, raw_possible=1)
10021002

10031003
@patch("xblocks_contrib.problem.capa.correctmap.CorrectMap.is_correct")
1004-
@patch("xmodule.capa_block.ProblemBlock.get_problem_html")
1004+
@patch("xmodule.capa_block._BuiltInProblemBlock.get_problem_html")
10051005
def test_submit_problem_correct_last_score(self, mock_html: Mock, mock_is_correct: Mock):
10061006
"""
10071007
Test the `last_score` grading method.
@@ -1034,7 +1034,7 @@ def test_submit_problem_correct_last_score(self, mock_html: Mock, mock_is_correc
10341034
assert block.score == Score(raw_earned=0, raw_possible=1)
10351035

10361036
@patch("xblocks_contrib.problem.capa.correctmap.CorrectMap.is_correct")
1037-
@patch("xmodule.capa_block.ProblemBlock.get_problem_html")
1037+
@patch("xmodule.capa_block._BuiltInProblemBlock.get_problem_html")
10381038
def test_submit_problem_correct_highest_score(self, mock_html: Mock, mock_is_correct: Mock):
10391039
"""
10401040
Test the `highest_score` grading method.
@@ -1066,7 +1066,7 @@ def test_submit_problem_correct_highest_score(self, mock_html: Mock, mock_is_cor
10661066
assert block.score == Score(raw_earned=1, raw_possible=1)
10671067

10681068
@patch("xblocks_contrib.problem.capa.correctmap.CorrectMap.is_correct")
1069-
@patch("xmodule.capa_block.ProblemBlock.get_problem_html")
1069+
@patch("xmodule.capa_block._BuiltInProblemBlock.get_problem_html")
10701070
def test_submit_problem_correct_first_score(self, mock_html: Mock, mock_is_correct: Mock):
10711071
"""
10721072
Test the `first_score` grading method.
@@ -1098,7 +1098,7 @@ def test_submit_problem_correct_first_score(self, mock_html: Mock, mock_is_corre
10981098
assert block.score == Score(raw_earned=0, raw_possible=1)
10991099

11001100
@patch("xblocks_contrib.problem.capa.correctmap.CorrectMap.is_correct")
1101-
@patch("xmodule.capa_block.ProblemBlock.get_problem_html")
1101+
@patch("xmodule.capa_block._BuiltInProblemBlock.get_problem_html")
11021102
def test_submit_problem_correct_average_score(self, mock_html: Mock, mock_is_correct: Mock):
11031103
"""
11041104
Test the `average_score` grading method.
@@ -1176,7 +1176,7 @@ def test_submit_problem_closed(self):
11761176

11771177
# Problem closed -- cannot submit
11781178
# Simulate that ProblemBlock.closed() always returns True
1179-
with patch("xmodule.capa_block.ProblemBlock.closed") as mock_closed:
1179+
with patch("xmodule.capa_block._BuiltInProblemBlock.closed") as mock_closed:
11801180
mock_closed.return_value = True
11811181
with pytest.raises(NotFoundError):
11821182
get_request_dict = {CapaFactory.input_key(): "3.14"}
@@ -1530,7 +1530,7 @@ def test_reset_problem(self):
15301530
block.choose_new_seed = Mock(wraps=block.choose_new_seed)
15311531

15321532
# Stub out HTML rendering
1533-
with patch("xmodule.capa_block.ProblemBlock.get_problem_html") as mock_html:
1533+
with patch("xmodule.capa_block._BuiltInProblemBlock.get_problem_html") as mock_html:
15341534
mock_html.return_value = "<div>Test HTML</div>"
15351535

15361536
# Reset the problem
@@ -1553,7 +1553,7 @@ def test_reset_problem_closed(self):
15531553
block = CapaFactory.create(rerandomize=RANDOMIZATION.ALWAYS)
15541554

15551555
# Simulate that the problem is closed
1556-
with patch("xmodule.capa_block.ProblemBlock.closed") as mock_closed:
1556+
with patch("xmodule.capa_block._BuiltInProblemBlock.closed") as mock_closed:
15571557
mock_closed.return_value = True
15581558

15591559
# Try to reset the problem
@@ -1696,7 +1696,7 @@ def test_rescore_problem_with_grading_method_enable(self):
16961696
assert block.lcp.context["attempt"] == 1
16971697
mock_get_rescore.assert_called()
16981698

1699-
@patch("xmodule.capa_block.ProblemBlock.publish_grade")
1699+
@patch("xmodule.capa_block._BuiltInProblemBlock.publish_grade")
17001700
def test_rescore_problem_grading_method_always_enabled(self, mock_publish_grade: Mock):
17011701
"""
17021702
Test the rescore method when grading method is always enabled by default.
@@ -1738,7 +1738,7 @@ def test_rescore_problem_grading_method_always_enabled(self, mock_publish_grade:
17381738

17391739
mock_publish_grade.assert_called_with(score=Score(raw_earned=0.33, raw_possible=1), only_if_higher=False)
17401740

1741-
@patch("xmodule.capa_block.ProblemBlock.publish_grade")
1741+
@patch("xmodule.capa_block._BuiltInProblemBlock.publish_grade")
17421742
def test_rescore_problem_grading_method_always_enabled_with_various_methods(self, mock_publish_grade: Mock):
17431743
"""
17441744
Test the rescore method when grading method is always enabled by default
@@ -1780,7 +1780,7 @@ def test_rescore_problem_grading_method_always_enabled_with_various_methods(self
17801780
block.rescore(only_if_higher=False)
17811781
assert block.score == Score(raw_earned=1, raw_possible=1)
17821782

1783-
@patch("xmodule.capa_block.ProblemBlock.publish_grade")
1783+
@patch("xmodule.capa_block._BuiltInProblemBlock.publish_grade")
17841784
def test_rescore_problem_update_grading_method(self, mock_publish_grade: Mock):
17851785
"""
17861786
Test the rescore method when the grading method is updated.
@@ -1944,7 +1944,7 @@ def test_get_score_with_grading_method(self):
19441944
self.assertEqual(score, expected_score)
19451945
self.assertEqual(block.score, expected_score)
19461946

1947-
@patch("xmodule.capa_block.ProblemBlock.score_from_lcp")
1947+
@patch("xmodule.capa_block._BuiltInProblemBlock.score_from_lcp")
19481948
def test_get_score_with_grading_method_updates_score(self, mock_score_from_lcp: Mock):
19491949
"""
19501950
Test that the `get_score_with_grading_method` method returns the correct score.
@@ -2065,7 +2065,7 @@ def test_save_problem_closed(self):
20652065
block = CapaFactory.create(done=False)
20662066

20672067
# Simulate that the problem is closed
2068-
with patch("xmodule.capa_block.ProblemBlock.closed") as mock_closed:
2068+
with patch("xmodule.capa_block._BuiltInProblemBlock.closed") as mock_closed:
20692069
mock_closed.return_value = True
20702070

20712071
# Try to save the problem

0 commit comments

Comments
 (0)