コード例 #1
0
 def test_frequency_greater_than_end_date_raises_validation_error(self):
     s1 = Schedule(content="placeholder",
                   frequency=timedelta(days=5),
                   end_date=default_date_time(),
                   start_date=default_date_time(days=1))
     with self.assertRaises(ValidationError):
         s1.full_clean()
コード例 #2
0
    def test_duplicate_schedule_is_invalid(self):
        start_date = default_date_time()
        end_date = default_date_time(days=1)
        s1 = Schedule.objects.create(start_date=start_date, end_date=end_date)

        with self.assertRaises(ValidationError):
            s2 = Schedule(start_date=start_date, end_date=end_date)
            s2.full_clean()
コード例 #3
0
 def test_stop_date_cannot_be_before_start_date(self):
     s1 = Schedule()
     s1.start_date = default_date_time()
     s1.end_date = s1.start_date - timedelta(days=5)
     # should raise error
     with self.assertRaises(ValidationError):
         s1.full_clean()
コード例 #4
0
    def test_update_schedule_frequency(self):
        serialize_input_data(start_date=default_date_time(),
                             end_date=default_date_time(days=5),
                             recipients=self.recipients)
        self.assertEqual(Schedule.objects.count(), 1)

        data = create_schedule_input_data(frequency=timedelta(days=2),
                                          end_date=default_date_time(days=5))
        schedule = Schedule.objects.first()
        updated_serializer = ScheduleSerializer(instance=schedule, data=data)
        self.assertEqual(updated_serializer.is_valid(), True)
        updated_serializer.save()
        self.assertEqual(updated_serializer.validated_data['frequency'],
                         timedelta(days=2))
        self.assertEqual(updated_serializer.validated_data['frequency'],
                         schedule.frequency)
        self.assertEqual(Schedule.objects.count(), 1)
コード例 #5
0
def create_schedule_input_data(description="placeholder",
                               subject="placeholder",
                               content="placeholder",
                               frequency=timedelta(days=1),
                               start_date=default_date_time(),
                               end_date=default_date_time(days=1),
                               status=Schedule.NOT_ADDED,
                               recipients=[]):
    data = {}
    data['description'] = description
    data['subject'] = subject
    data['content'] = content
    data['frequency'] = frequency
    data['start_date'] = start_date
    data['end_date'] = end_date
    data['status'] = status
    data['recipients'] = recipients
    return data
コード例 #6
0
def serialize_input_data(description="placeholder",
                         subject="placeholder",
                         content="placeholder",
                         frequency=timedelta(days=1),
                         start_date=default_date_time(),
                         end_date=default_date_time(days=1),
                         status=Schedule.NOT_ADDED,
                         recipients=[],
                         num_schedules=1):
    for num in range(num_schedules):
        data = create_schedule_input_data(description=description,
                                          subject=subject,
                                          content=f"{content}: {num}",
                                          frequency=frequency,
                                          start_date=start_date,
                                          end_date=end_date,
                                          status=status,
                                          recipients=recipients)
        serializer = ScheduleSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
コード例 #7
0
def start_date_after_today(start_date):
    """
    there must be valid start_date and end_date values
    check for valid start_date values
    """
    if start_date:
        if start_date < default_date_time():
            raise exceptions.ValidationError(
                START_DATE_CANNOT_BE_BEFORE_TODAY_ERROR
            )
    else:
        raise exceptions.ValidationError(START_DATE_NOT_ADDED_ERROR)
コード例 #8
0
    def test_update_frequency_gt_end_date_raises_validation_error(self):
        serialize_input_data(recipients=self.recipients)
        self.assertEqual(Schedule.objects.count(), 1)

        data = create_schedule_input_data(frequency=timedelta(days=5),
                                          end_date=default_date_time(days=1))
        schedule = Schedule.objects.first()
        updated_serializer = ScheduleSerializer(instance=schedule, data=data)
        updated_serializer.is_valid()
        self.assertEqual(updated_serializer.errors['non_field_errors'][0],
                         FREQUENCY_GREATER_THAN_END_DATE_ERROR)
        with self.assertRaises(ValidationError):
            updated_serializer.is_valid(raise_exception=True)
コード例 #9
0
 def test_save_method_performs_validation(self):
     s1 = Schedule()
     s1.start_date = default_date_time(days=2, subtract=True)
     with self.assertRaises(ValidationError):
         s1.save()
コード例 #10
0
 def test_start_date_cannot_be_before_now(self):
     s1 = Schedule()
     s1.start_date = default_date_time(days=5, subtract=True)
     # should raise error
     with self.assertRaises(ValidationError):
         s1.full_clean()