Fixed issue with choices validation when they are simple list/tuple,
after model.validate() did not get any error message. Added test to ensure that model.validate() set the correct error messages.
This commit is contained in:
parent
25cdf16cc0
commit
f5d02e1b10
1
AUTHORS
1
AUTHORS
@ -128,3 +128,4 @@ that much better:
|
|||||||
* Peter Teichman
|
* Peter Teichman
|
||||||
* Jakub Kot
|
* Jakub Kot
|
||||||
* Jorge Bastida
|
* Jorge Bastida
|
||||||
|
* Martin Alderete https://github.com/malderete
|
||||||
|
@ -252,7 +252,7 @@ class BaseField(object):
|
|||||||
elif value_to_check not in self.choices:
|
elif value_to_check not in self.choices:
|
||||||
msg = ('Value must be %s of %s' %
|
msg = ('Value must be %s of %s' %
|
||||||
(err_msg, unicode(self.choices)))
|
(err_msg, unicode(self.choices)))
|
||||||
self.error()
|
self.error(msg)
|
||||||
|
|
||||||
# check validation argument
|
# check validation argument
|
||||||
if self.validation is not None:
|
if self.validation is not None:
|
||||||
|
@ -1706,6 +1706,41 @@ class FieldTest(unittest.TestCase):
|
|||||||
|
|
||||||
Shirt.drop_collection()
|
Shirt.drop_collection()
|
||||||
|
|
||||||
|
|
||||||
|
def test_simple_choices_validation_invalid_value(self):
|
||||||
|
"""Ensure that error messages are correct.
|
||||||
|
"""
|
||||||
|
SIZES = ('S', 'M', 'L', 'XL', 'XXL')
|
||||||
|
COLORS = (('R', 'Red'), ('B', 'Blue'))
|
||||||
|
SIZE_MESSAGE = u"Value must be one of ('S', 'M', 'L', 'XL', 'XXL')"
|
||||||
|
COLOR_MESSAGE = u"Value must be one of ['R', 'B']"
|
||||||
|
|
||||||
|
class Shirt(Document):
|
||||||
|
size = StringField(max_length=3, choices=SIZES)
|
||||||
|
color = StringField(max_length=1, choices=COLORS)
|
||||||
|
|
||||||
|
Shirt.drop_collection()
|
||||||
|
|
||||||
|
shirt = Shirt()
|
||||||
|
shirt.validate()
|
||||||
|
|
||||||
|
shirt.size = "S"
|
||||||
|
shirt.color = "R"
|
||||||
|
shirt.validate()
|
||||||
|
|
||||||
|
shirt.size = "XS"
|
||||||
|
shirt.color = "G"
|
||||||
|
|
||||||
|
try:
|
||||||
|
shirt.validate()
|
||||||
|
except ValidationError, error:
|
||||||
|
# get the validation rules
|
||||||
|
error_dict = error.to_dict()
|
||||||
|
self.assertEqual(error_dict['size'], SIZE_MESSAGE)
|
||||||
|
self.assertEqual(error_dict['color'], COLOR_MESSAGE)
|
||||||
|
|
||||||
|
Shirt.drop_collection()
|
||||||
|
|
||||||
def test_file_fields(self):
|
def test_file_fields(self):
|
||||||
"""Ensure that file fields can be written to and their data retrieved
|
"""Ensure that file fields can be written to and their data retrieved
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user