def test_max_guests_for_dates(reservations): # Checking the most loaded day date_start = datetime.date(2018, 4, 28) date_end = datetime.date(2018, 5, 3) max_guests_number = Reservation.get_max_guests_for_dates( date_start, date_end) assert 3 == max_guests_number # Checking days out of reservation (should be 0) date_start = datetime.date(2018, 3, 1) date_end = datetime.date(2018, 3, 30) max_guests_number = Reservation.get_max_guests_for_dates( date_start, date_end) assert 0 == max_guests_number # Checking the date on the end of reservation (should be 0) date_start = datetime.date(2018, 5, 29) date_end = datetime.date(2018, 5, 31) max_guests_number = Reservation.get_max_guests_for_dates( date_start, date_end) assert 0 == max_guests_number # Checking the date on the beginning of reservation (should be 0) date_start = datetime.date(2018, 4, 20) date_end = datetime.date(2018, 4, 24) max_guests_number = Reservation.get_max_guests_for_dates( date_start, date_end) assert 0 == max_guests_number
def test_dates_by_range3(): """ Testing bad range: date_start > date_end """ date_start = datetime.date(2018, 5, 1) date_end = datetime.date(2018, 4, 30) dates = Reservation.get_dates_by_range(date_start, date_end) assert isinstance(dates, list) assert 0 == len(dates)
def test_dates_by_range2(): """ Test range in 1 day """ date_start = datetime.date(2018, 5, 1) date_end = datetime.date(2018, 5, 2) dates = Reservation.get_dates_by_range(date_start, date_end) assert isinstance(dates, list) assert 1 == len(dates)
def test_dates_by_range1(): """ Test range in several days """ date_start = datetime.date(2018, 4, 26) date_end = datetime.date(2018, 5, 5) dates = Reservation.get_dates_by_range(date_start, date_end) assert isinstance(dates, list) assert 9 == len(dates)
def reservations(guest): reservations = ( ((2018, 4, 24), (2018, 4, 29), ('Test1', '*****@*****.**')), ((2018, 4, 27), (2018, 5, 3), ('Test2', '*****@*****.**')), ((2018, 5, 1), (2018, 5, 10), ('Test3', '*****@*****.**')), ((2018, 5, 2), (2018, 5, 3), ('Test4', '*****@*****.**')), ((2018, 5, 3), (2018, 5, 6), ('Test5', '*****@*****.**')), ((2018, 5, 7), (2018, 5, 29), ('Test6', '*****@*****.**')), ) for date_start, date_end, (name, email) in reservations: guest, _ = Guest.objects.get_or_create(name=name, email=email) Reservation.create_reservation( guest, datetime.date(*date_start), datetime.date(*date_end), ) return reservations
def validate(self, attrs): overbooking = Config.get_parameter_value('overbooking') rooms_number = Config.get_parameter_value('rooms_number') if rooms_number == 0: raise ValidationError('Reservation forbidden', 403) max_guests_number = Reservation.get_max_guests_for_dates( attrs['date_start'], attrs['date_end']) if ((max_guests_number + 1) - rooms_number) / rooms_number * 100 > overbooking: raise ValidationError( 'Reservation limit exceeded for this date range', 400) return attrs
def create(self, request, *args, **kwargs): """ Add new reservation. Returns 400 error if reservation is not possible for the date range """ serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) data = dict(serializer.validated_data) guest = data.pop('guest') guest, _ = Guest.objects.get_or_create(**guest) reservation = Reservation.create_reservation(guest, **data) serializer = self.serializer_class(instance=reservation) return Response(serializer.data, 201)
def test_create_reservation(guest): date_start = datetime.date(2018, 4, 26) date_end = datetime.date(2018, 5, 4) reservation = Reservation.create_reservation(guest, date_start, date_end) assert 8 == ReservationDates.objects.filter( reservation_id=reservation.id).count()