Skip to content

Commit 91ff134

Browse files
committed
add test
1 parent 280a84f commit 91ff134

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

tests/validators/test_list.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Any, Callable, Dict, Iterator, List, Union
66

77
import pytest
8-
from dirty_equals import HasRepr, IsInstance, IsStr
8+
from dirty_equals import Contains, HasRepr, IsInstance, IsStr
99

1010
from pydantic_core import PydanticOmit, SchemaValidator, ValidationError, core_schema
1111

@@ -495,3 +495,37 @@ def f(x: int) -> int:
495495
]
496496

497497
assert next(gen) == 102
498+
499+
500+
@pytest.mark.parametrize('error_in_func', [True, False])
501+
def test_max_length_fail_fast(error_in_func: bool) -> None:
502+
calls: list[int] = []
503+
504+
def f(v: int) -> int:
505+
calls.append(v)
506+
if error_in_func:
507+
assert v < 10
508+
return v
509+
510+
s = core_schema.list_schema(
511+
core_schema.no_info_after_validator_function(f, core_schema.int_schema()), max_length=10
512+
)
513+
514+
v = SchemaValidator(s)
515+
516+
data = list(range(15))
517+
518+
with pytest.raises(ValidationError) as exc_info:
519+
v.validate_python(data)
520+
521+
assert len(calls) <= 11, len(calls) # we still run validation on the "extra" item
522+
523+
assert exc_info.value.errors(include_url=False) == Contains(
524+
{
525+
'type': 'too_long',
526+
'loc': (),
527+
'msg': 'List should have at most 10 items after validation, not 11',
528+
'input': data,
529+
'ctx': {'field_type': 'List', 'max_length': 10, 'actual_length': 11},
530+
}
531+
)

0 commit comments

Comments
 (0)