Skip to content

Commit be2e704

Browse files
authored
tests: Resolved "Missing Random Number Generator" warnings (#2911)
Resolved "Missing Random Number Generator" warnings in the test suite by explicitly passing a random number generator to grid instantiations.
1 parent 41f3028 commit be2e704

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

tests/test_solara_viz.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test Solara visualizations."""
22

3+
import random
34
import re
45
import unittest
56

@@ -204,6 +205,7 @@ def drawer(model):
204205
voronoi_model = mesa.Model()
205206
voronoi_model.grid = mesa.discrete_space.VoronoiGrid(
206207
centroids_coordinates=[(0, 1), (0, 0), (1, 0)],
208+
random=random.Random(42),
207209
)
208210
solara.render(
209211
SolaraViz(voronoi_model, components=[make_mpl_space_component(agent_portrayal)])

tests/test_space_drawer.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test space drawer classes for various grid types."""
22

3+
import random
34
from unittest.mock import MagicMock, patch
45

56
import altair as alt
@@ -20,12 +21,12 @@
2021

2122
@pytest.fixture
2223
def orthogonal_grid(): # noqa: D103
23-
return OrthogonalMooreGrid([5, 5])
24+
return OrthogonalMooreGrid([5, 5], random=random.Random(42))
2425

2526

2627
@pytest.fixture
2728
def hex_grid(): # noqa: D103
28-
return HexGrid([5, 5])
29+
return HexGrid([5, 5], random=random.Random(42))
2930

3031

3132
@pytest.fixture
@@ -38,13 +39,13 @@ def network_grid(): # noqa: D103
3839
G = nx.Graph() # noqa: N806
3940
G.add_nodes_from([0, 1, 2])
4041
G.add_edges_from([(0, 1), (1, 2)])
41-
return Network(G)
42+
return Network(G, random=random.Random(42))
4243

4344

4445
@pytest.fixture
4546
def voronoi_space(): # noqa: D103
4647
points = [(0, 0), (1, 0), (0, 1), (1, 1)]
47-
return VoronoiGrid(points)
48+
return VoronoiGrid(points, random=random.Random(42))
4849

4950

5051
class TestOrthogonalSpaceDrawer:
@@ -288,7 +289,7 @@ def test_get_clipped_segments(self, voronoi_space): # noqa: D102
288289

289290
class TestEdgeCases: # noqa: D101
290291
def test_single_point_voronoi_grid(self): # noqa: D102
291-
single_point_voronoi = VoronoiGrid([(0.5, 0.5)])
292+
single_point_voronoi = VoronoiGrid([(0.5, 0.5)], random=random.Random(42))
292293
drawer = VoronoiSpaceDrawer(single_point_voronoi)
293294
assert drawer.viz_xmin is not None
294295
assert drawer.viz_xmax is not None
@@ -300,13 +301,13 @@ def test_continuous_space_zero_size(self): # noqa: D102
300301

301302
def test_network_empty_graph(self): # noqa: D102
302303
empty_graph = nx.Graph()
303-
network = Network(empty_graph)
304+
network = Network(empty_graph, random=random.Random(42))
304305
drawer = NetworkSpaceDrawer(network)
305306
assert len(drawer.pos) == 0
306307
assert drawer.s_default == 1 # Default when no nodes
307308

308309
def test_hex_grid_single_cell(self): # noqa: D102
309-
grid = HexGrid([1, 1])
310+
grid = HexGrid([1, 1], random=random.Random(42))
310311
drawer = HexSpaceDrawer(grid)
311312
assert len(drawer.hexagons) == 1
312313
assert drawer.s_default == (180 / 1) ** 2

tests/test_space_renderer.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test cases for the SpaceRenderer class in Mesa."""
22

3+
import random
34
import re
45
from unittest.mock import MagicMock, patch
56

@@ -39,7 +40,9 @@ class CustomModel(mesa.Model):
3940

4041
def __init__(self, seed=None): # noqa: D107
4142
super().__init__(seed=seed)
42-
self.grid = mesa.discrete_space.OrthogonalMooreGrid([2, 2])
43+
self.grid = mesa.discrete_space.OrthogonalMooreGrid(
44+
[2, 2], random=random.Random(42)
45+
)
4346
self.layer = PropertyLayer("test", [2, 2], default_value=0)
4447

4548
self.grid.add_property_layer(self.layer)
@@ -59,16 +62,22 @@ def test_backend_selection():
5962
@pytest.mark.parametrize(
6063
"grid,expected_drawer",
6164
[
62-
(OrthogonalMooreGrid([2, 2]), OrthogonalSpaceDrawer),
65+
(
66+
OrthogonalMooreGrid([2, 2], random=random.Random(42)),
67+
OrthogonalSpaceDrawer,
68+
),
6369
(SingleGrid(width=2, height=2, torus=False), OrthogonalSpaceDrawer),
6470
(MultiGrid(width=2, height=2, torus=False), OrthogonalSpaceDrawer),
65-
(HexGrid([2, 2]), HexSpaceDrawer),
71+
(HexGrid([2, 2], random=random.Random(42)), HexSpaceDrawer),
6672
(HexSingleGrid(width=2, height=2, torus=False), HexSpaceDrawer),
6773
(HexMultiGrid(width=2, height=2, torus=False), HexSpaceDrawer),
68-
(Network(G=MagicMock()), NetworkSpaceDrawer),
74+
(Network(G=MagicMock(), random=random.Random(42)), NetworkSpaceDrawer),
6975
(NetworkGrid(g=MagicMock()), NetworkSpaceDrawer),
7076
(ContinuousSpace(x_max=2, y_max=2, torus=False), ContinuousSpaceDrawer),
71-
(VoronoiGrid([[0, 0], [1, 1]]), VoronoiSpaceDrawer),
77+
(
78+
VoronoiGrid([[0, 0], [1, 1]], random=random.Random(42)),
79+
VoronoiSpaceDrawer,
80+
),
7281
],
7382
)
7483
def test_space_drawer_selection(grid, expected_drawer):
@@ -91,14 +100,16 @@ def test_map_coordinates():
91100
# same for orthogonal grids
92101
assert np.array_equal(mapped["loc"], arr)
93102

94-
with patch.object(model, "grid", new=HexGrid([2, 2])):
103+
with patch.object(model, "grid", new=HexGrid([2, 2], random=random.Random(42))):
95104
sr = SpaceRenderer(model)
96105
mapped = sr._map_coordinates(args)
97106

98107
assert not np.array_equal(mapped["loc"], arr)
99108
assert mapped["loc"].shape == arr.shape
100109

101-
with patch.object(model, "grid", new=Network(G=MagicMock())):
110+
with patch.object(
111+
model, "grid", new=Network(G=MagicMock(), random=random.Random(42))
112+
):
102113
sr = SpaceRenderer(model)
103114
# Patch the space_drawer.pos to provide a mapping for the test
104115
sr.space_drawer.pos = {0: (0, 0), 1: (1, 1), 2: (2, 2), 3: (3, 3)}

0 commit comments

Comments
 (0)