From 3e69f306d44d72c9cb51b0cea5e4566cc26ffa42 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Thu, 5 Dec 2013 14:25:41 +0200 Subject: [PATCH] Add test for exporting answers to old survey questions. --- go/apps/surveys/tests/test_views.py | 75 ++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 23 deletions(-) diff --git a/go/apps/surveys/tests/test_views.py b/go/apps/surveys/tests/test_views.py index ba93b8e69..7652bf5b7 100644 --- a/go/apps/surveys/tests/test_views.py +++ b/go/apps/surveys/tests/test_views.py @@ -162,37 +162,66 @@ def test_action_export_user_data_get(self): self.assertEqual([], self.get_api_commands_sent()) self.assertContains(response, '>Send CSV via e-mail') + def setup_poll(self, questions=2, answer=False, user='user-1'): + question_numbers = list(range(1, 1 + questions)) + pm, poll = self.create_poll(self.conversation, questions=[ + { + 'copy': 'question-%d' % i, + 'label': 'label-%d' % i, + } for i in question_numbers + ]) + + if answer: + participant = pm.get_participant(poll.poll_id, user) + participant.has_unanswered_question = True + participant.set_last_question_index(0) + for i in question_numbers: + poll.submit_answer(participant, 'answer %d' % i) + participant.set_last_question_index(i) + + def check_csv_email(self, headers, answers, user='user-1'): + [email] = mail.outbox + [(file_name, contents, mime_type)] = email.attachments + + self.assertEqual(file_name, 'survey-data-export.zip') + + zipfile = ZipFile(StringIO(contents), 'r') + csv_contents = zipfile.open('survey-data-export.csv', 'r').read() + + lines = csv_contents.split('\r\n') + self.assertEqual(lines[0], ','.join(headers)) + self.assertTrue(lines[1].startswith('user-1,')) + self.assertTrue(lines[1].endswith(',' + ','.join(answers))) + def test_action_export_user_data_post(self): self.setup_conversation() - pm, poll = self.create_poll(self.conversation, questions=[{ - 'copy': 'question-1', - 'label': 'label-1', - }, { - 'copy': 'question-2', - 'label': 'label-2', - }]) - - participant = pm.get_participant(poll.poll_id, 'user-1') - participant.has_unanswered_question = True - participant.set_last_question_index(0) - poll.submit_answer(participant, 'answer 1') - participant.set_last_question_index(1) - poll.submit_answer(participant, 'answer 2') + self.setup_poll(questions=2, answer=True) response = self.client.post( self.get_action_view_url('download_user_data')) self.assertRedirects(response, self.get_view_url('show')) + self.check_csv_email( + headers=['user_id', 'user_timestamp', 'label-1', 'label-2'], + answers=['answer 1', 'answer 2'], + ) - [email] = mail.outbox - [(file_name, contents, mime_type)] = email.attachments + def test_action_export_user_data_post_with_old_questions(self): + self.setup_conversation() + self.setup_poll(questions=2, answer=True) - self.assertEqual(file_name, 'survey-data-export.zip') + # overwrite poll + pm, poll = self.create_poll(self.conversation, questions=[{ + 'copy': 'question-1', + 'label': 'label-1', + }]) - zipfile = ZipFile(StringIO(contents), 'r') - csv_contents = zipfile.open('survey-data-export.csv', 'r').read() + response = self.client.post( + self.get_action_view_url('download_user_data'), + {'include_old_questions': True}) - lines = csv_contents.split('\r\n') - self.assertEqual(lines[0], 'user_id,user_timestamp,label-1,label-2') - self.assertTrue(lines[1].startswith('user-1')) - self.assertTrue(lines[1].endswith(',answer 1,answer 2')) + self.assertRedirects(response, self.get_view_url('show')) + self.check_csv_email( + headers=['user_id', 'user_timestamp', 'label-1', 'label-2'], + answers=['answer 1', 'answer 2'], + )