|
1 | 1 | import unittest
|
2 |
| -from unittest.mock import patch |
| 2 | +from unittest.mock import Mock, patch |
3 | 3 |
|
4 |
| -from mesa.experimental.jupyter_viz import make_user_input |
| 4 | +import solara |
| 5 | + |
| 6 | +from mesa.experimental.jupyter_viz import JupyterViz, make_user_input |
5 | 7 |
|
6 | 8 |
|
7 | 9 | class TestMakeUserInput(unittest.TestCase):
|
@@ -46,3 +48,54 @@ def test_label_fallback(self, mock_solara):
|
46 | 48 | mock_solara.SliderInt.assert_called_with(
|
47 | 49 | name, value=value, min=None, max=None, step=None
|
48 | 50 | )
|
| 51 | + |
| 52 | + |
| 53 | +class TestJupyterViz(unittest.TestCase): |
| 54 | + @patch("mesa.experimental.jupyter_viz.make_space") |
| 55 | + def test_call_space_drawer(self, mock_make_space): |
| 56 | + mock_model_class = Mock() |
| 57 | + agent_portrayal = { |
| 58 | + "Shape": "circle", |
| 59 | + "color": "gray", |
| 60 | + } |
| 61 | + # initialize with space drawer unspecified (use default) |
| 62 | + # component must be rendered for code to run |
| 63 | + solara.render( |
| 64 | + JupyterViz( |
| 65 | + model_class=mock_model_class, |
| 66 | + model_params={}, |
| 67 | + agent_portrayal=agent_portrayal, |
| 68 | + ) |
| 69 | + ) |
| 70 | + # should call default method with class instance and agent portrayal |
| 71 | + mock_make_space.assert_called_with( |
| 72 | + mock_model_class.return_value, agent_portrayal |
| 73 | + ) |
| 74 | + |
| 75 | + # specify no space should be drawn; any false value should work |
| 76 | + for falsy_value in [None, False, 0]: |
| 77 | + mock_make_space.reset_mock() |
| 78 | + solara.render( |
| 79 | + JupyterViz( |
| 80 | + model_class=mock_model_class, |
| 81 | + model_params={}, |
| 82 | + agent_portrayal=agent_portrayal, |
| 83 | + space_drawer=falsy_value, |
| 84 | + ) |
| 85 | + ) |
| 86 | + # should call default method with class instance and agent portrayal |
| 87 | + assert mock_make_space.call_count == 0 |
| 88 | + |
| 89 | + # specify a custom space method |
| 90 | + altspace_drawer = Mock() |
| 91 | + solara.render( |
| 92 | + JupyterViz( |
| 93 | + model_class=mock_model_class, |
| 94 | + model_params={}, |
| 95 | + agent_portrayal=agent_portrayal, |
| 96 | + space_drawer=altspace_drawer, |
| 97 | + ) |
| 98 | + ) |
| 99 | + altspace_drawer.assert_called_with( |
| 100 | + mock_model_class.return_value, agent_portrayal |
| 101 | + ) |
0 commit comments