Skip to content

Commit 2aa8ea9

Browse files
committed
Add test_factory_constraints_passed_to_agents
1 parent 19c9b71 commit 2aa8ea9

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

tests/test_subsector.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from typing import Sequence, Text
2+
from unittest.mock import MagicMock, patch
23

34
import xarray as xr
4-
from pytest import fixture
5+
from pytest import fixture, raises
56

67

78
@fixture
@@ -134,3 +135,45 @@ def test_factory_smoke_test(model, technologies, tmp_path):
134135

135136
assert isinstance(subsector, Subsector)
136137
assert len(subsector.agents) == 2
138+
139+
140+
def test_factory_constraints_passed_to_agents(model, technologies, tmp_path):
141+
from muse import examples
142+
from muse.readers.toml import read_settings
143+
from muse.sectors.subsector import Subsector
144+
145+
examples.copy_model(model, tmp_path)
146+
settings = read_settings(tmp_path / "model" / "settings.toml")
147+
148+
# The constraints in the settings are not none
149+
assert len(settings.sectors.residential.subsectors.retro_and_new.constraints) > 0
150+
151+
class BreakException(Exception):
152+
pass
153+
154+
_withness = MagicMock()
155+
156+
def agent_factory(*args, **kwargs):
157+
_withness(*args, **kwargs)
158+
raise BreakException()
159+
160+
# We asses they are indeed passed to the agents factory
161+
with patch("muse.agents.agents_factory", new=agent_factory):
162+
with raises(BreakException):
163+
Subsector.factory(
164+
settings.sectors.residential.subsectors.retro_and_new, technologies
165+
)
166+
assert (
167+
_withness.call_args[1]["constraints"]
168+
== settings.sectors.residential.subsectors.retro_and_new.constraints
169+
)
170+
171+
# But if there are no constraints, we pass an empty tuple
172+
settings.sectors.residential.subsectors.retro_and_new.constraints.clear()
173+
_withness.reset_mock()
174+
with patch("muse.agents.agents_factory", new=agent_factory):
175+
with raises(BreakException):
176+
Subsector.factory(
177+
settings.sectors.residential.subsectors.retro_and_new, technologies
178+
)
179+
assert tuple(_withness.call_args[1]["constraints"]) == ()

0 commit comments

Comments
 (0)