Esempio n. 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)
Esempio n. 2
0
    def test_find_nearest_available_car_2(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=4,
                              position_y=4,
                              destination_x=9,
                              destination_y=9)
        customer_1.save()

        lowest_id_available_car = views.find_nearest_available_car(
            customer=Customer.objects.get(id=1),
            available_cars=Car.objects.filter(booking_state='FREE'))

        self.assertEqual(lowest_id_available_car.id, 3)
Esempio n. 3
0
    def test_get_cars_of_x_distance_to_customer(self):
        x_distance = 4
        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()

        cars_of_x_distance = views.get_cars_of_x_distance_to_customer(
            x_distance=x_distance,
            customer=Customer.objects.get(id=1),
            cars=Car.objects.all())

        self.assertEqual(len(cars_of_x_distance), 1)
        self.assertEqual(cars_of_x_distance[0].id, 3)
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
0
    def test_assign_car_to_customer(self):
        car_1 = Car(id=1,
                    position_x=0,
                    position_y=0,
                    customer=None,
                    booking_state='FREE')
        car_1.save()

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

        car = Car.objects.get(id=1)
        customer = Customer.objects.get(id=1)
        views.assign_car_to_customer(customer=customer, car=car)

        car = Car.objects.get(id=1)
        self.assertEqual(car.booking_state, 'ALLO')
        self.assertEqual(car.customer, customer)
Esempio n. 8
0
 def test_is_car_at_customer_destination_2(self):
     car_1 = Car(id=1,
                 position_x=0,
                 position_y=0,
                 customer=None,
                 booking_state='FREE')
     customer_1 = Customer(id=1,
                           position_x=0,
                           position_y=0,
                           destination_x=3,
                           destination_y=3)
     self.assertFalse(
         views.is_car_at_customer_destination(customer=customer_1,
                                              car=car_1))
Esempio n. 9
0
 def test_is_car_at_customer_destination_1(self):
     customer_4 = Customer(id=4,
                           position_x=2,
                           position_y=-2,
                           destination_x=8,
                           destination_y=4)
     car_4 = Car(id=4,
                 position_x=8,
                 position_y=4,
                 customer=None,
                 booking_state='FREE')
     self.assertTrue(
         views.is_car_at_customer_destination(customer=customer_4,
                                              car=car_4))
Esempio n. 10
0
 def test_is_customer_and_car_colocated_2(self):
     car_3 = Car(id=3,
                 position_x=-5,
                 position_y=-3,
                 customer=None,
                 booking_state='FREE')
     customer_3 = Customer(id=3,
                           position_x=-4,
                           position_y=12,
                           destination_x=3,
                           destination_y=-7)
     self.assertFalse(
         views.is_customer_and_car_colocated(customer=customer_3,
                                             car=car_3))
Esempio n. 11
0
 def test_is_customer_and_car_colocated_1(self):
     car_2 = Car(id=2,
                 position_x=2,
                 position_y=2,
                 customer=None,
                 booking_state='FREE')
     customer_2 = Customer(id=2,
                           position_x=2,
                           position_y=2,
                           destination_x=5,
                           destination_y=9)
     self.assertTrue(
         views.is_customer_and_car_colocated(customer=customer_2,
                                             car=car_2))
Esempio n. 12
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)