Skip to content

Commit

Permalink
check_if_number util function handles None, and add test (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
perolavsvendsen committed Oct 28, 2022
1 parent c3bf5c9 commit 17dc928
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/fmu/dataio/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ def nested_parameters_dict(
def check_if_number(value):
"""Check if value (str) looks like a number and return the converted value."""

if value is None:
return

res = None
try:
res = int(value)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_units/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Test the utils module"""

import pytest

from fmu.dataio import _utils as utils


@pytest.mark.parametrize(
"value, result",
[
(None, None),
("0", 0),
("1", 1),
("1.0", 1.0),
("0.0", 0.0),
("-1", -1),
("-1.0", -1.0),
("-999", -999),
("-999.12345678", -999.12345678),
("9999999999999999", 9999999999999999),
("abc", "abc"),
(False, False),
(True, True),
],
)
def test_check_if_number(value, result):
assert utils.check_if_number(value) == result

0 comments on commit 17dc928

Please sign in to comment.