From ceea79035d6451c5edf87d409dafc406273b65ff Mon Sep 17 00:00:00 2001 From: Thibaud Date: Thu, 13 Dec 2018 21:02:07 +0100 Subject: [PATCH] Update tests --- tests/configurator_tests.py | 76 ++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/tests/configurator_tests.py b/tests/configurator_tests.py index 5ca75b9..2bc914a 100644 --- a/tests/configurator_tests.py +++ b/tests/configurator_tests.py @@ -2,37 +2,17 @@ import os import shutil import tempfile import unittest + from configurator import configurator +# Absolute path of the directory the script is in +SCRIPTDIR = os.path.dirname(__file__) -class ConfiguratorTests(unittest.TestCase): - +class TemplateTests(unittest.TestCase): def setUp(self): - self.sim_path = tempfile.mkdtemp() self.sim_name = 'test_simulation' - - def tearDown(self): - shutil.rmtree(self.sim_path) - - def test_generate_scenario(self): - osm_file = os.path.abspath('sample.osm') - configurator.generate_scenario(osm_file, self.sim_path, self.sim_name) - self.assert_is_dir(self.sim_path) - # self.assert_is_dir(os.path.join(self.sim_path, 'log')) - generated_files = [ - f'{self.sim_name}.poly.xml', - f'{self.sim_name}.net.xml' - ] - for f in generated_files: - self.assert_is_file(os.path.join(self.sim_path, f)) - - def test_generate_mobility(self): - # The scenario must be generated before the mobility - osm_file = os.path.abspath('sample.osm') - configurator.generate_scenario(osm_file, self.sim_path, self.sim_name) - configurator.generate_mobility(self.sim_path, self.sim_name) - trips_file = os.path.join(self.sim_path, f'{self.sim_name}.trips.xml') - self.assert_is_file(trips_file) + self.sim_path = '/test_simulation' + self.log_path = '/test_simulation/log' def test_load_netconvert_template(self): tree = configurator.load_netconvert_template('test.osm', 'test_simulation') @@ -40,13 +20,21 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(tree.find('output/output-file').get('value'), f'{self.sim_name}.net.xml') self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.netconvert.log') - def test_load_sumoconfig_template(self): + def test_load_sumoconfig_template_default(self): tree = configurator.load_sumoconfig_template(self.sim_name) self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml') self.assertEqual(tree.find('input/route-files').get('value'), f'{self.sim_name}.rou.xml') self.assertEqual(tree.find('input/additional-files').get('value'), f'{self.sim_name}.poly.xml') self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.log') + def test_load_sumoconfig_template_with_routefiles(self): + routefiles = (f'{self.sim_name}.bus.rou.xml', f'{self.sim_name}.passenger.rou.xml') + tree = configurator.load_sumoconfig_template(self.sim_name, routefiles) + self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml') + self.assertEqual(tree.find('input/route-files').get('value'), ','.join(routefiles)) + self.assertEqual(tree.find('input/additional-files').get('value'), f'{self.sim_name}.poly.xml') + self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.log') + def test_load_polyconvert_template(self): tree = configurator.load_polyconvert_template( osm_file=f'{self.sim_name}.osm', @@ -59,6 +47,40 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(tree.find('output/output-file').get('value'), f'{self.sim_name}.poly.xml') self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.polyconvert.log') + +class GenerationTests(unittest.TestCase): + + def setUp(self): + self.base_path = tempfile.mkdtemp() + self.sim_name = 'test_simulation' + self.sim_path = os.path.join(self.base_path, self.sim_name) + self.log_path = os.path.join(self.sim_name, 'log') + + def tearDown(self): + shutil.rmtree(self.base_path) + + def test_generate_scenario(self): + osm_file = os.path.join(SCRIPTDIR, 'sample.osm') + configurator.generate_scenario(osm_file, self.sim_path, self.sim_name) + self.assert_is_dir(self.sim_path) + generated_files = [ + f'{self.sim_name}.poly.xml', + f'{self.sim_name}.net.xml' + ] + for f in generated_files: + self.assert_is_file(os.path.join(self.sim_path, f)) + + def test_generate_mobility(self): + # The scenario must be generated before the mobility + osm_file = os.path.join(SCRIPTDIR, 'sample.osm') + trips_file = os.path.join(self.sim_path, f'{self.sim_name}.trips.xml') + configurator.generate_scenario(osm_file, self.sim_path, self.sim_name) + routefiles = configurator.generate_mobility(self.sim_path, self.sim_name) + + self.assert_is_file(trips_file) + for f in routefiles: + self.assert_is_file(os.path.join(self.sim_path, f)) + def assert_exists(self, path): self.assertTrue(os.path.exists(path), msg=f'{path} does not exist')