def test_valid_normal_reservation(self): form_data = { "start_time": timezone.now() + timedelta(hours=1), "end_time": timezone.now() + timedelta(hours=2), "machine_name": self.machine.pk, } form = ReservationForm(data=form_data) self.assertTrue(form.is_valid())
def test_reservation_starting_before_current_time(self): form_data = { "start_time": timezone.now() - timedelta(hours=1), "end_time": timezone.now() + timedelta(hours=1), "machine_type": Printer3D.literal, "machine_name": self.machine.pk, } form = ReservationForm(data=form_data) self.assertFalse(form.is_valid())
def test_machine_of_other_type(self): sewing_machine = SewingMachine.objects.create(name="Sewing Machine", model="Generic") form_data = { "start_time": timezone.now() + timedelta(hours=1), "end_time": timezone.now() + timedelta(hours=2), "machine_type": Printer3D.literal, "machine_name": sewing_machine.pk } form = ReservationForm(data=form_data) self.assertFalse(form.is_valid())
def test_special_reservation_with_too_long_text(self): form_data = { "start_time": timezone.now() + timedelta(hours=1), "end_time": timezone.now() + timedelta(hours=2), "machine_name": self.machine.pk, "special": True, "special_text": "23 characters is enough" } form = ReservationForm(data=form_data) self.assertFalse(form.is_valid())
def test_special_reservation(self): form_data = { "start_time": timezone.now() + timedelta(hours=1), "end_time": timezone.now() + timedelta(hours=2), "machine_name": self.machine.pk, "special": True, "special_text": "Test text", } form = ReservationForm(data=form_data) self.assertTrue(form.is_valid())
def handle_post(self, request, **kwargs): """ Handles and validates update requests :param request: The HTTP request """ try: form = ReservationForm(request.POST) if form.is_valid(): return self.form_valid(form, **kwargs) except Exception: pass return self.get(request, **kwargs)
def test_event_reservation(self): form_data = { "start_time": timezone.now() + timedelta(hours=1), "end_time": timezone.now() + timedelta(hours=2), "machine_type": Printer3D.literal, "machine_name": self.machine.pk, "event": True, "event_pk": self.timeplace.pk, } form = ReservationForm(data=form_data) self.assertTrue(form.is_valid())
def test_invalid_event_reservation(self): form_data = { "start_time": timezone.now() + timedelta(hours=1), "end_time": timezone.now() + timedelta(hours=2), "machine_name": self.machine.pk, "event": True, # Since there is only one timeplace object we can invert its pk to get an invalid pk "event_pk": int(not self.timeplace.pk), } form = ReservationForm(data=form_data) self.assertFalse(form.is_valid())
def test_invalid_machine_type(self): form_data = { "start_time": timezone.now() + timedelta(hours=1), "end_time": timezone.now() + timedelta(hours=2), "machine_type": "this_will_never_be_a_valid_machine_type", "machine_name": self.machine.pk, } form = ReservationForm(data=form_data) try: form.is_valid() self.fail("Reservations with invalid machine types should not be possible") except KeyError: pass
def test_invalid_machine_name(self): form_data = { "start_time": timezone.now() + timedelta(hours=1), "end_time": timezone.now() + timedelta(hours=2), "machine_type": Printer3D.literal, # Since there is only one machine we can get an invalid primary key by just negating the current one "machine_name": not self.machine.pk } form = ReservationForm(data=form_data) try: form.is_valid() self.fail("Reservations should not be allowed for machines that do not exist") except KeyError: pass
def create_form(self, start_diff, end_diff, event=None, special=False, special_text=""): return ReservationForm(data=self.create_form_data( start_diff, end_diff, event, special, special_text))
def test_post_valid_form(self): view = self.get_view() view.request = post_request_with_user(self.user, data=self.create_form_data(1, 2)) # Set values to allow for context to be created view.new_reservation = False # Need to handle the valid form function valid_form_calls = {"calls": 0} view.form_valid = lambda x, **y: valid_form_calls.update( {"calls": valid_form_calls["calls"] + 1}) or HttpResponse() self.assertTrue(ReservationForm(view.request.POST).is_valid()) response = view.handle_post(view.request) self.assertEqual(200, response.status_code) self.assertEqual(1, valid_form_calls["calls"])
def test_post_invalid_form(self): view = self.get_view() view.request = post_request_with_user(self.user, data=self.create_form_data( -1, 2)) # Set values to allow for context to be created view.new_reservation = False view.sub_class = SewingMachine # Need to handle the valid form function, which should never be called view.form_valid = lambda x, **y: self.fail( "Valid form should never be called with an invalid form") self.assertFalse(ReservationForm(view.request.POST).is_valid()) response = view.handle_post(view.request, machine=self.machine) self.assertEqual(200, response.status_code)