Esempio n. 1
0
    def test_list(self):
        pollrun1 = factories.UniversalPollRun(
            poll=self.poll1, conducted_on=datetime.datetime(2014, 12, 1, tzinfo=pytz.UTC))
        Response.create_empty(
            self.unicef, pollrun1,
            Run.create(id=123, contact='C-001', created_on=timezone.now()))

        self.login(self.admin)
        response = self.url_get('unicef', reverse('contacts.contact_list'))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context['object_list']), 5)
        # no poll pollruns shown in "All Regions" view
        self.assertNotContains(response, "Farm Poll")

        url = '{}?search=an'.format(reverse('contacts.contact_list'))
        response = self.url_get('unicef', url)
        self.assertEqual(len(response.context['object_list']), 2)
        self.assertContains(response, "Ann")
        self.assertContains(response, "Dan")

        self.login(self.user1)

        response = self.url_get('unicef', reverse('contacts.contact_list'))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context['object_list']), 3)
        self.assertContains(response, "Farm Poll")
Esempio n. 2
0
    def test_create(self):
        pollrun = factories.UniversalPollRun(poll=self.poll1,
                                             conducted_on=timezone.now())
        response = Response.create_empty(
            self.unicef, pollrun,
            Run.create(id=123, contact='C-001', created_on=timezone.now()))

        answer1 = factories.Answer(response=response,
                                   question=self.poll1_question1,
                                   value="4.00000",
                                   category="1 - 5")
        self.assertEqual(answer1.response, response)
        self.assertEqual(answer1.question, self.poll1_question1)
        self.assertEqual(answer1.category, "1 - 5")
        self.assertEqual(answer1.value, "4.00000")

        answer2 = factories.Answer(response=response,
                                   question=self.poll1_question1,
                                   value="rain",
                                   category=dict(base="Rain", rwa="Imvura"))
        self.assertEqual(answer2.category, "Rain")

        answer3 = factories.Answer(response=response,
                                   question=self.poll1_question1,
                                   value="rain",
                                   category=dict(eng="Yes"))
        self.assertEqual(answer3.category, "Yes")
Esempio n. 3
0
    def test_list(self):
        url = reverse('msgs.message_list')

        # create a non-regional pollrun
        pollrun1 = factories.UniversalPollRun(poll=self.poll1,
                                              conducted_on=timezone.now())

        # send 1 message to all regions and 2 more to specific regions
        msg1 = Message.create(self.unicef, self.admin, "Test to all", pollrun1,
                              COHORT_ALL, None)
        msg2 = Message.create(self.unicef, self.admin, "Test to region #1",
                              pollrun1, COHORT_ALL, self.region1)
        msg3 = Message.create(self.unicef, self.admin, "Test to region #2",
                              pollrun1, COHORT_ALL, self.region2)

        self.login(self.admin)

        response = self.url_get('unicef', url)
        self.assertEqual(list(response.context['object_list']),
                         [msg3, msg2, msg1])

        self.switch_region(self.region1)

        # should still include message sent to all regions
        response = self.url_get('unicef', url)
        self.assertEqual(list(response.context['object_list']), [msg2, msg1])
