Skip to content

Commit

Permalink
parse_int should return None for empty string. (#1165)
Browse files Browse the repository at this point in the history
Fix #1164.
  • Loading branch information
ukanga authored Oct 25, 2017
1 parent b21e604 commit 2436448
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions onadata/apps/api/tests/viewsets/test_data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,13 @@ def test_data_start_limit(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 4)

# invalid start is ignored, all data is returned
request = self.factory.get('/', data={"start": "", "limit": 10},
**self.extra)
response = view(request, pk=formid)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 4)

def test_data_start_limit_sort(self):
self._make_submissions()
view = DataViewSet.as_view({'get': 'list'})
Expand Down
13 changes: 13 additions & 0 deletions onadata/libs/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# -*- coding=utf-8 -*-
"""
Data utility functions.
"""
import six


def parse_int(num):
"""
Parse integer from a string.
"""
is_empty = isinstance(num, six.string_types) and len(num) == 0
if is_empty:
return None
try:
return num and int(num)
except ValueError:
Expand Down

0 comments on commit 2436448

Please sign in to comment.