Ejemplo n.º 1
0
    def test_autocreate_project_report(self):
        """ Ensures that when an EventForm is saved, a project report
        corresponding to that event is created, depending on the needs_pr
        field.
        """
        # Create the fields for an Event, based on the event created in setUp,
        # simply with a different event name
        event_name_no_pr = 'My Event Without a PR'
        event_name_pr = 'My Event With a PR'
        fields = {
            'name': event_name_no_pr,
            'event_type': self.event.event_type.pk,
            'term': self.event.term.pk,
            'contact': self.user.pk,
            'committee': self.event.committee.pk,
            'restriction': Event.OFFICER,
            'location': self.event.location,
            'requirements_credit': self.event.requirements_credit,
            'max_guests_per_person': self.event.max_guests_per_person,
            'signup_limit': self.event.signup_limit,
            'start_datetime_0': self.start_date,
            'start_datetime_1': self.start_time,
            'end_datetime_0': self.end_date,
            'end_datetime_1': self.end_time,
            'needs_pr': False
        }

        # Ensure that saving the EventForm creates the event and that no
        # project report is created:
        EventForm(fields).save()
        event = get_object_or_none(Event, name=event_name_no_pr)
        self.assertIsNotNone(event)
        self.assertIsNone(event.project_report)
        self.assertFalse(ProjectReport.objects.all().exists())

        # Create event with form, requiring project report, and ensure PR
        # is created:
        fields.update({'name': event_name_pr, 'needs_pr': True})
        EventForm(fields).save()
        event = get_object_or_none(Event, name=event_name_pr)
        self.assertIsNotNone(event)
        self.assertTrue(ProjectReport.objects.all().exists())
        project_report = ProjectReport.objects.all()[0]

        # Check the properties of both the event and project report to ensure
        # that they were saved and match our form
        self.assertEqual(event.name, event_name_pr)
        self.assertEqual(project_report.title, event_name_pr)

        self.assertEqual(event.start_datetime.date(),
                         self.event.start_datetime.date())
        self.assertEqual(project_report.date, self.event.start_datetime.date())

        self.assertEqual(event.contact, self.user)
        self.assertEqual(project_report.author, self.user)

        self.assertEqual(event.term, self.event.term)
        self.assertEqual(project_report.term, self.event.term)

        self.assertEqual(project_report, event.project_report)
Ejemplo n.º 2
0
    def test_clean(self):
        form = EventForm()
        # Blank form should be invalid:
        self.assertFalse(form.is_valid())

        # Create a form with all fields logically/properly filled out:
        form = self.create_basic_event_form({
            'start_datetime_0': self.start_date,
            'start_datetime_1': self.start_time,
            'end_datetime_0': self.end_date,
            'end_datetime_1': self.end_time
        })
        self.assertTrue(form.is_valid())

        # Create an invalid form with invalid input for start and/or end time:
        start_error = ['Your start time is not in the proper format.']
        end_error = ['Your end time is not in the proper format.']
        end_before_start = ['Your event is scheduled to end before it starts.']
        # Missing start and end times:
        form = self.create_basic_event_form()
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors.get('start_datetime', None), start_error)
        self.assertEqual(form.errors.get('end_datetime', None), end_error)

        # Missing start time:
        form = self.create_basic_event_form({
            'end_datetime_0': self.end_date,
            'end_datetime_1': self.end_time
        })
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors.get('start_datetime', None), start_error)
        self.assertIsNone(form.errors.get('end_datetime', None))

        # Invalid (non-datetime) end time:
        # (Note that the same validation error will occur if end_datetime not
        # specified.)
        form = self.create_basic_event_form({
            'start_datetime_0': self.start_date,
            'start_datetime_1': self.start_time,
            'end_datetime_0': 'not a date',
            'end_datetime_1': 'not a time'
        })
        self.assertFalse(form.is_valid())
        self.assertIsNone(form.errors.get('start_datetime', None))
        self.assertEqual(form.errors.get('end_datetime', None), end_error)

        # Create a form with event end time before start time:
        form = self.create_basic_event_form({
            'start_datetime_0': self.end_date,
            'start_datetime_1': self.end_time,
            'end_datetime_0': self.start_date,
            'end_datetime_1': self.start_time
        })
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors.get('start_datetime', None),
                         end_before_start)
        self.assertEqual(form.errors.get('end_datetime', None),
                         end_before_start)
