Merge pull request #1240 from gukoff/long_in_floatfield

Added support for long values in FloatFields
This commit is contained in:
Omer Katz 2016-03-18 10:24:53 +02:00
commit c6cc0133b3
4 changed files with 31 additions and 5 deletions

View File

@ -8,6 +8,8 @@ import uuid
import warnings import warnings
from operator import itemgetter from operator import itemgetter
import six
try: try:
import dateutil import dateutil
except ImportError: except ImportError:
@ -260,10 +262,14 @@ class FloatField(BaseField):
return value return value
def validate(self, value): def validate(self, value):
if isinstance(value, int): if isinstance(value, six.integer_types):
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 and integer 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')

View File

@ -1,2 +1,3 @@
pymongo>=2.7.1
nose nose
pymongo>=2.7.1
six==1.10.0

View File

@ -78,7 +78,7 @@ setup(name='mongoengine',
long_description=LONG_DESCRIPTION, long_description=LONG_DESCRIPTION,
platforms=['any'], platforms=['any'],
classifiers=CLASSIFIERS, classifiers=CLASSIFIERS,
install_requires=['pymongo>=2.7.1'], install_requires=['pymongo>=2.7.1', 'six'],
test_suite='nose.collector', test_suite='nose.collector',
**extra_opts **extra_opts
) )

View File

@ -1,5 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys import sys
import six
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
sys.path[0:0] = [""] sys.path[0:0] = [""]
@ -399,20 +401,37 @@ 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()
for value, value_type in enumerate(six.integer_types):
big_person.height = value_type(value)
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.
""" """