def test_get_all_queued_events(self): patient = Patient() patient.save() sendable = InfoMessage(text="Test Message", way_of_communication = get_woc("sms")) sendable.recipient = patient sendable.save() self.assertEquals(scheduler.get_all_queued_events().count(), 0) schedule1 = ScheduledEvent(sendable=sendable, send_time=datetime.now(), state = "queued") schedule1.save() self.assertEquals(scheduler.get_all_queued_events().count(), 1) schedule2 = ScheduledEvent(sendable=sendable, send_time=(datetime.now() - timedelta(days=1)), state = "sent") schedule2.save() self.assertTrue(schedule1 in scheduler.get_all_queued_events()) self.assertFalse(schedule2 in scheduler.get_all_queued_events()) self.assertEquals(scheduler.get_all_queued_events().count(), 1) schedule2.state = "queued" schedule2.save() self.assertEquals(scheduler.get_all_queued_events().count(), 2) schedule1.delete() schedule2.delete()
def test_get_all_due_events(self): patient = Patient() patient.save() # The fixtures already contain two due events self.assertEquals(scheduler.get_all_due_events().count(), 2) sendable = InfoMessage(text="Test Message", way_of_communication = get_woc("sms")) sendable.recipient = patient sendable.save() schedule1 = ScheduledEvent(sendable=sendable, send_time=datetime.now()) schedule1.save() schedule2 = ScheduledEvent(sendable=sendable, send_time=(datetime.now() - timedelta(days=1))) schedule2.save() schedule3 = ScheduledEvent(sendable=sendable, send_time=(datetime.now() + timedelta(days=1))) schedule3.save() self.assertEquals(scheduler.get_all_due_events().count(), 4) self.assertTrue(schedule1 in scheduler.get_all_due_events()) self.assertTrue(schedule2 in scheduler.get_all_due_events()) self.assertFalse(schedule3 in scheduler.get_all_due_events()) schedule4 = ScheduledEvent(sendable=sendable, send_time=datetime.now(), state = "failed") schedule4.save() schedule5 = ScheduledEvent(sendable=sendable, send_time=(datetime.now() - timedelta(days=1)), state = "sent") schedule5.save() self.assertEquals(scheduler.get_all_due_events().count(), 4) schedule1.delete() schedule2.delete() schedule3.delete() schedule4.delete() schedule5.delete()
def create_messages_for_infoservice(infoservice, text): ''' Put together all information for an infomessage and calls InfoService.create_scheduled_event ''' for patient in infoservice.members.all(): info_message = InfoMessage() info_message.text = text subscription = Subscription.objects.filter(patient = patient, infoservice = infoservice)[0] info_message.recipient = patient info_message.send_time = datetime.now() info_message.way_of_communication = \ subscription.way_of_communication info_message.save() info_message.create_scheduled_event(datetime.now()) logger.info("Created %s", str(info_message))
def test_check_spool_files(self): def get_mock_status(filename): """ This is a mock method to represent check_spoolfile_status It is told which status to return by using the (in this case useless) filename parameter """ return filename original_get_spoolfile_status = scheduler.get_spoolfile_status scheduler.get_spoolfile_status = get_mock_status patient = Patient() patient.save() sendable = InfoMessage(text="Test Message", way_of_communication = get_woc("sms")) sendable.recipient = patient sendable.save() event1 = ScheduledEvent(sendable=sendable, send_time=datetime.now(), state = "queued", filename = "Completed") event1.save() event2 = ScheduledEvent(sendable=sendable, send_time=datetime.now(), state = "queued", filename = "Expired") event2.save() # now: the real testing scheduler.check_spool_files() self.assertEquals(ScheduledEvent.objects.get(pk = event1.pk).state, \ "done") self.assertEquals(ScheduledEvent.objects.get(pk = event2.pk).state, \ "new") event2.filename = "Expired" event2.save() scheduler.check_spool_files() self.assertEquals(ScheduledEvent.objects.get(pk = event2.pk).state, \ "new") self.assertEquals(ScheduledEvent.objects.get(pk = event2.pk).retry, 1) event2.retry = settings.ASTERISK_RETRY event2.filename = "Expired" event2.save() scheduler.check_spool_files() self.assertEquals(ScheduledEvent.objects.get(pk = event2.pk).state, \ "failed") event3 = ScheduledEvent(sendable=sendable, send_time=datetime.now(), state = "queued", filename = "Failed") event3.save() scheduler.check_spool_files() self.assertEquals(ScheduledEvent.objects.get(pk = event3.pk).state, \ "failed") # change everything back to normal event1.delete() event2.delete() event3.delete() scheduler.get_spoolfile_status = original_get_spoolfile_status