Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Add test for exporting answers to old survey questions.
Browse files Browse the repository at this point in the history
  • Loading branch information
hodgestar committed Dec 5, 2013
1 parent 8ee3baa commit 3e69f30
Showing 1 changed file with 52 additions and 23 deletions.
75 changes: 52 additions & 23 deletions go/apps/surveys/tests/test_views.py
Expand Up @@ -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</button>')

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'],
)

0 comments on commit 3e69f30

Please sign in to comment.