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