1
0
mirror of https://github.com/gsi-upm/soil synced 2024-09-20 23:41:41 +00:00
soil/tests/test_examples.py

78 lines
2.3 KiB
Python
Raw Normal View History

2018-12-04 08:54:29 +00:00
from unittest import TestCase
from unittest.case import SkipTest
2018-12-04 08:54:29 +00:00
import os
from os.path import join
from glob import glob
2018-12-04 08:54:29 +00:00
from soil import simulation
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):
"""Empty class that will be populated with auto-discovery tests for every example"""
2018-12-04 08:54:29 +00:00
pass
def get_test_for_sims(sims, path):
root = os.getcwd()
2018-12-04 08:54:29 +00:00
def wrapped(self):
run = False
for sim in sims:
if sim.skip_test and not FORCE_TESTS:
continue
run = True
iterations = sim.max_steps * sim.num_trials
if iterations < 0 or iterations > 1000:
sim.max_steps = 100
sim.num_trials = 1
envs = sim.run_simulation(dump=False)
assert envs
for env in envs:
assert env
assert env.now > 0
try:
n = sim.model_params["network_params"]["n"]
assert len(list(env.network_agents)) == n
except KeyError:
pass
assert env.schedule.steps > 0 # It has run
assert env.schedule.steps <= sim.max_steps # But not further than allowed
if not run:
raise SkipTest("Example ignored because all simulations are set up to be skipped.")
2018-12-04 08:54:29 +00:00
return wrapped
def add_example_tests():
sim_paths = {}
for path in glob(join(EXAMPLES, '**', '*.yml')):
2022-10-20 12:12:10 +00:00
if "soil_output" in path:
continue
if path not in sim_paths:
sim_paths[path] = []
for sim in simulation.iter_from_config(path):
sim_paths[path].append(sim)
for path in glob(join(EXAMPLES, '**', '*_sim.py')):
if path not in sim_paths:
sim_paths[path] = []
for sim in simulation.iter_from_py(path):
sim_paths[path].append(sim)
for (path, sims) in sim_paths.items():
test_case = get_test_for_sims(sims, path)
2018-12-04 08:54:29 +00:00
fname = os.path.basename(path)
test_case.__name__ = "test_example_file_%s" % fname
test_case.__doc__ = "%s should be a valid configuration" % fname
setattr(TestExamples, test_case.__name__, test_case)
del test_case
2018-12-04 08:54:29 +00:00
add_example_tests()