|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +# ActivitySim |
| 4 | +# See full license in LICENSE.txt. |
| 5 | +import importlib.resources |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | +from shutil import copytree |
| 9 | + |
| 10 | +import pandas as pd |
| 11 | +import pandas.testing as pdt |
| 12 | +import yaml |
| 13 | + |
| 14 | + |
| 15 | +def update_settings(settings_file, key, value): |
| 16 | + with open(settings_file, "r") as f: |
| 17 | + settings = yaml.safe_load(f) |
| 18 | + f.close() |
| 19 | + |
| 20 | + settings[key] = value |
| 21 | + |
| 22 | + with open(settings_file, "w") as f: |
| 23 | + yaml.safe_dump(settings, f) |
| 24 | + f.close() |
| 25 | + |
| 26 | + |
| 27 | +def test_trace_ids_have_same_hash(): |
| 28 | + def example_path(dirname): |
| 29 | + resource = os.path.join("examples", "prototype_mtc", dirname) |
| 30 | + return str(importlib.resources.files("activitysim").joinpath(resource)) |
| 31 | + |
| 32 | + def test_path(dirname): |
| 33 | + return os.path.join(os.path.dirname(__file__), dirname) |
| 34 | + |
| 35 | + new_configs_dir = test_path("configs") |
| 36 | + new_mp_configs_dir = test_path("configs_mp") |
| 37 | + new_settings_file = os.path.join(new_configs_dir, "settings.yaml") |
| 38 | + copytree(example_path("configs"), new_configs_dir) |
| 39 | + copytree(example_path("configs_mp"), new_mp_configs_dir) |
| 40 | + |
| 41 | + update_settings( |
| 42 | + new_settings_file, "trace_hh_id", 1932009 |
| 43 | + ) # Household in the prototype_mtc example with 11 people |
| 44 | + |
| 45 | + def check_csv_suffix(directory): |
| 46 | + suffix = None |
| 47 | + mismatched_files = [] |
| 48 | + for root, dirs, files in os.walk(directory): |
| 49 | + for filename in files: |
| 50 | + if filename.lower().endswith(".csv"): |
| 51 | + file_suffix = filename[-10:] |
| 52 | + if suffix is None: |
| 53 | + suffix = file_suffix |
| 54 | + elif file_suffix != suffix: |
| 55 | + mismatched_files.append(os.path.join(root, filename)) |
| 56 | + if mismatched_files: |
| 57 | + raise AssertionError( |
| 58 | + f"CSV files with mismatched suffixes: {mismatched_files}" |
| 59 | + ) |
| 60 | + |
| 61 | + file_path = os.path.join(os.path.dirname(__file__), "simulation.py") |
| 62 | + |
| 63 | + run_args = [ |
| 64 | + "-c", |
| 65 | + test_path("configs_mp"), |
| 66 | + "-c", |
| 67 | + test_path("configs"), |
| 68 | + "-d", |
| 69 | + example_path("data"), |
| 70 | + "-o", |
| 71 | + test_path("output"), |
| 72 | + ] |
| 73 | + |
| 74 | + try: |
| 75 | + os.mkdir(test_path("output")) |
| 76 | + except FileExistsError: |
| 77 | + pass |
| 78 | + |
| 79 | + subprocess.run(["coverage", "run", "-a", file_path] + run_args, check=True) |
| 80 | + |
| 81 | + check_csv_suffix(os.path.join(test_path("output"), "trace")) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + test_trace_ids_have_same_hash() |
0 commit comments