diff --git a/.gitignore b/.gitignore index ee3f5fc7..5434dbe4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ *.log *.zip *.pkl -config.json +./config.json data src diff --git a/README.md b/README.md index 3e11ca81..f780c3e7 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,10 @@ for more information. Included cities ----------------- -* [Florianópolis, Brazil](./osm2gtfs/creators/fenix/fenix.json) -* [Suburban trains in Costa Rica](./osm2gtfs/creators/incofer/incofer.json) -* [Accra, Ghana](./osm2gtfs/creators/accra/accra.json) -* [Managua, Ciudad Sandino](./osm2gtfs/creators/managua/managua.json) and [Estelí](./osm2gtfs/creators/esteli/esteli.json) in Nicaragua +* [Florianópolis, Brazil](./osm2gtfs/creators/br_florianopolis/config.json) +* [Suburban trains in Costa Rica](./osm2gtfs/creators/cr_gam/config.json) +* [Accra, Ghana](./osm2gtfs/creators/gh_accra/config.json) +* [Managua, Ciudad Sandino](./osm2gtfs/creators/ni_managua/config.json) and [Estelí](./osm2gtfs/creators/ni_esteli/config.json) in Nicaragua *Soon, also in your city* @@ -53,7 +53,7 @@ Use Example: - $ osm2gtfs -c osm2gtfs/creators/fenix/fenix.json + osm2gtfs -c osm2gtfs/creators/br_florianopolis/config.json License ------- diff --git a/osm2gtfs/core/configuration.py b/osm2gtfs/core/configuration.py index 8ac44c2d..f9d6fbd1 100644 --- a/osm2gtfs/core/configuration.py +++ b/osm2gtfs/core/configuration.py @@ -98,10 +98,10 @@ def _load_config(self, args): """ # Load config json file if args.config is not None: - config = Configuration._load_config_file(args.config) + config = Configuration.load_config_file(args.config) elif os.path.isfile('config.json'): with open("config.json") as json_file: - config = Configuration._load_config_file(json_file) + config = Configuration.load_config_file(json_file) else: sys.stderr.write("Error: No config.json file found.\n") sys.exit(0) @@ -109,7 +109,7 @@ def _load_config(self, args): return config @staticmethod - def _load_config_file(configfile): + def load_config_file(configfile): """ Loads json from config file diff --git a/osm2gtfs/core/creator_factory.py b/osm2gtfs/core/creator_factory.py index 8f6d9d56..9735f7f2 100644 --- a/osm2gtfs/core/creator_factory.py +++ b/osm2gtfs/core/creator_factory.py @@ -28,13 +28,14 @@ def __repr__(self): def get_agency_creator(self): selector = self.selector + try: module = importlib.import_module( ".creators." + selector + ".agency_creator_" + selector, package="osm2gtfs") agency_creator_override = getattr( - module, "AgencyCreator" + selector.capitalize()) - print("Agency creator: " + selector.capitalize()) + module, "AgencyCreator" + self._generate_class_name(selector)) + print("Agency creator: " + selector) return agency_creator_override(self.config) except ImportError: print("Agency creator: Default") @@ -42,13 +43,14 @@ def get_agency_creator(self): def get_feed_info_creator(self): selector = self.selector + try: module = importlib.import_module( ".creators." + selector + ".feed_info_creator_" + selector, package="osm2gtfs") feed_info_creator_override = getattr( - module, "FeedInfoCreator" + selector.capitalize()) - print("Feed info creator: " + selector.capitalize()) + module, "FeedInfoCreator" + self._generate_class_name(selector)) + print("Feed info creator: " + selector) return feed_info_creator_override(self.config) except ImportError: print("Feed info creator: Default") @@ -56,13 +58,14 @@ def get_feed_info_creator(self): def get_routes_creator(self): selector = self.selector + try: module = importlib.import_module( ".creators." + selector + ".routes_creator_" + selector, package="osm2gtfs") routes_creator_override = getattr( - module, "RoutesCreator" + selector.capitalize()) - print("Routes creator: " + selector.capitalize()) + module, "RoutesCreator" + self._generate_class_name(selector)) + print("Routes creator: " + selector) return routes_creator_override(self.config) except ImportError: print("Routes creator: Default") @@ -70,13 +73,14 @@ def get_routes_creator(self): def get_stops_creator(self): selector = self.selector + try: module = importlib.import_module( ".creators." + selector + ".stops_creator_" + selector, package="osm2gtfs") stops_creator_override = getattr( - module, "StopsCreator" + selector.capitalize()) - print("Stops creator: " + selector.capitalize()) + module, "StopsCreator" + self._generate_class_name(selector)) + print("Stops creator: " + selector) return stops_creator_override(self.config) except ImportError: print("Stops creator: Default") @@ -84,13 +88,14 @@ def get_stops_creator(self): def get_schedule_creator(self): selector = self.selector + try: module = importlib.import_module( ".creators." + selector + ".schedule_creator_" + selector, package="osm2gtfs") schedule_creator_override = getattr( - module, "ScheduleCreator" + selector.capitalize()) - print("Schedule creator: " + selector.capitalize()) + module, "ScheduleCreator" + self._generate_class_name(selector)) + print("Schedule creator: " + selector) return schedule_creator_override(self.config) except ImportError: print("Schedule creator: Default") @@ -98,14 +103,30 @@ def get_schedule_creator(self): def get_trips_creator(self): selector = self.selector + try: module = importlib.import_module( ".creators." + selector + ".trips_creator_" + selector, package="osm2gtfs") trips_creator_override = getattr( - module, "TripsCreator" + selector.capitalize()) - print("Trips creator: " + selector.capitalize()) + module, "TripsCreator" + self._generate_class_name(selector)) + print("Trips creator: " + selector) return trips_creator_override(self.config) except ImportError: print("Trips creator: Default") return TripsCreator(self.config) + + @staticmethod + def _generate_class_name(selector): + """ + Converts the underscore selector into class names sticking to Python's + naming convention. + """ + if "_" in selector: + split_selector = selector.split("_") + class_name = str() + for part in split_selector: + class_name += part.capitalize() + else: + class_name = selector.capitalize() + return class_name diff --git a/osm2gtfs/creators/accra/__init__.py b/osm2gtfs/creators/br_florianopolis/__init__.py similarity index 100% rename from osm2gtfs/creators/accra/__init__.py rename to osm2gtfs/creators/br_florianopolis/__init__.py diff --git a/osm2gtfs/creators/fenix/fenix.json b/osm2gtfs/creators/br_florianopolis/config.json similarity index 96% rename from osm2gtfs/creators/fenix/fenix.json rename to osm2gtfs/creators/br_florianopolis/config.json index 925b070f..561c9944 100644 --- a/osm2gtfs/creators/fenix/fenix.json +++ b/osm2gtfs/creators/br_florianopolis/config.json @@ -30,5 +30,5 @@ }, "schedule_source": "http://www.consorciofenix.com.br/api2/linhas.json", "output_file": "data/br-floripa.zip", - "selector": "fenix" + "selector": "br_florianopolis" } diff --git a/osm2gtfs/creators/fenix/routes_creator_fenix.py b/osm2gtfs/creators/br_florianopolis/routes_creator_br_florianopolis.py similarity index 97% rename from osm2gtfs/creators/fenix/routes_creator_fenix.py rename to osm2gtfs/creators/br_florianopolis/routes_creator_br_florianopolis.py index 0e1566ef..19f26b8d 100644 --- a/osm2gtfs/creators/fenix/routes_creator_fenix.py +++ b/osm2gtfs/creators/br_florianopolis/routes_creator_br_florianopolis.py @@ -5,7 +5,7 @@ from osm2gtfs.creators.routes_creator import RoutesCreator -class RoutesCreatorFenix(RoutesCreator): +class RoutesCreatorBrFlorianopolis(RoutesCreator): def add_routes_to_feed(self, feed, data): ''' diff --git a/osm2gtfs/creators/fenix/stops_creator_fenix.py b/osm2gtfs/creators/br_florianopolis/stops_creator_br_florianopolis.py similarity index 86% rename from osm2gtfs/creators/fenix/stops_creator_fenix.py rename to osm2gtfs/creators/br_florianopolis/stops_creator_br_florianopolis.py index 71f12461..a74dccf7 100644 --- a/osm2gtfs/creators/fenix/stops_creator_fenix.py +++ b/osm2gtfs/creators/br_florianopolis/stops_creator_br_florianopolis.py @@ -3,7 +3,7 @@ from osm2gtfs.creators.stops_creator import StopsCreator -class StopsCreatorFenix(StopsCreator): +class StopsCreatorBrFlorianopolis(StopsCreator): # Override construction of stop_id def _define_stop_id(self, stop): diff --git a/osm2gtfs/creators/fenix/trips_creator_fenix.py b/osm2gtfs/creators/br_florianopolis/trips_creator_br_florianopolis.py similarity index 99% rename from osm2gtfs/creators/fenix/trips_creator_fenix.py rename to osm2gtfs/creators/br_florianopolis/trips_creator_br_florianopolis.py index 59659a52..a63774ae 100644 --- a/osm2gtfs/creators/fenix/trips_creator_fenix.py +++ b/osm2gtfs/creators/br_florianopolis/trips_creator_br_florianopolis.py @@ -25,10 +25,10 @@ NO_DURATION = "não encontrado" -class TripsCreatorFenix(TripsCreator): +class TripsCreatorBrFlorianopolis(TripsCreator): def __init__(self, config): - super(TripsCreatorFenix, self).__init__(config) + super(TripsCreatorBrFlorianopolis, self).__init__(config) self.start_date = datetime.strptime(self.config['feed_info']['start_date'], "%Y%m%d") diff --git a/osm2gtfs/creators/fenix/__init__.py b/osm2gtfs/creators/cr_gam/__init__.py similarity index 100% rename from osm2gtfs/creators/fenix/__init__.py rename to osm2gtfs/creators/cr_gam/__init__.py diff --git a/osm2gtfs/creators/incofer/incofer.json b/osm2gtfs/creators/cr_gam/config.json similarity index 93% rename from osm2gtfs/creators/incofer/incofer.json rename to osm2gtfs/creators/cr_gam/config.json index 548f38bf..d0b495ea 100644 --- a/osm2gtfs/creators/incofer/incofer.json +++ b/osm2gtfs/creators/cr_gam/config.json @@ -33,6 +33,6 @@ "end_date": "20170910" }, "schedule_source": "https://raw.githubusercontent.com/jamescr/incofer-datos/master/input_incofer.json", - "output_file": "data/cr-incofer.zip", - "selector": "incofer" + "output_file": "data/cr-gam.zip", + "selector": "cr_gam" } diff --git a/osm2gtfs/creators/incofer/routes_creator_incofer.py b/osm2gtfs/creators/cr_gam/routes_creator_cr_gam.py similarity index 98% rename from osm2gtfs/creators/incofer/routes_creator_incofer.py rename to osm2gtfs/creators/cr_gam/routes_creator_cr_gam.py index 69bff29b..20953ee8 100644 --- a/osm2gtfs/creators/incofer/routes_creator_incofer.py +++ b/osm2gtfs/creators/cr_gam/routes_creator_cr_gam.py @@ -5,7 +5,7 @@ from osm2gtfs.creators.routes_creator import RoutesCreator -class RoutesCreatorIncofer(RoutesCreator): +class RoutesCreatorCrGam(RoutesCreator): def add_routes_to_feed(self, feed, data): ''' diff --git a/osm2gtfs/creators/incofer/stops_creator_incofer.py b/osm2gtfs/creators/cr_gam/stops_creator_cr_gam.py similarity index 87% rename from osm2gtfs/creators/incofer/stops_creator_incofer.py rename to osm2gtfs/creators/cr_gam/stops_creator_cr_gam.py index 19a68ff0..1d0ce152 100644 --- a/osm2gtfs/creators/incofer/stops_creator_incofer.py +++ b/osm2gtfs/creators/cr_gam/stops_creator_cr_gam.py @@ -3,7 +3,7 @@ from osm2gtfs.creators.stops_creator import StopsCreator -class StopsCreatorIncofer(StopsCreator): +class StopsCreatorCrGam(StopsCreator): # Override construction of stop_id def _define_stop_id(self, stop): diff --git a/osm2gtfs/creators/incofer/trips_creator_incofer.py b/osm2gtfs/creators/cr_gam/trips_creator_cr_gam.py similarity index 99% rename from osm2gtfs/creators/incofer/trips_creator_incofer.py rename to osm2gtfs/creators/cr_gam/trips_creator_cr_gam.py index de53491d..3d90e41d 100644 --- a/osm2gtfs/creators/incofer/trips_creator_incofer.py +++ b/osm2gtfs/creators/cr_gam/trips_creator_cr_gam.py @@ -7,7 +7,7 @@ from osm2gtfs.creators.trips_creator import TripsCreator -class TripsCreatorIncofer(TripsCreator): +class TripsCreatorCrGam(TripsCreator): def add_trips_to_feed(self, feed, data): diff --git a/osm2gtfs/creators/incofer/__init__.py b/osm2gtfs/creators/gh_accra/__init__.py similarity index 100% rename from osm2gtfs/creators/incofer/__init__.py rename to osm2gtfs/creators/gh_accra/__init__.py diff --git a/osm2gtfs/creators/accra/accra.json b/osm2gtfs/creators/gh_accra/config.json similarity index 92% rename from osm2gtfs/creators/accra/accra.json rename to osm2gtfs/creators/gh_accra/config.json index e6a5d013..348c5e7b 100644 --- a/osm2gtfs/creators/accra/accra.json +++ b/osm2gtfs/creators/gh_accra/config.json @@ -30,6 +30,6 @@ "start_date": "20170901", "end_date": "20180730" }, - "output_file": "data/accra.zip", - "selector": "accra" + "output_file": "data/gh-accra.zip", + "selector": "gh_accra" } diff --git a/osm2gtfs/creators/accra/routes_creator_accra.py b/osm2gtfs/creators/gh_accra/routes_creator_gh_accra.py similarity index 85% rename from osm2gtfs/creators/accra/routes_creator_accra.py rename to osm2gtfs/creators/gh_accra/routes_creator_gh_accra.py index adeb65ea..1469a3e9 100644 --- a/osm2gtfs/creators/accra/routes_creator_accra.py +++ b/osm2gtfs/creators/gh_accra/routes_creator_gh_accra.py @@ -3,7 +3,7 @@ from osm2gtfs.creators.routes_creator import RoutesCreator -class RoutesCreatorAccra(RoutesCreator): +class RoutesCreatorGhAccra(RoutesCreator): def add_routes_to_feed(self, feed, data): # Get routes information diff --git a/osm2gtfs/creators/accra/schedule_creator_accra.py b/osm2gtfs/creators/gh_accra/schedule_creator_gh_accra.py similarity index 80% rename from osm2gtfs/creators/accra/schedule_creator_accra.py rename to osm2gtfs/creators/gh_accra/schedule_creator_gh_accra.py index 6803db5d..b7719c9d 100644 --- a/osm2gtfs/creators/accra/schedule_creator_accra.py +++ b/osm2gtfs/creators/gh_accra/schedule_creator_gh_accra.py @@ -3,7 +3,7 @@ from osm2gtfs.creators.schedule_creator import ScheduleCreator -class ScheduleCreatorAccra(ScheduleCreator): +class ScheduleCreatorGhAccra(ScheduleCreator): def add_schedule_to_data(self, data): # Don't use any schedule source diff --git a/osm2gtfs/creators/accra/stops_creator_accra.py b/osm2gtfs/creators/gh_accra/stops_creator_gh_accra.py similarity index 98% rename from osm2gtfs/creators/accra/stops_creator_accra.py rename to osm2gtfs/creators/gh_accra/stops_creator_gh_accra.py index 9733e354..cf0b6376 100644 --- a/osm2gtfs/creators/accra/stops_creator_accra.py +++ b/osm2gtfs/creators/gh_accra/stops_creator_gh_accra.py @@ -31,7 +31,7 @@ def get_stop_id(stop): return stop.osm_id -class StopsCreatorAccra(StopsCreator): +class StopsCreatorGhAccra(StopsCreator): def add_stops_to_feed(self, feed, data): stops = data.get_stops() diff --git a/osm2gtfs/creators/accra/trips_creator_accra.py b/osm2gtfs/creators/gh_accra/trips_creator_gh_accra.py similarity index 99% rename from osm2gtfs/creators/accra/trips_creator_accra.py rename to osm2gtfs/creators/gh_accra/trips_creator_gh_accra.py index 9b27a87d..6ad142d6 100644 --- a/osm2gtfs/creators/accra/trips_creator_accra.py +++ b/osm2gtfs/creators/gh_accra/trips_creator_gh_accra.py @@ -7,7 +7,7 @@ from osm2gtfs.core.elements import Line -class TripsCreatorAccra(TripsCreator): +class TripsCreatorGhAccra(TripsCreator): service_weekday = None def add_trips_to_feed(self, feed, data): diff --git a/osm2gtfs/creators/esteli/esteli.json b/osm2gtfs/creators/ni_esteli/config.json similarity index 92% rename from osm2gtfs/creators/esteli/esteli.json rename to osm2gtfs/creators/ni_esteli/config.json index 85a1904b..bf0a2706 100644 --- a/osm2gtfs/creators/esteli/esteli.json +++ b/osm2gtfs/creators/ni_esteli/config.json @@ -30,6 +30,6 @@ "version": "0.1" }, "schedule_source": "https://github.com/mapanica/pt-data-esteli/raw/master/timetable.json", - "output_file": "data/ni-esteli.zip", - "selector": "esteli" + "output_file": "data/ni-esteli-gtfs.zip", + "selector": "ni_esteli" } diff --git a/osm2gtfs/creators/managua/managua.json b/osm2gtfs/creators/ni_managua/config.json similarity index 97% rename from osm2gtfs/creators/managua/managua.json rename to osm2gtfs/creators/ni_managua/config.json index 36b90910..71c74c28 100644 --- a/osm2gtfs/creators/managua/managua.json +++ b/osm2gtfs/creators/ni_managua/config.json @@ -34,5 +34,5 @@ }, "schedule_source": "https://raw.githubusercontent.com/mapanica/pt-data-managua/master/timetable.json", "output_file": "data/ni-managua-gtfs.zip", - "selector": "managua" + "selector": "ni_managua" } diff --git a/osm2gtfs/tests/README.md b/osm2gtfs/tests/README.md index ef0a6beb..ea46d576 100644 --- a/osm2gtfs/tests/README.md +++ b/osm2gtfs/tests/README.md @@ -1,11 +1,38 @@ osm2gtfs tests ============== -Accra ------- +To run all the tests (from the root `osm2gtfs` folder) : + + $ python -m unittest discover -v -t . + +## Core + +Tests for core components must be named and placed in the following schema: + `core/test_.py` + +### Creators Factory + +#### Description + +The creator factory allows certain parts of the GTFS generation from OSM data +to be overridden by different regions and networks. The tests make sure that +newly added creators follow the established naming convention of: + `_[_]` + +#### How to run + + $ python osm2gtfs/tests/core/tests_creator_factory.py + +## Creators + +Tests for core components must be named and placed in the following schema: + `creators/test_.py` + +### Accra, Ghana + #### Description -There are 3 unittests for the Accra GTFS generation +There are 3 acceptance tests for the Accra GTFS generation 1. Obtain mock-up data on stops from OpenStreetMap and cache it, 1. Obtain mock-up data on routes from OpenStreetMap and cache it, @@ -13,14 +40,9 @@ There are 3 unittests for the Accra GTFS generation #### Validation of the GTFS -The generated GTFS is checked against a reference GTFS file (accra_tests.zip.ref) -in the `tests/fixtures/accra/` folder. For the moment, only the size of each GTFS file is compared to the reference. +The generated GTFS is checked against a reference GTFS file (gh_accra_tests.zip.ref) +in the `tests/creators/fixtures/gh_accra/` folder. For the moment, only the size of each GTFS file is compared to the reference. #### How to run -To run all the tests (from the root `osm2gtfs` folder) : - - $ python -m unittest discover - -To run only the Accra tests : - $ python osm2gtfs/tests/tests_accra.py + $ python osm2gtfs/tests/creators/tests_gh_accra.py diff --git a/osm2gtfs/tests/core/__init__.py b/osm2gtfs/tests/core/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/osm2gtfs/tests/core/tests_creator_factory.py b/osm2gtfs/tests/core/tests_creator_factory.py new file mode 100644 index 00000000..381cefb2 --- /dev/null +++ b/osm2gtfs/tests/core/tests_creator_factory.py @@ -0,0 +1,115 @@ +# coding=utf-8 + +import unittest +import os +import importlib +from osm2gtfs.core.configuration import Configuration + + +class TestCoreCreatorFactory(unittest.TestCase): + + def test_naming_convention(self): + + path = os.path.dirname(__file__) + "/../../creators/" + creators = ['agency', 'feed_info', 'routes', 'schedule', 'stops', 'trips'] + + # pylint: disable=unused-variable + for subdir, dirs, files in os.walk(path): + + # Loop through available directories of creators + for selector in dirs: + + # Check if creator directory fits naming convention + self.assertTrue( + self._check_naming_convention(selector), + 'The path "creators/' + selector + + ' " doesn\'t fit the naming convention.') + + # Check for existing configuration file + config_file_path = path + selector + "/config.json" + self.assertTrue( + os.path.isfile(config_file_path), + 'No correctly named configuration file found for: ' + + selector) + + # Check whether selector in config file is correctly + config = Configuration.load_config_file(open(config_file_path)) + self.assertEqual( + selector, config['selector'], + "The selector (" + config['selector'] + + ") in config file is not equal to the directory name: " + + selector) + + # Loop through available files in creator directories and check + # whether they are correctly named + for inner_subdir, inner_dirs, inner_files in os.walk( + path + selector): + + for filename in inner_files: + # Check the Python files (and ignore the others) + if filename.endswith( + '.py') and not filename == "__init__.py": + + # Check if selector has been properly applied to + # file name + self.assertTrue( + filename.endswith(selector + '.py'), + "This file is not well named: " + filename) + + # Check for valid creators + split_filename = filename.split("_") + if split_filename[0] == "feed": + split_filename[0] = split_filename[0] + split_filename.pop(1) + self.assertTrue( + split_filename[0] in creators, + "This file is not well named: " + filename) + self.assertEqual( + split_filename[1], "creator", + "This file is not well named: " + filename) + + # Try to import the content of the creator module + correct_class_name_state = True + try: + module = importlib.import_module( + ".creators." + selector + "." + filename[:-3], + package="osm2gtfs") + except ImportError: + correct_class_name_state = False + + # Snake to camel case to test for correct class names + classname = ''.join(x.capitalize() for x in filename[:-3].split('_')) + try: + var = getattr(module, classname) + except AttributeError: + correct_class_name_state = False + + self.assertTrue(correct_class_name_state, + "The required class " + classname + + " wasn't found in " + filename + " ") + + def _check_naming_convention(self, selector): + check = True + if "_" in selector: + split_selector = selector.split("_") + + # Check for ISO 3166-2 country code + if not len(split_selector[0]) == 2: + check = False + + # Check for amount of split elements; can only be 2 or three + if len(split_selector) < 2 or len(split_selector) > 3: + check = False + else: + return False + return check + + +def load_tests(loader, tests, pattern): + # pylint: disable=unused-argument + test_cases = ['test_naming_convention'] + suite = unittest.TestSuite(map(TestCoreCreatorFactory, test_cases)) + return suite + + +if __name__ == '__main__': + unittest.main() diff --git a/osm2gtfs/tests/creators/__init__.py b/osm2gtfs/tests/creators/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/osm2gtfs/tests/fixtures/accra/accra_tests.zip.ref b/osm2gtfs/tests/creators/fixtures/gh_accra/gh_accra_tests.zip.ref similarity index 100% rename from osm2gtfs/tests/fixtures/accra/accra_tests.zip.ref rename to osm2gtfs/tests/creators/fixtures/gh_accra/gh_accra_tests.zip.ref diff --git a/osm2gtfs/tests/fixtures/accra/overpass-routes.xml b/osm2gtfs/tests/creators/fixtures/gh_accra/overpass-routes.xml similarity index 100% rename from osm2gtfs/tests/fixtures/accra/overpass-routes.xml rename to osm2gtfs/tests/creators/fixtures/gh_accra/overpass-routes.xml diff --git a/osm2gtfs/tests/fixtures/accra/overpass-stops.xml b/osm2gtfs/tests/creators/fixtures/gh_accra/overpass-stops.xml similarity index 100% rename from osm2gtfs/tests/fixtures/accra/overpass-stops.xml rename to osm2gtfs/tests/creators/fixtures/gh_accra/overpass-stops.xml diff --git a/osm2gtfs/tests/tests_accra.py b/osm2gtfs/tests/creators/tests_gh_accra.py similarity index 88% rename from osm2gtfs/tests/tests_accra.py rename to osm2gtfs/tests/creators/tests_gh_accra.py index 74e562cf..5a954415 100644 --- a/osm2gtfs/tests/tests_accra.py +++ b/osm2gtfs/tests/creators/tests_gh_accra.py @@ -17,8 +17,9 @@ class Args(): def __init__(self, config_file): self.config = open(config_file) - self.selector = "accra" - self.output = os.path.realpath(os.path.join(current_dir, "../../data/accra_tests.zip")) + self.selector = "gh_accra" + self.output = os.path.realpath( + os.path.join(current_dir, "../../../data/gh_accra_tests.zip")) def is_valid_gtfs(gtfs): @@ -175,13 +176,13 @@ def get_gtfs_infos(gtfs): return gtfs_infos -class TestAccra(unittest.TestCase): +class TestCreatorsGhAccra(unittest.TestCase): def setUp(self): - self.data_dir = os.path.realpath(os.path.join(current_dir, "../../data/")) + self.data_dir = os.path.realpath(os.path.join(current_dir, "../../../data/")) self.config_file = os.path.realpath( - os.path.join(current_dir, "../creators/accra/accra.json") + os.path.join(current_dir, "../../creators/gh_accra/config.json") ) - self.fixture_folder = os.path.join(os.path.realpath(current_dir), "fixtures/accra/") + self.fixture_folder = os.path.join(os.path.realpath(current_dir), "fixtures/gh_accra/") args = Args(self.config_file) self.config = Configuration(args) # deactivation of Overpass calls for unnamed stops @@ -189,7 +190,7 @@ def setUp(self): def test_refresh_routes_cache(self): data = OsmConnector(self.config) - cache_file = os.path.join(self.data_dir, "accra-routes.pkl") + cache_file = os.path.join(self.data_dir, "gh_accra-routes.pkl") mocked_overpass_data_file = os.path.join(self.fixture_folder, "overpass-routes.xml") if os.path.isfile(cache_file): os.remove(cache_file) @@ -200,12 +201,12 @@ def test_refresh_routes_cache(self): data.get_routes(refresh=True) self.assertTrue(os.path.isfile(cache_file), 'The routes cache file creation failed') cache = Cache() - routes = cache.read_data('accra-routes') + routes = cache.read_data('gh_accra-routes') self.assertEqual(len(routes), 277, 'Wrong count of routes in the cache file') def test_refresh_stops_cache(self): data = OsmConnector(self.config) - cache_file = os.path.join(self.data_dir, "accra-stops.pkl") + cache_file = os.path.join(self.data_dir, "gh_accra-stops.pkl") mocked_overpass_data_file = os.path.join(self.fixture_folder, "overpass-stops.xml") if os.path.isfile(cache_file): os.remove(cache_file) @@ -216,16 +217,18 @@ def test_refresh_stops_cache(self): data.get_stops(refresh=True) self.assertTrue(os.path.isfile(cache_file), 'The stops cache file creation failed') cache = Cache() - stops = cache.read_data('accra-stops') + stops = cache.read_data('gh_accra-stops') amount_of_stops = len(stops['regular']) + len(stops['stations']) self.assertEqual(amount_of_stops, 2529, 'Wrong count of stops in the cache file') def test_gtfs_from_cache(self): # the cache is generated by the previous two functions - routes_cache_file = os.path.join(self.data_dir, "accra-routes.pkl") - stops_file = os.path.join(self.data_dir, "accra-stops.pkl") - self.assertTrue(os.path.isfile(stops_file), "The stops cache file doesn't exists") - self.assertTrue(os.path.isfile(routes_cache_file), "The routes cache file doesn't exists") + routes_cache_file = os.path.join(self.data_dir, "gh_accra-routes.pkl") + stops_file = os.path.join(self.data_dir, "gh_accra-stops.pkl") + self.assertTrue( + os.path.isfile(stops_file), "The stops cache file doesn't exists") + self.assertTrue( + os.path.isfile(routes_cache_file), "The routes cache file doesn't exists") data = OsmConnector(self.config) @@ -248,10 +251,11 @@ def test_gtfs_from_cache(self): # Write GTFS feed.WriteGoogleTransitFeed(self.config.output) - gtfs_expected_result = os.path.join(self.fixture_folder, "accra_tests.zip.ref") - gtfs_generated_result = os.path.join(self.data_dir, "accra_tests.zip") - self.assertTrue(is_valid_gtfs(gtfs_generated_result), "The generated GTFS is not valid") - self.assertTrue(is_valid_gtfs(gtfs_expected_result), "The expected GTFS is not valid") + gtfs_expected_result = os.path.join(self.fixture_folder, "gh_accra_tests.zip.ref") + gtfs_generated_result = os.path.join(self.data_dir, "gh_accra_tests.zip") + self.assertTrue(is_valid_gtfs(gtfs_generated_result), 'The generated GTFS is not valid') + self.assertTrue(is_valid_gtfs(gtfs_expected_result), 'The expected GTFS is not valid') + self.assertTrue(is_identical_gtfs(gtfs_expected_result, gtfs_generated_result), "The generated GTFS is different from the expected one") gtfs_infos = get_gtfs_infos(gtfs_generated_result) @@ -270,8 +274,9 @@ def test_gtfs_from_cache(self): def load_tests(loader, tests, pattern): # pylint: disable=unused-argument test_cases = ['test_refresh_routes_cache', 'test_refresh_stops_cache', 'test_gtfs_from_cache'] - suite = unittest.TestSuite(map(TestAccra, test_cases)) + suite = unittest.TestSuite(map(TestCreatorsGhAccra, test_cases)) return suite + if __name__ == '__main__': unittest.main()