Fix long fields stored as int32 in Python 3. issue #1253

This commit is contained in:
Gilbert Gilb's 2016-03-15 14:34:22 +01:00 committed by Gilb's
parent c6cc0133b3
commit 7cc1a4eba0
3 changed files with 19 additions and 1 deletions

View File

@ -235,3 +235,4 @@ that much better:
* Steven Rossiter (https://github.com/BeardedSteve)
* Luo Peng (https://github.com/RussellLuo)
* Bryan Bennett (https://github.com/bbenne10)
* Gilb's Gilb's (https://github.com/gilbsgilbs)

View File

@ -19,7 +19,7 @@ else:
import pymongo
import gridfs
from bson import Binary, DBRef, SON, ObjectId
from bson import Binary, DBRef, SON, ObjectId, Int64
from mongoengine.errors import ValidationError
from mongoengine.python_support import (PY3, bin_type, txt_type,
@ -227,6 +227,9 @@ class LongField(BaseField):
pass
return value
def to_mongo(self, value, **kwargs):
return Int64(value)
def validate(self, value):
try:
value = long(value)

View File

@ -21,6 +21,7 @@ except ImportError:
from decimal import Decimal
from bson import Binary, DBRef, ObjectId
from bson.int64 import Int64
from mongoengine import *
from mongoengine.connection import get_db
@ -4114,6 +4115,19 @@ class EmbeddedDocumentListFieldTestCase(unittest.TestCase):
self.assertTrue(hasattr(CustomData.c_field, 'custom_data'))
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__':
unittest.main()