Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add min, max length support to iterables #612

Merged
merged 1 commit into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion plenum/common/messages/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,28 @@ def _specific_validation(self, val):
class IterableField(FieldBase):
_base_types = (list, tuple)

def __init__(self, inner_field_type: FieldValidator, **kwargs):
def __init__(self, inner_field_type: FieldValidator, min_length=None,
max_length=None, **kwargs):
assert inner_field_type
assert isinstance(inner_field_type, FieldValidator)
for m in (min_length, max_length):
if m is not None:
assert isinstance(m, int)
assert m > 0

self.inner_field_type = inner_field_type
self.min_length = min_length
self.max_length = max_length
super().__init__(**kwargs)

def _specific_validation(self, val):
if self.min_length is not None:
if len(val) < self.min_length:
return 'length should be at least {}'.format(self.min_length)
if self.max_length is not None:
if len(val) > self.max_length:
return 'length should be at most {}'.format(self.max_length)

for v in val:
check_er = self.inner_field_type.validate(v)
if check_er:
Expand Down
3 changes: 2 additions & 1 deletion plenum/test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,8 @@ def chk():
if fails >= acceptable_fails:
logger.debug('Too many fails, the last one: {}'.format(repr(ex)))
last_ex = ex
assert fails <= acceptable_fails, str(last_ex)
assert fails <= acceptable_fails, '{} out of {} failed. Last exception:' \
' {}'.format(fails, len(funcs), last_ex)

kwargs = {}
if retry_wait:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,34 @@ def test_valid_inner_type():
validator = IterableField(NonNegativeNumberField())
assert not validator.validate([1, 2, 3])
assert validator.validate([1, 2, -3])


def test_valid_min_max_vals():
with pytest.raises(Exception):
IterableField(NonNegativeNumberField(), min_length=0)

with pytest.raises(Exception):
IterableField(NonNegativeNumberField(), max_length=0)

with pytest.raises(Exception):
IterableField(NonNegativeNumberField(), min_length=-1)

with pytest.raises(Exception):
IterableField(NonNegativeNumberField(), max_length=-5)

with pytest.raises(Exception):
IterableField(NonNegativeNumberField(), min_length=2.5)

with pytest.raises(Exception):
IterableField(NonNegativeNumberField(), max_length=3.5)


def test_min_max_checks():
validator = IterableField(NonNegativeNumberField(), min_length=3,
max_length=10)
assert validator.validate(list(range(2)))

for i in range(3, 11):
assert not validator.validate(list(range(i)))

assert validator.validate(list(range(12)))