コード例 #1
0
    def test_get_lowest_distance(self):
        car_1 = Car(id=1,
                    position_x=1,
                    position_y=1,
                    customer=None,
                    booking_state='FREE')
        car_2 = Car(id=2,
                    position_x=2,
                    position_y=2,
                    customer=None,
                    booking_state='FREE')
        car_3 = Car(id=3,
                    position_x=3,
                    position_y=3,
                    customer=None,
                    booking_state='FREE')
        car_1.save()
        car_2.save()
        car_3.save()

        customer_1 = Customer(id=1,
                              position_x=5,
                              position_y=5,
                              destination_x=1,
                              destination_y=1)
        customer_1.save()

        lowest_distance = views.get_lowest_distance(
            customer=Customer.objects.get(id=1), cars=Car.objects.all())
        self.assertEqual(lowest_distance, 4)
コード例 #2
0
    def test_update_car_booking_state(self):
        customer_1 = Customer(id=1,
                              position_x=4,
                              position_y=4,
                              destination_x=8,
                              destination_y=8)
        customer_2 = Customer(id=2,
                              position_x=3,
                              position_y=3,
                              destination_x=5,
                              destination_y=-9)
        customer_1.save()
        customer_2.save()

        car_1 = Car(id=1,
                    position_x=0,
                    position_y=0,
                    customer=None,
                    booking_state='FREE')
        car_2 = Car(id=2,
                    position_x=4,
                    position_y=4,
                    customer=customer_1,
                    booking_state='ALLO')
        car_3 = Car(id=3,
                    position_x=5,
                    position_y=-9,
                    customer=customer_2,
                    booking_state='INTR')
        car_1.save()
        car_2.save()
        car_3.save()

        # Expect only 2 cars to change state
        cars = Car.objects.all()
        customers = Customer.objects.all()
        state_changes = views.update_car_booking_state(cars=cars,
                                                       customers=customers)
        self.assertEqual(state_changes, 2)

        # Expect car 1 to be in the FREE state and have no customer
        car_1 = Car.objects.get(id=1)
        self.assertEqual(car_1.booking_state, "FREE")
        self.assertEqual(car_1.customer, None)

        # Expect car 2 to be in the INTR state and have customer 1
        car_2 = Car.objects.get(id=2)
        self.assertEqual(car_2.booking_state, "INTR")
        self.assertEqual(car_2.customer, customer_1)

        # Exepect car 3 to be in the FREE state and have no customer
        car_3 = Car.objects.get(id=3)
        self.assertEqual(car_3.booking_state, "FREE")
        self.assertEqual(car_3.customer, None)
コード例 #3
0
def book(request):
    """
	given a customer of source (x1,y1) and destination (x2,y2)
	return the nearest car id and the total time needed for the car to
	1) pick up the customer
	2) send the customer to their destination

    expects a json in the request body of
	 {
	  "source": {
	    "x": x1,
	    "y": y1
	  },
	  "destination": {
	    "x": x2,
	    "y": y2
	  }
	}

	returns a json in the response body of
	{
	  "car_id": id,
	  "total_time": t
	}
	"""
    body = json.loads(request.body)
    customer = Customer(position_x=int(body['source']['x']),
                        position_y=int(body['source']['y']),
                        destination_x=int(body['destination']['x']),
                        destination_y=int(body['destination']['y']))
    customer.save()

    nearest_car = find_nearest_available_car(
        customer=customer,
        available_cars=Car.objects.filter(booking_state='FREE'))
    assign_car_to_customer(car=nearest_car, customer=customer)
    time_for_car_to_pickup_customer = distance_between_two_points(
        x1=customer.position_x,
        x2=nearest_car.position_x,
        y1=customer.position_y,
        y2=nearest_car.position_y)
    time_from_source_to_destination = distance_between_two_points(
        x1=customer.position_x,
        x2=customer.destination_x,
        y1=customer.position_y,
        y2=customer.destination_y)

    return JsonResponse(data={
        'car_id':
        nearest_car.id,
        'total_time':
        time_for_car_to_pickup_customer + time_from_source_to_destination
    },
                        safe=True)
