Added support for long values in FloatFields
This commit is contained in:
parent
815b2be7f7
commit
fed58f3920
@ -260,10 +260,14 @@ class FloatField(BaseField):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
if isinstance(value, int):
|
if isinstance(value, (int, long)):
|
||||||
value = float(value)
|
try:
|
||||||
|
value = float(value)
|
||||||
|
except OverflowError:
|
||||||
|
self.error('The value is too large to be converted to float')
|
||||||
|
|
||||||
if not isinstance(value, float):
|
if not isinstance(value, float):
|
||||||
self.error('FloatField only accepts float values')
|
self.error('FloatField only accepts float, int and long values')
|
||||||
|
|
||||||
if self.min_value is not None and value < self.min_value:
|
if self.min_value is not None and value < self.min_value:
|
||||||
self.error('Float value is too small')
|
self.error('Float value is too small')
|
||||||
|
@ -399,20 +399,36 @@ class FieldTest(unittest.TestCase):
|
|||||||
class Person(Document):
|
class Person(Document):
|
||||||
height = FloatField(min_value=0.1, max_value=3.5)
|
height = FloatField(min_value=0.1, max_value=3.5)
|
||||||
|
|
||||||
|
class BigPerson(Document):
|
||||||
|
height = FloatField()
|
||||||
|
|
||||||
person = Person()
|
person = Person()
|
||||||
person.height = 1.89
|
person.height = 1.89
|
||||||
person.validate()
|
person.validate()
|
||||||
|
|
||||||
person.height = '2.0'
|
person.height = '2.0'
|
||||||
self.assertRaises(ValidationError, person.validate)
|
self.assertRaises(ValidationError, person.validate)
|
||||||
|
|
||||||
person.height = 0.01
|
person.height = 0.01
|
||||||
self.assertRaises(ValidationError, person.validate)
|
self.assertRaises(ValidationError, person.validate)
|
||||||
|
|
||||||
person.height = 4.0
|
person.height = 4.0
|
||||||
self.assertRaises(ValidationError, person.validate)
|
self.assertRaises(ValidationError, person.validate)
|
||||||
|
|
||||||
person_2 = Person(height='something invalid')
|
person_2 = Person(height='something invalid')
|
||||||
self.assertRaises(ValidationError, person_2.validate)
|
self.assertRaises(ValidationError, person_2.validate)
|
||||||
|
|
||||||
|
big_person = BigPerson()
|
||||||
|
|
||||||
|
big_person.height = 1L
|
||||||
|
big_person.validate()
|
||||||
|
|
||||||
|
big_person.height = 2 ** 500
|
||||||
|
big_person.validate()
|
||||||
|
|
||||||
|
big_person.height = 2 ** 100000 # Too big for a float value
|
||||||
|
self.assertRaises(ValidationError, big_person.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.
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user