Exemplo n.º 1
0
    def test_no_event_file(self):
        evt1 = factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=self.default_event_time)
        evt2 = factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=self.default_event_time + timedelta(hours=1))

        resp = self.get_course_calendar_view()
        self.assertEqual(resp.status_code, 200)

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 2)
        self.assertDictEqual(
            events_json[0],
            {'id': evt1.pk, 'start': self.default_event_time.isoformat(),
             'allDay': False,
             'title': 'lecture 0'})
        self.assertDictEqual(
            events_json[1],
            {'id': evt2.pk,
             'start': (self.default_event_time + timedelta(hours=1)).isoformat(),
             'allDay': False,
             'title': 'lecture 1'})

        self.assertResponseContextEqual(resp, "event_info_list", [])
Exemplo n.º 2
0
    def test_events_file_with_events_test1(self):
        self.switch_to_fake_commit_sha()

        # lecture 1
        lecture1_start_time = self.default_event_time - timedelta(weeks=1)
        factories.EventFactory(kind=self.default_event_kind,
                               course=self.course,
                               time=lecture1_start_time,
                               ordinal=1)

        # lecture 2
        factories.EventFactory(kind=self.default_event_kind,
                               course=self.course,
                               time=self.default_event_time,
                               ordinal=2)

        resp = self.get_course_calender_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 2)
        self.assertDictEqual(
            events_json[0], {
                'id': 2,
                'start': self.default_event_time.isoformat(),
                'allDay': False,
                'title': 'Lecture 2'
            })

        self.assertDictEqual(
            events_json[1], {
                'id': 1,
                'color': "red",
                'start': lecture1_start_time.isoformat(),
                'allDay': False,
                'title': 'Alternative title for lecture 1',
                'url': '#event-1'
            })

        event_info_list = resp.context["event_info_list"]

        # lecture 2 doesn't create an EventInfo object
        self.assertEqual(len(event_info_list), 1)

        # check the attributes of EventInfo of lecture 1
        evt_info_dict = event_info_list[0].__dict__
        evt_description = evt_info_dict.pop("description")

        self.assertDictEqual(
            evt_info_dict, {
                "id": 1,
                "human_title": "Alternative title for lecture 1",
                "start_time": lecture1_start_time,
                "end_time": None
            })

        # make sure markup_to_html is called
        self.assertIn(
            'href="/course/test-course/flow/prequiz-linear-algebra/start/',
            evt_description)
Exemplo n.º 3
0
    def test_event_with_no_ordinal_uniqueness(self):
        kwargs = {
            "course": self.course,
            "time": now(),
            "kind": "some_kind",
            "ordinal": None
        }
        event = factories.EventFactory(**kwargs)

        # make sure it can be updated
        event.time = now() - timedelta(days=1)
        event.save()

        from django.core.exceptions import ValidationError
        with self.assertRaises(ValidationError):
            factories.EventFactory(**kwargs)
Exemplo n.º 4
0
    def test_unicode(self):
        event1 = factories.EventFactory(course=self.course,
                                        kind="my_event",
                                        ordinal=None)
        event2 = factories.EventFactory(course=self.course,
                                        kind="my_event2",
                                        ordinal=None)
        self.assertNotEqual(str(event1), str(event2))

        event3 = factories.EventFactory(course=self.course,
                                        kind="my_event3",
                                        ordinal=1)
        event4 = factories.EventFactory(course=self.course,
                                        kind="my_event3",
                                        ordinal=2)
        self.assertNotEqual(str(event3), str(event4))
Exemplo n.º 5
0
    def test_events_file_with_events_test3(self):
        self.switch_to_fake_commit_sha()

        exam_end_time = self.default_event_time + timedelta(hours=2)
        evt = factories.EventFactory(
            kind="exam", course=self.course,
            ordinal=None,
            time=self.default_event_time,
            end_time=exam_end_time)

        resp = self.get_course_calendar_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 1)

        self.assertDictEqual(
            events_json[0],
            {'id': evt.pk,
             'start': self.default_event_time.isoformat(),
             'allDay': False,
             'title': 'Exam',
             'color': 'red',
             'end': exam_end_time.isoformat()})

        self.assertResponseContextEqual(resp, "event_info_list", [])
Exemplo n.º 6
0
    def test_validate_kind_with_hyphen(self):
        with self.assertRaises(ValidationError) as cm:
            factories.EventFactory(course=self.course, kind="my-event")

        expected_error_msg = (
            "Should be lower_case_with_underscores, no spaces "
            "allowed.")
        self.assertIn(expected_error_msg, cm.exception.message_dict["kind"])
Exemplo n.º 7
0
 def test_post_success_starting_ordinal_not_specified_skip_exist(self):
     factories.EventFactory(
         course=self.course, kind=self.default_event_kind, ordinal=4)
     resp = self.post_create_recurring_events_view(
         data=self.get_post_create_recur_evt_data())
     self.assertEqual(resp.status_code, 200)
     self.assertEqual(Event.objects.count(), 6)
     self.assertListEqual(
         list(Event.objects.values_list("ordinal", flat=True)),
         [4, 11, 12, 13, 14, 15])
