def test_excludes_subs_w_all_non_followup_orgs(self): sub_to_include = factories.FormSubmissionWithOrgsFactory.create( date_received=get_old_date(), organizations=[self.followup_org]) sub_to_exclude = factories.FormSubmissionWithOrgsFactory.create( date_received=get_old_date(), organizations=[self.non_followup_org]) results = list(FollowupsService.get_submissions_due_for_follow_ups()) self.assertNotIn(sub_to_exclude, results) self.assertIn(sub_to_include, results)
def test_excludes_subs_w_updates_from_all_orgs(self): orgs = [self.followup_org, self.non_followup_org] sub_to_exclude = factories.FormSubmissionWithOrgsFactory.create( organizations=list(orgs), date_received=get_old_date()) sub_to_include = factories.FormSubmissionWithOrgsFactory.create( organizations=list(orgs), date_received=get_old_date()) for app in sub_to_exclude.applications.all(): factories.StatusUpdateWithNotificationFactory.create( application=app) results = list(FollowupsService.get_submissions_due_for_follow_ups()) self.assertNotIn(sub_to_exclude, results) self.assertIn(sub_to_include, results)
def test_filters_out_subs_with_previous_followup_flag(self): # given old submissions, some with old followups no_followup = factories.FormSubmissionWithOrgsFactory.create( date_received=get_old_date(), organizations=[self.followup_org]) sub_w_followup = factories.FormSubmissionWithOrgsFactory.create( date_received=get_old_date(), organizations=[self.followup_org], has_been_sent_followup=True) # if we grab subs that need followups results = FollowupsService.get_submissions_due_for_follow_ups() results_set = set(results) # we should only have ones that have not been flagged self.assertIn(no_followup, results_set) self.assertNotIn(sub_w_followup, results_set)
def test_can_start_at_particular_id_to_create_time_interval(self): # assume we have 4 old subs, 1 new sub old_subs = sorted([ factories.FormSubmissionWithOrgsFactory.create( date_received=get_old_date(), organizations=[self.followup_org]) for i in range(4) ], key=lambda s: s.date_received) new_sub = factories.FormSubmissionWithOrgsFactory.create( date_received=get_newer_date(), organizations=[self.followup_org]) # but we only want ones after the second oldest sub second_oldest_id = old_subs[1].id # and within the old subs, we still don't want ones that already # received followups applicant = factories.ApplicantFactory() followed_up_sub = old_subs[2] followed_up_sub.applicant = applicant followed_up_sub.has_been_sent_followup = True followed_up_sub.save() # when we get submissions due for follow ups, results = list(FollowupsService.get_submissions_due_for_follow_ups( after_id=second_oldest_id)) # we should only receive two: self.assertEqual(len(results), 2) # the second oldest self.assertIn(old_subs[1], results) # and not-as-old one that did not have a follow up self.assertIn(old_subs[3], results) # we should not receive # the oldest sub self.assertNotIn(old_subs[0], results) # the one with the follow up self.assertNotIn(followed_up_sub, results) # or the new sub self.assertNotIn(new_sub, results)
def test_expected_weekday_run(self, is_the_weekend): is_the_weekend.return_value = False org = Organization.objects.get(slug='ebclc') dates = sorted([mock.get_old_date() for i in range(464, 469)]) for date, pk in zip(dates, range(464, 469)): factories.FormSubmissionWithOrgsFactory.create( id=pk, date_received=date, organizations=[org], answers=get_answers_for_orgs( [org], contact_preferences=[ 'prefers_email', 'prefers_sms'], phone='4445551111', email='*****@*****.**', )) command = send_followups.Command() command.stdout = Mock() with self.assertLogs( 'project.services.logging_service', logging.INFO) as logs: command.handle() self.assertEqual( len(self.notifications.email_followup.send.mock_calls), 4) assertInLogsCount(logs, {'event_name=app_followup_sent': 4})
def test_expected_weekday_run(self, slack, is_the_weekend): is_the_weekend.return_value = False org = Organization.objects.get(slug='ebclc') dates = sorted([mock.get_old_date() for i in range(464, 469)]) for date, pk in zip(dates, range(464, 469)): applicant = Applicant() applicant.save() factories.FormSubmissionWithOrgsFactory.create( id=pk, applicant=applicant, date_received=date, organizations=[org], answers=get_answers_for_orgs( [org], contact_preferences=[ 'prefers_email', 'prefers_sms'], phone='4445551111', email='*****@*****.**', )) command = send_followups.Command() command.stdout = Mock() command.handle() self.assertEqual( len(slack.mock_calls), 1) self.assertEqual( len(self.notifications.email_followup.send.mock_calls), 4) self.assertEqual( len(self.notifications.slack_notification_sent.send.mock_calls), 4)
def test_expected_weekday_run(self, slack, is_the_weekend): is_the_weekend.return_value = False org = Organization.objects.get(slug='ebclc') dates = sorted([mock.get_old_date() for i in range(464, 469)]) for date, pk in zip(dates, range(464, 469)): factories.FormSubmissionWithOrgsFactory.create( id=pk, date_received=date, organizations=[org], answers=get_answers_for_orgs( [org], contact_preferences=[ 'prefers_email', 'prefers_sms'], phone='4445551111', email='*****@*****.**', )) command = send_followups.Command() command.stdout = Mock() with self.assertLogs( 'project.services.logging_service', logging.INFO) as logs: command.handle() self.assertEqual( len(slack.mock_calls), 1) self.assertEqual( len(self.notifications.email_followup.send.mock_calls), 4) self.assertEqual( len(self.notifications.slack_notification_sent.send.mock_calls), 4) assertInLogsCount(logs, {'event_name=app_followup_sent': 4})
def test_filters_out_subs_with_previous_followups(self): # given old submissions, some with old followups no_followup = mock.FormSubmissionFactory.create( date_received=get_old_date()) applicant = models.Applicant() applicant.save() sub_w_followup = mock.FormSubmissionFactory.create( date_received=get_old_date(), applicant=applicant) models.ApplicationEvent.log_followup_sent( applicant.id, contact_info=sub_w_followup.answers['email'], message_content="hey how are things going?") # if we grab subs that need followups results = FollowupsService.get_submissions_due_for_follow_ups() results_set = set(results) # we should only have ones that have not received followups self.assertIn(no_followup, results_set) self.assertNotIn(sub_w_followup, results_set)
def test_filters_out_new_submissions(self): # NOTE: this seems to raise a warning: # RuntimeWarning: DateTimeField FormSubmission.date_received # received a naive datetime (2016-11-07 00:00:00) while time zone # support is active. # but the datetime sent is definitely not naive, and the resulting # value of date_received is correct. # I don't know what causes the warning # given new submissions and older submissions old_sub = factories.FormSubmissionWithOrgsFactory.create( date_received=get_old_date(), organizations=[self.followup_org]) new_sub = factories.FormSubmissionWithOrgsFactory.create( date_received=get_newer_date(), organizations=[self.followup_org]) # we should only get submissions that are newer results = FollowupsService.get_submissions_due_for_follow_ups() results_set = set(results) self.assertIn(old_sub, results_set) self.assertNotIn(new_sub, results_set)
def test_can_start_at_particular_id_to_create_time_interval(self): # assume we have 4 old subs, 1 new sub old_subs = sorted([ mock.FormSubmissionFactory.create(date_received=get_old_date()) for i in range(4) ], key=lambda s: s.date_received) new_sub = mock.FormSubmissionFactory.create( date_received=get_newer_date()) # but we only want ones after the second oldest sub second_oldest_id = old_subs[1].id # and within the old subs, we still don't want ones that already # received followups applicant = models.Applicant() applicant.save() followed_up_sub = old_subs[2] followed_up_sub.applicant = applicant followed_up_sub.save() models.ApplicationEvent.log_followup_sent( applicant.id, contact_info=followed_up_sub.answers['email'], message_content="hey how are things going?") # when we get submissions due for follow ups, results = list( FollowupsService.get_submissions_due_for_follow_ups( after_id=second_oldest_id)) # we should only receive two: self.assertEqual(len(results), 2) # the second oldest self.assertIn(old_subs[1], results) # and not-as-old one that did not have a follow up self.assertIn(old_subs[3], results) # we should not receive # the oldest sub self.assertNotIn(old_subs[0], results) # the one with the follow up self.assertNotIn(followed_up_sub, results) # or the new sub self.assertNotIn(new_sub, results)
def test_can_start_at_particular_id_to_create_time_interval(self): # assume we have 4 old subs, 1 new sub old_subs = sorted([ factories.FormSubmissionWithOrgsFactory.create( date_received=get_old_date(), organizations=[self.followup_org]) for i in range(4) ], key=lambda s: s.date_received) new_sub = factories.FormSubmissionWithOrgsFactory.create( date_received=get_newer_date(), organizations=[self.followup_org]) # but we only want ones after the second oldest sub second_oldest_id = old_subs[1].id # and within the old subs, we still don't want ones that already # received followups applicant = factories.ApplicantFactory() followed_up_sub = old_subs[2] followed_up_sub.applicant = applicant followed_up_sub.has_been_sent_followup = True followed_up_sub.save() # when we get submissions due for follow ups, results = list( FollowupsService.get_submissions_due_for_follow_ups( after_id=second_oldest_id)) # we should only receive two: self.assertEqual(len(results), 2) # the second oldest self.assertIn(old_subs[1], results) # and not-as-old one that did not have a follow up self.assertIn(old_subs[3], results) # we should not receive # the oldest sub self.assertNotIn(old_subs[0], results) # the one with the follow up self.assertNotIn(followed_up_sub, results) # or the new sub self.assertNotIn(new_sub, results)
def test_includes_subs_w_one_followup_org(self): sub = factories.FormSubmissionWithOrgsFactory.create( organizations=[self.followup_org, self.non_followup_org], date_received=get_old_date()) results = list(FollowupsService.get_submissions_due_for_follow_ups()) self.assertIn(sub, results)