Пример #1
0
    def test_higher_priority_1(self):
        """Both exercise groups are occupied by Bolek and Lolek. Tola will
        enqueue to both with different priorities. She will end up in the group
        of higher priority regardless of the order in which Bolek and Lolek free
        up the places.
        """
        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 1, 12)):
            self.assertTrue(
                Record.enqueue_student(self.bolek,
                                       self.cooking_exercise_group_1))
            self.assertTrue(
                Record.enqueue_student(self.lolek,
                                       self.cooking_exercise_group_2))

        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 1, 13)):
            self.assertTrue(
                Record.enqueue_student(self.tola,
                                       self.cooking_exercise_group_1))
            self.assertTrue(
                Record.set_queue_priority(self.tola,
                                          self.cooking_exercise_group_1, 7))
            self.assertTrue(
                Record.enqueue_student(self.tola,
                                       self.cooking_exercise_group_2))
            self.assertTrue(
                Record.set_queue_priority(self.tola,
                                          self.cooking_exercise_group_2, 8))

        self.assertTrue(
            Record.is_recorded(self.tola, self.cooking_exercise_group_1))
        self.assertTrue(
            Record.is_recorded(self.tola, self.cooking_exercise_group_2))
        self.assertFalse(
            Record.is_enrolled(self.tola, self.cooking_exercise_group_1))
        self.assertFalse(
            Record.is_enrolled(self.tola, self.cooking_exercise_group_2))

        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 8, 12)):
            self.assertCountEqual(
                Record.remove_from_group(self.bolek,
                                         self.cooking_exercise_group_1),
                [self.cooking_exercise_group_1.pk])
        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 8, 13)):
            self.assertCountEqual(
                Record.remove_from_group(self.lolek,
                                         self.cooking_exercise_group_2),
                [self.cooking_exercise_group_2.pk])

        self.assertFalse(
            Record.is_recorded(self.tola, self.cooking_exercise_group_1))
        self.assertTrue(
            Record.is_enrolled(self.tola, self.cooking_exercise_group_2))
Пример #2
0
    def test_higher_priority_2(self):
        """The only difference between this test and the one above is the order
        in which Bolek and Lolek leave their groups.
        """
        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 1, 12)):
            self.assertTrue(
                Record.enqueue_student(self.bolek,
                                       self.cooking_exercise_group_1))
            self.assertTrue(
                Record.enqueue_student(self.lolek,
                                       self.cooking_exercise_group_2))

        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 1, 13)):
            self.assertTrue(
                Record.enqueue_student(self.tola,
                                       self.cooking_exercise_group_1))
            self.assertTrue(
                Record.set_queue_priority(self.tola,
                                          self.cooking_exercise_group_1, 7))
            self.assertTrue(
                Record.enqueue_student(self.tola,
                                       self.cooking_exercise_group_2))
            self.assertTrue(
                Record.set_queue_priority(self.tola,
                                          self.cooking_exercise_group_2, 8))

        self.assertTrue(
            Record.is_recorded(self.tola, self.cooking_exercise_group_1))
        self.assertTrue(
            Record.is_recorded(self.tola, self.cooking_exercise_group_2))
        self.assertFalse(
            Record.is_enrolled(self.tola, self.cooking_exercise_group_1))
        self.assertFalse(
            Record.is_enrolled(self.tola, self.cooking_exercise_group_2))

        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 8, 12)):
            self.assertCountEqual(
                Record.remove_from_group(self.lolek,
                                         self.cooking_exercise_group_2),
                [self.cooking_exercise_group_2.pk])
        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 8, 13)):
            self.assertCountEqual(
                Record.remove_from_group(self.bolek,
                                         self.cooking_exercise_group_1),
                [self.cooking_exercise_group_1.pk])

        self.assertFalse(
            Record.is_recorded(self.tola, self.cooking_exercise_group_1))
        self.assertTrue(
            Record.is_enrolled(self.tola, self.cooking_exercise_group_2))
