Skip to content

Commit

Permalink
include fields test for serde
Browse files Browse the repository at this point in the history
  • Loading branch information
atanas-balevsky committed Dec 28, 2023
1 parent 10fa94b commit a0b77e6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 0 additions & 2 deletions terracotta/server/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<digits>`")
elif not isinstance(data, (int, float)):
raise ValidationError("Must be a number or string like `p<digits>`")
42 changes: 42 additions & 0 deletions tests/server/test_fields.py
Original file line number Diff line number Diff line change
@@ -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<digits>`" 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)

0 comments on commit a0b77e6

Please sign in to comment.