From c0f61074d9a8915ca9e1157ac9356df698fbeb9c Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Mon, 22 Jul 2024 09:56:20 +0200 Subject: [PATCH 1/7] test_examples.py: Add batch_run tests --- test_examples.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test_examples.py b/test_examples.py index 50a46228..90d3446d 100644 --- a/test_examples.py +++ b/test_examples.py @@ -32,3 +32,16 @@ def test_model_steps(model_class): model = model_class() # Assume no arguments are needed for _ in range(10): model.step() + + +def get_batch_scripts(): + return [ + 'examples.bank_reserves.batch_run', + 'examples.sugarscape_g1mt.run' + ] + + +@pytest.mark.parametrize("script_module", get_batch_scripts()) +def test_batch_run(script_module): + module = importlib.import_module(script_module) + module.main() # Call the main function From ef387af1bc69a13a7508e5922741861d71ac1bfa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Jul 2024 07:57:49 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test_examples.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test_examples.py b/test_examples.py index 90d3446d..eafb9d3e 100644 --- a/test_examples.py +++ b/test_examples.py @@ -35,10 +35,7 @@ def test_model_steps(model_class): def get_batch_scripts(): - return [ - 'examples.bank_reserves.batch_run', - 'examples.sugarscape_g1mt.run' - ] + return ["examples.bank_reserves.batch_run", "examples.sugarscape_g1mt.run"] @pytest.mark.parametrize("script_module", get_batch_scripts()) From e8bcbf6521e605a765848d1133b90fb554092d20 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Mon, 22 Jul 2024 10:08:36 +0200 Subject: [PATCH 3/7] test_examples.py: Update path for batch_run --- test_examples.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test_examples.py b/test_examples.py index eafb9d3e..5c75d3e0 100644 --- a/test_examples.py +++ b/test_examples.py @@ -35,10 +35,21 @@ def test_model_steps(model_class): def get_batch_scripts(): - return ["examples.bank_reserves.batch_run", "examples.sugarscape_g1mt.run"] + return [ + 'examples.bank_reserves.batch_run', + 'examples.sugarscape_g1mt.run' + ] @pytest.mark.parametrize("script_module", get_batch_scripts()) def test_batch_run(script_module): - module = importlib.import_module(script_module) - module.main() # Call the main function + # Save the old sys.path + old_sys_path = sys.path[:] + try: + # Add the examples directory to the sys.path + sys.path.insert(0, os.path.abspath('examples')) + module = importlib.import_module(script_module) + module.main() # Call the main function + finally: + # Restore the original sys.path + sys.path = old_sys_path From fc68d55b5ff7dfb7220d9f4cb944901d795ce0fe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Jul 2024 08:09:50 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test_examples.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test_examples.py b/test_examples.py index 5c75d3e0..df447a54 100644 --- a/test_examples.py +++ b/test_examples.py @@ -35,10 +35,7 @@ def test_model_steps(model_class): def get_batch_scripts(): - return [ - 'examples.bank_reserves.batch_run', - 'examples.sugarscape_g1mt.run' - ] + return ["examples.bank_reserves.batch_run", "examples.sugarscape_g1mt.run"] @pytest.mark.parametrize("script_module", get_batch_scripts()) @@ -47,7 +44,7 @@ def test_batch_run(script_module): old_sys_path = sys.path[:] try: # Add the examples directory to the sys.path - sys.path.insert(0, os.path.abspath('examples')) + sys.path.insert(0, os.path.abspath("examples")) module = importlib.import_module(script_module) module.main() # Call the main function finally: From e2c724568888efc963c47821eeaed48055814ee0 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Mon, 22 Jul 2024 10:10:14 +0200 Subject: [PATCH 5/7] Update test_examples.py --- test_examples.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test_examples.py b/test_examples.py index df447a54..891d7886 100644 --- a/test_examples.py +++ b/test_examples.py @@ -1,5 +1,6 @@ import importlib import os +import sys import pytest from mesa import Model From 694b19d3588f51064d8f72bd5c7dbb56a3fe63bb Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Mon, 22 Jul 2024 10:13:17 +0200 Subject: [PATCH 6/7] Update test_examples.py --- test_examples.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/test_examples.py b/test_examples.py index 891d7886..3f068bd1 100644 --- a/test_examples.py +++ b/test_examples.py @@ -36,18 +36,22 @@ def test_model_steps(model_class): def get_batch_scripts(): - return ["examples.bank_reserves.batch_run", "examples.sugarscape_g1mt.run"] + return [ + ('examples.bank_reserves', 'batch_run'), + ('examples.sugarscape_g1mt', 'run') + ] -@pytest.mark.parametrize("script_module", get_batch_scripts()) -def test_batch_run(script_module): +@pytest.mark.parametrize("example_dir, script_module", get_batch_scripts()) +def test_batch_run(example_dir, script_module): # Save the old sys.path old_sys_path = sys.path[:] try: - # Add the examples directory to the sys.path - sys.path.insert(0, os.path.abspath("examples")) - module = importlib.import_module(script_module) + # Add the example directory to the sys.path + sys.path.insert(0, os.path.abspath(example_dir)) + module = importlib.import_module(f"{example_dir}.{script_module}") module.main() # Call the main function finally: # Restore the original sys.path sys.path = old_sys_path + From 147afd2e68db6942eb9e6b77503c69db1884c8fc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Jul 2024 08:14:09 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test_examples.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test_examples.py b/test_examples.py index 3f068bd1..f22230c4 100644 --- a/test_examples.py +++ b/test_examples.py @@ -37,8 +37,8 @@ def test_model_steps(model_class): def get_batch_scripts(): return [ - ('examples.bank_reserves', 'batch_run'), - ('examples.sugarscape_g1mt', 'run') + ("examples.bank_reserves", "batch_run"), + ("examples.sugarscape_g1mt", "run"), ] @@ -54,4 +54,3 @@ def test_batch_run(example_dir, script_module): finally: # Restore the original sys.path sys.path = old_sys_path -