Skip to content

Commit f8eafc6

Browse files
committed
Tests: Adding test for CodeExercise with ExerciseRegistry
1 parent e480910 commit f8eafc6

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

tests/test_code.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Callable, List
1+
import os
2+
from typing import Callable, List, Optional
23

34
import numpy as np
45
import pytest
@@ -8,7 +9,7 @@
89
from scwidgets.check import Check, CheckRegistry, ChecksResult
910
from scwidgets.code import CodeInput
1011
from scwidgets.cue import CueObject
11-
from scwidgets.exercise import CodeExercise
12+
from scwidgets.exercise import CodeExercise, ExerciseRegistry
1213

1314
from .test_check import multi_param_check, single_param_check
1415

@@ -76,7 +77,7 @@ def test_invalid_code_theme_raises_error(self):
7677

7778
def get_code_exercise(
7879
checks: List[Check],
79-
code: Callable = None,
80+
code: Optional[Callable] = None,
8081
include_checks=True,
8182
include_params=True,
8283
tunable_params=False,
@@ -221,3 +222,35 @@ def test_erroneous_run_code(self, code_ex):
221222
match="NameError in code input: name 'bug' is not defined.*",
222223
):
223224
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

Comments
 (0)