Skip to content

Commit

Permalink
[BB-993] Fix a bug in report generation where a survey response has n…
Browse files Browse the repository at this point in the history
…o choices key (#52)

* [BB-993] Fix a bug in report generation where a survey response has no choices key.

* Fix a similar bug in poll block report generation. Add a check to the unit test to be sure no rows are included in the report in the instance no actual responses have been recorded.

* Handle the case where the choices key exists, but is None.
  • Loading branch information
jamestait committed Mar 4, 2019
1 parent ff707e5 commit c5c2cc1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions poll/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,9 @@ def generate_report_data(self, user_state_iterator, limit_responses=None):
# End the iterator here
return

choice = user_state.state['choice'] # {u'submissions_count': 1, u'choice': u'R'}
choice = user_state.state.get('choice') # {u'submissions_count': 1, u'choice': u'R'}
if choice is None:
continue
report = {
self.ugettext('Question'): self.question,
self.ugettext('Answer'): answers_dict[choice]['label'],
Expand Down Expand Up @@ -1296,7 +1298,7 @@ def generate_report_data(self, user_state_iterator, limit_responses=None):
count = 0
for user_state in user_state_iterator:
# user_state.state={'submissions_count': 1, 'choices': {u'enjoy': u'Y', u'recommend': u'N', u'learn': u'M'}}
choices = user_state.state['choices']
choices = user_state.state.get('choices') or {}
for question_id, answer_id in choices.items():

if limit_responses is not None and count >= limit_responses:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def package_data(pkg, roots):

setup(
name='xblock-poll',
version='1.8.0',
version='1.8.1',
description='An XBlock for polling users.',
packages=[
'poll',
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_xblock_poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def mock_user_states(cls):
Mock(username='verified', state={'submissions_count': 1, 'choice': 'G'}),
Mock(username='staff', state={'submissions_count': 1, 'choice': 'B'}),
Mock(username='honor', state={'submissions_count': 1, 'choice': 'O'}),
Mock(username='audit', state={'submissions_count': 1}),
Mock(username='student', state={'submissions_count': 1, 'choice': None}),
)

def test_generate_report_data_dont_limit_responses(self):
Expand All @@ -91,6 +93,8 @@ def test_generate_report_data_dont_limit_responses(self):
('edx', {'Question': self.poll_block.question,
'Answer': 'Red',
'Submissions count': 1}))
self.assertNotIn('audit', [username for username, _ in report_data])
self.assertNotIn('student', [username for username, _ in report_data])

def test_generate_report_data_limit_responses(self):
"""
Expand Down Expand Up @@ -212,6 +216,19 @@ def mock_user_states(cls):
'choices': {'enjoy': 'Y', 'recommend': 'N', 'learn': 'M'}
}
),
Mock(
username='audit',
state={
'submissions_count': 1,
}
),
Mock(
username='student',
state={
'submissions_count': 1,
'choices': None,
}
),
)

def test_generate_report_data_dont_limit_responses(self):
Expand All @@ -230,6 +247,8 @@ def test_generate_report_data_dont_limit_responses(self):
set(['Yes', 'No', 'Maybe']),
set([data['Answer'] for _, data in report_data[:4]])
)
self.assertNotIn('audit', [username for username, _ in report_data])
self.assertNotIn('student', [username for username, _ in report_data])

def test_generate_report_data_limit_responses(self):
"""
Expand Down

0 comments on commit c5c2cc1

Please sign in to comment.