Skip to content

Commit

Permalink
Fix issue where row_number starts from 3 on a job report download
Browse files Browse the repository at this point in the history
  • Loading branch information
imdadahad committed Jan 19, 2017
1 parent dfc751e commit 6a48325
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,15 @@ def clear_file_buffer():

def format_notification_for_csv(notification):
from app import format_datetime_24h, format_notification_status
# row_number can be 0 so we need to check for it
row_num = notification.get('job_row_number')
if row_num is not None and row_num >= 0:
row_num += 1
else:
row_num = ''

return {
'Row number': int(notification['job_row_number']) + 2 if notification.get('job_row_number') else '',
'Row number': row_num,
'Recipient': notification['to'],
'Template': notification['template']['name'],
'Type': notification['template']['template_type'],
Expand Down
24 changes: 24 additions & 0 deletions tests/app/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,27 @@ def test_generate_notifications_csv_calls_twice_if_next_link(mocker):
# mock_calls[0][2] is the kwargs from first call
assert mock_get_notifications.mock_calls[0][2]['page'] == 1
assert mock_get_notifications.mock_calls[1][2]['page'] == 2


@pytest.mark.parametrize('row_number, expected_result', [
(None, ''),
(0, '1'),
(1, '2'),
(300, '301')
])
def test_generate_notifications_csv_formats_row_number_correctly(mocker, row_number, expected_result):
service_id = '1234'
response_with_job_row_zero = notification_json(service_id, rows=1, with_links=True, job_row_number=row_number)
mocker.patch(
'app.notification_api_client.get_notifications_for_service',
side_effect=[
response_with_job_row_zero
]
)

csv_content = generate_notifications_csv(service_id=service_id)
csv = DictReader(StringIO('\n'.join(csv_content)))
csv_rows = list(csv)

assert len(csv_rows) == 1
assert csv_rows[0].get('Row number') == expected_result

0 comments on commit 6a48325

Please sign in to comment.