Skip to content

Commit e978bbd

Browse files
committed
Update tests for model.time changes
Replace MagicMock(spec=Model) with real Model instances since mocks lack the new time attribute. Update assertions to use model.time instead of simulator.time. Add tests for time increment, step_duration, simulator attachment, and deprecation warning. Fix AgentSet constructor calls to use random= keyword argument.
1 parent 1128efe commit e978bbd

File tree

2 files changed

+94
-24
lines changed

2 files changed

+94
-24
lines changed

tests/test_devs.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def test_devs_simulator():
1414
simulator = DEVSimulator()
1515

1616
# setup
17-
model = MagicMock(spec=Model)
17+
model = Model()
1818
simulator.setup(model)
1919

2020
assert len(simulator.event_list) == 0
2121
assert simulator.model == model
22-
assert simulator.time == 0
22+
assert model.time == 0.0
2323

2424
# schedule_event_now
2525
fn1 = MagicMock()
@@ -43,38 +43,38 @@ def test_devs_simulator():
4343
simulator.run_for(0.8)
4444
fn1.assert_called_once()
4545
fn3.assert_called_once()
46-
assert simulator.time == 0.8
46+
assert model.time == 0.8
4747

4848
simulator.run_for(0.2)
4949
fn2.assert_called_once()
50-
assert simulator.time == 1.0
50+
assert model.time == 1.0
5151

5252
simulator.run_for(0.2)
53-
assert simulator.time == 1.2
53+
assert model.time == 1.2
5454

5555
with pytest.raises(ValueError):
5656
simulator.schedule_event_absolute(fn2, 0.5)
5757

5858
# step
5959
simulator = DEVSimulator()
60-
model = MagicMock(spec=Model)
60+
model = Model()
6161
simulator.setup(model)
6262

6363
fn = MagicMock()
6464
simulator.schedule_event_absolute(fn, 1.0)
6565
simulator.run_next_event()
6666
fn.assert_called_once()
67-
assert simulator.time == 1.0
67+
assert model.time == 1.0
6868
simulator.run_next_event()
69-
assert simulator.time == 1.0
69+
assert model.time == 1.0
7070

7171
simulator = DEVSimulator()
7272
with pytest.raises(Exception):
7373
simulator.run_next_event()
7474

7575
# cancel_event
7676
simulator = DEVSimulator()
77-
model = MagicMock(spec=Model)
77+
model = Model()
7878
simulator.setup(model)
7979
fn = MagicMock()
8080
event = simulator.schedule_event_relative(fn, 0.5)
@@ -85,7 +85,6 @@ def test_devs_simulator():
8585
simulator.reset()
8686
assert len(simulator.event_list) == 0
8787
assert simulator.model is None
88-
assert simulator.time == 0.0
8988

9089
# run without setup
9190
simulator = DEVSimulator()
@@ -94,15 +93,18 @@ def test_devs_simulator():
9493

9594
# setup with time advanced
9695
simulator = DEVSimulator()
97-
simulator.time = simulator.start_time + 1
98-
model = MagicMock(spec=Model)
99-
with pytest.raises(Exception):
96+
model = Model()
97+
model.time = 1.0 # Advance time before setup
98+
with pytest.raises(ValueError):
10099
simulator.setup(model)
101100

102101
# setup with event scheduled
103102
simulator = DEVSimulator()
104-
simulator.schedule_event_now(Mock())
105-
with pytest.raises(Exception):
103+
model = Model()
104+
simulator.event_list.add_event(
105+
SimulationEvent(1.0, Mock(), Priority.DEFAULT)
106+
)
107+
with pytest.raises(ValueError):
106108
simulator.setup(model)
107109

108110

@@ -111,7 +113,7 @@ def test_abm_simulator():
111113
simulator = ABMSimulator()
112114

113115
# setup
114-
model = MagicMock(spec=Model)
116+
model = Model()
115117
simulator.setup(model)
116118

117119
# schedule_event_next_tick
@@ -120,15 +122,25 @@ def test_abm_simulator():
120122
assert len(simulator.event_list) == 2
121123

