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

120 lines
4.1 KiB
Python
Raw Permalink Normal View History

2022-05-10 14:29:06 +00:00
from unittest import TestCase
import os
from os.path import join
2022-09-15 17:27:17 +00:00
from soil import simulation, serialization, config, network, agents
2022-05-10 14:29:06 +00:00
ROOT = os.path.abspath(os.path.dirname(__file__))
EXAMPLES = join(ROOT, '..', 'examples')
FORCE_TESTS = os.environ.get('FORCE_TESTS', '')
class TestConfig(TestCase):
def test_conversion(self):
2022-09-15 17:27:17 +00:00
expected = serialization.load_file(join(ROOT, "complete_converted.yml"))[0]
2022-05-10 14:29:06 +00:00
old = serialization.load_file(join(ROOT, "old_complete.yml"))[0]
2022-09-13 16:16:31 +00:00
converted_defaults = config.convert_old(old, strict=False)
converted = converted_defaults.dict(skip_defaults=True)
2022-09-15 17:27:17 +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)
2022-09-13 16:16:31 +00:00
return
2022-09-15 17:27:17 +00:00
assert a == b
2022-09-13 16:16:31 +00:00
2022-09-15 17:27:17 +00:00
isequal(converted, expected)
2022-09-13 16:16:31 +00:00
def test_topology_config(self):
netconfig = config.NetConfig(**{
'path': join(ROOT, 'test.gexf')
})
net = network.from_config(netconfig, dir_path=ROOT)
assert len(net.nodes) == 2
assert len(net.edges) == 1
def test_env_from_config(self):
"""
Simple configuration that tests that the graph is loaded, and that
network agents are initialized properly.
"""
config = {
'name': 'CounterAgent',
'network_params': {
'path': join(ROOT, 'test.gexf')
},
'agent_type': 'CounterModel',
# 'states': [{'times': 10}, {'times': 20}],
'max_time': 2,
'dry_run': True,
'num_trials': 1,
'environment_params': {
}
}
s = simulation.from_old_config(config)
env = s.get_env()
assert len(env.topologies['default'].nodes) == 2
assert len(env.topologies['default'].edges) == 1
assert len(env.agents) == 2
assert env.agents[0].topology == env.topologies['default']
2022-05-10 14:29:06 +00:00
2022-09-15 17:27:17 +00:00
def test_agents_from_config(self):
'''We test that the known complete configuration produces
the right agents in the right groups'''
cfg = serialization.load_file(join(ROOT, "complete_converted.yml"))[0]
s = simulation.from_config(cfg)
env = s.get_env()
assert len(env.topologies['default'].nodes) == 10
2022-09-16 16:13:39 +00:00
assert len(env.agents(group='network')) == 10
assert len(env.agents(group='environment')) == 1
2022-09-15 17:27:17 +00:00
2022-09-16 16:13:39 +00:00
assert sum(1 for a in env.agents(group='network', agent_type=agents.CounterModel)) == 4
assert sum(1 for a in env.agents(group='network', agent_type=agents.AggregatedCounter)) == 6
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)
2022-05-10 14:29:06 +00:00
# for s in simulation.all_from_config(path):
# iterations = s.config.max_time * s.config.num_trials
# if iterations > 1000:
# s.config.max_time = 100
# s.config.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
# assert env.now > 0 # It has run
# assert env.now <= config['max_time'] # But not further than allowed
# except KeyError:
# pass
return wrapped
def add_example_tests():
for config, path in serialization.load_files(
join(EXAMPLES, '*', '*.yml'),
join(EXAMPLES, '*.yml'),
):
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
setattr(TestConfig, p.__name__, p)
del p
add_example_tests()