def test_partner_export(self): url = reverse('statistics.dailycountexport_create') tz = pytz.timezone("Africa/Kampala") d1 = date(2016, 1, 1) d2 = date(2016, 1, 15) # Jan 1st with patch.object(timezone, 'now', return_value=self.anytime_on_day(d1, tz)): [msg] = self.new_messages(d1, 1) Case.get_or_open(self.unicef, self.user1, msg, 'summary', self.moh) # Jan 15th with patch.object(timezone, 'now', return_value=self.anytime_on_day(d2, tz)): [msg] = self.new_messages(d2, 1) Case.get_or_open(self.unicef, self.user1, msg, 'summary', self.moh) self.new_outgoing(self.user1, d1, 1) # Jan 1st self.new_outgoing(self.user3, d2, 1) # Jan 15th self.login(self.admin) response = self.url_post_json('unicef', url, { 'type': 'P', 'after': "2016-01-01", 'before': "2016-01-31" }) self.assertEqual(response.status_code, 200) export = DailyCountExport.objects.get() workbook = self.openWorkbook(export.filename) (replies_sheet, ave_reply_sheet, ave_closed_sheet, cases_opened_sheet, cases_closed_sheet) = workbook.sheets() self.assertEqual(replies_sheet.nrows, 32) self.assertExcelRow(replies_sheet, 0, ["Date", "MOH", "WHO"]) self.assertExcelRow(replies_sheet, 1, [d1, 1, 0], tz=tz) self.assertExcelRow(replies_sheet, 15, [d2, 0, 1], tz=tz) self.assertExcelRow(ave_reply_sheet, 0, ["Date", "MOH", "WHO"]) self.assertExcelRow(ave_reply_sheet, 1, [d1, 0, 0], tz=tz) self.assertExcelRow(ave_reply_sheet, 15, [d2, 0, 0], tz=tz) self.assertExcelRow(ave_closed_sheet, 0, ["Date", "MOH", "WHO"]) self.assertExcelRow(ave_closed_sheet, 1, [d1, 0, 0], tz=tz) self.assertExcelRow(ave_closed_sheet, 15, [d2, 0, 0], tz=tz) self.assertExcelRow(cases_opened_sheet, 0, ["Date", "MOH", "WHO"]) self.assertExcelRow(cases_opened_sheet, 1, [d1, 1, 0], tz=tz) self.assertExcelRow(cases_opened_sheet, 15, [d2, 1, 0], tz=tz) self.assertExcelRow(cases_closed_sheet, 0, ["Date", "MOH", "WHO"]) self.assertExcelRow(cases_closed_sheet, 1, [d1, 0, 0], tz=tz) self.assertExcelRow(cases_closed_sheet, 15, [d2, 0, 0], tz=tz)
def test_case_counts_opened(self): d1 = self.anytime_on_day(date(2015, 1, 1), pytz.timezone("Africa/Kampala")) msg2 = self.create_message(self.unicef, 234, self.ann, "Hello again", [self.aids], created_on=d1) with patch.object(timezone, 'now', return_value=d1): case = Case.get_or_open(self.unicef, self.user1, msg2, "Summary", self.moh)
def test_first_reply_counts(self): msg1 = self.create_message(self.unicef, 123, self.ann, "Hello 1", [self.aids]) msg2 = self.create_message(self.unicef, 234, self.ned, "Hello 2", [self.aids, self.pregnancy]) msg3 = self.create_message(self.unicef, 345, self.ann, "Hello 3", [self.pregnancy]) msg4 = self.create_message(self.nyaruka, 456, self.ned, "Hello 4", [self.code]) msg5 = self.create_message(self.unicef, 789, self.ann, "Hello 5", [self.code]) case1 = self.create_case(self.unicef, self.ann, self.moh, msg1, [self.aids]) case2 = self.create_case(self.unicef, self.ned, self.moh, msg2, [self.aids, self.pregnancy]) case3 = self.create_case(self.unicef, self.ann, self.who, msg3, [self.pregnancy]) case4 = self.create_case(self.unicef, self.ned, self.who, msg4, [self.code]) # create a case by "WHO" user and assign it to "WHO" partner case5 = Case.get_or_open(self.unicef, self.user3, msg5, "Hello", self.who) # create a case by "MOH" user and assign it to "WHO" partner msg6 = self.create_message(self.unicef, 678, self.ann, "Hello 6", [self.pregnancy]) case6 = Case.get_or_open(self.unicef, self.user1, msg6, "Hello", self.who) self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case1) self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ned, case=case2) self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case3) self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ned, case=case4) self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case6) self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], "A").total(), 5) self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], "A").seconds(), 5) # First reply only counted for re-assignments, immediate replies NOT counted self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").total(), 0) self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").seconds(), 0) self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").average(), 0) # First replies should be counted for cases not created by self self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], "A").total(), 1) self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], "A").seconds(), 1) self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], "A").average(), 1) # check a reassigned case response case5.reassign(self.user3, self.moh) self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case5) self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], "A").total(), 6) self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], "A").seconds(), 6) self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").total(), 1) self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").seconds(), 1) self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").average(), 1) # check empty partner metrics self.assertEqual(DailySecondTotalCount.get_by_partner([self.klab], "A").average(), 0) self.assertEqual(DailySecondTotalCount.objects.count(), 8) squash_counts() self.assertEqual(DailySecondTotalCount.objects.count(), 3)
def test_case_counts_closed(self): d1 = self.anytime_on_day(date(2015, 1, 1), pytz.timezone("Africa/Kampala")) msg2 = self.create_message(self.unicef, 234, self.ann, "Hello again", [self.aids], created_on=d1) with patch.object(timezone, "now", return_value=d1): case = Case.get_or_open(self.unicef, self.user1, msg2, "Summary", self.moh) case.close(self.user1, note="closing case") self.assertEqual( DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_OPENED).day_totals(), [(date(2015, 1, 1), 1)], ) self.assertEqual( DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_CLOSED).day_totals(), [(date(2015, 1, 1), 1)], ) self.assertEqual( DailyCount.get_by_org([self.unicef], DailyCount.TYPE_CASE_OPENED).day_totals(), [(date(2015, 1, 1), 1)] ) self.assertEqual( DailyCount.get_by_org([self.unicef], DailyCount.TYPE_CASE_CLOSED).day_totals(), [(date(2015, 1, 1), 1)] ) self.assertEqual( DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_OPENED).day_totals(), [(date(2015, 1, 1), 1)], ) self.assertEqual( DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_CLOSED).day_totals(), [(date(2015, 1, 1), 1)], ) case.reopen(self.user1, "Re-opened") case.close(self.user1, "Re-closed") self.assertEqual( DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_OPENED).day_totals(), [(date(2015, 1, 1), 1)], ) self.assertEqual( DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_CLOSED).day_totals(), [(date(2015, 1, 1), 1)], ) self.assertEqual( DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_OPENED).day_totals(), [(date(2015, 1, 1), 1)], ) self.assertEqual( DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_CLOSED).day_totals(), [(date(2015, 1, 1), 1)], )
def test_user_export(self): url = reverse("statistics.dailycountexport_create") tz = pytz.timezone("Africa/Kampala") d1 = date(2016, 1, 1) d2 = date(2016, 1, 15) # Jan 1st with patch.object(timezone, "now", return_value=self.anytime_on_day(d1, tz)): [msg] = self.new_messages(d1, 1) Case.get_or_open(self.unicef, self.user1, msg, "summary", self.moh) # Jan 15th with patch.object(timezone, "now", return_value=self.anytime_on_day(d2, tz)): [msg] = self.new_messages(d2, 1) Case.get_or_open(self.unicef, self.user1, msg, "summary", self.moh) self.new_outgoing(self.user1, d1, 1) # Jan 1st self.new_outgoing(self.user3, d2, 1) # Jan 15th self.login(self.admin) response = self.url_post_json("unicef", url, { "type": "U", "after": "2016-01-01", "before": "2016-01-31" }) self.assertEqual(response.status_code, 200) export = DailyCountExport.objects.get() workbook = self.openWorkbook(export.filename) (replies_sheet, cases_opened_sheet, cases_closed_sheet) = workbook.sheets() self.assertEqual(replies_sheet.nrows, 32) self.assertExcelRow(replies_sheet, 0, ["Date", "Carol", "Evan", "Kidus", "Rick"]) self.assertExcelRow(replies_sheet, 1, [d1, 0, 1, 0, 0], tz=tz) self.assertExcelRow(replies_sheet, 15, [d2, 1, 0, 0, 0], tz=tz) self.assertExcelRow(cases_opened_sheet, 0, ["Date", "Carol", "Evan", "Kidus", "Rick"]) self.assertExcelRow(cases_opened_sheet, 1, [d1, 0, 1, 0, 0], tz=tz) self.assertExcelRow(cases_opened_sheet, 15, [d2, 0, 1, 0, 0], tz=tz) self.assertExcelRow(cases_closed_sheet, 0, ["Date", "Carol", "Evan", "Kidus", "Rick"]) self.assertExcelRow(cases_closed_sheet, 1, [d1, 0, 0, 0, 0], tz=tz) self.assertExcelRow(cases_closed_sheet, 15, [d2, 0, 0, 0, 0], tz=tz)
def test_first_reply_counts(self): msg1 = self.create_message(self.unicef, 123, self.ann, "Hello 1", [self.aids]) msg2 = self.create_message(self.unicef, 234, self.ned, "Hello 2", [self.aids, self.pregnancy]) msg3 = self.create_message(self.unicef, 345, self.ann, "Hello 3", [self.pregnancy]) msg4 = self.create_message(self.nyaruka, 456, self.ned, "Hello 4", [self.code]) msg5 = self.create_message(self.unicef, 789, self.ann, "Hello 5", [self.code]) msg6 = self.create_message(self.unicef, 678, self.ann, "Hello 6", [self.pregnancy]) case1 = self.create_case(self.unicef, self.ann, self.moh, msg1, [self.aids]) case2 = self.create_case(self.unicef, self.ned, self.moh, msg2, [self.aids, self.pregnancy]) case3 = self.create_case(self.unicef, self.ann, self.who, msg3, [self.pregnancy]) case4 = self.create_case(self.unicef, self.ned, self.who, msg4, [self.code]) # create a case by "WHO" user and assign it to "WHO" partner case5 = Case.get_or_open(self.unicef, self.user3, msg5, 'Hello', self.who) # create a case by "MOH" user and assign it to "WHO" partner case6 = Case.get_or_open(self.unicef, self.user1, msg6, 'Hello', self.who) self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case1) self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ned, case=case2) self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case3) self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ned, case=case4) self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case6) self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], 'A').total(), 5) self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], 'A').seconds(), 5) # First reply only counted for re-assignments, immediate replies NOT counted self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').total(), 0) self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').seconds(), 0) self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').average(), 0) # First replies should be counted for cases not created by self self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], 'A').total(), 1) self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], 'A').seconds(), 1) self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], 'A').average(), 1) # check a reassigned case response case5.reassign(self.user3, self.moh) self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case5) self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], 'A').total(), 6) self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], 'A').seconds(), 6) self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').total(), 1) self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').seconds(), 1) self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').average(), 1) # check empty partner metrics self.assertEqual(DailySecondTotalCount.get_by_partner([self.klab], 'A').average(), 0) self.assertEqual(DailySecondTotalCount.objects.count(), 8) squash_counts() self.assertEqual(DailySecondTotalCount.objects.count(), 3)
def test_partner_export(self): url = reverse('statistics.dailycountexport_create') tz = pytz.timezone("Africa/Kampala") d1 = date(2016, 1, 1) d2 = date(2016, 1, 15) # Jan 1st with patch.object(timezone, 'now', return_value=self.anytime_on_day(d1, tz)): [msg] = self.new_messages(d1, 1) Case.get_or_open(self.unicef, self.user1, msg, 'summary', self.moh) # Jan 15th with patch.object(timezone, 'now', return_value=self.anytime_on_day(d2, tz)): [msg] = self.new_messages(d2, 1) Case.get_or_open(self.unicef, self.user1, msg, 'summary', self.moh) self.new_outgoing(self.user1, d1, 1) # Jan 1st self.new_outgoing(self.user3, d2, 1) # Jan 15th self.login(self.admin) response = self.url_post_json('unicef', url, {'type': 'P', 'after': "2016-01-01", 'before': "2016-01-31"}) self.assertEqual(response.status_code, 200) export = DailyCountExport.objects.get() workbook = self.openWorkbook(export.filename) (replies_sheet, ave_reply_sheet, ave_closed_sheet, cases_opened_sheet, cases_closed_sheet) = workbook.sheets() self.assertEqual(replies_sheet.nrows, 32) self.assertExcelRow(replies_sheet, 0, ["Date", "MOH", "WHO"]) self.assertExcelRow(replies_sheet, 1, [d1, 1, 0], tz=tz) self.assertExcelRow(replies_sheet, 15, [d2, 0, 1], tz=tz) self.assertExcelRow(ave_reply_sheet, 0, ["Date", "MOH", "WHO"]) self.assertExcelRow(ave_reply_sheet, 1, [d1, 0, 0], tz=tz) self.assertExcelRow(ave_reply_sheet, 15, [d2, 0, 0], tz=tz) self.assertExcelRow(ave_closed_sheet, 0, ["Date", "MOH", "WHO"]) self.assertExcelRow(ave_closed_sheet, 1, [d1, 0, 0], tz=tz) self.assertExcelRow(ave_closed_sheet, 15, [d2, 0, 0], tz=tz) self.assertExcelRow(cases_opened_sheet, 0, ["Date", "MOH", "WHO"]) self.assertExcelRow(cases_opened_sheet, 1, [d1, 1, 0], tz=tz) self.assertExcelRow(cases_opened_sheet, 15, [d2, 1, 0], tz=tz) self.assertExcelRow(cases_closed_sheet, 0, ["Date", "MOH", "WHO"]) self.assertExcelRow(cases_closed_sheet, 1, [d1, 0, 0], tz=tz) self.assertExcelRow(cases_closed_sheet, 15, [d2, 0, 0], tz=tz)
def setUp(self): super(SubscriptionPodTest, self).setUp() self.contact = self.create_contact(self.unicef, 'test_id', "Tester") self.msg1 = self.create_message( self.unicef, 123, self.contact, "Test message") self.case = Case.get_or_open( self.unicef, self.user1, self.msg1, "Summary", self.moh) self.base_url = 'http://example.com/' self.pod = SubscriptionPod( apps.get_app_config('family_connect_subscription_pod'), SubscriptionPodConfig({ 'index': 23, 'title': "My subscription Pod", 'url': "http://example.com/", 'token': "test_token", })) self.subscription_data = { "url": "http://example.com/api/v1/subscriptions/sub_id/", "id": "sub_id", "version": 1, "identity": "C-002", "messageset": 1, "next_sequence_number": 1, "lang": "eng", "active": True, "completed": False, "schedule": 1, "process_status": 0, "metadata": None, "created_at": "2016-07-22T15:53:42.282902Z", "updated_at": "2016-09-06T17:17:54.746390Z" }
def test_case_counts_opened(self): d1 = self.anytime_on_day(date(2015, 1, 1), pytz.timezone("Africa/Kampala")) msg2 = self.create_message( self.unicef, 234, self.ann, "Hello again", [self.aids], created_on=d1) with patch.object(timezone, 'now', return_value=d1): case = Case.get_or_open( self.unicef, self.user1, msg2, "Summary", self.moh) self.assertEqual( DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_OPENED).day_totals(), [(date(2015, 1, 1), 1)]) self.assertEqual( DailyCount.get_by_org([self.unicef], DailyCount.TYPE_CASE_OPENED).day_totals(), [(date(2015, 1, 1), 1)]) self.assertEqual( DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_CLOSED).day_totals(), []) self.assertEqual( DailyCount.get_by_org([self.unicef], DailyCount.TYPE_CASE_CLOSED).day_totals(), []) self.assertEqual( DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_OPENED).day_totals(), [(date(2015, 1, 1), 1)]) self.assertEqual( DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_CLOSED).day_totals(), [])
def setUp(self): super(RegistrationPodTest, self).setUp() self.contact = self.create_contact(self.unicef, 'test_id', "Mother") self.msg1 = self.create_message( self.unicef, 123, self.contact, "Hello") self.case = Case.get_or_open( self.unicef, self.user1, self.msg1, "Summary", self.moh) self.url = 'http://example.com/registration/?mother_id=' + \ self.contact.uuid self.pod = RegistrationPod( apps.get_app_config('family_connect_registration_pod'), RegistrationPodConfig({ 'index': 23, 'title': "My registration Pod", 'url': "http://example.com/registration/", 'token': "test_token", 'contact_id_fieldname': "mother_id", 'field_mapping': [ {"field": "mama_name", "field_name": "Mother Name"}, {"field": "mama_surname", "field_name": "Mother Surname"}, {"field": "last_period_date", "field_name": "Date of last period"}, {"field": "language", "field_name": "Language Preference"}, {"field": "mama_id_type", "field_name": "ID Type"}, {"field": "mama_id_no", "field_name": "ID Number"}, {"field": "msg_receiver", "field_name": "Message Receiver"}, {"field": "receiver_id", "field_name": "Receiver ID"}, {"field": "hoh_name", "field_name": "Head of Household Name"}, {"field": "hoh_surname", "field_name": "Head of Household Surname"}, {"field": "hoh_id", "field_name": "Head of Household ID"}, {"field": "operator_id", "field_name": "Operator ID"}, {"field": "msg_type", "field_name": "Receives Messages As"}, ] }))
def test_cases_charts(self): opened_url = reverse("statistics.cases_opened_chart") closed_url = reverse("statistics.cases_closed_chart") self.assertLoginRedirect(self.url_get("unicef", opened_url), opened_url) self.assertLoginRedirect(self.url_get("unicef", closed_url), closed_url) tz = pytz.timezone("Africa/Kampala") d1 = date(2020, 1, 1) d2 = date(2020, 3, 15) # Jan 1st with patch.object(timezone, "now", return_value=self.anytime_on_day(d1, tz)): [msg] = self.new_messages(d1, 1) c = Case.get_or_open(self.unicef, self.user1, msg, "summary", self.moh) c.close(self.user1) # Mar 15th with patch.object(timezone, "now", return_value=self.anytime_on_day(d2, tz)): [msg] = self.new_messages(d2, 1) Case.get_or_open(self.unicef, self.user1, msg, "summary", self.moh) self.login(self.user3) # simulate making requests in April with patch.object(timezone, "now", return_value=datetime(2016, 4, 20, 9, 0, tzinfo=pytz.UTC)): response = self.url_get("unicef", opened_url) self.assertEqual( response.json, { "categories": [ "May", "June", "July", "August", "September", "October", "November", "December", "January", "February", "March", "April", ], "series": [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0], }, ) response = self.url_get("unicef", closed_url) self.assertEqual( response.json, { "categories": [ "May", "June", "July", "August", "September", "October", "November", "December", "January", "February", "March", "April", ], "series": [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], }, )