Пример #3
0
    def test_student_autoremoved_from_group(self):
        """Bolek switches seminar group for "Mycie Naczyń".

        In the meantime, Lolek tries to join the first group. He waits in queue,
        but is pulled in when Bolek leaves a vacancy.
        """
        # Bolek joins group 1.
        with patch(RECORDS_DATETIME, mock_datetime(2011, 12, 5, 12)):
            self.assertTrue(
                Record.enqueue_student(self.bolek, self.washing_up_seminar_1))
        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.washing_up_seminar_1,
                                  status=RecordStatus.ENROLLED).exists())

        # Lolek tries to join group 1 and is enqueued.
        with patch(RECORDS_DATETIME, mock_datetime(2011, 12, 5, 12)):
            self.assertTrue(
                Record.enqueue_student(self.lolek, self.washing_up_seminar_1))
        self.assertFalse(
            Record.objects.filter(student=self.lolek,
                                  group=self.washing_up_seminar_1,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertTrue(
            Record.objects.filter(student=self.lolek,
                                  group=self.washing_up_seminar_1,
                                  status=RecordStatus.QUEUED).exists())

        # Bolek switches the group.
        with patch(RECORDS_DATETIME, mock_datetime(2011, 12, 5, 12, 5)):
            self.assertTrue(
                Record.enqueue_student(self.bolek, self.washing_up_seminar_2))
        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.washing_up_seminar_2,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertFalse(
            Record.objects.filter(student=self.bolek,
                                  group=self.washing_up_seminar_1,
                                  status=RecordStatus.ENROLLED).exists())

        # Lolek should be pulled in.
        self.assertTrue(
            Record.objects.filter(student=self.lolek,
                                  group=self.washing_up_seminar_1,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertFalse(
            Record.objects.filter(student=self.lolek,
                                  group=self.washing_up_seminar_1,
                                  status=RecordStatus.QUEUED).exists())
Пример #4
0
    def test_one_isim_student(self):
        """When one ISIM student comes, he can take the guaranteed place."""
        with freeze_time(self.opening_time + timedelta(seconds=5)):
            Record.enqueue_student(self.bolek, self.group)
        with freeze_time(self.opening_time + timedelta(seconds=10)):
            Record.enqueue_student(self.lolek, self.group)
        with freeze_time(self.opening_time + timedelta(seconds=15)):
            Record.enqueue_student(self.reksio, self.group)

        # At this point one guy (Reksio) is in the queue.
        self.assertTrue(Record.is_enrolled(self.bolek, self.group))
        self.assertTrue(Record.is_enrolled(self.lolek, self.group))
        self.assertFalse(Record.is_enrolled(self.reksio, self.group))
        self.assertTrue(Record.is_recorded(self.reksio, self.group))

        with freeze_time(self.opening_time + timedelta(minutes=1)):
            Record.enqueue_student(self.tola, self.group)

        # Reksio is still waiting, but Tola took the ISIM guaranteed spot.
        self.assertTrue(Record.is_enrolled(self.bolek, self.group))
        self.assertTrue(Record.is_enrolled(self.lolek, self.group))
        self.assertFalse(Record.is_enrolled(self.reksio, self.group))
        self.assertTrue(Record.is_recorded(self.reksio, self.group))
        self.assertTrue(Record.is_enrolled(self.tola, self.group))

        with freeze_time(self.opening_time + timedelta(minutes=5)):
            Record.remove_from_group(self.lolek, self.group)

        self.assertTrue(Record.is_enrolled(self.bolek, self.group))
        self.assertTrue(Record.is_enrolled(self.reksio, self.group))
        self.assertFalse(Record.is_recorded(self.lolek, self.group))
        self.assertTrue(Record.is_enrolled(self.tola, self.group))

        with freeze_time(self.opening_time + timedelta(minutes=10)):
            Record.enqueue_student(self.lolek, self.group)

        # Now Lolek sits in the queue.
        self.assertTrue(Record.is_recorded(self.lolek, self.group))
        self.assertFalse(Record.is_enrolled(self.lolek, self.group))

        with freeze_time(self.opening_time + timedelta(minutes=15)):
            Record.remove_from_group(self.tola, self.group)

        # Now that Tola has left, the ISIM spot is free, but Lolek is still in
        # the queue.
        self.assertFalse(Record.is_recorded(self.tola, self.group))
        self.assertTrue(Record.is_recorded(self.lolek, self.group))
        self.assertFalse(Record.is_enrolled(self.lolek, self.group))
Пример #5
0
    def test_bolek_comes_before_lolek(self):
        """Bolek will be first to enroll into the groups. Lolek will remain in
        the queue of the exercise group, yet he will fit in the lecture
        group."""
        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 1, 12)):
            self.assertCountEqual(
                Record.enqueue_student(self.bolek,
                                       self.cooking_exercise_group_1), [
                                           self.cooking_exercise_group_1.pk,
                                           self.cooking_lecture_group.pk
                                       ])
        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 1, 12, 1)):
            self.assertCountEqual(
                Record.enqueue_student(self.lolek,
                                       self.cooking_exercise_group_1), [
                                           self.cooking_exercise_group_1.pk,
                                           self.cooking_lecture_group.pk
                                       ])

        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.cooking_exercise_group_1,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.cooking_lecture_group,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertFalse(
            Record.objects.filter(student=self.lolek,
                                  group=self.cooking_exercise_group_1,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertTrue(
            Record.objects.filter(student=self.lolek,
                                  group=self.cooking_lecture_group,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertTrue(
            Record.objects.filter(student=self.lolek,
                                  group=self.cooking_exercise_group_1,
                                  status=RecordStatus.QUEUED).exists())
        self.assertFalse(
            Record.objects.filter(student=self.lolek,
                                  group=self.cooking_lecture_group,
                                  status=RecordStatus.QUEUED).exists())
Пример #6
0
    def test_simple_enrollment(self):
        """Bolek will just enqueue into the group."""
        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 1, 12)):
            self.assertListEqual(
                Record.enqueue_student(self.bolek,
                                       self.knitting_lecture_group),
                [self.knitting_lecture_group.pk])

        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.knitting_lecture_group,
                                  status=RecordStatus.ENROLLED).exists())
