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_get_schedule(self): serialize_input_data(recipients=self.recipients, num_schedules=5) pk = [s.pk for s in Schedule.objects.all()] response_1 = self.client.get(path=f'{self.schedule_base_url}{pk[0]}/') self.assertEqual(response_1.status_code, 200) response_2 = self.client.get(path=f'{self.schedule_base_url}{pk[1]}/') self.assertEqual(response_2.status_code, 200)
def test_delete_schedule(self): serialize_input_data(recipients=self.recipients) # schedule = Schedule.objects.all() pk = [s.pk for s in Schedule.objects.all()] response = self.client.delete(path=f'{self.schedule_base_url}{pk[0]}/') self.assertEqual(response.status_code, 204) self.assertEqual(Schedule.objects.count(), 0) self.assertEqual(Recipient.objects.count(), len(self.recipients)) self.assertEqual(len(Schedule.recipients.through.objects.all()), 0)
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_get_schedule_list(self): recipients = self.recipients num_schedules = 5 serialize_input_data(num_schedules=num_schedules, recipients=recipients) response = self.client.get(self.schedule_base_url) bytes_stream = BytesIO(response.content) response_json = JSONParser().parse(bytes_stream) self.assertEqual(response.status_code, 200) self.assertEqual(len(response_json), num_schedules)
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_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_serializer_can_serialize_queryset(self): recipients = [{ 'name': 'test', 'email_address': '*****@*****.**' }, { 'name': 'test2', 'email_address': '*****@*****.**' }] num_schedules = 10 serialize_input_data(recipients=recipients, num_schedules=num_schedules) schedules = Schedule.objects.all() serialized_schedules = ScheduleSerializer(instance=schedules, many=True) self.assertEqual(len(schedules), num_schedules) self.assertEqual(len(serialized_schedules.data), num_schedules)
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_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 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_creating_interval_adds_celery_task(self): serialize_input_data(recipients=self.recipients) schedule = Schedule.objects.first() self.assertIsNotNone(app.conf.beat_schedule[str(schedule.frequency)])
def test_create_interval_when_creating_schedule(self): serialize_input_data(recipients=self.recipients) schedule = Schedule.objects.first() interval = Interval.objects.first() self.assertEqual(schedule.frequency, interval.interval) self.assertEqual(Interval.objects.count(), 1)