def test_date_to_percentage(self): self.assertEqual( date_to_percentage(parse_datetime_localized("2017-03-05 12:00")), 50) self.assertEqual( date_to_percentage(parse_datetime_localized("2017-03-05 00:00")), 0) self.assertEqual( date_to_percentage(parse_datetime_localized("2017-03-05 17:00")), (17 / 24) * 100) self.assertEqual( date_to_percentage(parse_datetime_localized("2017-03-05 17:25")), (17 / 24 + 25 / (24 * 60)) * 100)
def test_form_valid_event_reservation(self, now_mock): now_mock.return_value = parse_datetime_localized("2018-08-12 12:00") self.user.add_perms('make_queue.can_create_event_reservation') now = timezone.localtime() reservation = Reservation.objects.create( machine=self.machine, user=self.user, start_time=now + timedelta(hours=1), end_time=now + timedelta(hours=2), event=self.timeplace, ) self.timeplace = TimePlace.objects.create( event=self.event, start_time=now + timedelta(hours=1), end_time=now + timedelta(hours=2)) form = self.create_form(start_time_diff=1, end_time_diff=2, event=self.timeplace) self.assertTrue(form.is_valid()) view = self.get_view(reservation_pk=reservation.pk) response = view.form_valid(form, reservation_pk=reservation.pk) self.assertEqual(Reservation.objects.count(), 1) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(Reservation.objects.first().event, self.timeplace)
def test_disallow_overlapping_reservations(self, now_mock): now_mock.return_value = parse_datetime_localized("2018-03-12 12:00") self.user_quota.number_of_reservations = 3 self.user_quota.save() self.check_reservation_valid(self.create_reservation(timedelta(hours=1), timedelta(hours=2)), "Saving should be valid") # Start before, end inside self.check_reservation_invalid(self.create_reservation(timedelta(minutes=50), timedelta(hours=1, minutes=50)), "Reservation should not be able to end inside another") # Start inside, end after self.check_reservation_invalid( self.create_reservation(timedelta(hours=1, minutes=10), timedelta(hours=2, minutes=10)), "Reservation should not be able to end inside another") # Start inside, end inside self.check_reservation_invalid( self.create_reservation(timedelta(hours=1, minutes=10), timedelta(hours=1, minutes=50)), "Reservation should not be able to start and end inside another") # Start before, end after self.check_reservation_invalid(self.create_reservation(timedelta(minutes=50), timedelta(hours=2, minutes=10)), "Reservation should not be able to encapsulate another") # End at the start time of other self.check_reservation_valid(self.create_reservation(timedelta(hours=0), timedelta(hours=1)), "A reservation should be allowed to end at the same time another one starts") # Start at the end time of other self.check_reservation_valid(self.create_reservation(timedelta(hours=2), timedelta(hours=3)), "A reservation should be allowed to start at the same time another one ends")
def test_calendar_reservation_url(self, now_mock): now_mock.return_value = parse_datetime_localized("2018-12-09 12:24") user = User.objects.create_user("user", "*****@*****.**", "weak_pass") printer_machine_type = MachineType.objects.get(pk=1) Quota.objects.create(user=user, number_of_reservations=2, ignore_rules=True, machine_type=printer_machine_type) printer = Machine.objects.create(name="U1", location="S1", machine_model="Ultimaker", status=Machine.Status.AVAILABLE, machine_type=printer_machine_type) Printer3DCourse.objects.create(user=user, username=user.username, name=user.get_full_name(), date=timezone.now()) reservation = Reservation.objects.create(user=user, machine=printer, event=None, start_time=timezone.now(), end_time=timezone.now() + timedelta(hours=2)) self.assertEqual(calendar_url_reservation(reservation), current_calendar_url(printer))
def test_is_current_data(self, now_mock): now_mock.return_value = parse_datetime_localized("2017-03-05 11:18") self.assertTrue(is_current_date(timezone.now().date())) self.assertTrue( is_current_date((timezone.now() + timedelta(hours=1)).date())) self.assertFalse( is_current_date((timezone.now() + timedelta(days=1)).date())) self.assertFalse( is_current_date((timezone.now() + timedelta(days=-1)).date()))
def test_form_valid_normal_reservation(self, now_mock): now_mock.return_value = parse_datetime_localized("2018-08-12 12:00") reservation = self.create_reservation( self.create_form(start_time_diff=1, end_time_diff=2)) form = self.create_form(start_time_diff=1, end_time_diff=3) self.assertTrue(form.is_valid()) view = self.get_view(reservation_pk=reservation.pk) response = view.form_valid(form, reservation_pk=reservation.pk) self.assertEqual(Reservation.objects.count(), 1) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(Reservation.objects.first().end_time, timezone.localtime() + timedelta(hours=3))
def test_post_unchangeable_reservation(self, now_mock): now_mock.return_value = parse_datetime_localized("2018-08-12 12:00") reservation = self.create_reservation( self.create_form(start_time_diff=1, end_time_diff=2)) now_mock.return_value = timezone.localtime() + timedelta(hours=2, minutes=1) view = self.get_view(reservation_pk=reservation.pk) view.request.method = 'POST' response = view.dispatch(view.request, reservation_pk=reservation.pk) # An unchangeable reservation should have redirect self.assertEqual(response.status_code, HTTPStatus.FOUND)
def test_form_valid_changed_machine(self, now_mock): now_mock.return_value = parse_datetime_localized("2018-08-12 12:00") reservation = self.create_reservation( self.create_form(start_time_diff=1, end_time_diff=2)) old_machine = self.machine self.machine = Machine.objects.create( name="M1", machine_model="Generic", machine_type=self.sewing_machine_type) form = self.create_form(start_time_diff=1, end_time_diff=3) self.assertTrue(form.is_valid()) view = self.get_view(reservation_pk=reservation.pk) response = view.form_valid(form, reservation_pk=reservation.pk) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(Reservation.objects.count(), 1) self.assertEqual(Reservation.objects.first().end_time, timezone.localtime() + timedelta(hours=2)) self.assertEqual(Reservation.objects.first().machine, old_machine)
def test_current_calendar_url(self, now_mock): now_mock.return_value = parse_datetime_localized("2017-12-26 12:34") printer_machine_type = MachineType.objects.get(pk=1) printer = Machine.objects.create( name="U1", location="S1", machine_model="Ultimaker", machine_type=printer_machine_type, status=Machine.Status.AVAILABLE, ) self.assertEqual( current_calendar_url(printer), reverse('machine_detail', kwargs={ 'year': 2017, 'week': 52, 'pk': printer.pk }), )
def test_event_queryset_correctly_filters_past_and_future_events( self, now_mock): now = parse_datetime_localized("2021-04-15 12:00") now_mock.return_value = now def create_event( event_type: Event.Type, title_and_relative_start_and_end_time_tuples: Tuple[str, Tuple[ Tuple[int, int], ...]] ) -> Event: title, relative_start_and_end_time_tuples = title_and_relative_start_and_end_time_tuples event = Event.objects.create( title=f"{event_type.name.lower()}_{title}", event_type=event_type, ) for relative_start_time, relative_end_time in relative_start_and_end_time_tuples: TimePlace.objects.create( event=event, publication_time=now + timedelta( days=1 ), # publication time should not affect the `past()` or `future()` methods start_time=now + timedelta(hours=relative_start_time), end_time=now + timedelta(hours=relative_end_time), ) return event def create_standalone( title_and_relative_start_and_end_time_tuples: Tuple[str, Tuple[ Tuple[int, int], ...]] ) -> Event: return create_event(Event.Type.STANDALONE, title_and_relative_start_and_end_time_tuples) def create_repeating( title_and_relative_start_and_end_time_tuples: Tuple[str, Tuple[ Tuple[int, int], ...]] ) -> Event: return create_event(Event.Type.REPEATING, title_and_relative_start_and_end_time_tuples) all_ended = ("all_ended", ( (-3, -2), (-2, -1), )) some_ended_and_some_not_started = ("some_ended_and_some_not_started", ( (-3, -2), (2, 3), )) some_ended_and_some_ongoing = ("some_ended_and_some_ongoing", ( (-3, -2), (-1, 1), )) some_ongoing_and_some_not_started = ( "some_ongoing_and_some_not_started", ( (-1, 1), (2, 3), )) all_not_started = ("all_not_started", ( (1, 2), (2, 3), )) none = ("none", ()) standalone_with_all_ended = create_standalone(all_ended) standalone_with_some_ended_and_some_not_started = create_standalone( some_ended_and_some_not_started) standalone_with_some_ended_and_some_ongoing = create_standalone( some_ended_and_some_ongoing) standalone_with_some_ongoing_and_some_not_started = create_standalone( some_ongoing_and_some_not_started) standalone_with_all_not_started = create_standalone(all_not_started) standalone_with_none = create_standalone(none) repeating_with_all_ended = create_repeating(all_ended) repeating_with_some_ended_and_some_not_started = create_repeating( some_ended_and_some_not_started) repeating_with_some_ended_and_some_ongoing = create_repeating( some_ended_and_some_ongoing) repeating_with_some_ongoing_and_some_not_started = create_repeating( some_ongoing_and_some_not_started) repeating_with_all_not_started = create_repeating(all_not_started) repeating_with_none = create_repeating(none) past_events_set = set_without_duplicates(self, Event.objects.past()) future_events_set = set_without_duplicates(self, Event.objects.future()) self.assertSetEqual( past_events_set, { standalone_with_all_ended, repeating_with_all_ended, repeating_with_some_ended_and_some_not_started, repeating_with_some_ended_and_some_ongoing, }) self.assertSetEqual( future_events_set, { standalone_with_some_ended_and_some_not_started, standalone_with_some_ended_and_some_ongoing, standalone_with_some_ongoing_and_some_not_started, standalone_with_all_not_started, repeating_with_some_ended_and_some_not_started, repeating_with_some_ended_and_some_ongoing, repeating_with_some_ongoing_and_some_not_started, repeating_with_all_not_started, }) # Events with no timeplaces should not be counted among past or future events self.assertNotIn(standalone_with_none, past_events_set) self.assertNotIn(repeating_with_none, past_events_set) self.assertNotIn(standalone_with_none, future_events_set) self.assertNotIn(repeating_with_none, future_events_set)
def set_mock_value(time: str): now_mock.return_value = parse_datetime_localized( f"2017-03-05 {time}")