def test_update_schedule_end_date_before_start_raises_error(self): serialize_input_data(recipients=self.recipients) schedule = Schedule.objects.first() new_data = create_schedule_input_data() new_data['start_date'] = new_data['end_date'] + timedelta(days=1) serializer = ScheduleSerializer(instance=schedule, data=new_data) with self.assertRaises(ValidationError): serializer.is_valid(raise_exception=True)
def test_recipient_in_db_not_raises_validation_error(self): recipients = [{'name': 'test', 'email_address': '*****@*****.**'}] data_1 = create_schedule_input_data(content="placeholder 1", recipients=recipients) data_2 = create_schedule_input_data(content="placeholder 2", recipients=recipients) serializer_1 = ScheduleSerializer(data=data_1) serializer_2 = ScheduleSerializer(data=data_2) self.assertEqual(serializer_1.is_valid(), True) serializer_1.save() self.assertEqual(serializer_2.is_valid(), True) serializer_2.save()
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)
def test_update_schedule_status_to_paused(self): serialize_input_data(recipients=self.recipients) schedule = Schedule.objects.first() self.assertEqual(schedule.status, Schedule.NOT_ADDED) new_data = create_schedule_input_data() new_data['status'] = Schedule.PAUSED serializer = ScheduleSerializer(instance=schedule, data=new_data) self.assertEqual(serializer.is_valid(), True) if serializer.is_valid(): serializer.save() self.assertEqual(schedule.status, Schedule.PAUSED) self.assertEqual(Schedule.objects.count(), 1)
def test_recipients_more_than_500_raises_validation_error(self): data = {} recipients = [{ 'name': 'test', 'email_address': f'test{idx}@test.com' } for idx in range(501)] data = create_schedule_input_data(recipients=recipients) serializer = ScheduleSerializer(data=data) serializer.is_valid() self.assertEqual(serializer.errors['non_field_errors'][0], RECIPIENTS_GREATER_THAN_500_ERROR) with self.assertRaises(ValidationError): serializer.is_valid(raise_exception=True)
def schedule_one(request, pk): """ view to get or update or delete a single schedule corresponding to the primary key passed as an argument to the view. """ try: schedule = Schedule.objects.get(pk=pk) except Schedule.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = ScheduleSerializer(instance=schedule) return JsonResponse(serializer.data, status=200) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = ScheduleSerializer(instance=schedule, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': schedule.delete() return HttpResponse(status=204)
def test_recipient_duplication_raises_validation_error(self): recipients = [{ 'name': 'test', 'email_address': '*****@*****.**' }, { 'name': 'test', 'email_address': '*****@*****.**' }] data = create_schedule_input_data(recipients=recipients) serializer = ScheduleSerializer(data=data) serializer.is_valid() self.assertEqual(serializer.errors['non_field_errors'][0], RECIPIENTS_CONTAIN_DUPLICATES_ERROR) with self.assertRaises(ValidationError): serializer.is_valid(raise_exception=True)
def test_invalid_email_addresses_raise_validation_error(self): recipients = [{ 'name': 'test', 'email_address': 'test' }, { 'name': 'test', 'email_address': '*****@*****.**' }] data = create_schedule_input_data(recipients=recipients) schedule_serializer = ScheduleSerializer(data=data) self.assertEqual(schedule_serializer.is_valid(), False) self.assertEqual( schedule_serializer.errors['recipients'][0]['email_address'][0], INVALID_EMAIL_ADDRESS_ERROR) self.assertEqual(Schedule.objects.count(), 0) self.assertEqual(Recipient.objects.count(), 0) with self.assertRaises(ValidationError): schedule_serializer.is_valid(raise_exception=True)
def test_update_interval_when_updating_schedule(self): serialize_input_data(recipients=self.recipients) schedule = Schedule.objects.first() interval = Interval.objects.first() self.assertEqual(schedule.frequency, interval.interval) new_data = create_schedule_input_data() new_data['frequency'] = timedelta(days=3) new_data['end_date'] = new_data['start_date'] + timedelta(days=5) serializer = ScheduleSerializer(instance=schedule, data=new_data) self.assertEqual(serializer.is_valid(), True) if serializer.is_valid(): serializer.save() interval = Interval.objects.get(interval=new_data['frequency']) self.assertEqual(Schedule.objects.count(), 1) self.assertEqual(Interval.objects.count(), 2) self.assertEqual(schedule.frequency, new_data['frequency']) self.assertEqual(schedule.frequency, interval.interval)
def test_update_schedule_content(self): serialize_input_data(recipients=self.recipients) self.assertEqual(Schedule.objects.count(), 1) data = create_schedule_input_data(content="new content") schedule = Schedule.objects.first() updated_serializer = ScheduleSerializer(instance=schedule, data=data) self.assertEqual(updated_serializer.is_valid(), True) updated_serializer.save() self.assertEqual(schedule.content, updated_serializer.data['content']) self.assertEqual(schedule.content, "new content")
def test_serializer_can_deserialize_json(self): recipients = [{ 'name': 'test', 'email_address': '*****@*****.**' }, { 'name': 'test2', 'email_address': '*****@*****.**' }] data = create_schedule_input_data(recipients=recipients) serializer = ScheduleSerializer(data=data) self.assertEqual(serializer.is_valid(), True) schedule = serializer.save() self.assertIsInstance(schedule, Schedule) self.assertEqual(Schedule.objects.count(), 1) self.assertEqual(Recipient.objects.count(), 2)
def schedule_list(request): """ view to fetch all schedules stored or to add a new schedule """ if request.method == 'GET': schedules = Schedule.objects.all() serializers = ScheduleSerializer(instance=schedules, many=True) return JsonResponse(serializers.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = ScheduleSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def test_update_schedule_recipients(self): serialize_input_data(recipients=self.recipients) self.assertEqual(Schedule.objects.count(), 1) recipients = [{'name': 'test', 'email_address': '*****@*****.**'}] data = create_schedule_input_data(recipients=recipients) schedule = Schedule.objects.first() updated_serializer = ScheduleSerializer(instance=schedule, data=data) self.assertEqual(updated_serializer.is_valid(), True) updated_serializer.save() updated_recipients = schedule.recipients.all() self.assertEqual( updated_recipients[0].email_address, updated_serializer.data['recipients'][0]['email_address']) self.assertEqual(updated_recipients[0].email_address, '*****@*****.**')
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)
def test_schedule_unique_together_validation(self): recipients = [{'name': 'test', 'email_address': '*****@*****.**'}] data_1 = create_schedule_input_data(recipients=recipients) serializer_1 = ScheduleSerializer(data=data_1) serializer_1.is_valid() serializer_1.save() data_2 = create_schedule_input_data(recipients=recipients) serializer_2 = ScheduleSerializer(data=data_2) serializer_2.is_valid() self.assertEqual(serializer_2.errors['non_field_errors'][0], FIELDS_NOT_UNIQUE_TOGETHER_ERROR) with self.assertRaises(ValidationError): serializer_2.is_valid(raise_exception=True)
def test_updating_interval_adds_new_celery_task(self): serialize_input_data(recipients=self.recipients) schedule = Schedule.objects.first() interval = Interval.objects.first() self.assertEqual(interval.interval, schedule.frequency) self.assertIsNotNone(app.conf.beat_schedule[str(interval.interval)]) self.assertEqual(Interval.objects.count(), 1) new_data = create_schedule_input_data() new_data['frequency'] = timedelta(days=3) new_data['end_date'] = new_data['start_date'] + timedelta(days=5) serializer = ScheduleSerializer(instance=schedule, data=new_data) if serializer.is_valid(): serializer.save() interval = Interval.objects.get(interval=new_data['frequency']) self.assertIsNotNone(app.conf.beat_schedule[str(interval.interval)]) # self.assertEqual(len(app.conf.beat_schedule), 2) self.assertEqual(Interval.objects.count(), 2)
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()