|
1 | 1 | from collections.abc import Sequence |
| 2 | +from unittest.mock import MagicMock, patch |
2 | 3 |
|
3 | 4 | import xarray as xr |
4 | | -from pytest import fixture |
| 5 | +from pytest import fixture, raises |
5 | 6 |
|
6 | 7 |
|
7 | 8 | @fixture |
@@ -134,3 +135,45 @@ def test_factory_smoke_test(model, technologies, tmp_path): |
134 | 135 |
|
135 | 136 | assert isinstance(subsector, Subsector) |
136 | 137 | 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