Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/formpack/schema/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,10 @@ def parse_values(self, raw_values):
if self.data_type == 'integer':
yield int(raw_values)
else:
yield float(raw_values)
value = float(raw_values)
if not math.isfinite(value):
raise ValueError(f'Non-finite float value: {raw_values!r}')
yield value

def format(self, val, xls_types_as_text=True, *args, **kwargs):
if val is None:
Expand Down
54 changes: 54 additions & 0 deletions tests/test_autoreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,57 @@ def test_stats_with_non_numeric_value_for_numeric_field(self):
]
for i, stat in enumerate(stats):
assert stat == expected[i]

def test_stats_with_non_finite_float_value(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a test for when every response is infinite?

"""
Non-finite float values (Inf, -Inf, nan) for a decimal field should
be treated as if no response was provided (not_provided).
"""
title = 'Just one decimal'
schemas = [
{
'content': {
'survey': [
{
'type': 'decimal',
'name': 'the_number',
'label': 'Enter the number!',
}
]
}
}
]
submissions = [
{'the_number': '1.0'},
{'the_number': '2.0'},
{'the_number': '3.0'},
{'the_number': 'Inf'},
{'the_number': '-Inf'},
{'the_number': 'nan'},
]
fp = FormPack(schemas, title)

report = fp.autoreport()
stats = report.get_stats(submissions)

assert stats.submissions_count == len(submissions)

stats = [(str(repr(f)), n, d) for f, n, d in stats]
expected = [
(
"<NumField name='the_number' type='decimal'>",
'the_number',
{
'mean': 2.0,
'median': 2.0,
'mode': '*',
'not_provided': 3,
'provided': 3,
'show_graph': False,
'stdev': 1.0,
'total_count': 6,
},
)
]
for i, stat in enumerate(stats):
assert stat == expected[i]