Skip to content

Commit c6bcc6c

Browse files
committed
Switch test_call_space_drawer to using pytest
This is because the unittest version is not sufficient to handle patching the `__new__` method.
1 parent 29f6194 commit c6bcc6c

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ dev = [
5555
"pytest >= 4.6",
5656
"pytest-cov",
5757
"sphinx",
58+
"pytest-mock",
5859
]
5960
docs = [
6061
"sphinx",

tests/test_jupyter_viz.py

+48-45
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ipyvuetify as vw
55
import solara
66

7+
import mesa
78
from mesa.experimental.jupyter_viz import JupyterViz, Slider, UserInputs
89

910

@@ -81,59 +82,61 @@ def Test(user_params):
8182
assert slider_int.step is None
8283

8384

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,
104106
)
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()
126116
solara.render(
127117
JupyterViz(
128-
model_class=mock_model_class,
118+
model_class=mesa.Model,
129119
model_params={},
130120
agent_portrayal=agent_portrayal,
131-
space_drawer=altspace_drawer,
121+
space_drawer=falsy_value,
132122
)
133123
)
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,
136135
)
136+
)
137+
altspace_drawer.assert_called_with(
138+
model, agent_portrayal
139+
)
137140

138141

139142
def test_slider():

0 commit comments

Comments
 (0)