Ejemplo n.º 3
0
    def setup_cancel_event(self, extra_fields=None, delete_pr=True):
        """ Creates a basic event and returns a cancellation form.

        The extra_fields kwarg is used to pass a dictionary of additional
        fields to include when creating the form. Note that an event name
        should be supplied to the extra args to allow for a valid event.
        """
        fields = {'event_type': self.event.event_type.pk,
                  'term': self.event.term.pk,
                  'contact': self.user.pk,
                  'committee': self.event.committee.pk,
                  'restriction': Event.OFFICER,
                  'location': self.event.location,
                  'requirements_credit': self.event.requirements_credit,
                  'max_guests_per_person': self.event.max_guests_per_person,
                  'signup_limit': self.event.signup_limit,
                  'start_datetime_0': self.start_date,
                  'start_datetime_1': self.start_time,
                  'end_datetime_0': self.end_date,
                  'end_datetime_1': self.end_time,
                  'needs_pr': False}
        d_fields = {'delete_report': True}
        no_d_fields = {'delete_report': False}
        if extra_fields:
            fields.update(extra_fields)
        EventForm(fields).save()
        if delete_pr:
            return EventCancelForm(d_fields)
        else:
            return EventCancelForm(no_d_fields)
Ejemplo n.º 4
0
    def test_clean(self):
        form = EventForm()
        # Blank form should be invalid:
        self.assertFalse(form.is_valid())

        # Create a form with all fields logically/properly filled out:
        form = self.create_basic_event_form(
            {'start_datetime_0': self.start_date,
             'start_datetime_1': self.start_time,
             'end_datetime_0': self.end_date,
             'end_datetime_1': self.end_time})
        self.assertTrue(form.is_valid())

        # Create an invalid form with invalid input for start and/or end time:
        start_error = ['Your start time is not in the proper format.']
        end_error = ['Your end time is not in the proper format.']
        end_before_start = ['Your event is scheduled to end before it starts.']
        # Missing start and end times:
        form = self.create_basic_event_form()
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors.get('start_datetime', None),
                         start_error)
        self.assertEqual(form.errors.get('end_datetime', None),
                         end_error)

        # Missing start time:
        form = self.create_basic_event_form(
            {'end_datetime_0': self.end_date,
             'end_datetime_1': self.end_time})
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors.get('start_datetime', None),
                         start_error)
        self.assertIsNone(form.errors.get('end_datetime', None))

        # Invalid (non-datetime) end time:
        # (Note that the same validation error will occur if end_datetime not
        # specified.)
        form = self.create_basic_event_form(
            {'start_datetime_0': self.start_date,
             'start_datetime_1': self.start_time,
             'end_datetime_0': 'not a date',
             'end_datetime_1': 'not a time'})
        self.assertFalse(form.is_valid())
        self.assertIsNone(form.errors.get('start_datetime', None))
        self.assertEqual(form.errors.get('end_datetime', None),
                         end_error)

        # Create a form with event end time before start time:
        form = self.create_basic_event_form(
            {'start_datetime_0': self.end_date,
             'start_datetime_1': self.end_time,
             'end_datetime_0': self.start_date,
             'end_datetime_1': self.start_time})
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors.get('start_datetime', None),
                         end_before_start)
        self.assertEqual(form.errors.get('end_datetime', None),
                         end_before_start)
