From edc428a3fd1a85d008a6f2fdf8fe29244c5b8453 Mon Sep 17 00:00:00 2001 From: Francia Csaba Date: Mon, 5 Feb 2024 15:29:06 +0100 Subject: [PATCH] moved tests to enhancer --- .gitignore | 1 + amarillo/plugins/enhancer/tests/__init__.py | 0 amarillo/plugins/enhancer/tests/stops.csv | 5 + amarillo/plugins/enhancer/tests/stops.json | 39 +++++ amarillo/plugins/enhancer/tests/test_gtfs.py | 142 ++++++++++++++++++ .../enhancer/tests/test_stops_store.py | 24 +++ .../plugins/enhancer/tests/test_trip_store.py | 23 +++ config | 5 + logging.conf | 22 +++ 9 files changed, 261 insertions(+) create mode 100644 amarillo/plugins/enhancer/tests/__init__.py create mode 100644 amarillo/plugins/enhancer/tests/stops.csv create mode 100644 amarillo/plugins/enhancer/tests/stops.json create mode 100644 amarillo/plugins/enhancer/tests/test_gtfs.py create mode 100644 amarillo/plugins/enhancer/tests/test_stops_store.py create mode 100644 amarillo/plugins/enhancer/tests/test_trip_store.py create mode 100644 config create mode 100644 logging.conf diff --git a/.gitignore b/.gitignore index 5d381cc..d33bc9a 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,4 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +data/ \ No newline at end of file diff --git a/amarillo/plugins/enhancer/tests/__init__.py b/amarillo/plugins/enhancer/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/amarillo/plugins/enhancer/tests/stops.csv b/amarillo/plugins/enhancer/tests/stops.csv new file mode 100644 index 0000000..ed419bb --- /dev/null +++ b/amarillo/plugins/enhancer/tests/stops.csv @@ -0,0 +1,5 @@ +stop_id;stop_code;stop_lat;stop_lon;stop_name +mfdz:x;x;52.11901;14.2;Stop x +mfdz:y;y;53.1;14.01;Stop y +mfdz:z;z;54.11;14.0;Stop z +mfdz:Ang001;Ang001;53.11901;14.015776;Mitfahrbank Biesenbrow diff --git a/amarillo/plugins/enhancer/tests/stops.json b/amarillo/plugins/enhancer/tests/stops.json new file mode 100644 index 0000000..5744c13 --- /dev/null +++ b/amarillo/plugins/enhancer/tests/stops.json @@ -0,0 +1,39 @@ +{ + "data": { + "pointsOfInterest": [ + { + "id": "14622", + "externalId": "bbnavi:12073:0001", + "name": "Parkbank", + "description": "Parkbank", + "dataProvider": { + "id": "1", + "name": "Administrator" + }, + "addresses": [ + { + "street": "Hauptstrasse", + "city": "Wittenberge", + "zip": "12345", + "geoLocation": { + "latitude": 52.9932971109789, + "longitude": 11.767383582547 + } + } + ], + "openStreetMap": { + "capacity": 112, + "capacityCharging": "2", + "capacityDisabled": "", + "fee": "No", + "lit": "Yes", + "parking": "", + "shelter": "No", + "surface": "", + "utilization": "", + "website": "" + } + } + ] + } +} \ No newline at end of file diff --git a/amarillo/plugins/enhancer/tests/test_gtfs.py b/amarillo/plugins/enhancer/tests/test_gtfs.py new file mode 100644 index 0000000..3fbe97c --- /dev/null +++ b/amarillo/plugins/enhancer/tests/test_gtfs.py @@ -0,0 +1,142 @@ +from amarillo.tests.sampledata import carpool_1234, data1, carpool_repeating_json, stop_issue +from amarillo.plugins.enhancer.services.gtfs_export import GtfsExport +from amarillo.plugins.enhancer.services.gtfs import GtfsRtProducer +from amarillo.plugins.enhancer.services.stops import StopsStore +from amarillo.plugins.enhancer.services.trips import TripStore +from amarillo.models.Carpool import Carpool +from datetime import datetime +import time +import pytest + + +def test_gtfs_generation(): + cp = Carpool(**data1) + stops_store = StopsStore() + trips_store = TripStore(stops_store) + trips_store.put_carpool(cp) + + exporter = GtfsExport(None, None, trips_store, stops_store) + exporter.export('target/tests/test_gtfs_generation/test.gtfs.zip', "target/tests/test_gtfs_generation") + +def test_correct_stops(): + cp = Carpool(**stop_issue) + stops_store = StopsStore([{"url": "https://datahub.bbnavi.de/export/rideshare_points.geojson", "vicinity": 250}]) + stops_store.load_stop_sources() + trips_store = TripStore(stops_store) + trips_store.put_carpool(cp) + assert len(trips_store.trips) == 1 + + +class TestTripConverter: + + def setup_method(self, method): + self.stops_store = StopsStore([{"url": "https://datahub.bbnavi.de/export/rideshare_points.geojson", "vicinity": 50}]) + self.trips_store = TripStore(self.stops_store) + + def test_as_one_time_trip_as_delete_update(self): + cp = Carpool(**data1) + self.trips_store.put_carpool(cp) + trip = next(iter(self.trips_store.trips.values())) + + converter = GtfsRtProducer(self.trips_store) + json = converter._as_delete_updates(trip, datetime(2022,4,11)) + + assert json == [{ + 'trip': { + 'tripId': 'mfdz:Eins', + 'startTime': '23:59:00', + 'startDate': '20220530', + 'scheduleRelationship': 'CANCELED', + 'routeId': 'mfdz:Eins' + } + }] + + def test_as_one_time_trip_as_added_update(self): + cp = Carpool(**data1) + self.trips_store.put_carpool(cp) + trip = next(iter(self.trips_store.trips.values())) + + converter = GtfsRtProducer(self.trips_store) + json = converter._as_added_updates(trip, datetime(2022,4,11)) + assert json == [{ + 'trip': { + 'tripId': 'mfdz:Eins', + 'startTime': '23:59:00', + 'startDate': '20220530', + 'scheduleRelationship': 'ADDED', + 'routeId': 'mfdz:Eins', + '[transit_realtime.trip_descriptor]': { + 'routeUrl' : 'https://mfdz.de/trip/123', + 'agencyId' : 'mfdz', + 'route_long_name' : 'abc nach xyz', + 'route_type': 1551 + } + }, + 'stopTimeUpdate': [{ + 'stopSequence': 1, + 'arrival': { + 'time': time.mktime(datetime(2022,5,30,23,59,0).timetuple()), + 'uncertainty': 600 + }, + 'departure': { + 'time': time.mktime(datetime(2022,5,30,23,59,0).timetuple()), + 'uncertainty': 600 + }, + 'stopId': 'mfdz:12073:001', + 'scheduleRelationship': 'SCHEDULED', + 'stop_time_properties': { + '[transit_realtime.stop_time_properties]': { + 'dropoffType': 'NONE', + 'pickupType': 'COORDINATE_WITH_DRIVER' + } + } + }, + { + 'stopSequence': 2, + 'arrival': { + 'time': time.mktime(datetime(2022,5,31,0,16,45,0).timetuple()), + 'uncertainty': 600 + }, + 'departure': { + 'time': time.mktime(datetime(2022,5,31,0,16,45,0).timetuple()), + 'uncertainty': 600 + }, + + 'stopId': 'de:12073:900340137::3', + 'scheduleRelationship': 'SCHEDULED', + 'stop_time_properties': { + '[transit_realtime.stop_time_properties]': { + 'dropoffType': 'COORDINATE_WITH_DRIVER', + 'pickupType': 'NONE' + } + } + }] + }] + + def test_as_periodic_trip_as_delete_update(self): + cp = Carpool(**carpool_repeating_json) + self.trips_store.put_carpool(cp) + trip = next(iter(self.trips_store.trips.values())) + + converter = GtfsRtProducer(self.trips_store) + json = converter._as_delete_updates(trip, datetime(2022,4,11)) + + assert json == [{ + 'trip': { + 'tripId': 'mfdz:Zwei', + 'startTime': '15:00:00', + 'startDate': '20220411', + 'scheduleRelationship': 'CANCELED', + 'routeId': 'mfdz:Zwei' + } + }, + { + 'trip': { + 'tripId': 'mfdz:Zwei', + 'startTime': '15:00:00', + 'startDate': '20220418', + 'scheduleRelationship': 'CANCELED', + 'routeId': 'mfdz:Zwei' + } + } + ] \ No newline at end of file diff --git a/amarillo/plugins/enhancer/tests/test_stops_store.py b/amarillo/plugins/enhancer/tests/test_stops_store.py new file mode 100644 index 0000000..95cd3a2 --- /dev/null +++ b/amarillo/plugins/enhancer/tests/test_stops_store.py @@ -0,0 +1,24 @@ +from amarillo.plugins.enhancer.services import stops +from amarillo.models.Carpool import StopTime + +def test_load_stops_from_file(): + store = stops.StopsStore([{"url": "amarillo/plugins/enhancer/tests/stops.csv", "vicinity": 50}]) + store.load_stop_sources() + assert len(store.stopsDataFrames[0]['stops']) > 0 + +def test_load_csv_stops_from_web_(): + store = stops.StopsStore([{"url": "https://data.mfdz.de/mfdz/stops/custom.csv", "vicinity": 50}]) + store.load_stop_sources() + assert len(store.stopsDataFrames[0]['stops']) > 0 + +def test_load_geojson_stops_from_web_(): + store = stops.StopsStore([{"url": "https://datahub.bbnavi.de/export/rideshare_points.geojson", "vicinity": 50}]) + store.load_stop_sources() + assert len(store.stopsDataFrames[0]['stops']) > 0 + +def test_find_closest_stop(): + store = stops.StopsStore([{"url": "amarillo/plugins/enhancer/tests/stops.csv", "vicinity": 50}]) + store.load_stop_sources() + carpool_stop = StopTime(name="start", lat=53.1191, lon=14.01577) + stop = store.find_closest_stop(carpool_stop, 1000) + assert stop.name=='Mitfahrbank Biesenbrow' diff --git a/amarillo/plugins/enhancer/tests/test_trip_store.py b/amarillo/plugins/enhancer/tests/test_trip_store.py new file mode 100644 index 0000000..6447668 --- /dev/null +++ b/amarillo/plugins/enhancer/tests/test_trip_store.py @@ -0,0 +1,23 @@ +from amarillo.tests.sampledata import cp1, carpool_repeating +from amarillo.plugins.enhancer.services.trips import TripStore +from amarillo.plugins.enhancer.services.stops import StopsStore + + +import logging +logger = logging.getLogger(__name__) + +def test_trip_store_put_one_time_carpool(): + trip_store = TripStore(StopsStore()) + + t = trip_store.put_carpool(cp1) + assert t != None + assert len(t.stop_times) >= 2 + assert t.stop_times[0].stop_id == 'mfdz:12073:001' + assert t.stop_times[-1].stop_id == 'de:12073:900340137::3' + +def test_trip_store_put_repeating_carpool(): + trip_store = TripStore(StopsStore()) + + t = trip_store.put_carpool(carpool_repeating) + assert t != None + assert len(t.stop_times) >= 2 diff --git a/config b/config new file mode 100644 index 0000000..fadf873 --- /dev/null +++ b/config @@ -0,0 +1,5 @@ +# Bounding-Box Germany +ride2go_query_data = '{ "southWestCoordinates": { "lat": 47.3, "lon": 5.98 }, "northEastCoordinates": { "lat": 54.99, "lon": 15.02 }, "lastModifiedSinceDays": 180 }' +env = 'PROD' +graphhopper_base_url = 'https://api.mfdz.de/gh' +stop_sources_file = 'conf/stop_sources.json' \ No newline at end of file diff --git a/logging.conf b/logging.conf new file mode 100644 index 0000000..429da8e --- /dev/null +++ b/logging.conf @@ -0,0 +1,22 @@ +[loggers] +keys=root + +[handlers] +keys=consoleHandler + +[formatters] +keys=simpleFormatter + +[logger_root] +level=INFO +handlers=consoleHandler +propagate=yes + +[handler_consoleHandler] +class=StreamHandler +level=DEBUG +formatter=simpleFormatter +args=(sys.stdout,) + +[formatter_simpleFormatter] +format=%(asctime)s - %(name)s - %(levelname)s - %(message)s \ No newline at end of file