|
1 | 1 | # ActivitySim |
2 | 2 | # See full license in LICENSE.txt. |
3 | 3 | import os |
| 4 | +import shutil |
4 | 5 | import subprocess |
5 | 6 | import sys |
6 | 7 | from pathlib import Path |
| 8 | +import tempfile |
7 | 9 |
|
8 | 10 | import pandas as pd |
9 | 11 | import pandas.testing as pdt |
10 | 12 | import pytest |
| 13 | +from pydantic import ValidationError |
11 | 14 |
|
12 | 15 | from activitysim.core import workflow |
13 | 16 |
|
@@ -175,6 +178,74 @@ def test_sandag_abm3_progressive(use_sharrow): |
175 | 178 | regress(out_dir, filename="final_joint_tour_participants.csv") |
176 | 179 |
|
177 | 180 |
|
| 181 | +def test_extension_settings_checker(): |
| 182 | + """Test that the extension settings checker works as expected.""" |
| 183 | + import activitysim.abm # register components # noqa: F401 |
| 184 | + |
| 185 | + out_dir = _test_path("output-extension-settings-checker") |
| 186 | + out_dir.mkdir(exist_ok=True) |
| 187 | + out_dir.joinpath(".gitignore").write_text("**\n") |
| 188 | + |
| 189 | + settings = dict( |
| 190 | + cleanup_pipeline_after_run=False, |
| 191 | + treat_warnings_as_errors=True, |
| 192 | + households_sample_size=100, |
| 193 | + chunk_size=0, |
| 194 | + use_shadow_pricing=True, |
| 195 | + ) |
| 196 | + tags = ["-hh100"] |
| 197 | + |
| 198 | + # create a copy of the resident settings directory in a temporary location |
| 199 | + # to avoid modifying the original settings files |
| 200 | + resident_configs_dir = Path(_example_path(r"configs/resident")) |
| 201 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 202 | + tmp_resident_configs_dir = Path(tmpdir) / "resident" |
| 203 | + tmp_resident_configs_dir.mkdir() |
| 204 | + for item in resident_configs_dir.iterdir(): |
| 205 | + if item.is_file(): |
| 206 | + shutil.copy(item, tmp_resident_configs_dir) |
| 207 | + |
| 208 | + # modify an extension settings file to include an invalid setting |
| 209 | + with open(tmp_resident_configs_dir / "av_ownership.yaml", "r") as f: |
| 210 | + av_ownership_settings = f.read() |
| 211 | + av_ownership_settings = av_ownership_settings.replace("LOGIT_TYPE: MNL", "LOGIT_TYPE: BAD") |
| 212 | + with open(tmp_resident_configs_dir / "av_ownership.yaml", "w") as f: |
| 213 | + f.write(av_ownership_settings) |
| 214 | + |
| 215 | + with open(tmp_resident_configs_dir / "av_ownership.yaml", "r") as f: |
| 216 | + av_ownership_settings_ = f.read() |
| 217 | + print("----") |
| 218 | + print(av_ownership_settings_) |
| 219 | + print("----") |
| 220 | + |
| 221 | + state = workflow.State.make_default( |
| 222 | + configs_dir=( |
| 223 | + _example_path(r"configs/common"), |
| 224 | + tmp_resident_configs_dir, |
| 225 | + ), |
| 226 | + data_dir=_example_path("data"), |
| 227 | + output_dir=out_dir, |
| 228 | + settings=settings, |
| 229 | + ) |
| 230 | + |
| 231 | + state.import_extensions("extensions") |
| 232 | + state.logging.config_logger() |
| 233 | + |
| 234 | + assert state.settings.models == EXPECTED_MODELS |
| 235 | + assert state.settings.chunk_size == 0 |
| 236 | + |
| 237 | + # step_name = EXPECTED_MODELS[0] |
| 238 | + |
| 239 | + with pytest.raises(ValidationError, match="1 validation error for AVOwnershipSettings"): |
| 240 | + for step_name in EXPECTED_MODELS[:5]: |
| 241 | + if step_name == "av_ownership": |
| 242 | + break |
| 243 | + # if we get here without a ValidationError, the settings checker is |
| 244 | + # NOT working correctly on the extensions, we should have raised |
| 245 | + # the validation error sooner |
| 246 | + state.run.by_name(step_name) |
| 247 | + |
| 248 | + |
178 | 249 | if __name__ == "__main__": |
179 | 250 | # run_test_sandag_abm3(multiprocess=True) |
180 | 251 | test_sandag_abm3_progressive(use_sharrow=False) |
|
0 commit comments