Ejemplo n.º 5
0
    def test_form_modifying_project_report(self):
        """ Ensures that when an EventForm is saved and a project report (PR)
        already exists for that event, that the PR is updated if the event
        still needs it or the PR is deleted if not.
        """
        # Fill out the form for the existing event and require a PR:
        fields = {'name': self.event.name,
                  'event_type': self.event.event_type.pk,
                  'term': self.event.term.pk,
                  'contact': self.user.pk,
                  'committee': self.event.committee.pk,
                  'restriction': Event.OFFICER,
                  'location': self.event.location,
                  'requirements_credit': self.event.requirements_credit,
                  'max_guests_per_person': self.event.max_guests_per_person,
                  'signup_limit': self.event.signup_limit,
                  'start_datetime_0': self.start_date,
                  'start_datetime_1': self.start_time,
                  'end_datetime_0': self.end_date,
                  'end_datetime_1': self.end_time,
                  'needs_pr': True}
        form = EventForm(data=fields, instance=self.event)
        # Ensure that saving the EventForm for the existing event creates a PR:
        self.assertIsNone(self.event.project_report)  # No PR yet
        form.save()
        self.assertIsNotNone(self.event.project_report)  # Now has a PR
        project_report = ProjectReport.objects.all().get()
        self.assertEquals(self.event.project_report, project_report)
        self.assertEquals(self.event.name, project_report.title)

        # Ensure that saving a new version of the EventForm with an update to
        # an event field updates the existing event PR
        new_name = 'My New Event Name'
        fields.update({'name': new_name})
        form = EventForm(data=fields, instance=self.event)
        form.save()
        self.assertEquals(new_name, self.event.name)
        project_report = ProjectReport.objects.all().get()
        self.assertEquals(self.event.name, project_report.title)

        # Now modify the form to mark that a project report is not needed,
        # ensure that the PR is deleted when the form is saved
        fields.update({'needs_pr': False})
        form = EventForm(data=fields, instance=self.event)
        form.save()
        self.assertFalse(ProjectReport.objects.all().exists())
        self.assertIsNone(self.event.project_report)
Ejemplo n.º 6
0
    def create_basic_event_form(self, extra_fields=None):
        """Returns an event form with some of the common fields filled out.

        The extra_fields kwarg is used to pass a dictionary of additional
        fields to include when creating the form.  Note that without specifying
        some additional fields, the form returned is not a valid form, as event
        start and end times are required fields.
        """
        fields = {'name': self.event.name,
                  'event_type': self.event.event_type.pk,
                  'term': self.event.term.pk,
                  'contact': self.user.pk,
                  'committee': self.event.committee.pk,
                  'restriction': Event.OFFICER,
                  'location': self.event.location,
                  'requirements_credit': self.event.requirements_credit,
                  'max_guests_per_person': self.event.max_guests_per_person,
                  'signup_limit': self.event.signup_limit}
        if extra_fields:
            fields.update(extra_fields)
        return EventForm(fields)
Ejemplo n.º 7
0
    def test_form_modifying_project_report(self):
        """ Ensures that when an EventForm is saved and a project report (PR)
        already exists for that event, that the PR is updated if the event
        still needs it or the PR is deleted if not.
        """
        # Fill out the form for the existing event and require a PR:
        fields = {
            'name': self.event.name,
            'event_type': self.event.event_type.pk,
            'term': self.event.term.pk,
            'contact': self.user.pk,
            'committee': self.event.committee.pk,
            'restriction': Event.OFFICER,
            'location': self.event.location,
            'requirements_credit': self.event.requirements_credit,
            'max_guests_per_person': self.event.max_guests_per_person,
            'signup_limit': self.event.signup_limit,
            'start_datetime_0': self.start_date,
            'start_datetime_1': self.start_time,
            'end_datetime_0': self.end_date,
            'end_datetime_1': self.end_time,
            'needs_pr': True
        }
        form = EventForm(data=fields, instance=self.event)
        # Ensure that saving the EventForm for the existing event creates a PR:
        self.assertIsNone(self.event.project_report)  # No PR yet
        form.save()
        self.assertIsNotNone(self.event.project_report)  # Now has a PR
        project_report = ProjectReport.objects.all().get()
        self.assertEquals(self.event.project_report, project_report)
        self.assertEquals(self.event.name, project_report.title)

        # Ensure that saving a new version of the EventForm with an update to
        # an event field updates the existing event PR
        new_name = 'My New Event Name'
        fields.update({'name': new_name})
        form = EventForm(data=fields, instance=self.event)
        form.save()
        self.assertEquals(new_name, self.event.name)
        project_report = ProjectReport.objects.all().get()
        self.assertEquals(self.event.name, project_report.title)

        # Now modify the form to mark that a project report is not needed,
        # ensure that the PR is deleted when the form is saved
        fields.update({'needs_pr': False})
        form = EventForm(data=fields, instance=self.event)
        form.save()
        self.assertFalse(ProjectReport.objects.all().exists())
        self.assertIsNone(self.event.project_report)