Skip to content

Commit

Permalink
Prevent zeros coercing to nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbrittain committed Oct 8, 2024
1 parent 4dbfd05 commit 61a92e3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/InsightBoard/pages/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,23 +332,29 @@ def clean_value(x, key_name=None, dtypes={}):
)
)

if "number" in target_types and isinstance(x, str):
try:
if "." in x:
n = float(x)
if math.isnan(n):
return None
else:
n = int(x)
return n
except Exception:
pass

if "integer" in target_types and isinstance(x, str):
try:
return int(x) # int cannot be nan
except Exception:
pass
if "number" in target_types:
if isinstance(x, int) or isinstance(x, float):
return x
elif isinstance(x, str):
try:
if "." in x:
n = float(x)
if math.isnan(n):
return None
else:
n = int(x)
return n
except Exception:
pass

if "integer" in target_types:
if isinstance(x, int):
return x
elif isinstance(x, str):
try:
return int(x) # int cannot be nan
except Exception:
pass

if "boolean" in target_types and isinstance(x, str):
try:
Expand Down
3 changes: 3 additions & 0 deletions tests/pages/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def test_clean_value():
number_type = ["k", {"k": {"type": "number"}}]
integer_type = ["k", {"k": {"type": "integer"}}]
array_type = ["k", {"k": {"type": "array"}}]
number_or_null_type = ["k", {"k": {"type": ["number", "null"]}}]
assert clean_value("1") == "1"
assert clean_value("1", *number_type) == 1
assert clean_value("1", *integer_type) == 1
Expand All @@ -149,6 +150,8 @@ def test_clean_value():
assert clean_value('["a", "b", "c"]', *array_type) == ["a", "b", "c"]
assert clean_value("'a', 'b', 'c'", *array_type) == ["a", "b", "c"]
assert clean_value("a, b, c", *array_type) == ["a", "b", "c"]
assert clean_value("0", *number_or_null_type) == 0
assert clean_value(0, *number_or_null_type) == 0


def test_update_edited_data():
Expand Down

0 comments on commit 61a92e3

Please sign in to comment.