def test_only_take_station(self): """ Only take the station, no change """ time = datetime(year=2000, month=1, day=1) ns1 = MRTStation('NS1', 'NAME1', time) ns2 = MRTStation('NS2', 'NAME1', time) ns1.add_next_station(ns2) ns2.add_next_station(ns1) travel_step_1 = TravelStep(ns1, ns1, time, duration=timedelta()) travel_step_2 = TravelStep(ns1, ns2, time, duration=timedelta(minutes=6)) travel_plan = TravelPlan() travel_plan.step_list.append(travel_step_1) travel_plan.step_list.append(travel_step_2) readable_plan = travel_plan.get_readable_plan() assert readable_plan.is_reachable() assert readable_plan.message == 'Total travel time is 6 minutes' assert len(readable_plan.step_list) == 1 step0 = readable_plan.step_list[0] assert step0.get_readable_action() == 'Take NS line' special_color_list = readable_plan.get_special_color_list() assert len(special_color_list) == 1 assert special_color_list[0] == 'NAME1(NS1)NAME1(NS2)NS'
def test_unreachable_plan(self): """ Test build an unreachable plan """ travel_plan_unreachable = TravelPlan.build_unreachable_plan() assert len(travel_plan_unreachable.get_travel_station_list()) == 0 assert travel_plan_unreachable.get_travel_time_in_min() is None
def find_best_plan(plan_list: list) -> TravelPlan: """ Find the best travel plan. :param plan_list: list of TravelPlan :return: the best plan """ # current best plan is unreachable best_plan = TravelPlan.build_unreachable_plan() for plan in plan_list: if plan.is_better_than(best_plan): best_plan = plan return best_plan
def search_by_name(self, src_name: str, des_name: str, cur_time: datetime) -> TravelPlan: """ Search from a station to a destination station name Caution that the outcome for name to name may not be unique, we will pick the shortest one :param src_name: the source station name :param des_name: the destination station name :param cur_time: the time when start travel in src_station :return: the shortest travel plan """ if not src_name or not des_name: return TravelPlan.build_empty_station_plan() elif src_name not in self.mrt_map.name2station or des_name not in self.mrt_map.name2station: return TravelPlan.build_invalid_station_plan() src_list = self.mrt_map.get_station_by_name(src_name) plan_list = [] for src_station in src_list: plan_list.append(self.search(src_station, des_name, cur_time)) return self.find_best_plan(plan_list)
def test_unreachable_plan(self): """ Test the behavior of an unreachable plan """ travel_plan_unreachable = TravelPlan.build_unreachable_plan() readable_plan = travel_plan_unreachable.get_readable_plan() assert not readable_plan.is_reachable() assert readable_plan.message == 'No invalid action, you can not go further or the path is blocked' assert len(readable_plan.step_list) == 1 step0 = readable_plan.step_list[0] assert step0.get_readable_action( ) == 'No invalid action, you can not go further or the path is blocked'
def test_only_change_station(self): """ Only change the station, no taking """ time = datetime(year=2000, month=1, day=1) ns1 = MRTStation('NS1', 'NAME1', time) dt1 = MRTStation('DT1', 'NAME1', time) travel_step_1 = TravelStep(ns1, ns1, time, duration=timedelta()) travel_step_2 = TravelStep(ns1, dt1, time, duration=timedelta(minutes=7)) travel_plan = TravelPlan() travel_plan.step_list.append(travel_step_1) travel_plan.step_list.append(travel_step_2) readable_plan = travel_plan.get_readable_plan() assert readable_plan.is_reachable() assert readable_plan.message == 'Total travel time is 7 minutes' assert len(readable_plan.step_list) == 1 step0 = readable_plan.step_list[0] assert step0.get_readable_action() == 'Change from NS line to DT line'
def test_is_better_than(self): """ Test the is better than function """ dummy_datetime = datetime(year=2000, month=1, day=1) dummy_station = MRTStation('NS1', '', dummy_datetime) travel_step_1 = TravelStep(dummy_station, dummy_station, dummy_datetime, duration=timedelta(minutes=1)) travel_step_2 = TravelStep(dummy_station, dummy_station, dummy_datetime, duration=timedelta(minutes=2)) travel_step_unreachable = TravelPlan.build_unreachable_plan() travel_plan_1 = TravelPlan.build_from_final_step(travel_step_1) travel_plan_2 = TravelPlan.build_from_final_step(travel_step_2) assert not travel_step_unreachable.is_better_than(travel_plan_1) assert travel_plan_1.is_better_than(travel_step_unreachable) assert travel_plan_1.is_better_than(travel_plan_2) assert not travel_plan_2.is_better_than(travel_plan_1)
def test_already_arrived(self): """ Test the behavior when you already arrived the station before travel """ time = datetime(year=2000, month=1, day=1) s1 = MRTStation('NS1', '', time) travel_step_1 = TravelStep(s1, s1, time, duration=timedelta()) travel_plan_1 = TravelPlan.build_from_final_step(travel_step_1) readable_plan = travel_plan_1.get_readable_plan() assert readable_plan.is_reachable() assert readable_plan.message == 'Total travel time is 0 minutes' assert len(readable_plan.step_list) == 1 step0 = readable_plan.step_list[0] assert step0.get_readable_action( ) == 'No more action needed, you are already arrived'