|
4 | 4 | import ipyvuetify as vw |
5 | 5 | import solara |
6 | 6 |
|
| 7 | +import mesa |
7 | 8 | from mesa.experimental.jupyter_viz import JupyterViz, Slider, UserInputs |
8 | 9 |
|
9 | 10 |
|
@@ -81,59 +82,61 @@ def Test(user_params): |
81 | 82 | assert slider_int.step is None |
82 | 83 |
|
83 | 84 |
|
84 | | -class TestJupyterViz(unittest.TestCase): |
85 | | - @patch("mesa.experimental.components.matplotlib.SpaceMatplotlib") |
86 | | - def test_call_space_drawer(self, mock_space_matplotlib): |
87 | | - mock_model_class = Mock() |
88 | | - mock_model_class.__name__ = "MockModelClass" |
89 | | - agent_portrayal = { |
90 | | - "Shape": "circle", |
91 | | - "color": "gray", |
92 | | - } |
93 | | - current_step = 0 |
94 | | - seed = 0 |
95 | | - dependencies = [current_step, seed] |
96 | | - # initialize with space drawer unspecified (use default) |
97 | | - # component must be rendered for code to run |
98 | | - solara.render( |
99 | | - JupyterViz( |
100 | | - model_class=mock_model_class, |
101 | | - model_params={}, |
102 | | - agent_portrayal=agent_portrayal, |
103 | | - ) |
| 85 | +def test_call_space_drawer(mocker): |
| 86 | + mock_space_matplotlib = mocker.patch("mesa.experimental.components.matplotlib.SpaceMatplotlib") |
| 87 | + |
| 88 | + model = mesa.Model() |
| 89 | + mocker.patch.object(mesa.Model, "__new__", return_value=model) |
| 90 | + mocker.patch.object(mesa.Model, "__init__", return_value=None) |
| 91 | + |
| 92 | + agent_portrayal = { |
| 93 | + "Shape": "circle", |
| 94 | + "color": "gray", |
| 95 | + } |
| 96 | + current_step = 0 |
| 97 | + seed = 0 |
| 98 | + dependencies = [current_step, seed] |
| 99 | + # initialize with space drawer unspecified (use default) |
| 100 | + # component must be rendered for code to run |
| 101 | + solara.render( |
| 102 | + JupyterViz( |
| 103 | + model_class=mesa.Model, |
| 104 | + model_params={}, |
| 105 | + agent_portrayal=agent_portrayal, |
104 | 106 | ) |
105 | | - # should call default method with class instance and agent portrayal |
106 | | - mock_space_matplotlib.assert_called_with( |
107 | | - mock_model_class.return_value, agent_portrayal, dependencies=dependencies |
108 | | - ) |
109 | | - |
110 | | - # specify no space should be drawn; any false value should work |
111 | | - for falsy_value in [None, False, 0]: |
112 | | - mock_space_matplotlib.reset_mock() |
113 | | - solara.render( |
114 | | - JupyterViz( |
115 | | - model_class=mock_model_class, |
116 | | - model_params={}, |
117 | | - agent_portrayal=agent_portrayal, |
118 | | - space_drawer=falsy_value, |
119 | | - ) |
120 | | - ) |
121 | | - # should call default method with class instance and agent portrayal |
122 | | - assert mock_space_matplotlib.call_count == 0 |
123 | | - |
124 | | - # specify a custom space method |
125 | | - altspace_drawer = Mock() |
| 107 | + ) |
| 108 | + # should call default method with class instance and agent portrayal |
| 109 | + mock_space_matplotlib.assert_called_with( |
| 110 | + model, agent_portrayal, dependencies=dependencies |
| 111 | + ) |
| 112 | + |
| 113 | + # specify no space should be drawn; any false value should work |
| 114 | + for falsy_value in [None, False, 0]: |
| 115 | + mock_space_matplotlib.reset_mock() |
126 | 116 | solara.render( |
127 | 117 | JupyterViz( |
128 | | - model_class=mock_model_class, |
| 118 | + model_class=mesa.Model, |
129 | 119 | model_params={}, |
130 | 120 | agent_portrayal=agent_portrayal, |
131 | | - space_drawer=altspace_drawer, |
| 121 | + space_drawer=falsy_value, |
132 | 122 | ) |
133 | 123 | ) |
134 | | - altspace_drawer.assert_called_with( |
135 | | - mock_model_class.return_value, agent_portrayal |
| 124 | + # should call default method with class instance and agent portrayal |
| 125 | + assert mock_space_matplotlib.call_count == 0 |
| 126 | + |
| 127 | + # specify a custom space method |
| 128 | + altspace_drawer = Mock() |
| 129 | + solara.render( |
| 130 | + JupyterViz( |
| 131 | + model_class=mesa.Model, |
| 132 | + model_params={}, |
| 133 | + agent_portrayal=agent_portrayal, |
| 134 | + space_drawer=altspace_drawer, |
136 | 135 | ) |
| 136 | + ) |
| 137 | + altspace_drawer.assert_called_with( |
| 138 | + model, agent_portrayal |
| 139 | + ) |
137 | 140 |
|
138 | 141 |
|
139 | 142 | def test_slider(): |
|
0 commit comments