diff --git a/api/serialization/util/__init__.py b/api/serialization/util/__init__.py index 6bdcd95..0b182a9 100644 --- a/api/serialization/util/__init__.py +++ b/api/serialization/util/__init__.py @@ -50,7 +50,9 @@ def instance_to_model(instance: Any, * , use_class: Type=None, skip_classes: Lis if instance is None: return None - if isinstance(instance, (str, int, float)): + if isinstance(instance, str): + return instance + if isinstance(instance, (int, float)): if math.isnan(instance): return None return instance diff --git a/test/serialization/test_util.py b/test/serialization/test_util.py new file mode 100644 index 0000000..0a73604 --- /dev/null +++ b/test/serialization/test_util.py @@ -0,0 +1,24 @@ +import pytest +import math + +from api.serialization.util import instance_to_model + +@pytest.mark.parametrize("target, result, error", [ + (None, None, None), + ("foo", "foo", None), + (1, 1, None), + (0.1, 0.1, None), + (math.nan, None, None), + ([1, 2], [1, 2], None), + ((1, 2), [1, 2], None), + ({"foo": "bar", "one": 2}, {"foo": "bar", "one": 2}, None), + ({"foo": [1, "bar"]}, {"foo": [1, "bar"]}, None), + ({"foo": (1, "bar")}, {"foo": [1, "bar"]}, None), +]) +def test_instance_to_model(target, result, error): + if error: + with pytest.raises(error): + instance_to_model(target) + pass + return + assert result == instance_to_model(target)