diff --git a/terracotta/server/fields.py b/terracotta/server/fields.py index af758299..0a4b9d84 100644 --- a/terracotta/server/fields.py +++ b/terracotta/server/fields.py @@ -30,5 +30,3 @@ def validate_stretch_range(data: Any) -> None: if isinstance(data, str): if not re.match("^p\\d+$", data): raise ValidationError("Percentile format is `p`") - elif not isinstance(data, (int, float)): - raise ValidationError("Must be a number or string like `p`") diff --git a/tests/server/test_fields.py b/tests/server/test_fields.py new file mode 100644 index 00000000..34d13fd7 --- /dev/null +++ b/tests/server/test_fields.py @@ -0,0 +1,42 @@ +from marshmallow import ValidationError +import pytest +from terracotta.server.singleband import SinglebandOptionSchema + + +@pytest.mark.parametrize( + "args, expected", + [("[0, 1]", [0, 1]), ('["p2", "p28"]', ["p2", "p28"]), (None, None)], +) +def test_serde(args, expected): + args = {"stretch_range": args} + option_schema = SinglebandOptionSchema() + loaded = option_schema.load(args) + assert loaded["stretch_range"] == expected + dumped = option_schema.dump(loaded) + assert dumped["stretch_range"] == expected + + +def test_serde_validation(): + option_schema = SinglebandOptionSchema() + with pytest.raises(ValidationError) as exc_info: + args = {"stretch_range": '["t2", "p28"]'} + option_schema.load(args) + assert "Percentile format is `p`" in str(exc_info.value) + + with pytest.raises(ValidationError) as exc_info: + args = {"stretch_range": '[{}, "p28"]'} + option_schema.load(args) + assert "Must be a string or a number" in str(exc_info.value) + + +def test_serde_bad_type(): + option_schema = SinglebandOptionSchema() + with pytest.raises(ValidationError) as exc_info: + dump_args = {"stretch_range": [0, {"bad": "type"}]} + option_schema.dump(dump_args) + assert "Must be a string or a number" in str(exc_info.value) + + with pytest.raises(ValidationError) as exc_info: + load_args = {"stretch_range": '[0, {"bad": "type"}]'} + option_schema.load(load_args) + assert "Must be a string or a number" in str(exc_info.value)