def test_confine_to_station(self): passenger = self.passenger order = create_test_order(passenger) confining_station = create_another_TLV_station() order.confining_station = confining_station order.save() # order, should get confining station resuscitate_work_stations() self.assertTrue( choose_workstation(order).station == confining_station, "confining station is expected") # order out of confining station service radius, should still assign the order order.from_lat, order.from_lon = FAR_AWAY_LAT, FAR_AWAY_LON order.to_lat, order.to_lon = None, None order.save() refresh_order(order) self.assertTrue( choose_workstation(order).station == confining_station, "confining station is expected") # create a REJECTED assignment by confining_station, should get None assignment = OrderAssignment( order=order, station=confining_station, work_station=confining_station.work_stations.all()[0], status=REJECTED) assignment.save() refresh_order(order) self.assertTrue( choose_workstation(order) is None, "no ws is expected.")
def _run_ignored_or_rejected_test(self, status): order = self.order tel_aviv = self.tel_aviv_station tel_aviv_ws1 = tel_aviv.work_stations.all()[0] tel_aviv_ws2 = tel_aviv.work_stations.all()[1] tel_aviv_2 = create_another_TLV_station() tel_aviv_ws3 = tel_aviv_2.work_stations.all()[0] resuscitate_work_stations() # create an IGNORED/REJECTED assignment by first station (ws1, ws2), should get ws3 assignment = OrderAssignment(order=order, station=tel_aviv, work_station=tel_aviv_ws1, status=status) assignment.save() ws_list = compute_ws_list(order) self.assertTrue( tel_aviv_ws1 not in ws_list and tel_aviv_ws2 not in ws_list, "this station ignored the order.") next_ws = choose_workstation(order) self.assertTrue(next_ws == tel_aviv_ws3, "tel aviv ws3 is expected.") # create an IGNORED/REJECTED assignment by ws3, should get None assignment = OrderAssignment(order=order, station=tel_aviv_2, work_station=tel_aviv_ws3, status=status) assignment.save() refresh_order(order) self.assertTrue( choose_workstation(order) is None, "no ws is expected.")
def setUp(self): setup_testing_env.setup() self.passenger = create_passenger() self.order = create_test_order() self.station = Station.objects.get(name='test_station_1') self.work_station = WorkStation.objects.filter(station=self.station)[0] self.assignment = OrderAssignment(order=self.order, station=self.station, work_station=self.work_station) self.assignment.save() # TODO_WB: remove when we have new fixtures phone1 = Phone(local_phone=u'1234567', station=self.station) phone1.save() phone2 = Phone(local_phone=u'0000000', station=self.station) phone2.save()
def test_choose_workstation_order(self): order = self.order resuscitate_work_stations() station1 = self.tel_aviv_station # should have 2 work stations (from fixtures) station2 = create_another_TLV_station(num_of_ws=2) resuscitate_work_stations() ws_list = compute_ws_list(order) # stations should alternate self.assertTrue( ws_list[0].station == ws_list[2].station and ws_list[1].station == ws_list[3].station, "stations should alternate") # work stations should alternate self.assertTrue(ws_list[0] != ws_list[2] and ws_list[1] != ws_list[3], "work stations should alternate") # # scenario: station x -> station y (reject) -> station x # order = create_test_order() first_ws = choose_workstation(order) second_ws = choose_workstation(order) assignment = OrderAssignment(order=order, station=second_ws.station, work_station=second_ws, status=REJECTED) assignment.save() refresh_order(order) third_ws = choose_workstation(order) self.assertTrue(third_ws.station == first_ws.station) # # scenario: station x -> station y (not taken) -> station x -> station y other ws # order = create_test_order() first_ws = choose_workstation(order) second_ws = choose_workstation(order) assignment = OrderAssignment(order=order, station=second_ws.station, work_station=second_ws, status=NOT_TAKEN) assignment.save() third_ws = choose_workstation(order) fourth_ws = choose_workstation(order) self.assertTrue(second_ws.station == fourth_ws.station and second_ws != fourth_ws)
def _run_ignored_or_rejected_test(self, status): order = self.order tel_aviv = self.tel_aviv_station tel_aviv_ws1 = tel_aviv.work_stations.all()[0] tel_aviv_ws2 = tel_aviv.work_stations.all()[1] tel_aviv_2 = create_another_TLV_station() tel_aviv_ws3 = tel_aviv_2.work_stations.all()[0] resuscitate_work_stations() # create an IGNORED/REJECTED assignment by first station (ws1, ws2), should get ws3 assignment = OrderAssignment(order=order, station=tel_aviv, work_station=tel_aviv_ws1, status=status) assignment.save() ws_list = compute_ws_list(order) self.assertTrue(tel_aviv_ws1 not in ws_list and tel_aviv_ws2 not in ws_list, "this station ignored the order.") next_ws = choose_workstation(order) self.assertTrue(next_ws == tel_aviv_ws3, "tel aviv ws3 is expected.") # create an IGNORED/REJECTED assignment by ws3, should get None assignment = OrderAssignment(order=order, station=tel_aviv_2, work_station=tel_aviv_ws3, status=status) assignment.save() refresh_order(order) self.assertTrue(choose_workstation(order) is None, "no ws is expected.")
def assign_order(order): """ Assign the order to a workstation and return the assignment. """ passenger = order.passenger work_station = choose_workstation(order) if not work_station: raise NoWorkStationFoundError("Could not find a valid station") # create an OrderAssignment assignment = OrderAssignment(order=order, station=work_station.station, work_station=work_station) assignment.pickup_address_in_ws_lang = translate_address_for_ws(work_station, order, 'from') assignment.dropoff_address_in_ws_lang = translate_address_for_ws(work_station, order, 'to') assignment.save() work_station.last_assignment_date = assignment.create_date work_station.save() work_station.station.last_assignment_date = assignment.create_date work_station.station.save() try: order.change_status(new_status=models.ASSIGNED) log_event(EventType.ORDER_ASSIGNED, passenger=passenger, order=order, order_assignment=assignment, station=work_station.station, work_station=work_station) # emit the signal only if the order was successfully marked as ASSIGNED orderassignment_created_signal.send(sender="orderassignment_created_signal", obj=assignment) except UpdateStatusError: logging.error("Cannot assign order: %d" % order.id) return assignment
def test_confine_to_station(self): passenger = self.passenger order = create_test_order(passenger) confining_station = create_another_TLV_station() order.confining_station = confining_station order.save() # order, should get confining station resuscitate_work_stations() self.assertTrue(choose_workstation(order).station == confining_station, "confining station is expected") # order out of confining station service radius, should still assign the order order.from_lat, order.from_lon = FAR_AWAY_LAT, FAR_AWAY_LON order.to_lat, order.to_lon = None, None order.save() refresh_order(order) self.assertTrue(choose_workstation(order).station == confining_station, "confining station is expected") # create a REJECTED assignment by confining_station, should get None assignment = OrderAssignment(order=order, station=confining_station, work_station=confining_station.work_stations.all()[0], status=REJECTED) assignment.save() refresh_order(order) self.assertTrue(choose_workstation(order) is None, "no ws is expected.")
def show_order(request, work_station): order_id = request.POST.get("order_id") if order_id == station_connection_manager.DUMMY_ID: return JSONResponse({"pk": order_id, "status": ASSIGNED, "from_raw": translate_to_ws_lang(station_connection_manager.DUMMY_ADDRESS, work_station), "seconds_passed": 5}) order_id = int(order_id) try: order_assignment = order_manager.show_order(order_id, work_station) except ShowOrderError: logging.error("ShowOrderError") return HttpResponseBadRequest("ShowOrderError") result = OrderAssignment.serialize_for_workstation(order_assignment, base_time=order_assignment.show_date) return JSONResponse(result)
def test_choose_workstation_order(self): order = self.order resuscitate_work_stations() station1 = self.tel_aviv_station # should have 2 work stations (from fixtures) station2 = create_another_TLV_station(num_of_ws=2) resuscitate_work_stations() ws_list = compute_ws_list(order) # stations should alternate self.assertTrue(ws_list[0].station == ws_list[2].station and ws_list[1].station == ws_list[3].station, "stations should alternate") # work stations should alternate self.assertTrue(ws_list[0] != ws_list[2] and ws_list[1] != ws_list[3], "work stations should alternate") # # scenario: station x -> station y (reject) -> station x # order = create_test_order() first_ws = choose_workstation(order) second_ws = choose_workstation(order) assignment = OrderAssignment(order=order, station=second_ws.station, work_station=second_ws, status=REJECTED) assignment.save() refresh_order(order) third_ws = choose_workstation(order) self.assertTrue(third_ws.station == first_ws.station) # # scenario: station x -> station y (not taken) -> station x -> station y other ws # order = create_test_order() first_ws = choose_workstation(order) second_ws = choose_workstation(order) assignment = OrderAssignment(order=order, station=second_ws.station, work_station=second_ws, status=NOT_TAKEN) assignment.save() third_ws = choose_workstation(order) fourth_ws = choose_workstation(order) self.assertTrue(second_ws.station == fourth_ws.station and second_ws != fourth_ws)
class OrderManagerTest(TestCase): """Unit test for the logic of the order manager.""" fixtures = ['countries.yaml', 'cities.yaml', 'ordering_test_data.yaml'] def setUp(self): setup_testing_env.setup() self.passenger = create_passenger() self.order = create_test_order() self.station = Station.objects.get(name='test_station_1') self.work_station = WorkStation.objects.filter(station=self.station)[0] self.assignment = OrderAssignment(order=self.order, station=self.station, work_station=self.work_station) self.assignment.save() # TODO_WB: remove when we have new fixtures phone1 = Phone(local_phone=u'1234567', station=self.station) phone1.save() phone2 = Phone(local_phone=u'0000000', station=self.station) phone2.save() def test_book_order(self): order = self.order response = self.client.post( reverse('ordering.order_manager.book_order'), data={"order_id": order.id}) # working stations are dead, assignment should fail self.assertTrue( (response.content, response.status_code) == (NO_MATCHING_WORKSTATIONS_FOUND, 200), "Assignment should fail: workstations are dead") # resuscitate work stations and try again resuscitate_work_stations() response = self.client.post( reverse('ordering.order_manager.book_order'), data={"order_id": order.id}) self.assertTrue( (response.content, response.status_code) == (ORDER_HANDLED, 200), "Assignment should succeed: workstations are live") # timed out order order = refresh_order(order) order.create_date = datetime.datetime.now() - datetime.timedelta( seconds=ORDER_HANDLE_TIMEOUT + 1) order.save() response = self.client.post( reverse('ordering.order_manager.book_order'), data={"order_id": order.id}) self.assertTrue( (response.content, response.status_code) == (ORDER_TIMEOUT, 200), "Assignment should fail: order time out") def test_update_future_pickup(self): order = self.order self.assertTrue( order.future_pickup == False, "new orders should have future_pickup == False by default") self.client.post( reverse('ordering.order_manager.update_future_pickup'), data={"order_id": order.id}) self.assertTrue(order.future_pickup == False, "future_pickup changed from False to True") order.future_pickup = True order.save() self.client.post( reverse('ordering.order_manager.update_future_pickup'), data={"order_id": order.id}) self.assertTrue(order.future_pickup == True, "future_pickup should changed to True") def test_redispatch_orders(self): assignment = self.assignment # NOT_TAKEN assignment assignment.create_date = datetime.datetime.now() - datetime.timedelta( seconds=ORDER_TEASER_TIMEOUT + 1) assignment.save() self.client.post( reverse('ordering.order_manager.redispatch_pending_orders'), data={"order_assignment_id": assignment.id}) assignment = OrderAssignment.objects.get(id=assignment.id) # refresh self.assertTrue(assignment.status == NOT_TAKEN, "assignment should be marked as not taken.") # IGNORED assignment assignment.change_status(new_status=ASSIGNED) assignment.create_date = datetime.datetime.now() - datetime.timedelta( seconds=ORDER_ASSIGNMENT_TIMEOUT + 1) assignment.save() self.client.post( reverse('ordering.order_manager.redispatch_ignored_orders'), data={"order_assignment_id": assignment.id}) assignment = OrderAssignment.objects.get(id=assignment.id) # refresh self.assertTrue(assignment.status == IGNORED, "assignment should be marked as ignored.") def test_show_and_accept_order(self): order = self.order assignment = self.assignment order.change_status(PENDING, ASSIGNED) self.assertTrue(assignment.status == PENDING) order_manager.show_order(assignment.order.id, assignment.work_station) assignment = OrderAssignment.objects.get(id=assignment.id) # refresh self.assertTrue(assignment.status == ASSIGNED and assignment.show_date, "show_order failed") order_manager.accept_order(order, pickup_time=5, station=self.station) self.assertTrue((order.status, order.pickup_time, order.station) == (ACCEPTED, 5, self.station), "accept_order failed")
class OrderManagerTest(TestCase): """Unit test for the logic of the order manager.""" fixtures = ['countries.yaml', 'cities.yaml', 'ordering_test_data.yaml'] def setUp(self): setup_testing_env.setup() self.passenger = create_passenger() self.order = create_test_order() self.station = Station.objects.get(name='test_station_1') self.work_station = WorkStation.objects.filter(station=self.station)[0] self.assignment = OrderAssignment(order=self.order, station=self.station, work_station=self.work_station) self.assignment.save() # TODO_WB: remove when we have new fixtures phone1 = Phone(local_phone=u'1234567', station=self.station) phone1.save() phone2 = Phone(local_phone=u'0000000', station=self.station) phone2.save() def test_book_order(self): order = self.order response = self.client.post(reverse('ordering.order_manager.book_order'), data={"order_id": order.id}) # working stations are dead, assignment should fail self.assertTrue((response.content, response.status_code) == (NO_MATCHING_WORKSTATIONS_FOUND, 200), "Assignment should fail: workstations are dead") # resuscitate work stations and try again resuscitate_work_stations() response = self.client.post(reverse('ordering.order_manager.book_order'), data={"order_id": order.id}) self.assertTrue((response.content, response.status_code) == (ORDER_HANDLED, 200), "Assignment should succeed: workstations are live") # timed out order order = refresh_order(order) order.create_date = datetime.datetime.now() - datetime.timedelta(seconds=ORDER_HANDLE_TIMEOUT+1) order.save() response = self.client.post(reverse('ordering.order_manager.book_order'), data={"order_id": order.id}) self.assertTrue((response.content, response.status_code) == (ORDER_TIMEOUT, 200), "Assignment should fail: order time out") def test_update_future_pickup(self): order = self.order self.assertTrue(order.future_pickup == False, "new orders should have future_pickup == False by default") self.client.post(reverse('ordering.order_manager.update_future_pickup'), data={"order_id": order.id}) self.assertTrue(order.future_pickup == False, "future_pickup changed from False to True") order.future_pickup = True order.save() self.client.post(reverse('ordering.order_manager.update_future_pickup'), data={"order_id": order.id}) self.assertTrue(order.future_pickup == True, "future_pickup should changed to True") def test_redispatch_orders(self): assignment = self.assignment # NOT_TAKEN assignment assignment.create_date = datetime.datetime.now() - datetime.timedelta(seconds=ORDER_TEASER_TIMEOUT+1) assignment.save() self.client.post(reverse('ordering.order_manager.redispatch_pending_orders'), data={"order_assignment_id": assignment.id}) assignment = OrderAssignment.objects.get(id=assignment.id) # refresh self.assertTrue(assignment.status == NOT_TAKEN, "assignment should be marked as not taken.") # IGNORED assignment assignment.change_status(new_status=ASSIGNED) assignment.create_date = datetime.datetime.now() - datetime.timedelta(seconds=ORDER_ASSIGNMENT_TIMEOUT+1) assignment.save() self.client.post(reverse('ordering.order_manager.redispatch_ignored_orders'), data={"order_assignment_id": assignment.id}) assignment = OrderAssignment.objects.get(id=assignment.id) # refresh self.assertTrue(assignment.status == IGNORED, "assignment should be marked as ignored.") def test_show_and_accept_order(self): order = self.order assignment = self.assignment order.change_status(PENDING, ASSIGNED) self.assertTrue(assignment.status == PENDING) order_manager.show_order(assignment.order.id, assignment.work_station) assignment = OrderAssignment.objects.get(id=assignment.id) # refresh self.assertTrue(assignment.status == ASSIGNED and assignment.show_date, "show_order failed") order_manager.accept_order(order, pickup_time=5, station=self.station) self.assertTrue((order.status, order.pickup_time, order.station) == (ACCEPTED, 5, self.station), "accept_order failed")
def push_order(order_assignment): """ Retrieve the order and workstation from an assignment and add the order to the workstation's queue. """ orders = OrderAssignment.serialize_for_workstation(order_assignment) _do_push(order_assignment.work_station, orders)
def get_workstation_orders(request, work_station): order_assignments_for_ws = OrderAssignment.objects.filter(work_station = work_station, status = models.ASSIGNED) return JSONResponse(OrderAssignment.serialize_for_workstation(order_assignments_for_ws))