def test_has_available_seats_full(self): """ check if reserve_seat({{trip}}) stops reserving at capacity """ trip_id = Trip.objects.first() trip_route = trip_id.trip_route trip_stop_a = trip_route.tripstop_set.get(stop_number=1) trip_stop_b = trip_route.tripstop_set.get(stop_number=2) num_reservations = 50 bus_capacity = trip_id.bus.capacity customer = Customer.objects.first() for i in range(num_reservations): bus_seat = BusSeat.objects.get(bus=trip_id.bus, order=(i % bus_capacity)) res = create_reservation( trip=trip_id, bus_seat=bus_seat, customer=customer, departure_station=trip_stop_a.station, arrival_station=trip_stop_b.station, ) # check new `last_reserved_seat` value reservations_num = get_seats_reserved( trip=trip_id, departure_stop=trip_stop_a, arrival_stop=trip_stop_b, ).count() self.assertEqual(reservations_num, bus_capacity)
def test_has_available_seats_case8(self): """ CASE 8 Trip Route: A - B - C - D - E Reservation1: D to E Reservation2: A to B ◦--◦ ◦--◦ when making reservation2 it should say that the free seats = capacity """ # [Step1] retrieve required data # [Step1.1] get first trip and get its first 4 stops trip_id = Trip.objects.first() trip_route = trip_id.trip_route trip_stop_a = trip_route.tripstop_set.get(stop_number=1) trip_stop_b = trip_route.tripstop_set.get(stop_number=2) trip_stop_d = trip_route.tripstop_set.get(stop_number=4) trip_stop_e = trip_route.tripstop_set.get(stop_number=5) # [Step1.2] get any customer and the first seat in the trip's bus customer = Customer.objects.first() bus_seat = BusSeat.objects.get(bus=trip_id.bus, order=1) # [Step2] reserve from A to C create_reservation( trip=trip_id, bus_seat=bus_seat, customer=customer, departure_station=trip_stop_d.station, arrival_station=trip_stop_e.station, ) # [Step3] there now should be a reservation if you want to reserve from B to D reservations_num = get_seats_reserved( trip=trip_id, departure_stop=trip_stop_a, arrival_stop=trip_stop_b, ).count() self.assertEqual(reservations_num, 0)
def post(self, request, format=None): # [Step1] retrieve and validate body from request serializer = self.InputSerializer(data=request.data) serializer.is_valid(raise_exception=True) new_reservation = create_reservation( **serializer.validated_data, customer=self.request.user.customer, ) if not new_reservation: err_json = {'error_message': 'this trip has no available seats'} return Response(err_json, status=status.HTTP_400_BAD_REQUEST) # [Step4] return reservation data serializer = self.OutputSerializer(new_reservation) return Response(serializer.data)