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")
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")
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")
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")
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")
def test_from_run(self): # a complete run run = Run.create( id=1234, flow='F-001', # flow UUID for poll #1 contact='C-001', completed=True, values=[ RunValueSet.create(category="1 - 50", node='RS-001', text="6", value="6.00000000", label="Number of sheep", time=datetime.datetime( 2014, 1, 2, 3, 4, 5, 6, pytz.UTC)), RunValueSet.create( category="1 - 25", node='RS-002', text="4", value="4.00000000", label="Number of goats", time=datetime.datetime(2015, 1, 2, 3, 4, 5, 6, pytz.UTC), ), ], steps=[], # not used created_on=datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC), ) response1 = Response.from_run(self.unicef, run) self.assertEqual(response1.contact, self.contact1) self.assertEqual(response1.created_on, datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response1.updated_on, datetime.datetime(2015, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response1.status, Response.STATUS_COMPLETE) self.assertEqual(len(response1.answers.all()), 2) answers = list(response1.answers.order_by('question_id')) self.assertEqual(answers[0].question, self.poll1_question1) self.assertEqual(answers[0].value, "6.00000000") self.assertEqual(answers[0].category, "1 - 50") self.assertEqual(answers[0].submitted_on, datetime.datetime(2014, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(answers[1].question, self.poll1_question2) self.assertEqual(answers[1].value, "4.00000000") self.assertEqual(answers[1].category, "1 - 25") self.assertEqual(answers[1].submitted_on, datetime.datetime(2015, 1, 2, 3, 4, 5, 6, pytz.UTC)) # an partially complete run run = Run.create( id=2345, flow='F-001', # flow UUID for poll #1 contact='C-002', completed=False, values=[ RunValueSet.create( category="1 - 50", node='RS-001', text="6", value="6.00000000", label="Number of sheep", time=datetime.datetime(2014, 1, 2, 3, 4, 5, 6, pytz.UTC), ), ], steps=[], # not used created_on=datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC), ) response2 = Response.from_run(self.unicef, run) self.assertEqual(response2.contact, self.contact2) self.assertEqual(response2.created_on, datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response2.updated_on, datetime.datetime(2014, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response2.status, Response.STATUS_PARTIAL) self.assertEqual(len(response2.answers.all()), 1) # now completed run = Run.create( id=2345, flow='F-001', # flow UUID for poll #1 contact='C-002', completed=True, values=[ RunValueSet.create( category="1 - 50", node='RS-001', text="6", value="6.00000000", label="Number of sheep", time=datetime.datetime(2014, 1, 2, 3, 4, 5, 6, pytz.UTC), ), RunValueSet.create( category="1 - 25", node='RS-002', text="4", value="4.00000000", label="Number of goats", time=datetime.datetime(2015, 1, 2, 3, 4, 5, 6, pytz.UTC), ), ], steps=[], # not used created_on=datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC), ) response3 = Response.from_run(self.unicef, run) self.assertEqual(response3.contact, self.contact2) self.assertEqual(response3.created_on, datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response3.updated_on, datetime.datetime(2015, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response3.status, Response.STATUS_COMPLETE) self.assertEqual(len(response3.answers.all()), 2) # an empty run run = Run.create( id=3456, flow='F-001', # flow UUID for poll #1 contact='C-003', completed=False, values=[], steps=[], # not used created_on=datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC), ) response4 = Response.from_run(self.unicef, run) self.assertEqual(response4.contact, self.contact3) self.assertEqual(response4.created_on, datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response4.updated_on, datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response4.status, Response.STATUS_EMPTY) self.assertEqual(len(response4.answers.all()), 0) # new run for same contact should de-activate old response run = Run.create( id=4567, flow='F-001', # flow UUID for poll #1 contact='C-003', completed=False, values=[], steps=[], # not used created_on=datetime.datetime(2013, 1, 2, 3, 0, 0, 0, pytz.UTC), ) response5 = Response.from_run(self.unicef, run) self.assertFalse(Response.objects.get(pk=response4.pk).is_active) self.assertEqual(response5.contact, self.contact3) # same run if we call again self.assertEqual(Response.from_run(self.unicef, run), response5)
def test_from_run(self): # a complete run run = Run.create( id=1234, flow='F-001', # flow UUID for poll #1 contact='C-001', completed=True, values=[ RunValueSet.create( category="1 - 50", node='RS-001', text="6", value="6.00000000", label="Number of sheep", time=datetime.datetime(2014, 1, 2, 3, 4, 5, 6, pytz.UTC) ), RunValueSet.create( category="1 - 25", node='RS-002', text="4", value="4.00000000", label="Number of goats", time=datetime.datetime(2015, 1, 2, 3, 4, 5, 6, pytz.UTC), ), ], steps=[], # not used created_on=datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC), ) response1 = Response.from_run(self.unicef, run) self.assertEqual(response1.contact, self.contact1) self.assertEqual( response1.created_on, datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual( response1.updated_on, datetime.datetime(2015, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response1.status, Response.STATUS_COMPLETE) self.assertEqual(len(response1.answers.all()), 2) answers = list(response1.answers.order_by('question_id')) self.assertEqual(answers[0].question, self.poll1_question1) self.assertEqual(answers[0].value, "6.00000000") self.assertEqual(answers[0].category, "1 - 50") self.assertEqual( answers[0].submitted_on, datetime.datetime(2014, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(answers[1].question, self.poll1_question2) self.assertEqual(answers[1].value, "4.00000000") self.assertEqual(answers[1].category, "1 - 25") self.assertEqual( answers[1].submitted_on, datetime.datetime(2015, 1, 2, 3, 4, 5, 6, pytz.UTC)) # an partially complete run run = Run.create( id=2345, flow='F-001', # flow UUID for poll #1 contact='C-002', completed=False, values=[ RunValueSet.create( category="1 - 50", node='RS-001', text="6", value="6.00000000", label="Number of sheep", time=datetime.datetime(2014, 1, 2, 3, 4, 5, 6, pytz.UTC), ), ], steps=[], # not used created_on=datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC), ) response2 = Response.from_run(self.unicef, run) self.assertEqual(response2.contact, self.contact2) self.assertEqual( response2.created_on, datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual( response2.updated_on, datetime.datetime(2014, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response2.status, Response.STATUS_PARTIAL) self.assertEqual(len(response2.answers.all()), 1) # now completed run = Run.create( id=2345, flow='F-001', # flow UUID for poll #1 contact='C-002', completed=True, values=[ RunValueSet.create( category="1 - 50", node='RS-001', text="6", value="6.00000000", label="Number of sheep", time=datetime.datetime(2014, 1, 2, 3, 4, 5, 6, pytz.UTC), ), RunValueSet.create( category="1 - 25", node='RS-002', text="4", value="4.00000000", label="Number of goats", time=datetime.datetime(2015, 1, 2, 3, 4, 5, 6, pytz.UTC), ), ], steps=[], # not used created_on=datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC), ) response3 = Response.from_run(self.unicef, run) self.assertEqual(response3.contact, self.contact2) self.assertEqual( response3.created_on, datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual( response3.updated_on, datetime.datetime(2015, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response3.status, Response.STATUS_COMPLETE) self.assertEqual(len(response3.answers.all()), 2) # an empty run run = Run.create( id=3456, flow='F-001', # flow UUID for poll #1 contact='C-003', completed=False, values=[], steps=[], # not used created_on=datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC), ) response4 = Response.from_run(self.unicef, run) self.assertEqual(response4.contact, self.contact3) self.assertEqual( response4.created_on, datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual( response4.updated_on, datetime.datetime(2013, 1, 2, 3, 4, 5, 6, pytz.UTC)) self.assertEqual(response4.status, Response.STATUS_EMPTY) self.assertEqual(len(response4.answers.all()), 0) # new run for same contact should de-activate old response run = Run.create( id=4567, flow='F-001', # flow UUID for poll #1 contact='C-003', completed=False, values=[], steps=[], # not used created_on=datetime.datetime(2013, 1, 2, 3, 0, 0, 0, pytz.UTC), ) response5 = Response.from_run(self.unicef, run) self.assertFalse(Response.objects.get(pk=response4.pk).is_active) self.assertEqual(response5.contact, self.contact3) # same run if we call again self.assertEqual(Response.from_run(self.unicef, run), response5)
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") # category counts for question #1 self.assertEqual( pollrun.get_answer_category_counts(self.poll1_question1), [("1 - 5", 2), ("6 - 10", 1)]) self.assertEqual( pollrun.get_answer_category_counts( self.poll1_question1, [self.region1]), [("1 - 5", 2)]) self.assertEqual( pollrun.get_answer_category_counts( self.poll1_question1, [self.region2]), [("6 - 10", 1)]) self.assertEqual( pollrun.get_answer_category_counts( self.poll1_question1, [self.region3]), []) # and from cache... (lists rather than tuples due to JSON serialization) with self.assertNumQueries(0): self.assertEqual( pollrun.get_answer_category_counts( self.poll1_question1), [["1 - 5", 2], ["6 - 10", 1]]) self.assertEqual( pollrun.get_answer_category_counts( self.poll1_question1, [self.region1]), [["1 - 5", 2]]) self.assertEqual( pollrun.get_answer_category_counts( self.poll1_question1, [self.region2]), [["6 - 10", 1]]) self.assertEqual( pollrun.get_answer_category_counts( self.poll1_question1, [self.region3]), []) # auto-range category counts for question #1 self.assertEqual( pollrun.get_answer_auto_range_counts(self.poll1_question1), [('2 - 3', 1), ('4 - 5', 1), ('6 - 7', 0), ('8 - 9', 1), ('10 - 11', 0)]) self.assertEqual( pollrun.get_answer_auto_range_counts( self.poll1_question1, [self.region1]), [('3', 1), ('4', 1), ('5', 0), ('6', 0), ('7', 0)]) self.assertEqual( pollrun.get_answer_auto_range_counts( self.poll1_question1, [self.region2]), [('8', 1), ('9', 0), ('10', 0), ('11', 0), ('12', 0)]) self.assertEqual( pollrun.get_answer_auto_range_counts( self.poll1_question1, [self.region3]), []) # numeric averages for question #1 self.assertEqual( pollrun.get_answer_numeric_average(self.poll1_question1), 5.0) self.assertEqual( pollrun.get_answer_numeric_average( self.poll1_question1, [self.region1]), 3.5) self.assertEqual( pollrun.get_answer_numeric_average( self.poll1_question1, [self.region2]), 8.0) self.assertEqual( pollrun.get_answer_numeric_average( self.poll1_question1, [self.region3]), 0.0) # word counts for question #2 self.assertEqual( pollrun.get_answer_word_counts(self.poll1_question2), [("rainy", 3), ("sunny", 2), ('مطر', 1)]) self.assertEqual( pollrun.get_answer_word_counts( self.poll1_question2, [self.region1]), [("rainy", 3)]) self.assertEqual( pollrun.get_answer_word_counts( self.poll1_question2, [self.region2]), [("sunny", 2)]) self.assertEqual( pollrun.get_answer_word_counts( self.poll1_question2, [self.region3]), [('مطر', 1)])