1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-22 11:12:29 +00:00
soil/tests/test_config.py

83 lines
2.3 KiB
Python
Raw Normal View History

from unittest import TestCase, skip
2022-05-10 14:29:06 +00:00
import os
2022-10-06 13:49:10 +00:00
import yaml
import copy
2022-05-10 14:29:06 +00:00
from os.path import join
2022-10-06 13:49:10 +00:00
from soil import simulation, serialization, config, network, agents, utils
2022-05-10 14:29:06 +00:00
ROOT = os.path.abspath(os.path.dirname(__file__))
EXAMPLES = join(ROOT, "..", "examples")
2022-05-10 14:29:06 +00:00
FORCE_TESTS = os.environ.get("FORCE_TESTS", "")
2022-05-10 14:29:06 +00:00
2022-10-06 13:49:10 +00:00
def isequal(a, b):
if isinstance(a, dict):
for (k, v) in a.items():
if v:
isequal(a[k], b[k])
else:
assert not b.get(k, None)
return
assert a == b
# @skip("new versions of soil do not rely on configuration files")
2022-05-10 14:29:06 +00:00
class TestConfig(TestCase):
2022-09-15 17:27:17 +00:00
def test_torvalds_config(self):
sim = simulation.from_config(os.path.join(ROOT, "test_config.yml"))
2023-04-20 15:56:44 +00:00
MAX_STEPS = 10
INTERVAL = 2
assert sim.interval == INTERVAL
assert sim.max_steps == MAX_STEPS
envs = sim.run()
assert len(envs) == 1
env = envs[0]
assert env.interval == 2
assert env.count_agents() == 3
2023-04-20 15:56:44 +00:00
assert env.now == INTERVAL * MAX_STEPS
2022-09-15 17:27:17 +00:00
2022-05-10 14:29:06 +00:00
def make_example_test(path, cfg):
def wrapped(self):
root = os.getcwd()
2022-09-13 16:16:31 +00:00
print(path)
s = simulation.from_config(cfg)
2023-04-20 15:56:44 +00:00
iterations = s.max_time * s.iterations
if iterations > 1000:
s.max_time = 100
2023-04-20 15:56:44 +00:00
s.iterations = 1
if cfg.skip_test and not FORCE_TESTS:
self.skipTest('Example ignored.')
envs = s.run_simulation(dump=False)
assert envs
for env in envs:
assert env
try:
2023-04-20 15:56:44 +00:00
n = cfg.parameters['topology']['params']['n']
assert len(list(env.network_agents)) == n
assert env.now > 0 # It has run
assert env.now <= cfg.max_time # But not further than allowed
except KeyError:
pass
2022-05-10 14:29:06 +00:00
return wrapped
def add_example_tests():
for config, path in serialization.load_files(
join(EXAMPLES, "*", "*.yml"),
join(EXAMPLES, "*.yml"),
2022-05-10 14:29:06 +00:00
):
p = make_example_test(path=path, cfg=config)
fname = os.path.basename(path)
p.__name__ = "test_example_file_%s" % fname
p.__doc__ = "%s should be a valid configuration" % fname
2022-05-10 14:29:06 +00:00
setattr(TestConfig, p.__name__, p)
del p
add_example_tests()