def test_end_to_end(): init_pos = Position(13.3764, 52.5461) dest1 = Position(13.4014, 52.5478) dest2 = Position(13.3393, 52.5053) clock = Clock() # to start not from the beginning clock.tick() clock.tick() router = LinearRouter(clock) engine = VehicleEngine(init_pos, router, clock) engine.start_move(dest1) assert engine.eta == 8 assert engine.current_position == init_pos clock.tick() clock.tick() assert engine.current_position != init_pos for _ in range(engine.eta - clock.clock_time): clock.tick() assert engine.current_position == dest1 assert not engine.is_moving() assert engine.eta == clock.clock_time engine.end_move() assert not engine.is_moving() assert engine.eta == clock.clock_time
def test_Dispatcher(): clock = Clock() router = LinearRouter(clock) fleet = Fleet(clock, router) vehicle = Vehicle(clock) fleet.infleet(vehicle, Position(13.3764, 52.5461)) cnt = Dispatcher() cnt.step() booking = Booking(clock, Position(13.4014, 52.5478), Position(13.3764, 52.5461)) itinerary = Itinerary(101, vehicle) itinerary.move_to(Position(13.4014, 52.5478)) itinerary.pickup(booking) itinerary.dropoff(booking) cnt.dispatch(itinerary) cnt.step() assert vehicle.is_moving assert booking.is_waiting_pickup() booking.set_pickup() cnt.dispatch(itinerary) cnt.step() assert booking.is_waiting_dropoff()
def test_dont_move_vehicle(): init_pos = Position(13.3764, 52.5461) dest1 = Position(13.4014, 52.5478) clock = Clock() router = LinearRouter(clock) engine = VehicleEngine(init_pos, router, clock) v = Vehicle(clock) v.install_engine(engine) assert v.position == init_pos assert v.destination is None v.move_to(init_pos) assert not v.engine.is_moving() assert v.engine.current_position == init_pos assert v.state == States.idling assert v.position == init_pos v.step() assert v.state == States.idling assert not v.engine.is_moving() assert v.engine.destination is None assert v.position == init_pos assert v.state == States.idling assert not v.engine.is_moving() assert v.engine.current_position == init_pos assert not v.is_moving
def test_change_flow(): clock = Clock() booking = Booking(clock, Position(13.4014, 52.5478), Position(13.3393, 52.5053), 3) booking.set_matched() assert booking.is_matched() is True with pytest.raises(Exception): booking._set_pending() booking.set_waiting_pickup() assert booking.is_waiting_pickup() is True with pytest.raises(Exception): booking.set_matched() booking.set_pickup() assert booking.is_pickup() is True with pytest.raises(Exception): booking.set_waiting_pickup() booking.set_waiting_dropoff() assert booking.is_waiting_dropoff() is True with pytest.raises(Exception): booking.set_pickup() booking.set_dropoff() assert booking.is_dropoff() is True with pytest.raises(Exception): booking.set_waiting_dropoff()
def test_Dispatcher_3(): clock = Clock() router = LinearRouter(clock) fleet = Fleet(clock, router) vehicle = Vehicle(clock) fleet.infleet(vehicle, Position(13.3764, 52.5461)) cnt = Dispatcher() booking = Booking(clock, Position(13.4014, 52.5478), Position(13.3764, 52.5461)) itinerary = Itinerary(101, vehicle) job1 = itinerary.pickup(booking) job2 = itinerary.move_to(Position(13.4014, 52.5478)) job3 = itinerary.dropoff(booking) cnt.dispatch(itinerary) cnt.step() # finish 2 jobs in 1 step assert itinerary.current_job is job2 assert booking.is_waiting_dropoff()
def test_expire(): clock = Clock() booking = Booking(clock, Position(13.4014, 52.5478), Position(13.3393, 52.5053), 3) booking.set_expired() with pytest.raises(Exception): booking.set_matched()
def test_pending_state(): clock = Clock() booking = Booking(clock, Position(13.4014, 52.5478), Position(13.3393, 52.5053), 3) assert booking.is_pending() is True with pytest.raises(Exception): booking._set_pending()
def test_route_destination(): origin = Position(-73.99780, 40.71205) destination = Position(-73.99567, 40.71689) clock = Clock(time_step=10, time_unit="s") router = LinearRouter(clock=clock) assert router.calculate_route(origin, destination).destination == destination
def test_map_match(): clock = Clock(time_step=1, time_unit="m") router = LinearRouter(speed=12, clock=clock) pos1 = Position(-0.39376, 39.5145) pos2 = Position(-0.38874, 39.503) for pos in [pos1, pos2]: pos_m = router.map_match(pos) assert pos == pos_m assert pos.id != pos_m.id
def test_create(): pickup = Position(13.3393, 52.5053) dropoff = Position(13.4014, 52.5478) seats = 30 preferences = {"cat": "AV"} clock = Clock() booking = Booking(clock, pickup, dropoff, seats, preferences) assert booking.seats == seats assert booking.pickup == pickup assert booking.dropoff == dropoff assert booking.preferences == preferences
def test_router2d_2(): speed_kmph = 17 nyc_pos = Position(-73.935242, 40.730610) nyc_pos_shift = Position(-73.935, 40.730610) clock = Clock(time_step=1, time_unit="s") router = LinearRouter(speed=speed_kmph, clock=clock) assert router.estimate_duration(nyc_pos, nyc_pos_shift) == 5 route = router.calculate_route(nyc_pos, nyc_pos_shift) assert len(route.coordinates) == 6 for p in route.coordinates: assert p.lat == nyc_pos.lat
def test_on_state_changed_pickup_position(): pickup = Position(13.4014, 52.5478) dropoff = Position(13.3393, 52.5053) clock = Clock() booking = Booking(clock, pickup, dropoff, 3) event_data = MagicMock() event_data.transition = MagicMock() for state in (States.dropoff.value, States.complete.value): event_data.transition.dest = state event_data.kwargs = {} booking.on_state_changed(event_data) assert event_data.kwargs["position"] == dropoff.to_dict()
def test_do_current_job_2(): init_pos = Position(13.3764, 52.5461) dest = Position(13.4014, 52.5478) clock = Clock() router = LinearRouter(clock) engine = VehicleEngine(init_pos, router, clock) v = Vehicle(clock) v.install_engine(engine) itinerary = Itinerary(2, v) itinerary.move_to(dest) do_job(itinerary) assert v.is_moving
def create_fleet(clock): # use LinearRouter router to move vehicles router = routers.LinearRouter(clock=clock) fleet = Fleet(clock, router) vehicle = Vehicle(clock) fleet.infleet(vehicle, Position(13.4021, 52.5471)) return fleet
def test_router2d(): speed_kmph = 25 nyc_pos = Position(-73.935242, 40.730610) nyc_pos_shift = Position(-73.935, 40.7306) # monutes clock = Clock(time_step=1, time_unit="m") router = LinearRouter(speed=speed_kmph, clock=clock) assert router.estimate_duration(nyc_pos, nyc_pos) == 0 route = router.calculate_route(nyc_pos, nyc_pos) assert route.duration == 0 assert route.distance == 0 assert len(route.coordinates) == 1 assert route.approximate_position(clock.clock_time) == nyc_pos assert route.approximate_position(clock.clock_time + 1) == nyc_pos assert router.estimate_duration(nyc_pos, nyc_pos_shift) == 1 for i in range(10): clock.tick() assert router.estimate_duration(nyc_pos, nyc_pos) == 0 # seconds clock = Clock(time_step=1, time_unit="s") router = LinearRouter(speed=speed_kmph, clock=clock) assert router.estimate_duration(nyc_pos, nyc_pos_shift) == 3 route = router.calculate_route(nyc_pos, nyc_pos_shift) assert route.duration == 3 assert pytest.approx(route.distance, 3) == 0.02 assert len(route.coordinates) == 4 assert route.approximate_position(clock.clock_time) == nyc_pos assert route.approximate_position(clock.clock_time + 1) != nyc_pos assert route.approximate_position(clock.clock_time + 1) == route.coordinates[1] assert route.approximate_position(clock.clock_time + 3) == nyc_pos_shift assert route.approximate_position(clock.clock_time + 3) == route.coordinates[-1]
def test_real_vehicle(): init_pos = Position(13.3764, 52.5461) dest = Position(13.4014, 52.5478) clock = Clock() router = LinearRouter(clock) engine = VehicleEngine(init_pos, router, clock) v = Vehicle(clock) v.install_engine(engine) itinerary = Itinerary(111, v) itinerary.move_to(dest) assert not v.is_moving move_vehicle(itinerary) assert v.is_moving
def test_update_next_bookings(): v = create_vehicle() clock = Clock() b1 = Booking(clock, Position(13.4014, 52.5478), Position(13.3393, 52.5053)) b2 = Booking(clock, Position(13.4014, 52.5478), Position(13.3393, 52.5053)) itinerary = Itinerary(3434, v) itinerary.move_to(111) itinerary.pickup(b1) itinerary.dropoff(b1) itinerary.pickup(b2) itinerary.dropoff(b2) update_next_bookings(itinerary) assert b1.is_waiting_pickup() assert b2.is_waiting_pickup() b1.set_pickup() update_next_bookings(itinerary) assert b1.is_waiting_dropoff() assert b2.is_waiting_pickup()
def test_on_state_changed_pickup_position(): pickup = Position(13.4014, 52.5478) dropoff = Position(13.3393, 52.5053) clock = Clock() booking = Booking(clock, pickup, dropoff, 3) event_data = MagicMock() event_data.transition = MagicMock() for state in ( States.pending.value, States.matched.value, States.pickup.value, States.expired.value, States.waiting_dropoff.value, States.waiting_pickup.value, States.expired.value, ): event_data.transition.dest = state event_data.kwargs = {} booking.on_state_changed(event_data) assert event_data.kwargs["position"] == pickup.to_dict()
def test_move_vehicle(): init_pos = Position(13.3764, 52.5461) dest1 = Position(13.4014, 52.5478) clock = Clock() router = LinearRouter(clock) engine = VehicleEngine(init_pos, router, clock) v = Vehicle(clock) v.install_engine(engine) v.move_to(dest1) assert v.engine.is_moving() assert v.state == States.moving_to v.step() assert v.state == States.moving_to assert v.engine.is_moving() clock.tick() v.step() assert v.engine.is_moving() for _ in range(1000): clock.tick() v.step() if not v.engine.is_moving(): assert v.position == dest1 assert v.state == States.idling else: assert v.position != dest1 assert v.destination == dest1 assert v.state == States.moving_to
def test_on_state_changed_pending(): pickup = Position(13.4014, 52.5478) dropoff = Position(13.3393, 52.5053) clock = Clock() booking = Booking(clock, pickup, dropoff, 3) event_data = MagicMock() event_data.transition = MagicMock() # test to pending event_data.transition.dest = States.pending.value event_data.kwargs = {} booking.on_state_changed(event_data) assert event_data.kwargs["position"] == pickup.to_dict() assert event_data.kwargs["dropoff"] == dropoff.to_dict()
def create_booking(clock): pickup = Position(13.3752, 52.5467) dropoff = Position(13.4014, 52.5478) return Booking(clock, pickup=pickup, dropoff=dropoff)