Esempio n. 4
0
    def test_create(self):
        self.mock_temba_client.create_broadcast.return_value = Broadcast.create()
        now = timezone.now()

        # create non-regional pollrun with 3 responses (1 complete, 1 partial, 1 empty)
        pollrun1 = factories.UniversalPollRun(
            poll=self.poll1, conducted_on=timezone.now())

        factories.Response(
            pollrun=pollrun1, contact=self.contact1,
            created_on=now, updated_on=now, status=Response.STATUS_COMPLETE)
        factories.Response(
            pollrun=pollrun1, contact=self.contact2,
            created_on=now, updated_on=now, status=Response.STATUS_PARTIAL)
        factories.Response(
            pollrun=pollrun1, contact=self.contact4,
            created_on=now, updated_on=now, status=Response.STATUS_EMPTY)

        msg1 = Message.create(
            self.unicef, self.admin, "Test #1", pollrun1, COHORT_ALL, None)
        self.assertEqual(msg1.org, self.unicef)
        self.assertEqual(msg1.sent_by, self.admin)
        self.assertIsNotNone(msg1.sent_on)
        self.assertEqual(msg1.text, "Test #1")
        self.assertEqual(msg1.pollrun, pollrun1)
        self.assertEqual(msg1.cohort, COHORT_ALL)
        self.assertEqual(msg1.region, None)
        self.assertEqual(list(msg1.recipients.order_by('pk')),
                         [self.contact1, self.contact2, self.contact4])
        self.assertEqual(str(msg1), "Test #1")

        self.assertEqual(msg1.as_json(), dict(id=msg1.pk, recipients=3))

        msg2 = Message.create(
            self.unicef, self.admin, "Test #1", pollrun1, COHORT_RESPONDENTS,
            None)
        self.assertEqual(msg2.cohort, COHORT_RESPONDENTS)
        self.assertEqual(msg2.region, None)
        self.assertEqual(list(msg2.recipients.order_by('pk')), [self.contact1])

        msg3 = Message.create(
            self.unicef, self.admin, "Test #1", pollrun1,
            COHORT_NONRESPONDENTS, None)
        self.assertEqual(msg3.cohort, COHORT_NONRESPONDENTS)
        self.assertEqual(msg3.region, None)
        self.assertEqual(list(msg3.recipients.order_by('pk')),
                         [self.contact2, self.contact4])

        msg4 = Message.create(
            self.unicef, self.admin, "Test #1", pollrun1, COHORT_ALL,
            self.region1)
        self.assertEqual(msg4.cohort, COHORT_ALL)
        self.assertEqual(msg4.region, self.region1)
        self.assertEqual(list(msg4.recipients.order_by('pk')),
                         [self.contact1, self.contact2])
Esempio n. 5
0
    def test_answer_aggregation(self):
        self.contact5.language = 'ara'
        self.contact5.save()

        pollrun = factories.UniversalPollRun(poll=self.poll1,
                                             conducted_on=timezone.now())

        response1 = Response.create_empty(
            self.unicef, pollrun,
            Run.create(id=123, contact='C-001', created_on=timezone.now()))
        factories.Answer(response=response1,
                         question=self.poll1_question1,
                         value="4.00000",
                         category="1 - 5")
        factories.Answer(response=response1,
                         question=self.poll1_question2,
                         value="It's very rainy",
                         category="All Responses")

        response2 = Response.create_empty(
            self.unicef, pollrun,
            Run.create(id=234, contact='C-002', created_on=timezone.now()))
        factories.Answer(response=response2,
                         question=self.poll1_question1,
                         value="3.00000",
                         category="1 - 5")
        factories.Answer(response=response2,
                         question=self.poll1_question2,
                         value="rainy and rainy",
                         category="All Responses")

        response3 = Response.create_empty(
            self.unicef, pollrun,
            Run.create(id=345, contact='C-004', created_on=timezone.now()))
        factories.Answer(response=response3,
                         question=self.poll1_question1,
                         value="8.00000",
                         category="6 - 10")
        factories.Answer(response=response3,
                         question=self.poll1_question2,
                         value="Sunny sunny",
                         category="All Responses")

        response4 = Response.create_empty(
            self.unicef, pollrun,
            Run.create(id=456, contact='C-005', created_on=timezone.now()))
        factories.Answer(response=response4,
                         question=self.poll1_question2,
                         value="مطر",
                         category="All Responses")
Esempio n. 6
0
    def test_is_last_for_region(self):
        pollrun1 = factories.RegionalPollRun(poll=self.poll1,
                                             region=self.region1,
                                             conducted_on=timezone.now())
        pollrun2 = factories.UniversalPollRun(poll=self.poll1,
                                              conducted_on=timezone.now())
        pollrun3 = factories.RegionalPollRun(poll=self.poll1,
                                             region=self.region2,
                                             conducted_on=timezone.now())

        # pollrun #2 covers region #1 and is newer
        self.assertFalse(pollrun1.is_last_for_region(self.region1))
        # pollrun #1 didn't cover region #2
        self.assertFalse(pollrun1.is_last_for_region(self.region2))
        self.assertTrue(pollrun2.is_last_for_region(self.region1))
        # pollrun #3 covers region #2 and is newer
        self.assertFalse(pollrun2.is_last_for_region(self.region2))
        self.assertTrue(pollrun3.is_last_for_region(self.region2))
