Fix long fields stored as int32 in Python 3. issue #1253
This commit is contained in:
parent
c6cc0133b3
commit
7cc1a4eba0
1
AUTHORS
1
AUTHORS
@ -235,3 +235,4 @@ that much better:
|
|||||||
* Steven Rossiter (https://github.com/BeardedSteve)
|
* Steven Rossiter (https://github.com/BeardedSteve)
|
||||||
* Luo Peng (https://github.com/RussellLuo)
|
* Luo Peng (https://github.com/RussellLuo)
|
||||||
* Bryan Bennett (https://github.com/bbenne10)
|
* Bryan Bennett (https://github.com/bbenne10)
|
||||||
|
* Gilb's Gilb's (https://github.com/gilbsgilbs)
|
||||||
|
@ -19,7 +19,7 @@ else:
|
|||||||
|
|
||||||
import pymongo
|
import pymongo
|
||||||
import gridfs
|
import gridfs
|
||||||
from bson import Binary, DBRef, SON, ObjectId
|
from bson import Binary, DBRef, SON, ObjectId, Int64
|
||||||
|
|
||||||
from mongoengine.errors import ValidationError
|
from mongoengine.errors import ValidationError
|
||||||
from mongoengine.python_support import (PY3, bin_type, txt_type,
|
from mongoengine.python_support import (PY3, bin_type, txt_type,
|
||||||
@ -227,6 +227,9 @@ class LongField(BaseField):
|
|||||||
pass
|
pass
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def to_mongo(self, value, **kwargs):
|
||||||
|
return Int64(value)
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
try:
|
try:
|
||||||
value = long(value)
|
value = long(value)
|
||||||
|
@ -21,6 +21,7 @@ except ImportError:
|
|||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from bson import Binary, DBRef, ObjectId
|
from bson import Binary, DBRef, ObjectId
|
||||||
|
from bson.int64 import Int64
|
||||||
|
|
||||||
from mongoengine import *
|
from mongoengine import *
|
||||||
from mongoengine.connection import get_db
|
from mongoengine.connection import get_db
|
||||||
@ -4114,6 +4115,19 @@ class EmbeddedDocumentListFieldTestCase(unittest.TestCase):
|
|||||||
self.assertTrue(hasattr(CustomData.c_field, 'custom_data'))
|
self.assertTrue(hasattr(CustomData.c_field, 'custom_data'))
|
||||||
self.assertEqual(custom_data['a'], CustomData.c_field.custom_data['a'])
|
self.assertEqual(custom_data['a'], CustomData.c_field.custom_data['a'])
|
||||||
|
|
||||||
|
def test_long_field_is_stored_as_long(self):
|
||||||
|
"""
|
||||||
|
Tests that long fields are stored as long in mongo, even if long value
|
||||||
|
is small enough to be an int.
|
||||||
|
"""
|
||||||
|
class TestDocument(Document):
|
||||||
|
some_long = LongField()
|
||||||
|
|
||||||
|
doc = TestDocument(some_long=42).save()
|
||||||
|
db = get_db()
|
||||||
|
self.assertTrue(isinstance(get_db().test_document.find()[0]['some_long'], Int64))
|
||||||
|
self.assertTrue(isinstance(doc.some_long, (int, long,)))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user