2018-12-04 08:54:29 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
import os
|
|
|
|
from os.path import join
|
2022-11-13 19:31:05 +00:00
|
|
|
from glob import glob
|
2018-12-04 08:54:29 +00:00
|
|
|
|
2022-11-13 19:31:05 +00:00
|
|
|
from soil import simulation, config
|
2018-12-04 08:54:29 +00:00
|
|
|
|
|
|
|
ROOT = os.path.abspath(os.path.dirname(__file__))
|
2022-10-16 15:54:03 +00:00
|
|
|
EXAMPLES = join(ROOT, "..", "examples")
|
2018-12-04 08:54:29 +00:00
|
|
|
|
2022-10-16 15:54:03 +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-11-13 19:31:05 +00:00
|
|
|
def get_test_for_sim(sim, path):
|
|
|
|
root = os.getcwd()
|
|
|
|
iterations = sim.max_steps * sim.num_trials
|
|
|
|
if iterations < 0 or iterations > 1000:
|
|
|
|
sim.max_steps = 100
|
|
|
|
sim.num_trials = 1
|
|
|
|
|
2018-12-04 08:54:29 +00:00
|
|
|
def wrapped(self):
|
2022-11-13 19:31:05 +00:00
|
|
|
envs = sim.run_simulation(dry_run=True)
|
|
|
|
assert envs
|
|
|
|
for env in envs:
|
|
|
|
assert env
|
|
|
|
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
|
2022-10-16 15:54:03 +00:00
|
|
|
|
2018-12-04 08:54:29 +00:00
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
def add_example_tests():
|
2022-11-13 19:31:05 +00:00
|
|
|
sim_paths = []
|
|
|
|
for path in glob(join(EXAMPLES, '**', '*.yml')):
|
2022-10-20 12:12:10 +00:00
|
|
|
if "soil_output" in path:
|
2022-10-20 07:14:50 +00:00
|
|
|
continue
|
2022-11-13 19:31:05 +00:00
|
|
|
for sim in simulation.iter_from_config(path):
|
|
|
|
sim_paths.append((sim, path))
|
|
|
|
for path in glob(join(EXAMPLES, '**', '*.py')):
|
|
|
|
for sim in simulation.iter_from_py(path):
|
|
|
|
sim_paths.append((sim, path))
|
|
|
|
|
|
|
|
for (sim, path) in sim_paths:
|
|
|
|
if sim.skip_test and not FORCE_TESTS:
|
|
|
|
continue
|
|
|
|
test_case = get_test_for_sim(sim, path)
|
2018-12-04 08:54:29 +00:00
|
|
|
fname = os.path.basename(path)
|
2022-11-13 19:31:05 +00:00
|
|
|
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()
|