def test_should_filter_poll_responses_by_poll_id(self): poll_response = self.poll_response.copy() poll_response['text'] = 'some text that does not have the keyword' PollResponse(**poll_response).save() response = self.client.get(self.API_ENDPOINT, { "poll": self.poll.id, "format": "json" }) self.assertEqual(200, response.status_code) self.assertEqual(0, len(response.data)) other_poll_response_attr = self.poll_response.copy() other_poll_response_attr['poll'] = self.poll PollResponse(**other_poll_response_attr).save() response = self.client.get(self.API_ENDPOINT, { "poll": self.poll.id, "format": "json" }) self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.data)) self.assertDictContainsSubset(self.expected_poll_response, response.data[0]) self.assertEqual('NECOC Volunteer', response.data[0]['source']) self.assertEqual('Kampala >> Bukoto', response.data[0]['location']) self.assertIsNotNone(response.data[0]['id'])
def test_save_poll_response(self): self.poll_response['poll'] = self.poll PollResponse(**self.poll_response).save() poll_response = PollResponse.objects(**self.poll_response) self.assertEqual(1, poll_response.count())
def test_responses_get_both_poll_and_location(self): poll_response_attr = self.poll_response.copy() poll_response_attr['text'] = "NECOCPoll haha %s she no want designer" % self.poll.keyword poll_response = PollResponse(**poll_response_attr).save() self.assertEqual(self.poll, poll_response.poll) self.assertEqual(self.village, poll_response.location) self.assertEqual('Kampala >> Bukoto', poll_response.location_str())
def test_should_process_and_categorise_yesno_poll_responses_when_yesno_is_misplaced(self): self.poll_attr['ptype'] = 'yesno' self.poll_attr['keyword'] = 'akeyword' poll = Poll(**self.poll_attr).save() self.poll_response['text'] = 'NECOCPoll a very bad yes answer' PollResponse(**self.poll_response).save() poll_response = PollResponse.objects(**self.poll_response) self.assertEqual(1, poll_response.count()) yes_category = ResponseCategory.objects(**dict(poll=poll, name='yes')).first() self.assertEqual(1, PollResponseCategory.objects.count()) self.assertEqual(1, PollResponseCategory.objects(**dict(response_category=yes_category, poll_response=poll_response.first())).count())
def test_should_save_no_text_poll_response(self): response = self.client.post(self.API_ENDPOINT, data=self.no_text_response) self.assertEqual(201, response.status_code) retrieved_poll_response = PollResponse.objects(**self.no_text_message) self.assertEqual(1, retrieved_poll_response.count())
def test_should_assign_response_location_to_mobile_user_location(self): response = self.client.post(self.API_ENDPOINT, data=self.expected_poll_response) retrieved_poll_response = PollResponse.objects(**self.poll_response) self.assertEqual(201, response.status_code) self.assertEqual(self.village, retrieved_poll_response[0].location)
def test_should_create_rapid_pro_poll_response(self): response = self.client.post(self.API_ENDPOINT, data=self.expected_poll_response) self.assertEqual(201, response.status_code) retrieved_poll_response = PollResponse.objects(**self.poll_response) self.assertEqual(1, retrieved_poll_response.count())
def test_should_serialize_poll_response_object(self): poll_response = PollResponse(**self.poll_response).save() serialized_object = PollResponseSerializer(poll_response) serialized_poll = PollSerializer(self.poll) serialized_data_with_source = dict( self.serialized_data.items() + { 'id': str(poll_response.id), 'source': 'NECOC Volunteer', 'poll': serialized_poll.data, 'location': 'Kampala >> Bukoto' }.items()) expected_response_fields = [ 'text', 'relayer', 'phone', 'time', 'location', 'run', 'id' ] expected_poll_fields = [ 'name', 'keyword', 'question', 'id', 'target_locations' ] for field in expected_response_fields: self.assertEqual(serialized_data_with_source[field], serialized_object.data[field]) for field in expected_poll_fields: self.assertEqual(serialized_data_with_source['poll'][field], serialized_object.data['poll'][field])
def test_should_process_special_test_response(self): response = self.client.post(self.API_ENDPOINT, data=self.special_response_for_rapidtester) self.assertEqual(201, response.status_code) retrieved_poll_response = PollResponse.objects(**self.special_response) self.assertEqual(0, retrieved_poll_response.count())
def test_responses_with_no_matching_keyword_does_not_get_poll(self): poll_response_attr = self.poll_response.copy() poll_response_attr['text'] = "NECOCPoll not_keyword haha she no want designer" self.assertNotIn('poll', poll_response_attr.keys()) poll_response = PollResponse(**poll_response_attr).save() self.assertIsNone(poll_response.poll)
def test_responses_with_keyword_is_automatically_associated_to_poll(self): poll_response_attr = self.poll_response.copy() poll_response_attr['text'] = "NECOCPoll haha %s she no want designer" % self.poll.keyword self.assertNotIn('poll', poll_response_attr.keys()) poll_response = PollResponse(**poll_response_attr).save() self.assertEqual(self.poll, poll_response.poll)
def test_responses_with_misplaced_keywords_get_assigned_poll(self): poll_response_attr = self.poll_response.copy() poll_response_attr['text'] = "NECOCPoll haha she no want %s designer" % self.poll.keyword self.assertNotIn('poll', poll_response_attr.keys()) poll_response = PollResponse(**poll_response_attr).save() self.assertIsNotNone(poll_response.poll) self.assertEqual(self.poll, poll_response.poll)
def test_should_return_response_csv_when_csv_endpoint_is_called(self): PollResponse(**self.poll_response).save() response = self.client.get(self.CSV_ENDPOINT, format='csv') expected_response = "source,phone,text,time,location,poll_name\n" \ "NECOC Volunteer,+256775019449,NECOCPoll %s there are 4 or 5,%s,Kampala >> Bukoto,%s" % \ (self.poll_attr['keyword'], self.date_time, self.poll_attr['name']) self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.data))
def test_should_get_rapid_pro_poll_response(self): PollResponse(**self.poll_response).save() response = self.client.get(self.API_ENDPOINT, format='json') self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.data)) self.assertDictContainsSubset(self.expected_poll_response, response.data[0]) self.assertEqual('NECOC Volunteer', response.data[0]['source']) self.assertEqual('Kampala >> Bukoto', response.data[0]['location']) self.assertIsNotNone(response.data[0]['id'])
def test_should_create_yesno_poll_response(self): self.poll_attr['ptype'] = 'yesno' self.poll_attr['keyword'] = 'weiredkey' self.poll_attr['question'] = 'Are there any disasters in your area? YES or NO' self.poll = Poll(**self.poll_attr).save() self.expected_poll_response['text'] = 'NECOCPoll.yes' self.poll_response['text'] = 'NECOCPoll.yes' response = self.client.post(self.API_ENDPOINT, data=self.expected_poll_response) self.assertEqual(201, response.status_code) retrieved_poll_response = PollResponse.objects(**self.poll_response) self.assertEqual(1, retrieved_poll_response.count()) self.assertTrue(retrieved_poll_response[0].is_categorized())
def test_should_create_yesno_poll_response(self): self.poll_attr['ptype'] = 'yesno' self.poll_attr['keyword'] = 'weiredkey' self.poll_attr[ 'question'] = 'Are there any disasters in your area? YES or NO' self.poll = Poll(**self.poll_attr).save() self.expected_poll_response['text'] = 'NECOCPoll.yes' self.poll_response['text'] = 'NECOCPoll.yes' response = self.client.post(self.API_ENDPOINT, data=self.expected_poll_response) self.assertEqual(201, response.status_code) retrieved_poll_response = PollResponse.objects(**self.poll_response) self.assertEqual(1, retrieved_poll_response.count()) self.assertTrue(retrieved_poll_response[0].is_categorized())
def test_should_process_and_categorise_yesno_poll_responses(self): self.poll_attr['ptype'] = 'yesno' self.poll_attr['keyword'] = 'akeyword' poll = Poll(**self.poll_attr).save() self.poll_response['text'] = 'yes' PollResponse(**self.poll_response).save() poll_response = PollResponse.objects(**self.poll_response) self.assertEqual(1, poll_response.count()) yes_category = ResponseCategory.objects(**dict(poll=poll, name='yes')).first() self.assertEqual(1, PollResponseCategory.objects.count()) self.assertEqual(1, PollResponseCategory.objects(**dict(response_category=yes_category, poll_response=poll_response.first())).count()) #a 'NO' Response self.poll_response['text'] = 'nah' PollResponse(**self.poll_response).save() poll_response = PollResponse.objects(**self.poll_response) self.assertEqual(1, poll_response.count()) no_category = ResponseCategory.objects(**dict(poll=poll, name='no')).first() self.assertEqual(2, PollResponseCategory.objects.count()) self.assertEqual(1, PollResponseCategory.objects(**dict(response_category=no_category, poll_response=poll_response.first())).count()) #a 'unknown' Response self.poll_response['text'] = 'very wiered response' PollResponse(**self.poll_response).save() poll_response = PollResponse.objects(**self.poll_response) self.assertEqual(1, poll_response.count()) unknown_category = ResponseCategory.objects(**dict(poll=poll, name='unknown')).first() self.assertEqual(3, PollResponseCategory.objects.count()) #bad response not associated with poll self.assertEqual(1, PollResponseCategory.objects(**dict(response_category=unknown_category, poll_response=poll_response.first())).count()) #should count results results_count = poll.yesno_poll_stats() self.assertEqual(1, results_count['yes']) self.assertEqual(1, results_count['unknown']) self.assertEqual(1, results_count['no'])
def test_should_create_dotted_poll_response(self): response = self.client.post(self.API_ENDPOINT, data=self.dotted_response) self.assertEqual(201, response.status_code) retrieved_poll_response = PollResponse.objects(**self.dotted_message) self.assertEqual(1, retrieved_poll_response.count())
def test_should_process_special_test_response(self): response = self.client.post(self.API_ENDPOINT, data=self.special_response_for_rapidtester) self.assertEqual(201, response.status_code) retrieved_poll_response = PollResponse.objects(**self.special_response) self.assertEqual(0, retrieved_poll_response.count())
def test_should_save_wrong_format_poll_response(self): response = self.client.post(self.API_ENDPOINT, data=self.wrong_format_response) self.assertEqual(201, response.status_code) retrieved_poll_response = PollResponse.objects(**self.wrong_format_message) self.assertEqual(1, retrieved_poll_response.count())
def test_should_assign_response_location_to_mobile_user_location(self): response = self.client.post(self.API_ENDPOINT, data=self.expected_poll_response) retrieved_poll_response = PollResponse.objects(**self.poll_response) self.assertEqual(201, response.status_code) self.assertEqual(self.village, retrieved_poll_response[0].location)
def test_fields(self): expected_fields = ['text', 'created_at', 'phone_no', 'received_at', 'location', 'poll'] rapidpro_message = PollResponse() for field in expected_fields: self.assertTrue(hasattr(rapidpro_message, field))
def test_poll_response_source(self): poll_response = PollResponse(**self.poll_response) self.assertEqual('NECOC Volunteer', poll_response.source())
def test_poll_response_knows_its_mobile_user(self): poll_response = PollResponse(**self.poll_response) self.assertEqual(self.mobile_user, poll_response.mobile_user())
def test_poll_response_gets_the_location_of_its_responder(self): rapid_pro_poll_response = PollResponse(**self.poll_response).save() self.assertEqual(self.village, rapid_pro_poll_response.location)
def test_response_location_is_none_if_mobile_user_not_registered(self): some_non_registered_number = '1234' self.poll_response['phone_no'] = some_non_registered_number poll_response = PollResponse(**self.poll_response).save() self.assertIsNone(poll_response.location)