Exemplo n.º 8
0
    def test_available_kind_choices(self):
        factories.EventFactory(
            course=self.course, kind="some_kind1", ordinal=None)
        factories.EventFactory(
            course=self.course, kind="some_kind2", ordinal=1)
        another_course = factories.CourseFactory(identifier="another-course")
        factories.EventFactory(
            course=another_course, kind="some_kind3", ordinal=1)
        factories.EventFactory(
            course=another_course, kind="some_kind4", ordinal=1)
        form = calendar.RecurringEventForm(self.course.identifier)
        self.assertIn(
            '<option value="some_kind1">some_kind1</option>', form.as_p())
        self.assertIn(
            '<option value="some_kind2">some_kind2</option>', form.as_p())

        self.assertNotIn(
            '<option value="some_kind3">some_kind3</option>', form.as_p())
        self.assertNotIn(
            '<option value="some_kind4">some_kind4</option>', form.as_p())
Exemplo n.º 9
0
    def setUpTestData(cls):  # noqa
        super(RenumberEventsTest, cls).setUpTestData()
        times = []
        now_time = now()
        for i in range(5):
            times.append(now_time)
            now_time += timedelta(weeks=1)
            factories.EventFactory(
                kind=cls.default_event_kind, ordinal=i * 2 + 1, time=now_time)

        cls.evt1, cls.evt2, cls.evt3, cls.evt4, cls.evt5 = Event.objects.all()

        for i in range(2):
            factories.EventFactory(
                kind="another_kind", ordinal=i * 3 + 1, time=now_time)
            now_time += timedelta(weeks=1)

        cls.evt_another_kind1, cls.evt_another_kind2 = (
            Event.objects.filter(kind="another_kind"))
        cls.evt_another_kind1_ordinal = cls.evt_another_kind1.ordinal
        cls.evt_another_kind2_ordinal = cls.evt_another_kind2.ordinal
Exemplo n.º 10
0
    def test_clean_end_time(self):
        with self.assertRaises(ValidationError) as cm:
            factories.EventFactory(course=self.course,
                                   time=now(),
                                   kind="some_kind",
                                   end_time=now() - timedelta(seconds=1))

        expected_error_msg = "End time must not be ahead of start time."
        self.assertIn(expected_error_msg,
                      cm.exception.message_dict["end_time"])

        # make sure end_time >= time is valid
        factories.EventFactory(course=self.course,
                               time=now(),
                               kind="some_kind",
                               end_time=now())

        factories.EventFactory(course=self.course,
                               time=now(),
                               kind="some_kind",
                               end_time=now() + timedelta(seconds=1))
Exemplo n.º 11
0
    def test_post_event_already_exists_starting_ordinal_not_specified(self):
        factories.EventFactory(
            course=self.course, kind=self.default_event_kind, ordinal=4)
        resp = self.post_create_recurring_events_view(
            data=self.get_post_create_recur_evt_data())
        self.assertEqual(resp.status_code, 200)

        # created and ordinal is not None
        self.assertEqual(
            Event.objects.filter(
                kind=self.default_event_kind, ordinal__isnull=False).count(), 6)

        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("Events created.")
Exemplo n.º 12
0
    def test_post_event_already_exists_starting_ordinal_specified(self):
        factories.EventFactory(
            course=self.course, kind=self.default_event_kind, ordinal=7)
        resp = self.post_create_recurring_events_view(
            data=self.get_post_create_recur_evt_data(starting_ordinal=4))
        self.assertEqual(resp.status_code, 200)

        # not created
        self.assertEqual(Event.objects.count(), 1)

        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith(
            "EventAlreadyExists: 'lecture 7' already exists. "
            "No events created.")
Exemplo n.º 13
0
    def test_hidden_event_not_shown(self):
        factories.EventFactory(kind=self.default_event_kind,
                               course=self.course,
                               time=self.default_event_time)
        factories.EventFactory(kind=self.default_event_kind,
                               course=self.course,
                               shown_in_calendar=False,
                               time=self.default_event_time +
                               timedelta(hours=1))

        resp = self.get_course_calender_view()
        self.assertEqual(resp.status_code, 200)

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 1)
        self.assertDictEqual(
            events_json[0], {
                'id': 1,
                'start': self.default_event_time.isoformat(),
                'allDay': False,
                'title': 'lecture 0'
            })
        self.assertResponseContextEqual(resp, "event_info_list", [])