Esempio n. 7
0
    def test_completion(self):
        date1 = datetime.datetime(2014, 1, 1, 7, tzinfo=pytz.UTC)

        # pollrun with no responses (complete or incomplete) has null completion
        pollrun = factories.UniversalPollRun(poll=self.poll1,
                                             conducted_on=date1)

        # add a incomplete response from contact in region #1
        response1 = factories.Response(pollrun=pollrun,
                                       contact=self.contact1,
                                       created_on=date1,
                                       updated_on=date1,
                                       status=Response.STATUS_EMPTY)

        self.assertEqual(list(pollrun.get_responses()), [response1])
        self.assertDictEqual(
            pollrun.get_response_counts(), {
                Response.STATUS_EMPTY: 1,
                Response.STATUS_PARTIAL: 0,
                Response.STATUS_COMPLETE: 0,
            })

        self.assertEqual(list(pollrun.get_responses(self.region1)),
                         [response1])
        self.assertDictEqual(
            pollrun.get_response_counts(self.region1), {
                Response.STATUS_EMPTY: 1,
                Response.STATUS_PARTIAL: 0,
                Response.STATUS_COMPLETE: 0,
            })

        self.assertEqual(list(pollrun.get_responses(self.region2)), [])
        self.assertDictEqual(
            pollrun.get_response_counts(self.region2), {
                Response.STATUS_EMPTY: 0,
                Response.STATUS_PARTIAL: 0,
                Response.STATUS_COMPLETE: 0,
            })

        # add a complete response from another contact in region #1
        response2 = factories.Response(pollrun=pollrun,
                                       contact=self.contact2,
                                       created_on=date1,
                                       updated_on=date1,
                                       status=Response.STATUS_COMPLETE)

        self.assertEqual(list(pollrun.get_responses().order_by('pk')),
                         [response1, response2])
        self.assertDictEqual(
            pollrun.get_response_counts(), {
                Response.STATUS_EMPTY: 1,
                Response.STATUS_PARTIAL: 0,
                Response.STATUS_COMPLETE: 1,
            })

        self.assertEqual(
            list(pollrun.get_responses(self.region1).order_by('pk')),
            [response1, response2])
        self.assertDictEqual(
            pollrun.get_response_counts(self.region1), {
                Response.STATUS_EMPTY: 1,
                Response.STATUS_PARTIAL: 0,
                Response.STATUS_COMPLETE: 1,
            })

        self.assertEqual(list(pollrun.get_responses(self.region2)), [])
        self.assertDictEqual(
            pollrun.get_response_counts(self.region2), {
                Response.STATUS_EMPTY: 0,
                Response.STATUS_PARTIAL: 0,
                Response.STATUS_COMPLETE: 0,
            })

        # add a complete response from contact in different region
        response3 = factories.Response(pollrun=pollrun,
                                       contact=self.contact4,
                                       created_on=date1,
                                       updated_on=date1,
                                       status=Response.STATUS_COMPLETE)

        self.assertEqual(list(pollrun.get_responses().order_by('pk')),
                         [response1, response2, response3])
        self.assertDictEqual(
            pollrun.get_response_counts(), {
                Response.STATUS_EMPTY: 1,
                Response.STATUS_PARTIAL: 0,
                Response.STATUS_COMPLETE: 2,
            })

        self.assertEqual(
            list(pollrun.get_responses(self.region1).order_by('pk')),
            [response1, response2])
        self.assertDictEqual(
            pollrun.get_response_counts(self.region1), {
                Response.STATUS_EMPTY: 1,
                Response.STATUS_PARTIAL: 0,
                Response.STATUS_COMPLETE: 1,
            })

        self.assertEqual(list(pollrun.get_responses(self.region2)),
                         [response3])
        self.assertDictEqual(
            pollrun.get_response_counts(self.region2), {
                Response.STATUS_EMPTY: 0,
                Response.STATUS_PARTIAL: 0,
                Response.STATUS_COMPLETE: 1,
            })
