Skip to content

Commit d295f96

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 64f2216 commit d295f96

File tree

2 files changed

+91
-24
lines changed

2 files changed

+91
-24
lines changed

tests/test_devs.py

Lines changed: 28 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,16 @@ 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(SimulationEvent(1.0, Mock(), Priority.DEFAULT))
105+
with pytest.raises(ValueError):
106106
simulator.setup(model)
107107

108108

@@ -111,7 +111,7 @@ def test_abm_simulator():
111111
simulator = ABMSimulator()
112112

113113
# setup
114-
model = MagicMock(spec=Model)
114+
model = Model()
115115
simulator.setup(model)
116116

117117
# schedule_event_next_tick
@@ -120,15 +120,25 @@ def test_abm_simulator():
120120
assert len(simulator.event_list) == 2
121121

122122
simulator.run_for(3)
123-
assert model.step.call_count == 3
124-
assert simulator.time == 3
123+
assert model.steps == 3
124+
assert model.time == 3.0
125125

126126
# run without setup
127127
simulator = ABMSimulator()
128128
with pytest.raises(Exception):
129129
simulator.run_until(10)
130130

131131

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

tests/test_model.py

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44

55
from mesa.agent import Agent, AgentSet
6+
from mesa.experimental.devs.simulator import DEVSimulator
67
from mesa.model import Model
78

89

@@ -11,24 +12,80 @@ def test_model_set_up():
1112
model = Model()
1213
assert model.running is True
1314
assert model.steps == 0
15+
assert model.time == 0.0
16+
assert model.step_duration == 1.0
17+
assert model._simulator is None
18+
19+
model.step()
20+
assert model.steps == 1
21+
assert model.time == 1.0
22+
23+
24+
def test_model_time_increment():
25+
"""Test that time increments correctly with steps."""
26+
model = Model()
27+
28+
for i in range(5):
29+
model.step()
30+
assert model.steps == i + 1
31+
assert model.time == float(i + 1)
32+
33+
34+
def test_model_step_duration():
35+
"""Test custom step_duration."""
36+
# Default step_duration
37+
model = Model()
38+
model.step()
39+
assert model.time == 1.0
40+
41+
# Custom step_duration
42+
model = Model(step_duration=0.25)
43+
assert model.step_duration == 0.25
44+
1445
model.step()
1546
assert model.steps == 1
47+
assert model.time == 0.25
48+
49+
model.step()
50+
assert model.steps == 2
51+
assert model.time == 0.5
52+
53+
# Larger step_duration
54+
model = Model(step_duration=10.0)
55+
model.step()
56+
assert model.time == 10.0
57+
model.step()
58+
assert model.time == 20.0
59+
60+
61+
def test_model_time_with_simulator():
62+
"""Test that simulator controls time when attached."""
63+
model = Model()
64+
simulator = DEVSimulator()
65+
simulator.setup(model)
66+
67+
# Simulator is now attached
68+
assert model._simulator is simulator
69+
70+
# Time should not auto-increment when simulator is attached
71+
# (In practice, the simulator controls stepping, but we can test the flag)
72+
model._user_step() # Call user step directly to avoid wrapped_step
73+
# Time unchanged because simulator controls it
1674

1775

1876
def test_running():
1977
"""Test Model is running."""
2078

2179
class TestModel(Model):
22-
steps = 0
23-
2480
def step(self):
25-
"""Increase steps until 10."""
81+
"""Stop at step 10."""
2682
if self.steps == 10:
2783
self.running = False
2884

2985
model = TestModel()
3086
model.run_model()
3187
assert model.steps == 10
88+
assert model.time == 10.0
3289

3390

3491
def test_seed(seed=23):
@@ -78,7 +135,7 @@ def test_reset_rng(newseed=42):
78135

79136

80137
def test_agent_types():
81-
"""Test Mode.agent_types property."""
138+
"""Test Model.agent_types property."""
82139

83140
class TestAgent(Agent):
84141
pass
@@ -102,8 +159,8 @@ class Sheep(Agent):
102159
wolf = Wolf(model)
103160
sheep = Sheep(model)
104161

105-
assert model.agents_by_type[Wolf] == AgentSet([wolf], model)
106-
assert model.agents_by_type[Sheep] == AgentSet([sheep], model)
162+
assert model.agents_by_type[Wolf] == AgentSet([wolf], random=model.random)
163+
assert model.agents_by_type[Sheep] == AgentSet([sheep], random=model.random)
107164
assert len(model.agents_by_type) == 2
108165

109166

0 commit comments

Comments
 (0)