Merge pull request #462 from bool-dev/master
Fixes #458, DecimalField now ignores incorrect values until validate is called just like FloatField
This commit is contained in:
commit
1145c72b01
@ -304,7 +304,10 @@ class DecimalField(BaseField):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
# Convert to string for python 2.6 before casting to Decimal
|
# Convert to string for python 2.6 before casting to Decimal
|
||||||
|
try:
|
||||||
value = decimal.Decimal("%s" % value)
|
value = decimal.Decimal("%s" % value)
|
||||||
|
except decimal.InvalidOperation:
|
||||||
|
return value
|
||||||
return value.quantize(self.precision, rounding=self.rounding)
|
return value.quantize(self.precision, rounding=self.rounding)
|
||||||
|
|
||||||
def to_mongo(self, value):
|
def to_mongo(self, value):
|
||||||
|
@ -384,6 +384,9 @@ class FieldTest(unittest.TestCase):
|
|||||||
person.height = 4.0
|
person.height = 4.0
|
||||||
self.assertRaises(ValidationError, person.validate)
|
self.assertRaises(ValidationError, person.validate)
|
||||||
|
|
||||||
|
person_2 = Person(height='something invalid')
|
||||||
|
self.assertRaises(ValidationError, person_2.validate)
|
||||||
|
|
||||||
def test_decimal_validation(self):
|
def test_decimal_validation(self):
|
||||||
"""Ensure that invalid values cannot be assigned to decimal fields.
|
"""Ensure that invalid values cannot be assigned to decimal fields.
|
||||||
"""
|
"""
|
||||||
@ -405,6 +408,11 @@ class FieldTest(unittest.TestCase):
|
|||||||
self.assertRaises(ValidationError, person.validate)
|
self.assertRaises(ValidationError, person.validate)
|
||||||
person.height = Decimal('4.0')
|
person.height = Decimal('4.0')
|
||||||
self.assertRaises(ValidationError, person.validate)
|
self.assertRaises(ValidationError, person.validate)
|
||||||
|
person.height = 'something invalid'
|
||||||
|
self.assertRaises(ValidationError, person.validate)
|
||||||
|
|
||||||
|
person_2 = Person(height='something invalid')
|
||||||
|
self.assertRaises(ValidationError, person_2.validate)
|
||||||
|
|
||||||
Person.drop_collection()
|
Person.drop_collection()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user