Esempio n. 8
0
    def setUp(self):
        super(TestChartBaseline, self).setUp()

        self.org = factories.Org()

        self.contact1 = factories.Contact(org=self.org, name="Apple")
        self.contact2 = factories.Contact(org=self.org, name="Blueberry")
        self.contact3 = factories.Contact(org=self.org, name="Cherry")

        self.start_date = datetime.datetime(2015, 1, 1, 8, tzinfo=pytz.utc)

        self.baseline = factories.Question(poll__org=self.org)
        self.baseline_pollrun1 = factories.UniversalPollRun(
            poll=self.baseline.poll, conducted_on=self.start_date)
        self.baseline_pollrun2 = factories.UniversalPollRun(
            poll=self.baseline.poll,
            conducted_on=self.start_date + relativedelta(days=1))

        self.follow_up = factories.Question(poll__org=self.org)
        self.follow_up_pollrun1 = factories.UniversalPollRun(
            poll=self.follow_up.poll,
            conducted_on=self.start_date + relativedelta(days=1))
        self.follow_up_pollrun2 = factories.UniversalPollRun(
            poll=self.follow_up.poll,
            conducted_on=self.start_date + relativedelta(days=2))
        self.follow_up_pollrun3 = factories.UniversalPollRun(
            poll=self.follow_up.poll,
            conducted_on=self.start_date + relativedelta(days=3))

        self.baseline_term = BaselineTerm.objects.create(
            org=self.org,
            name="Test Baseline Term",
            start_date=self.start_date,
            end_date=self.start_date + relativedelta(days=3),
            baseline_poll=self.baseline.poll,
            baseline_question=self.baseline,
            follow_up_poll=self.follow_up.poll,
            follow_up_question=self.follow_up,
            y_axis_title="# cats")

        # Create an answer for each contact.
        contacts = [self.contact1, self.contact2, self.contact3]
        for i, contact in enumerate(contacts, 1):
            for j, pollrun in enumerate(self.baseline.poll.pollruns.all(), 1):
                factories.Answer(response__contact=contact,
                                 response__pollrun=pollrun,
                                 question=self.baseline,
                                 value=10 * i * j,
                                 submitted_on=self.start_date +
                                 relativedelta(days=j - 1))

            for j, pollrun in enumerate(self.follow_up.poll.pollruns.all(), 1):
                factories.Answer(response__contact=contact,
                                 response__pollrun=pollrun,
                                 question=self.follow_up,
                                 value=7 * i * j,
                                 submitted_on=self.start_date +
                                 relativedelta(days=j))

        # Empty filter form for testing.
        self.filter_form = BaselineTermFilterForm(
            org=self.org, baseline_term=self.baseline_term, data_regions=None)
Esempio n. 9
0
    def setUp(self):
        super(PollChartTest, self).setUp()

        self.org = factories.Org()

        self.poll = factories.Poll(org=self.org)

        self.region1 = factories.Region(org=self.org, name="Beta")
        self.region2 = factories.Region(org=self.org, name="Acme")

        self.question1 = factories.Question(
            poll=self.poll, question_type=models.Question.TYPE_MULTIPLE_CHOICE)
        self.question2 = factories.Question(
            poll=self.poll, question_type=models.Question.TYPE_OPEN)
        self.question3 = factories.Question(
            poll=self.poll, question_type=models.Question.TYPE_NUMERIC)

        self.pollrun = factories.UniversalPollRun(poll=self.poll)

        self.contact1 = factories.Contact(org=self.org, region=self.region1)
        self.response1 = factories.Response(
            contact=self.contact1, pollrun=self.pollrun,
            status=models.Response.STATUS_COMPLETE)
        factories.Answer(
            response=self.response1, question=self.question1,
            value="4.00000", category="1 - 5")
        factories.Answer(
            response=self.response1, question=self.question2,
            value="It's very rainy", category="All Responses")
        factories.Answer(
            response=self.response1, question=self.question3,
            value="4.00000", category="1 - 5")

        self.contact2 = factories.Contact(org=self.org, region=self.region1)
        self.response2 = factories.Response(
            contact=self.contact2, pollrun=self.pollrun,
            status=models.Response.STATUS_COMPLETE)
        factories.Answer(
            response=self.response2, question=self.question1,
            value="3.00000", category="1 - 5")
        factories.Answer(
            response=self.response2, question=self.question2,
            value="rainy and rainy", category="All Responses")
        factories.Answer(
            response=self.response2, question=self.question3,
            value="3.00000", category="1 - 5")

        self.contact3 = factories.Contact(org=self.org, region=self.region2)
        self.response3 = factories.Response(
            contact=self.contact3, pollrun=self.pollrun,
            status=models.Response.STATUS_COMPLETE)
        factories.Answer(
            response=self.response3, question=self.question1,
            value="8.00000", category="6 - 10")
        factories.Answer(
            response=self.response3, question=self.question2,
            value="Sunny sunny", category="All Responses")
        factories.Answer(
            response=self.response3, question=self.question3,
            value="8.00000", category="6 - 10")

        self.pollruns = models.PollRun.objects.filter(pk=self.pollrun.pk)
        self.responses = models.Response.objects.filter(pollrun=self.pollrun)