From a5bf4a9d12bf026841b8afe932d239b8b3ae8e9e Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Sat, 30 Nov 2024 09:45:19 +0100 Subject: [PATCH] Fix usage of `exercise_key` without `ExerciseRegistry` Because the `exercise_key` is used for the `ExerciseRegistry` and the `CheckRegistry` we need to ensure the `CodeExercise` can be run with each individual one and both. --- .../exercise/_widget_code_exercise.py | 9 +++++++-- tests/test_code.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/scwidgets/exercise/_widget_code_exercise.py b/src/scwidgets/exercise/_widget_code_exercise.py index eba08a3..f211080 100644 --- a/src/scwidgets/exercise/_widget_code_exercise.py +++ b/src/scwidgets/exercise/_widget_code_exercise.py @@ -176,8 +176,13 @@ def __init__( "code and params do no match: " + compatibility_result ) - CheckableWidget.__init__(self, check_registry, exercise_key) - ExerciseWidget.__init__(self, exercise_registry, exercise_key) + name = kwargs.get("name", exercise_key) + CheckableWidget.__init__(self, check_registry, name) + if exercise_registry is not None: + ExerciseWidget.__init__(self, exercise_registry, exercise_key) + else: + # otherwise ExerciseWidget constructor will raise an error + ExerciseWidget.__init__(self, None, None) self._code = code self._output = CueOutput() diff --git a/tests/test_code.py b/tests/test_code.py index 5c399c9..529262b 100644 --- a/tests/test_code.py +++ b/tests/test_code.py @@ -355,3 +355,21 @@ def test_figure(self): assert isinstance(code_ex.figure, Figure) assert code_ex.output.figure is code_ex.figure assert code_ex.outputs[0] is code_ex.output + + def test_consrtuction_with_registries(self): + """Because the exercise key is used for the `ExerciseRegistry` and the + `CheckRegistry` we need to ensure the `CodeExercise` can be run with + each individual one and both""" + CodeExercise( + exercise_key="some_key", + check_registry=CheckRegistry(), + ) + CodeExercise( + exercise_key="some_key", + exercise_registry=ExerciseRegistry(), + ) + CodeExercise( + exercise_key="some_key", + check_registry=CheckRegistry(), + exercise_registry=ExerciseRegistry(), + )