コード例 #4
0
    def test_move_cars_1(self):
        customer_1 = Customer(id=1,
                              position_x=4,
                              position_y=4,
                              destination_x=8,
                              destination_y=8)
        customer_2 = Customer(id=2,
                              position_x=3,
                              position_y=3,
                              destination_x=5,
                              destination_y=-9)
        customer_1.save()
        customer_2.save()

        car_1 = Car(id=1,
                    position_x=0,
                    position_y=0,
                    customer=None,
                    booking_state='FREE')
        car_2 = Car(id=2,
                    position_x=1,
                    position_y=1,
                    customer=customer_1,
                    booking_state='ALLO')
        car_3 = Car(id=3,
                    position_x=5,
                    position_y=5,
                    customer=customer_2,
                    booking_state='INTR')
        car_1.save()
        car_2.save()
        car_3.save()

        # Expect only 2 cars to move
        cars = Car.objects.all()
        movements = views.move_cars(cars=cars)
        self.assertEqual(movements, 2)

        # Expect car 1 position to be unchanged
        car_1 = Car.objects.get(id=1)
        self.assertEqual(car_1.position_x, 0)
        self.assertEqual(car_1.position_y, 0)

        # Expect car 2 to move 1 right towards customer
        car_2 = Car.objects.get(id=2)
        self.assertEqual(car_2.position_x, 2)
        self.assertEqual(car_2.position_y, 1)

        # Expect car 3 to move 1 down towards customer destination
        car_3 = Car.objects.get(id=3)
        self.assertEqual(car_3.position_x, 5)
        self.assertEqual(car_3.position_y, 4)
コード例 #5
0
    def test_advance_world(self):
        customer_1 = Customer(id=1,
                              position_x=5,
                              position_y=5,
                              destination_x=-1,
                              destination_y=-1)
        customer_2 = Customer(id=1,
                              position_x=4,
                              position_y=4,
                              destination_x=3,
                              destination_y=3)
        customer_1.save()
        customer_2.save()

        car_1 = Car(id=1,
                    position_x=1,
                    position_y=1,
                    customer=None,
                    booking_state='FREE')
        car_2 = Car(id=2,
                    position_x=2,
                    position_y=2,
                    customer=customer_1,
                    booking_state='ALLO')
        car_3 = Car(id=3,
                    position_x=2,
                    position_y=3,
                    customer=customer_2,
                    booking_state='INTR')
        car_1.save()
        car_2.save()
        car_3.save()

        time = Time(tick=0)
        time.save()

        new_tick = views.advance_world(cars=Car.objects.all(),
                                       customers=Customer.objects.all(),
                                       current_time=Time.objects.get(id=1))

        # Check positions of cars 1,2,3
        # car_1 does not move
        # booking_state remains in FREE
        car_1 = Car.objects.get(id=1)
        self.assertEqual(car_1.position_x, 1)
        self.assertEqual(car_1.position_y, 1)
        self.assertEqual(car_1.customer, None)
        self.assertEqual(car_1.booking_state, 'FREE')
        # car_2 moved 1 right to towards customer_1 source
        # booking_state remains ALLO
        car_2 = Car.objects.get(id=2)
        self.assertEqual(car_2.position_x, 3)
        self.assertEqual(car_2.position_y, 2)
        self.assertEqual(car_2.customer, customer_1)
        self.assertEqual(car_2.booking_state, 'ALLO')
        # car_3 moved 1 right to drop off customer_2 at destination
        # booking_state changes from INTR to FREE
        car_3 = Car.objects.get(id=3)
        self.assertEqual(car_3.position_x, 3)
        self.assertEqual(car_3.position_y, 3)
        self.assertEqual(car_3.customer, None)
        self.assertEqual(car_3.booking_state, 'FREE')

        # Check that tick is incremented
        self.assertEqual(new_tick, 1)