Skip to content

Commit e480910

Browse files
authored
Fixing unlinked traits in WCI due to invalid code_theme (PR #54)
Fixes #53
2 parents 4a1737e + b1ba4cd commit e480910

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

src/scwidgets/code/_widget_code_input.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ class CodeInput(WidgetCodeInput):
1313
Small wrapper around WidgetCodeInput that controls the output
1414
"""
1515

16+
valid_code_themes = ["nord", "solarizedLight", "basicLight"]
17+
1618
def __init__(
1719
self,
1820
function: Optional[types.FunctionType] = None,
1921
function_name: Optional[str] = None,
2022
function_parameters: Optional[str] = None,
2123
docstring: Optional[str] = None,
2224
function_body: Optional[str] = None,
23-
code_theme: str = "default",
25+
code_theme: str = "basicLight",
2426
):
2527
if function is not None:
2628
function_name = (
@@ -46,6 +48,14 @@ def __init__(
4648
function_name, function_parameters, docstring, function_body, code_theme
4749
)
4850

51+
# this list is retrieved from
52+
# https://github.com/osscar-org/widget-code-input/blob/eb10ca0baee65dd3bf62c9ec5d9cb2f152932ff5/js/widget.js#L249-L253
53+
if code_theme not in CodeInput.valid_code_themes:
54+
raise ValueError(
55+
f"Given code_theme {code_theme!r} invalid. Please use one of "
56+
f"the values {CodeInput.valid_code_themes}"
57+
)
58+
4959
@property
5060
def function(self) -> types.FunctionType:
5161
"""

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def _selenium_driver(nb_path):
143143
# )
144144
# )
145145
restart_kernel_button = None
146-
waiting_time = 10
146+
waiting_time = 20
147147
start = time.time()
148148

149149
while restart_kernel_button is None and time.time() - start < waiting_time:
@@ -219,6 +219,7 @@ def _selenium_driver(nb_path):
219219
(By.CLASS_NAME, "jp-Notebook-ExecutionIndicator"), "data-status", "idle"
220220
)
221221
)
222+
time.sleep(1)
222223

223224
return selenium
224225

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# cell_metadata_filter: -all
5+
# text_representation:
6+
# extension: .py
7+
# format_name: light
8+
# format_version: '1.5'
9+
# jupytext_version: 1.15.0
10+
# kernelspec:
11+
# display_name: Python 3 (ipykernel)
12+
# language: python
13+
# name: python3
14+
# ---
15+
16+
# +
17+
import time
18+
19+
import scwidgets
20+
from scwidgets.code import CodeInput
21+
22+
# -
23+
24+
scwidgets.get_css_style()
25+
26+
# Test 1:
27+
# -------
28+
# Test if CodeInput traits are updading the widget view
29+
30+
31+
# +
32+
def foo():
33+
return "init"
34+
35+
36+
ci = CodeInput(foo)
37+
ci
38+
# -
39+
40+
time.sleep(1)
41+
ci.function_body = """return 'change'"""

tests/test_code.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ def test_get_code(self):
6767
):
6868
CodeInput.get_code(lambda x: x)
6969

70+
def test_invalid_code_theme_raises_error(self):
71+
with pytest.raises(
72+
ValueError, match=r"Given code_theme 'invalid_theme' invalid.*"
73+
):
74+
CodeInput(TestCodeInput.mock_function_1, code_theme="invalid_theme")
75+
7076

7177
def get_code_exercise(
7278
checks: List[Check],

tests/test_widgets.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,24 @@ def test_privacy_policy(selenium_driver):
249249
).click()
250250

251251

252+
def test_scwidgets_code_input(selenium_driver):
253+
"""
254+
Tests the widget of the module code
255+
256+
:param selenium_driver: see conftest.py
257+
"""
258+
driver = selenium_driver("tests/notebooks/widget_scwidgets_code_input.ipynb")
259+
260+
nb_cells = NotebookCellList(driver)
261+
# Test 1:
262+
# -------
263+
264+
# Tests if change in function_body changed the widget view
265+
time.sleep(2)
266+
code_input_lines = nb_cells[2].find_elements(By.CLASS_NAME, CODE_MIRROR_CLASS_NAME)
267+
assert "return 'change'" in code_input_lines[-1].text
268+
269+
252270
class TestExerciseWidgets:
253271
prefix = "pytest"
254272

0 commit comments

Comments
 (0)