def test_get_error_message_non_event(self): view = self.get_view() form = self.create_form(1, 2) self.assertTrue(form.is_valid()) reservation = Reservation(user=self.user, start_time=form.cleaned_data["start_time"], end_time=form.cleaned_data["end_time"], machine=self.machine) self.assertEqual(view.get_error_message(form, reservation), "Tidspunktet er ikke lengre tilgjengelig")
def test_get_error_message_too_far_in_the_future(self): view = self.get_view() form = self.create_form(24 * 7, 24 * 7 + 1) self.assertTrue(form.is_valid()) reservation = Reservation(user=self.user, start_time=form.cleaned_data["start_time"], end_time=form.cleaned_data["end_time"], machine=self.machine) self.assertEqual(view.get_error_message(form, reservation), "Reservasjoner kan bare lages 7 dager frem i tid")
def form_valid(self, form, **kwargs): """ Creates a reservation from a valid ReservationForm :param form: The valid reservation form :return: HTTP response """ reservation = Reservation(start_time=form.cleaned_data["start_time"], end_time=form.cleaned_data["end_time"], user=self.request.user, machine=form.cleaned_data["machine"], comment=form.cleaned_data["comment"]) if form.cleaned_data["event"]: reservation.event = form.cleaned_data["event"] if form.cleaned_data["special"]: reservation.special = True reservation.special_text = form.cleaned_data["special_text"] return self.validate_and_save(reservation, form)
def test_validate_and_save_valid_reservation(self): view = self.get_view() form = self.create_form(1, 2) self.assertTrue(form.is_valid()) reservation = Reservation(user=self.user, start_time=form.cleaned_data["start_time"], end_time=form.cleaned_data["end_time"], machine=self.machine) response = view.validate_and_save(reservation, form) self.assertEqual(1, Reservation.objects.count()) self.assertEqual(302, response.status_code)
def test_get_error_message_event(self): view = self.get_view() form = self.create_form(1, 2, event=self.event) self.assertTrue(form.is_valid()) reservation = Reservation(user=self.user, start_time=form.cleaned_data["start_time"], end_time=form.cleaned_data["end_time"], machine=self.machine) self.user.user_permissions.add( Permission.objects.get(name="Can create event reservation")) self.assertEqual( view.get_error_message(form, reservation), "Tidspunktet eller eventen, er ikke lengre tilgjengelig")
def create_reservation(self, timedelta_start, timedelta_end, event=None, user=None, machine=None, special=False, special_text=""): machine = self.machine if machine is None else machine user = self.user if user is None else user return Reservation(user=user, machine=machine, event=event, start_time=timezone.now() + timedelta_start, end_time=timezone.now() + timedelta_end, special=special, special_text=special_text)
def test_validate_and_save_non_valid_reservation(self): view = self.get_view() # Set values to allow for context to be created view.new_reservation = False form = self.create_form(1, 2) self.assertTrue(form.is_valid()) # Test with collision Reservation.objects.create(user=self.user, start_time=form.cleaned_data["start_time"], end_time=form.cleaned_data["end_time"], machine=self.machine) reservation = Reservation(user=self.user, start_time=form.cleaned_data["start_time"], end_time=form.cleaned_data["end_time"], machine=self.machine) response = view.validate_and_save(reservation, form) # Second reservation should not be saved self.assertEqual(1, Reservation.objects.count()) # 200 to re render the form self.assertEqual(200, response.status_code)