|
1 | | -from typing import Callable, List |
| 1 | +import os |
| 2 | +from typing import Callable, List, Optional |
2 | 3 |
|
3 | 4 | import numpy as np |
4 | 5 | import pytest |
|
8 | 9 | from scwidgets.check import Check, CheckRegistry, ChecksResult |
9 | 10 | from scwidgets.code import CodeInput |
10 | 11 | from scwidgets.cue import CueObject |
11 | | -from scwidgets.exercise import CodeExercise |
| 12 | +from scwidgets.exercise import CodeExercise, ExerciseRegistry |
12 | 13 |
|
13 | 14 | from .test_check import multi_param_check, single_param_check |
14 | 15 |
|
@@ -76,7 +77,7 @@ def test_invalid_code_theme_raises_error(self): |
76 | 77 |
|
77 | 78 | def get_code_exercise( |
78 | 79 | checks: List[Check], |
79 | | - code: Callable = None, |
| 80 | + code: Optional[Callable] = None, |
80 | 81 | include_checks=True, |
81 | 82 | include_params=True, |
82 | 83 | tunable_params=False, |
@@ -221,3 +222,35 @@ def test_erroneous_run_code(self, code_ex): |
221 | 222 | match="NameError in code input: name 'bug' is not defined.*", |
222 | 223 | ): |
223 | 224 | code_ex.run_code(**code_ex.parameters) |
| 225 | + |
| 226 | + @pytest.mark.parametrize( |
| 227 | + "function", |
| 228 | + [ |
| 229 | + None, |
| 230 | + TestCodeInput.mock_function_1, |
| 231 | + ], |
| 232 | + ) |
| 233 | + def test_save_registry(self, function): |
| 234 | + """ |
| 235 | + Verifies that the CodeExercise works with an answer_registry. |
| 236 | + """ |
| 237 | + |
| 238 | + def print_success(code_ex: CodeExercise | None): |
| 239 | + code_ex.cue_outputs[0].display_object = "Success" |
| 240 | + |
| 241 | + cue_output = CueObject("Not initialized") |
| 242 | + exercise_registry = ExerciseRegistry() |
| 243 | + |
| 244 | + code_ex = CodeExercise( |
| 245 | + code=function, |
| 246 | + parameters={"parameter": fixed(5)}, |
| 247 | + exercise_registry=exercise_registry, |
| 248 | + exercise_key="test_save_registry_ex", |
| 249 | + cue_outputs=[cue_output], |
| 250 | + update_func=print_success, |
| 251 | + ) |
| 252 | + |
| 253 | + exercise_registry._student_name_text.value = "test_save_registry-student_name" |
| 254 | + exercise_registry.create_new_file() |
| 255 | + code_ex._save_button.click() |
| 256 | + os.remove("test_save_registry-student_name.json") |
0 commit comments