Exemplo n.º 14
0
    def test_no_ordinal_event_not_renumbered(self):

        no_ordinal_event = factories.EventFactory(
            kind=self.default_event_kind, ordinal=None)

        resp = self.post_renumber_events_view(
            data=self.get_post_renumber_evt_data(
                starting_ordinal=3, preserve_ordinal_order=True))
        self.assertEqual(resp.status_code, 200)

        all_evts_with_ordinal = Event.objects.filter(
            kind=self.default_event_kind, ordinal__isnull=False)
        self.assertEqual(all_evts_with_ordinal.count(), 5)
        self.assertListEqual(
            list(all_evts_with_ordinal.values_list("ordinal", flat=True)),
            [3, 4, 5, 6, 7])

        no_ordinal_event.refresh_from_db()
        self.assertEqual(no_ordinal_event.kind, self.default_event_kind)
        self.assertIsNone(no_ordinal_event.ordinal)

        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("Events renumbered.")
Exemplo n.º 15
0
    def test_all_day_event(self):
        self.switch_to_fake_commit_sha()

        # lecture 2, no end_time
        lecture2_start_time = datetime.datetime(2019, 1, 1, tzinfo=pytz.UTC)

        self.mock_get_now_or_fake_time.return_value = (
                lecture2_start_time + timedelta(minutes=5))

        lecture2_evt = factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            all_day=True,
            time=lecture2_start_time, ordinal=2)

        # lecture 3
        lecture3_start_time = lecture2_start_time + timedelta(weeks=1)
        factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=lecture3_start_time, ordinal=3)

        resp = self.get_course_calendar_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 2)

        self.assertDictEqual(
            events_json[1],
            {'id': lecture2_evt.pk, 'start': lecture2_start_time.isoformat(),
             'allDay': True,
             'title': 'Lecture 2',
             'url': '#event-%i' % lecture2_evt.pk})

        # now we add end_time of lecture 2 evt to a time which is not midnight
        lecture2_end_time = lecture2_start_time + timedelta(hours=18)
        lecture2_evt.end_time = lecture2_end_time
        lecture2_evt.save()

        resp = self.get_course_calendar_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 2)

        self.assertDictEqual(
            events_json[1],
            {'id': lecture2_evt.pk, 'start': lecture2_start_time.isoformat(),
             'allDay': True,
             'title': 'Lecture 2',
             'url': '#event-%i' % lecture2_evt.pk,
             'end': lecture2_end_time.isoformat()
             })

        # now we update end_time of lecture 2 evt to midnight
        while True:
            local_t = as_local_time(lecture2_end_time)
            end_midnight = datetime.time(tzinfo=local_t.tzinfo)
            if local_t.time() == end_midnight:
                lecture2_evt.end_time = lecture2_end_time
                lecture2_evt.save()
                break

            lecture2_end_time += timedelta(hours=1)

        resp = self.get_course_calendar_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 2)

        self.assertDictEqual(
            events_json[1],
            {'id': lecture2_evt.pk, 'start': lecture2_start_time.isoformat(),
             'allDay': True,
             'title': 'Lecture 2',
             'url': '#event-%i' % lecture2_evt.pk,
             'end': lecture2_end_time.isoformat()
             })
Exemplo n.º 16
0
    def test_events_file_with_events_test2(self):
        self.switch_to_fake_commit_sha()

        self.mock_get_now_or_fake_time.return_value = (
                self.default_event_time + timedelta(minutes=5))

        # lecture 2
        evt_lecture2 = factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=self.default_event_time, ordinal=2)

        # lecture 3
        lecture3_start_time = self.default_event_time + timedelta(weeks=1)
        evt_lecture3 = factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=lecture3_start_time, ordinal=3)

        # test event
        test_start_time = self.default_event_time + timedelta(minutes=1)
        evt_all_day = factories.EventFactory(
            kind="test", course=self.course, all_day=True,
            time=test_start_time,
            ordinal=None)

        resp = self.get_course_calendar_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 3)

        self.assertDictEqual(
            events_json[0],
            {'id': evt_lecture3.pk, 'start': (lecture3_start_time).isoformat(),
             'allDay': False,
             'title': 'Lecture 3'})

        self.assertDictEqual(
            events_json[1],
            {'id': evt_lecture2.pk, 'start': self.default_event_time.isoformat(),
             'allDay': False,
             'title': 'Lecture 2',
             'url': '#event-%i' % evt_lecture2.pk})

        self.assertDictEqual(
            events_json[2],
            {'id': evt_all_day.pk, 'start': test_start_time.isoformat(),
             'allDay': True,
             'title': 'test'})

        event_info_list = resp.context["event_info_list"]

        # only lecture 2 create an EventInfo object
        self.assertEqual(len(event_info_list), 1)

        # check the attributes of EventInfo of lecture 1
        evt_info_dict = event_info_list[0].__dict__
        evt_description = evt_info_dict.pop("description")

        self.assertDictEqual(
            evt_info_dict,
            {"id": evt_lecture2.pk, "human_title": "Lecture 2",
             "start_time": self.default_event_time,
             "end_time": None})

        self.assertIn(
            'Can you see this?',
            evt_description)

        # lecture 2's description exceeded show_description_until
        self.mock_get_now_or_fake_time.return_value = (
                lecture3_start_time + timedelta(minutes=5))

        # no EventInfo object
        resp = self.get_course_calendar_view()
        self.assertResponseContextEqual(resp, "event_info_list", [])