Пример #7
0
    def test_student_exceeds_the_35_limit(self):
        """Bolek will try to sign up to "Gotowanie" and "Szydełkowanie" before
        35 points limit abolition. He should be successful with "Gotowanie",
        which costs exactly 35 ECTS, but not with the second enrollment.
        """
        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 1, 12)):
            self.assertTrue(
                Record.enqueue_student(self.bolek, self.cooking_lecture_group))
        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.cooking_lecture_group,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertEqual(
            Record.student_points_in_semester(self.bolek, self.semester), 35)

        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 1, 12, 5)):
            # He should be able to join the queue.
            self.assertTrue(
                Record.enqueue_student(self.bolek,
                                       self.knitting_lecture_group))
        # His enrollment with "Gotowanie" should still exist.
        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.cooking_lecture_group,
                                  status=RecordStatus.ENROLLED).exists())
        # His record with "Szydełkowanie" should be removed.
        self.assertFalse(
            Record.objects.filter(student=self.bolek,
                                  group=self.knitting_lecture_group,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.knitting_lecture_group,
                                  status=RecordStatus.REMOVED).exists())
        self.assertEqual(
            Record.student_points_in_semester(self.bolek, self.semester), 35)
Пример #8
0
def prototype_action(request, group_id):
    """Performs actions requested by timetable prototype.

    HTTP response 204 (successful with no content to send back) will be returned
    if the pin operation is performed with no obstacles. If the student is not
    allowed to perform an operation, 403 (forbidden) status shall be returned.
    """
    student = request.user.student
    group: Group
    try:
        group = Group.objects.get(pk=group_id)
    except Group.DoesNotExist:
        raise Http404
    # Axios sends POST data in json rather than _Form-Encoded_.
    data = json.loads(request.body.decode('utf-8'))
    action = data.get('action')
    if action == 'pin':
        Pin.objects.get_or_create(student_id=student.pk, group_id=group_id)
        return HttpResponse(status=204)
    if action == 'unpin':
        Pin.objects.filter(student_id=student.pk, group_id=group_id).delete()
        return HttpResponse(status=204)
    if action == 'enqueue':
        group_ids = Record.enqueue_student(student, group)
        if group_ids:
            # When the student joins the queue of a class, the accompanying
            # lecture group might need to be displayed (if he is automatically
            # enqueued in that). We hence send him the information about these
            # groups.
            groups = Group.objects.filter(pk__in=group_ids).select_related(
                'teacher', 'teacher__user', 'course', 'course__semester').prefetch_related(
                    'term', 'term__classrooms', 'guaranteed_spots', 'guaranteed_spots__role')
            for group in groups:
                group.is_enqueued = True
            groups_dicts = build_group_list(groups)
            return JsonResponse(groups_dicts, safe=False)
        else:
            return HttpResponse(status=403)
    if action == 'dequeue':
        group_ids = Record.remove_from_group(student, group)
        if group_ids:
            return JsonResponse(group_ids, safe=False)
        else:
            return HttpResponse(status=403)
    # If the request action is not among the above, we return Bad Request
    # response.
    return HttpResponse(status=400, content=action)
