|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | # pylint: disable=protected-access |
| 6 | +from datetime import datetime, timedelta |
6 | 7 | from unittest import TestCase |
7 | 8 | from unittest.mock import Mock |
8 | 9 |
|
9 | 10 | import ddt |
| 11 | +from pytz import UTC |
10 | 12 |
|
11 | 13 | from xblock import scorable |
12 | 14 |
|
@@ -84,3 +86,95 @@ def test_scoring_error(self): |
84 | 86 | block._scoring_error = True |
85 | 87 | with self.assertRaises(RuntimeError): |
86 | 88 | block.rescore(only_if_higher=False) |
| 89 | + |
| 90 | + |
| 91 | +@ddt.ddt |
| 92 | +class ShowCorrectnessTest(TestCase): |
| 93 | + """ |
| 94 | + Tests the correctness_available method |
| 95 | + """ |
| 96 | + |
| 97 | + def setUp(self): |
| 98 | + super().setUp() |
| 99 | + |
| 100 | + now = datetime.now(UTC) |
| 101 | + day_delta = timedelta(days=1) |
| 102 | + self.yesterday = now - day_delta |
| 103 | + self.today = now |
| 104 | + self.tomorrow = now + day_delta |
| 105 | + |
| 106 | + def test_show_correctness_default(self): |
| 107 | + """ |
| 108 | + Test that correctness is visible by default. |
| 109 | + """ |
| 110 | + assert scorable.ShowCorrectness.correctness_available() |
| 111 | + |
| 112 | + @ddt.data( |
| 113 | + (scorable.ShowCorrectness.ALWAYS, True), |
| 114 | + (scorable.ShowCorrectness.ALWAYS, False), |
| 115 | + # Any non-constant values behave like "always" |
| 116 | + ("", True), |
| 117 | + ("", False), |
| 118 | + ("other-value", True), |
| 119 | + ("other-value", False), |
| 120 | + ) |
| 121 | + @ddt.unpack |
| 122 | + def test_show_correctness_always(self, show_correctness, has_staff_access): |
| 123 | + """ |
| 124 | + Test that correctness is visible when show_correctness is turned on. |
| 125 | + """ |
| 126 | + assert scorable.ShowCorrectness.correctness_available( |
| 127 | + show_correctness=show_correctness, has_staff_access=has_staff_access |
| 128 | + ) |
| 129 | + |
| 130 | + @ddt.data(True, False) |
| 131 | + def test_show_correctness_never(self, has_staff_access): |
| 132 | + """ |
| 133 | + Test that show_correctness="never" hides correctness from learners and course staff. |
| 134 | + """ |
| 135 | + assert not scorable.ShowCorrectness.correctness_available( |
| 136 | + show_correctness=scorable.ShowCorrectness.NEVER, has_staff_access=has_staff_access |
| 137 | + ) |
| 138 | + |
| 139 | + @ddt.data( |
| 140 | + # Correctness not visible to learners if due date in the future |
| 141 | + ("tomorrow", False, False), |
| 142 | + # Correctness is visible to learners if due date in the past |
| 143 | + ("yesterday", False, True), |
| 144 | + # Correctness is visible to learners if due date in the past (just) |
| 145 | + ("today", False, True), |
| 146 | + # Correctness is visible to learners if there is no due date |
| 147 | + (None, False, True), |
| 148 | + # Correctness is visible to staff if due date in the future |
| 149 | + ("tomorrow", True, True), |
| 150 | + # Correctness is visible to staff if due date in the past |
| 151 | + ("yesterday", True, True), |
| 152 | + # Correctness is visible to staff if there is no due date |
| 153 | + (None, True, True), |
| 154 | + ) |
| 155 | + @ddt.unpack |
| 156 | + def test_show_correctness_past_due(self, due_date_str, has_staff_access, expected_result): |
| 157 | + """ |
| 158 | + Test show_correctness="past_due" to ensure: |
| 159 | + * correctness is always visible to course staff |
| 160 | + * correctness is always visible to everyone if there is no due date |
| 161 | + * correctness is visible to learners after the due date, when there is a due date. |
| 162 | + """ |
| 163 | + if due_date_str is None: |
| 164 | + due_date = None |
| 165 | + else: |
| 166 | + due_date = getattr(self, due_date_str) |
| 167 | + assert ( |
| 168 | + scorable.ShowCorrectness.correctness_available( |
| 169 | + scorable.ShowCorrectness.PAST_DUE, due_date, has_staff_access |
| 170 | + ) == expected_result |
| 171 | + ) |
| 172 | + |
| 173 | + @ddt.data(True, False) |
| 174 | + def test_show_correctness_never_but_include_grade(self, has_staff_access): |
| 175 | + """ |
| 176 | + Test that show_correctness="never_but_include_grade" hides correctness from learners and course staff. |
| 177 | + """ |
| 178 | + assert not scorable.ShowCorrectness.correctness_available( |
| 179 | + show_correctness=scorable.ShowCorrectness.NEVER_BUT_INCLUDE_GRADE, has_staff_access=has_staff_access |
| 180 | + ) |
0 commit comments