-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docstring for jupyterviz make_user_input that documents supported…
… inputs (#1784) * Add docstring for make_user_input that documents supported inputs * Use field name as user input fallback label; error on unsupported type * Preliminary unit tests for jupyter viz make_user_input method * Remove unused variable flagged by ruff lint
- Loading branch information
Showing
2 changed files
with
82 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
from mesa.experimental.jupyter_viz import make_user_input | ||
|
||
|
||
class TestMakeUserInput(unittest.TestCase): | ||
def test_unsupported_type(self): | ||
"""unsupported input type should raise ValueError""" | ||
# bogus type | ||
with self.assertRaisesRegex(ValueError, "not a supported input type"): | ||
make_user_input(10, "input", {"type": "bogus"}) | ||
# no type is specified | ||
with self.assertRaisesRegex(ValueError, "not a supported input type"): | ||
make_user_input(10, "input", {}) | ||
|
||
@patch("mesa.experimental.jupyter_viz.solara") | ||
def test_slider_int(self, mock_solara): | ||
value = 10 | ||
name = "num_agents" | ||
options = { | ||
"type": "SliderInt", | ||
"label": "number of agents", | ||
"min": 10, | ||
"max": 20, | ||
"step": 1, | ||
} | ||
make_user_input(value, name, options) | ||
mock_solara.SliderInt.assert_called_with( | ||
options["label"], | ||
value=value, | ||
min=options["min"], | ||
max=options["max"], | ||
step=options["step"], | ||
) | ||
|
||
@patch("mesa.experimental.jupyter_viz.solara") | ||
def test_label_fallback(self, mock_solara): | ||
"""name should be used as fallback label""" | ||
value = 10 | ||
name = "num_agents" | ||
options = { | ||
"type": "SliderInt", | ||
} | ||
make_user_input(value, name, options) | ||
mock_solara.SliderInt.assert_called_with( | ||
name, value=value, min=None, max=None, step=None | ||
) |