def test_list_without_stoping_point(self): list_processor = WaypointListProcessor( self.waypoints_list_without_stoping_point) assert list_processor.get_trips() == [ Trip(distance=567954.096, start=Waypoint(timestamp='2018-08-10T20:10:22Z', lat=54.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:22:22Z', lat=59.64987, lng=12.41039)) ]
def test_list_starts_with_not_moving_points(self): # List beginning with same points, which means that # the car has not moved yet list_processor = WaypointListProcessor( self.waypoints_list_starts_with_not_moving_points) assert list_processor.get_trips() == [ Trip(distance=779395.536, start=Waypoint(timestamp='2018-08-10T20:10:22Z', lat=52.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:17:22Z', lat=59.54987, lng=12.41039)) ]
def test_stream_starts_with_not_moving_points(self): stream_processor = WaypointStreamProcessor() results = [] for waypoint in self.waypoints_stream_starts_with_not_moving_points: results.append(stream_processor.process_waypoint(waypoint)) assert results == [ None, None, None, None, None, None, None, None, Trip(distance=556802.412, start=Waypoint(timestamp='2018-08-10T20:16:22Z', lat=54.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:24:22Z', lat=59.54987, lng=12.41039)) ]
def test_list_with_two_trips(self): list_processor = WaypointListProcessor( self.waypoints_list_with_two_trips) assert list_processor.get_trips() == [ Trip(distance=222593.124, start=Waypoint(timestamp='2018-08-10T20:04:22Z', lat=52.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:06:22Z', lat=54.54987, lng=12.41039)), Trip(distance=556802.412, start=Waypoint(timestamp='2018-08-10T20:10:22Z', lat=54.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:17:22Z', lat=59.54987, lng=12.41039)) ]
def test_stream_with_two_trips(self): stream_processor = WaypointStreamProcessor() results = [] for waypoint in self.waypoints_stream_with_two_trips: results.append(stream_processor.process_waypoint(waypoint)) assert results == [ None, None, None, None, None, None, Trip(distance=445406.435, start=Waypoint(timestamp='2018-08-10T20:15:22Z', lat=54.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:23:22Z', lat=58.54987, lng=12.41039)), None, None, Trip(distance=222809.097, start=Waypoint(timestamp='2018-08-10T20:29:22Z', lat=58.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:31:22Z', lat=60.54987, lng=12.41039)) ]
def create_point(self, timestamp: str, lat: str, lng: str) -> Waypoint: """ Coverts string params into Waypoint :param timestamp: str :param lat: str :param lng: str :return: Waypoint """ ts = datetime.strptime(timestamp, self.TIMESTAMP_FORMAT) latitude = float(lat) longitude = float(lng) return Waypoint(ts, latitude, longitude)
def test_trip_waypoint_format(self): data_points = [ Trip(distance=1125.0192831925704, start=Waypoint(timestamp='2018-08-10T20:04:22Z', lat=52.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:10:22Z', lat=52.55998, lng=12.41039)) ] expected_result = [{ 'start': { 'timestamp': '2018-08-10T20:04:22Z', 'lat': 52.54987, 'lng': 12.41039 }, 'end': { 'timestamp': '2018-08-10T20:10:22Z', 'lat': 52.55998, 'lng': 12.41039 }, 'distance': 1125.0192831925704 }] assert trip_waypoint_format(data_points) == expected_result
class TestWaypointStreamProcessor(FixtureTestWaypointStreamProcessor): @pytest.mark.parametrize( "current_point, next_point, expected", [(Waypoint("2018-08-10T20:04:22Z", 52.54987, 12.41039), Waypoint("2018-08-10T20:04:22Z", 51.54987, 12.41039), True), (Waypoint("2018-08-10T20:04:22Z", 52.54987, 12.41039), Waypoint("2018-08-10T20:04:22Z", 52.54987, 12.41039), False)]) def test_car_on_move(self, current_point, next_point, expected): list_processor = WaypointListProcessor([]) assert list_processor._car_in_move(current_point, next_point) == expected @pytest.mark.parametrize( "points, expected", [ # stop for 2 mintues ([ Waypoint("2018-08-10T20:04:22Z", 51.54987, 12.41039), Waypoint("2018-08-10T20:06:22Z", 51.54987, 12.41039) ], False), # points with 1 point ([Waypoint("2018-08-10T20:04:22Z", 52.54987, 12.41039)], False), # stop for more than 3 mintues ([ Waypoint("2018-08-10T20:04:22Z", 51.54987, 12.41039), Waypoint("2018-08-10T20:10:22Z", 51.54987, 12.41039) ], True), ]) def test_car_trip_has_ended(self, points, expected): list_processor = WaypointListProcessor([]) assert list_processor._car_trip_has_ended(points) == expected def test_stream_with_one_trip(self): stream_processor = WaypointStreamProcessor() results = [] for waypoint in self.waypoints_stream_with_one_trip: results.append(stream_processor.process_waypoint(waypoint)) assert results == [ None, None, None, None, Trip(distance=556802.412, start=Waypoint(timestamp='2018-08-10T20:10:22Z', lat=54.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:16:22Z', lat=59.54987, lng=12.41039)) ] def test_stream_starts_with_not_moving_points(self): stream_processor = WaypointStreamProcessor() results = [] for waypoint in self.waypoints_stream_starts_with_not_moving_points: results.append(stream_processor.process_waypoint(waypoint)) assert results == [ None, None, None, None, None, None, None, None, Trip(distance=556802.412, start=Waypoint(timestamp='2018-08-10T20:16:22Z', lat=54.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:24:22Z', lat=59.54987, lng=12.41039)) ] def test_stream_with_two_trips(self): stream_processor = WaypointStreamProcessor() results = [] for waypoint in self.waypoints_stream_with_two_trips: results.append(stream_processor.process_waypoint(waypoint)) assert results == [ None, None, None, None, None, None, Trip(distance=445406.435, start=Waypoint(timestamp='2018-08-10T20:15:22Z', lat=54.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:23:22Z', lat=58.54987, lng=12.41039)), None, None, Trip(distance=222809.097, start=Waypoint(timestamp='2018-08-10T20:29:22Z', lat=58.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:31:22Z', lat=60.54987, lng=12.41039)) ]
class TestWaypointListProcessor(FixtureTestWaypointListProcessor): @pytest.mark.parametrize( "current_point, next_point, expected", [(Waypoint("2018-08-10T20:04:22Z", 52.54987, 12.41039), Waypoint("2018-08-10T20:04:22Z", 51.54987, 12.41039), True), (Waypoint("2018-08-10T20:04:22Z", 52.54987, 12.41039), Waypoint("2018-08-10T20:04:22Z", 52.54987, 12.41039), False)]) def test_car_on_move(self, current_point, next_point, expected): list_processor = WaypointListProcessor([]) assert list_processor._car_in_move(current_point, next_point) == expected @pytest.mark.parametrize( "points, expected", [ # stop for 2 mintues ([ Waypoint("2018-08-10T20:04:22Z", 51.54987, 12.41039), Waypoint("2018-08-10T20:06:22Z", 51.54987, 12.41039) ], False), # points with 1 point ([Waypoint("2018-08-10T20:04:22Z", 52.54987, 12.41039)], False), # stop for more than 3 mintues ([ Waypoint("2018-08-10T20:04:22Z", 51.54987, 12.41039), Waypoint("2018-08-10T20:10:22Z", 51.54987, 12.41039) ], True), ]) def test_car_trip_has_ended(self, points, expected): list_processor = WaypointListProcessor([]) assert list_processor._car_trip_has_ended(points) == expected def test_list_without_stoping_point(self): list_processor = WaypointListProcessor( self.waypoints_list_without_stoping_point) assert list_processor.get_trips() == [ Trip(distance=567954.096, start=Waypoint(timestamp='2018-08-10T20:10:22Z', lat=54.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:22:22Z', lat=59.64987, lng=12.41039)) ] def test_list_with_two_trips(self): list_processor = WaypointListProcessor( self.waypoints_list_with_two_trips) assert list_processor.get_trips() == [ Trip(distance=222593.124, start=Waypoint(timestamp='2018-08-10T20:04:22Z', lat=52.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:06:22Z', lat=54.54987, lng=12.41039)), Trip(distance=556802.412, start=Waypoint(timestamp='2018-08-10T20:10:22Z', lat=54.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:17:22Z', lat=59.54987, lng=12.41039)) ] def test_list_starts_with_not_moving_points(self): # List beginning with same points, which means that # the car has not moved yet list_processor = WaypointListProcessor( self.waypoints_list_starts_with_not_moving_points) assert list_processor.get_trips() == [ Trip(distance=779395.536, start=Waypoint(timestamp='2018-08-10T20:10:22Z', lat=52.54987, lng=12.41039), end=Waypoint(timestamp='2018-08-10T20:17:22Z', lat=59.54987, lng=12.41039)) ] def test_list_with_distance_less_15m(self): list_processor = WaypointListProcessor( self.waypoints_list_with_distance_less_15m) assert list_processor.get_trips() == []
class FixtureTestWaypointStreamProcessor(): waypoints_stream_with_one_trip = [ Waypoint("2018-08-10T20:10:22Z", 54.54987, 12.41039), Waypoint("2018-08-10T20:15:22Z", 57.54987, 12.41039), Waypoint("2018-08-10T20:16:22Z", 59.54987, 12.41039), Waypoint("2018-08-10T20:17:22Z", 59.54987, 12.41039), Waypoint("2018-08-10T20:22:22Z", 59.54987, 12.41039) ] waypoints_stream_starts_with_not_moving_points = [ Waypoint("2018-08-10T20:10:22Z", 54.54987, 12.41039), Waypoint("2018-08-10T20:15:22Z", 54.54987, 12.41039), Waypoint("2018-08-10T20:16:22Z", 54.54987, 12.41039), Waypoint("2018-08-10T20:17:22Z", 56.54987, 12.41039), Waypoint("2018-08-10T20:22:22Z", 57.54987, 12.41039), Waypoint("2018-08-10T20:23:22Z", 58.54987, 12.41039), Waypoint("2018-08-10T20:24:22Z", 59.54987, 12.41039), Waypoint("2018-08-10T20:26:22Z", 59.54987, 12.41039), Waypoint("2018-08-10T20:28:22Z", 59.54987, 12.41039) ] waypoints_stream_with_two_trips = [ Waypoint("2018-08-10T20:10:22Z", 54.54987, 12.41039), Waypoint("2018-08-10T20:15:22Z", 54.54987, 12.41039), Waypoint("2018-08-10T20:16:22Z", 55.54987, 12.41039), Waypoint("2018-08-10T20:17:22Z", 56.54987, 12.41039), Waypoint("2018-08-10T20:22:22Z", 57.54987, 12.41039), Waypoint("2018-08-10T20:23:22Z", 58.54987, 12.41039), Waypoint("2018-08-10T20:29:22Z", 58.54987, 12.41039), Waypoint("2018-08-10T20:30:22Z", 59.54987, 12.41039), Waypoint("2018-08-10T20:31:22Z", 60.54987, 12.41039), Waypoint("2018-08-10T20:38:22Z", 60.54987, 12.41039) ]
class FixtureTestWaypointListProcessor(): waypoints_list_without_stoping_point = [ Waypoint("2018-08-10T20:10:22Z", 54.54987, 12.41039), Waypoint("2018-08-10T20:15:22Z", 57.54987, 12.41039), Waypoint("2018-08-10T20:16:22Z", 58.54987, 12.41039), Waypoint("2018-08-10T20:17:22Z", 58.54982, 12.41039), Waypoint("2018-08-10T20:22:22Z", 59.64987, 12.41039) ] waypoints_list_with_two_trips = [ Waypoint("2018-08-10T20:04:22Z", 52.54987, 12.41039), Waypoint("2018-08-10T20:06:22Z", 54.54987, 12.41039), Waypoint("2018-08-10T20:08:22Z", 54.54987, 12.41039), Waypoint("2018-08-10T20:10:22Z", 54.54987, 12.41039), Waypoint("2018-08-10T20:15:22Z", 57.54987, 12.41039), Waypoint("2018-08-10T20:16:22Z", 58.54987, 12.41039), Waypoint("2018-08-10T20:17:22Z", 59.54987, 12.41039), Waypoint("2018-08-10T20:22:22Z", 59.54987, 12.41039) ] waypoints_list_starts_with_not_moving_points = [ Waypoint("2018-08-10T20:04:22Z", 52.54987, 12.41039), Waypoint("2018-08-10T20:06:22Z", 52.54987, 12.41039), Waypoint("2018-08-10T20:08:22Z", 52.54987, 12.41039), Waypoint("2018-08-10T20:10:22Z", 52.54987, 12.41039), Waypoint("2018-08-10T20:15:22Z", 57.54987, 12.41039), Waypoint("2018-08-10T20:16:22Z", 58.54987, 12.41039), Waypoint("2018-08-10T20:17:22Z", 59.54987, 12.41039), Waypoint("2018-08-10T20:18:22Z", 59.54987, 12.41039) ] waypoints_list_with_distance_less_15m = [ Waypoint("2018-08-10T20:04:22Z", 52.54987, 12.41039), Waypoint("2018-08-10T20:06:22Z", 52.54987, 12.41031), Waypoint("2018-08-10T20:10:22Z", 52.54991, 12.41036) ]
def convert_data_to_waypoints(data_points): return [Waypoint(**point) for point in data_points]