Skip to content

Commit 98d0a21

Browse files
committed
Renaming also validates
1 parent dcddf18 commit 98d0a21

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

tests/v/test_fluent.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ def test_rename(c: Converter) -> None:
8080
assert c.structure(unstructured, Model) == instance
8181

8282

83+
def test_rename_also_validates(c: Converter) -> None:
84+
"""Renaming a field and validating works."""
85+
customize(c, Model, V(f(Model).b).rename("B").ensure(is_lowercase))
86+
87+
instance = Model(1, "A", ["1"], [1], "", 0, 0, {"a": 1})
88+
89+
unstructured = c.unstructure(instance)
90+
91+
# Customize only affects structuring currently.
92+
unstructured["B"] = unstructured.pop("b")
93+
94+
if c.detailed_validation:
95+
with raises(ClassValidationError) as exc_info:
96+
c.structure(unstructured, Model)
97+
98+
assert transform_error(exc_info.value) == [
99+
"invalid value ('A' not lowercase) @ $.b"
100+
]
101+
else:
102+
with raises(ValueError) as exc_info:
103+
c.structure(unstructured, Model)
104+
105+
assert repr(exc_info.value) == "ValueError(\"'A' not lowercase\")"
106+
107+
unstructured["B"] = instance.b = "a"
108+
assert instance == c.structure(unstructured, Model)
109+
110+
83111
def test_simple_string_validation(c: Converter) -> None:
84112
"""Simple string validation works."""
85113
customize(c, Model, V(f(Model).b).ensure(is_lowercase))

0 commit comments

Comments
 (0)