Пример #9
0
    def test_lecture_group_also_enrolled(self):
        """Bolek will just enqueue into the exercises group. He should also be
        enrolled into the lecture."""

        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 1, 12)):
            self.assertCountEqual(
                Record.enqueue_student(self.bolek,
                                       self.cooking_exercise_group_1), [
                                           self.cooking_exercise_group_1.pk,
                                           self.cooking_lecture_group.pk
                                       ])

        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.cooking_exercise_group_1,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.cooking_lecture_group,
                                  status=RecordStatus.ENROLLED).exists())
Пример #10
0
    def test_exercise_group_also_removed(self):
        """Like above bolek will enqueue into exercise group. Then he'll leave.

        He first should be automatically pulled into the lecture group. Then he
        will unenroll from the lecture group and he should be removed from the
        exercise group as well.
        """
        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 1, 12)):
            self.assertCountEqual(
                Record.enqueue_student(self.bolek,
                                       self.cooking_exercise_group_1), [
                                           self.cooking_exercise_group_1.pk,
                                           self.cooking_lecture_group.pk
                                       ])

        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.cooking_exercise_group_1,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertTrue(
            Record.objects.filter(student=self.bolek,
                                  group=self.cooking_lecture_group,
                                  status=RecordStatus.ENROLLED).exists())

        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 2, 12)):
            self.assertCountEqual(
                Record.remove_from_group(self.bolek,
                                         self.cooking_lecture_group), [
                                             self.cooking_exercise_group_1.pk,
                                             self.cooking_lecture_group.pk
                                         ])

        self.assertFalse(
            Record.objects.filter(student=self.bolek,
                                  group=self.cooking_exercise_group_1,
                                  status=RecordStatus.ENROLLED).exists())
        self.assertFalse(
            Record.objects.filter(student=self.bolek,
                                  group=self.cooking_lecture_group,
                                  status=RecordStatus.ENROLLED).exists())
Пример #11
0
    def test_isim_not_involved(self):
        """When ISIM students are not involved, enrollment behaves normally."""
        with freeze_time(self.opening_time + timedelta(seconds=5)):
            Record.enqueue_student(self.bolek, self.group)
        with freeze_time(self.opening_time + timedelta(seconds=10)):
            Record.enqueue_student(self.lolek, self.group)
        with freeze_time(self.opening_time + timedelta(seconds=15)):
            Record.enqueue_student(self.reksio, self.group)

        self.assertTrue(Record.is_enrolled(self.bolek, self.group))
        self.assertTrue(Record.is_enrolled(self.lolek, self.group))
        self.assertFalse(Record.is_enrolled(self.reksio, self.group))
        self.assertTrue(Record.is_recorded(self.reksio, self.group))

        with freeze_time(self.opening_time + timedelta(minutes=5)):
            Record.remove_from_group(self.lolek, self.group)

        self.assertTrue(Record.is_enrolled(self.bolek, self.group))
        self.assertTrue(Record.is_enrolled(self.reksio, self.group))
        self.assertFalse(Record.is_recorded(self.lolek, self.group))