122124
simulator.run_for(3)
123-
assert model.step.call_count == 3
124-
assert simulator.time == 3
125+
assert model.steps == 3
126+
assert model.time == 3.0
125127

126128
# run without setup
127129
simulator = ABMSimulator()
128130
with pytest.raises(Exception):
129131
simulator.run_until(10)
130132

131133

134+
def test_simulator_time_deprecation():
135+
"""Test that simulator.time emits deprecation warning."""
136+
simulator = DEVSimulator()
137+
model = Model()
138+
simulator.setup(model)
139+
140+
with pytest.warns(DeprecationWarning, match="simulator.time is deprecated"):
141+
_ = simulator.time
142+
143+
132144
def test_simulation_event():
133145
"""Tests for SimulationEvent class."""
134146
some_test_function = MagicMock()

tests/test_model.py

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,82 @@ def test_model_set_up():
1111
model = Model()
1212
assert model.running is True
1313
assert model.steps == 0
14+
assert model.time == 0.0
15+
assert model.step_duration == 1.0
16+
assert model._simulator is None
17+
1418
model.step()
1519
assert model.steps == 1
20+
assert model.time == 1.0
21+
22+
23+
def test_model_time_increment():
24+
"""Test that time increments correctly with steps."""
25+
model = Model()
26+
27+
for i in range(5):
28+
model.step()
29+
assert model.steps == i + 1
30+
assert model.time == float(i + 1)
31+
32+
33+
def test_model_step_duration():
34+
"""Test custom step_duration."""
35+
# Default step_duration
36+
model = Model()
37+
model.step()
38+
assert model.time == 1.0
39+
40+
# Custom step_duration
41+
model = Model(step_duration=0.25)
42+
assert model.step_duration == 0.25
43+
44+
model.step()
45+
assert model.steps == 1
46+
assert model.time == 0.25
47+
48+
model.step()
49+
assert model.steps == 2
50+
assert model.time == 0.5
51+
52+
# Larger step_duration
53+
model = Model(step_duration=10.0)
54+
model.step()
55+
assert model.time == 10.0
56+
model.step()
57+
assert model.time == 20.0
58+
59+
60+
def test_model_time_with_simulator():
61+
"""Test that simulator controls time when attached."""
62+
from mesa.experimental.devs.simulator import DEVSimulator
63+
64+
model = Model()
65+
simulator = DEVSimulator()
66+
simulator.setup(model)
67+
68+
# Simulator is now attached
69+
assert model._simulator is simulator
70+
71+
# Time should not auto-increment when simulator is attached
72+
# (In practice, the simulator controls stepping, but we can test the flag)
73+
model._user_step() # Call user step directly to avoid wrapped_step
74+
# Time unchanged because simulator controls it
1675

1776

1877
def test_running():
1978
"""Test Model is running."""
2079

2180
class TestModel(Model):
22-
steps = 0
23-
2481
def step(self):
25-
"""Increase steps until 10."""
82+
"""Stop at step 10."""
2683
if self.steps == 10:
2784
self.running = False
2885

2986
model = TestModel()
3087
model.run_model()
3188
assert model.steps == 10
89+
assert model.time == 10.0
3290

3391

3492
def test_seed(seed=23):
@@ -78,7 +136,7 @@ def test_reset_rng(newseed=42):
78136

79137

80138
def test_agent_types():
81-
"""Test Mode.agent_types property."""
139+
"""Test Model.agent_types property."""
82140

83141
class TestAgent(Agent):
84142
pass
@@ -102,8 +160,8 @@ class Sheep(Agent):
102160
wolf = Wolf(model)
103161
sheep = Sheep(model)
104162

105-
assert model.agents_by_type[Wolf] == AgentSet([wolf], model)
106-
assert model.agents_by_type[Sheep] == AgentSet([sheep], model)
163+
assert model.agents_by_type[Wolf] == AgentSet([wolf], random=model.random)
164+
assert model.agents_by_type[Sheep] == AgentSet([sheep], random=model.random)
107165
assert len(model.agents_by_type) == 2
108166

109167

0 commit comments

Comments
 (0)