1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 15:32:29 +00:00
soil/tests/test_examples.py

58 lines
1.7 KiB
Python
Raw Normal View History

2018-12-04 08:54:29 +00:00
from unittest import TestCase
import os
from os.path import join
2022-10-13 20:43:16 +00:00
from soil import serialization, simulation, config
2018-12-04 08:54:29 +00:00
ROOT = os.path.abspath(os.path.dirname(__file__))
EXAMPLES = join(ROOT, "..", "examples")
2018-12-04 08:54:29 +00:00
FORCE_TESTS = os.environ.get("FORCE_TESTS", "")
2019-02-19 20:17:19 +00:00
2018-12-04 08:54:29 +00:00
class TestExamples(TestCase):
pass
2022-10-13 20:43:16 +00:00
def make_example_test(path, cfg):
2018-12-04 08:54:29 +00:00
def wrapped(self):
root = os.getcwd()
2022-10-13 20:43:16 +00:00
for s in simulation.iter_from_config(cfg):
iterations = s.max_steps * s.num_trials
if iterations < 0 or iterations > 1000:
s.max_steps = 100
s.num_trials = 1
assert isinstance(cfg, config.Config)
if getattr(cfg, "skip_test", False) and not FORCE_TESTS:
self.skipTest("Example ignored.")
2019-04-26 17:22:45 +00:00
envs = s.run_simulation(dry_run=True)
assert envs
for env in envs:
assert env
try:
n = cfg.model_params["network_params"]["n"]
2019-04-26 17:22:45 +00:00
assert len(list(env.network_agents)) == n
except KeyError:
pass
2022-10-13 20:43:16 +00:00
assert env.schedule.steps > 0 # It has run
assert env.schedule.steps <= s.max_steps # But not further than allowed
2018-12-04 08:54:29 +00:00
return wrapped
def add_example_tests():
2022-10-13 20:43:16 +00:00
for cfg, path in serialization.load_files(
join(EXAMPLES, "**", "*.yml"),
2019-04-26 17:22:45 +00:00
):
2022-10-20 12:12:10 +00:00
if "soil_output" in path:
continue
2022-10-13 20:43:16 +00:00
p = make_example_test(path=path, cfg=config.Config.from_raw(cfg))
2018-12-04 08:54:29 +00:00
fname = os.path.basename(path)
p.__name__ = "test_example_file_%s" % fname
p.__doc__ = "%s should be a valid configuration" % fname
2018-12-04 08:54:29 +00:00
setattr(TestExamples, p.__name__, p)
del p
add_example_tests()