Пример #12
0
    def test_waiting_students_number(self):
        """Check whether we get correct number of waiting students of given type.

        Our exercise groups have limit for 1 person.
        Bolek is in cooking_exercise_group_1 and Lolek is in cooking_exercise_group_2.
        Tola is in queues of all above groups.
        Bolek changed his mind and want to be in cooking_exercise_group_2.
        Lolek also want to join other group(cooking_exercise_group_1).
        We have 2 enrolled Records and 4 enqueued.
        Only Lola isn't enrolled in any group.
        That's why we should return 1.
        """
        with patch(RECORDS_DATETIME, mock_datetime(2011, 10, 8, 12)):
            self.cooking_exercise_group_1.limit = 1
            self.cooking_exercise_group_2.limit = 1
            Record.enqueue_student(self.bolek, self.cooking_exercise_group_1)
            Record.enqueue_student(self.lolek, self.cooking_exercise_group_2)
            Record.enqueue_student(self.tola, self.cooking_exercise_group_1)
            Record.enqueue_student(self.tola, self.cooking_exercise_group_2)
            Record.enqueue_student(self.bolek, self.cooking_exercise_group_2)
            Record.enqueue_student(self.lolek, self.cooking_exercise_group_1)

            expected_waiting = {
                self.cooking_exercise_group_1.course_id: {
                    self.cooking_exercise_group_1.type: 1
                }
            }
            self.assertDictEqual(
                Record.list_waiting_students(
                    [self.cooking_exercise_group_1.course]), expected_waiting)
Пример #13
0
    def test_two_isim_students(self):
        """When there are two ISIM students, they compete for the regular spots
        and the guaranteed spot at the same time.
        """
        with freeze_time(self.opening_time + timedelta(seconds=5)):
            Record.enqueue_student(self.bolek, self.group)
        with freeze_time(self.opening_time + timedelta(seconds=10)):
            Record.enqueue_student(self.lolek, self.group)
        with freeze_time(self.opening_time + timedelta(seconds=15)):
            Record.enqueue_student(self.tola, self.group)
        # Bolek and Lolek are in the regular spots, Tola has taken the
        # ISIM-guaranteed extra spot.
        self.assertTrue(Record.is_enrolled(self.bolek, self.group))
        self.assertTrue(Record.is_enrolled(self.lolek, self.group))
        self.assertTrue(Record.is_enrolled(self.tola, self.group))

        with freeze_time(self.opening_time + timedelta(seconds=20)):
            Record.enqueue_student(self.reksio, self.group)
        with freeze_time(self.opening_time + timedelta(seconds=25)):
            Record.enqueue_student(self.uszatek, self.group)
        # Now both Reksio and Uszatek are waiting.
        self.assertFalse(Record.is_enrolled(self.reksio, self.group))
        self.assertFalse(Record.is_enrolled(self.uszatek, self.group))

        with freeze_time(self.opening_time + timedelta(seconds=30)):
            Record.remove_from_group(self.tola, self.group)
        # Now Uszatek jumps into the guaranteed spot even though he is not first
        # in line.
        self.assertFalse(Record.is_enrolled(self.reksio, self.group))
        self.assertTrue(Record.is_enrolled(self.uszatek, self.group))

        with freeze_time(self.opening_time + timedelta(seconds=35)):
            Record.enqueue_student(self.tola, self.group)
            Record.remove_from_group(self.lolek, self.group)
        # Now Reksio takes up the regular spot. Tola is waiting.
        self.assertTrue(Record.is_enrolled(self.reksio, self.group))
        self.assertFalse(Record.is_enrolled(self.tola, self.group))

        with freeze_time(self.opening_time + timedelta(seconds=40)):
            Record.enqueue_student(self.lolek, self.group)
        with freeze_time(self.opening_time + timedelta(seconds=45)):
            Record.remove_from_group(self.bolek, self.group)
        # Now Tola assumes the regular spot, because she was in line before
        # Lolek.
        self.assertTrue(Record.is_enrolled(self.uszatek, self.group))
        self.assertTrue(Record.is_enrolled(self.tola, self.group))
        self.assertFalse(Record.is_enrolled(self.lolek, self.group))