Skip to content

Commit

Permalink
The List validator now passes with objects implementing the list
Browse files Browse the repository at this point in the history
interface.

refs #18
  • Loading branch information
jaimegildesagredo committed Jan 29, 2014
1 parent c22141d commit 7ae5184
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
3 changes: 2 additions & 1 deletion booby/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import re
import functools
import collections

from booby import errors

Expand Down Expand Up @@ -181,7 +182,7 @@ def __init__(self, *validators):
self.validators = validators

def validate(self, value):
if not isinstance(value, list):
if not isinstance(value, collections.MutableSequence):
raise errors.ValidationError('should be a list')

for i in value:
Expand Down
58 changes: 58 additions & 0 deletions tests/unit/validators/test_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import collections

from expects import expect
from .._helpers import stub_validator

from booby import validators, errors


class TestList(object):
def test_should_pass_if_value_is_a_list(self):
self.validator(['foo', 'bar'])

def test_should_pass_if_value_is_a_mutable_sequence(self):
self.validator(MyList('foo', 'bar'))

def test_should_fail_if_value_is_not_a_list(self):
expect(lambda: self.validator(object())).to.raise_error(
errors.ValidationError, 'should be a list')

def test_should_fail_if_value_is_none(self):
expect(lambda: self.validator(None)).to.raise_error(
errors.ValidationError, 'should be a list')

def test_should_fail_if_inner_validator_fails(self):
def inner_validator(value):
if value == 'bar':
raise errors.ValidationError('invalid')

self.validator = validators.List(stub_validator, inner_validator)

expect(lambda: self.validator(['foo', 'bar'])).to.raise_error(
errors.ValidationError, 'invalid')

def setup(self):
self.validator = validators.List()


class MyList(collections.MutableSequence):
def __init__(self, *args):
self._store = list(args)

def __getitem__(self, index):
return self._store[index]

def __setitem__(self, index, value):
pass

def __delitem__(self, index):
pass

def __len__(self):
pass

def insert(self, index, value):
pass
26 changes: 0 additions & 26 deletions tests/unit/validators/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,31 +112,5 @@ def setup(self):
self.validator = validators.Model(User)


class TestList(object):
def test_when_value_is_not_a_list_then_raises_validation_error(self):
expect(lambda: self.validator(object())).to.raise_error(
errors.ValidationError, 'should be a list')

def test_when_value_is_none_then_raises_validation_error(self):
expect(lambda: self.validator(None)).to.raise_error(
errors.ValidationError, 'should be a list')

def test_when_value_is_a_list_then_does_not_raise(self):
self.validator(['foo', 'bar'])

def test_when_inner_validator_raises_validation_error_then_raises_validation_error(self):
def inner_validator(value):
if value == 'bar':
raise errors.ValidationError('invalid')

self.validator = validators.List(stub_validator, inner_validator)

expect(lambda: self.validator(['foo', 'bar'])).to.raise_error(
errors.ValidationError, 'invalid')

def setup(self):
self.validator = validators.List()


class User(models.Model):
name = fields.String()

0 comments on commit 7ae5184

Please sign in to comment.