def test_exports_questions_with_numeric_answers(self): question_text = "%s | %s | %s" % (self.section_1.title, self.sub_section.title, self.primary_question.text) question_text1 = "%s | %s | %s" % ( self.section_1.title, self.sub_section.title, self.question1.text) question_text_2 = "%s | %s | %s" % ( self.section_1.title, self.sub_section.title, self.question2.text) answer_id = "R_%s_%s_%s" % (self.primary_question.UID, self.primary_question.UID, self.option.UID) answer_id_1 = "R_%s_%s_1" % (self.primary_question.UID, self.question1.UID) answer_id_2 = "R_%s_%s_%d_1" % (self.primary_question.UID, self.question1.UID, 1) expected_data = [ self.headings, "UGX\t%s\t2013\t%s\t%s\t%s" % (self.country.name, answer_id.encode('base64').strip(), question_text, self.option.text), "UGX\t%s\t2013\t%s\t%s\t%s" % (self.country.name, answer_id_1.encode('base64').strip(), question_text1, '23.00'), "UGX\t%s\t2013\t%s\t%s\t%s" % (self.country.name, answer_id_2.encode('base64').strip(), question_text_2, '1.00') ] export_to_text_service = ExportToTextService(self.questionnaire) actual_data = export_to_text_service.get_formatted_responses() self.assertEqual(len(expected_data), len(actual_data)) self.assertIn(expected_data[0], actual_data) self.assertIn(expected_data[1], actual_data)
def test_draft_answers_does_not_show_on_extract_questionnaire_answers( self): Question.objects.all().delete() question = Question.objects.create(text='what do you drink?', UID='abc123', answer_type='Text') parent = QuestionGroup.objects.create(subsection=self.sub_section, order=2) parent.question.add(question) QuestionGroupOrder.objects.create(question=question, question_group=parent, order=1) country = Country.objects.create(name="Uganda", code="UGX") answer1 = TextAnswer.objects.create(question=question, country=country, response="tusker lager", status=Answer.DRAFT_STATUS) answer_group1 = AnswerGroup.objects.create(grouped_question=parent, row=1) answer_group1.answer.add(answer1) expected_data = [ self.headings, ] export_to_text_service = ExportToTextService(self.questionnaire) actual_data = export_to_text_service.get_formatted_responses() self.assertEqual(len(expected_data), len(actual_data)) self.assertIn(expected_data[0], actual_data)
def post(self, request, *args, **kwargs): questionnaire = Questionnaire.objects.get( id=request.POST['questionnaire']) formatted_responses = ExportToTextService( questionnaire).get_formatted_responses() response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename="%s-%s.txt"' % ( questionnaire.name, questionnaire.year) response.write("\r\n".join(formatted_responses)) return response
def test_exports_single_question_in_a_group_questionnaire(self): Question.objects.all().delete() question = Question.objects.create(text='what do you drink?', UID='abc123', answer_type='Text') parent = QuestionGroup.objects.create(subsection=self.sub_section, order=2) parent.question.add(question) QuestionGroupOrder.objects.create(question=question, question_group=parent, order=1) country = Country.objects.create(name="Uganda", code="UGX") answer1 = TextAnswer.objects.create(question=question, country=country, response="tusker lager", status=Answer.SUBMITTED_STATUS) answer_group1 = AnswerGroup.objects.create(grouped_question=parent, row=1) answer_group1.answer.add(answer1) question_text = "%s | %s | %s" % ( self.section_1.title, self.sub_section.title, question.text) answer_id = "R_%s_%s_1" % (question.UID, question.UID) expected_data = [ self.headings, "UGX\t%s\t2013\t%s\t%s\t%s" % (self.country.name, answer_id.encode('base64').strip(), question_text, 'tusker lager') ] export_to_text_service = ExportToTextService(self.questionnaire) actual_data = export_to_text_service.get_formatted_responses() self.assertEqual(len(expected_data), len(actual_data)) self.assertIn(expected_data[0], actual_data)
def test_exports_questions_with_two_rows_answer_grid(self): primary_question_answer2 = MultiChoiceAnswer.objects.create( question=self.primary_question, country=self.country, status=Answer.SUBMITTED_STATUS, response=self.option2) question1_answer2 = NumericalAnswer.objects.create( question=self.question1, country=self.country, status=Answer.SUBMITTED_STATUS, response=4) question2_answer2 = NumericalAnswer.objects.create( question=self.question2, country=self.country, status=Answer.SUBMITTED_STATUS, response=55) answer_group2 = AnswerGroup.objects.create( grouped_question=self.parent, row=2) answer_group2.answer.add(primary_question_answer2, question1_answer2, question2_answer2) question_text = "%s | %s | %s" % (self.section_1.title, self.sub_section.title, self.primary_question.text) question_text1 = "%s | %s | %s" % ( self.section_1.title, self.sub_section.title, self.question1.text) question_text_2 = "%s | %s | %s" % ( self.section_1.title, self.sub_section.title, self.question2.text) answer_id = "R_%s_%s_%s" % (self.primary_question.UID, self.primary_question.UID, self.option.UID) answer_id_1 = "R_%s_%s_1" % (self.primary_question.UID, self.question1.UID) answer_id_2 = "R_%s_%s_1" % (self.primary_question.UID, self.question2.UID) answer_id_10 = "R_%s_%s_%s" % (self.primary_question.UID, self.primary_question.UID, self.option2.UID) answer_id_11 = "R_%s_%s_2" % (self.primary_question.UID, self.question1.UID) answer_id_21 = "R_%s_%s_2" % (self.primary_question.UID, self.question2.UID) expected_data = [ self.headings, "UGX\t%s\t2013\t%s\t%s\t%s" % (self.country.name, answer_id.encode('base64').strip(), question_text, self.option.text), "UGX\t%s\t2013\t%s\t%s\t%s" % (self.country.name, answer_id_1.encode('base64').strip(), question_text1, '23.00'), "UGX\t%s\t2013\t%s\t%s\t%s" % (self.country.name, answer_id_2.encode('base64').strip(), question_text_2, '1.00'), "UGX\t%s\t2013\t%s\t%s\t%s" % (self.country.name, answer_id_10.encode('base64').strip(), question_text, self.option2.text), "UGX\t%s\t2013\t%s\t%s\t%s" % (self.country.name, answer_id_11.encode('base64').strip(), question_text1, '4.00'), "UGX\t%s\t2013\t%s\t%s\t%s" % (self.country.name, answer_id_21.encode('base64').strip(), question_text_2, '55.00') ] export_to_text_service = ExportToTextService(self.questionnaire) actual_data = export_to_text_service.get_formatted_responses() self.assertEqual(len(expected_data), len(actual_data)) self.assertIn(expected_data[0], actual_data) self.assertIn(expected_data[1], actual_data) self.assertIn(expected_data[2], actual_data) self.assertIn(expected_data[3], actual_data) self.assertIn(expected_data[4], actual_data) self.assertIn(expected_data[5], actual_data)