Fix tests and imports. issue #1253

This commit is contained in:
Gilb's 2016-03-15 20:50:34 +01:00
parent 87a2358a65
commit d651d0d472
2 changed files with 23 additions and 15 deletions

View File

@ -19,7 +19,12 @@ else:
import pymongo
import gridfs
from bson import Binary, DBRef, SON, ObjectId, Int64
from bson import Binary, DBRef, SON, ObjectId
try:
from bson.int64 import Int64
except ImportError:
Int64 = long
from mongoengine.errors import ValidationError
from mongoengine.python_support import (PY3, bin_type, txt_type,

View File

@ -21,7 +21,10 @@ except ImportError:
from decimal import Decimal
from bson import Binary, DBRef, ObjectId
try:
from bson.int64 import Int64
except ImportError:
Int64 = long
from mongoengine import *
from mongoengine.connection import get_db
@ -3606,6 +3609,19 @@ class FieldTest(unittest.TestCase):
self.assertRaises(FieldDoesNotExist, test)
def test_long_field_is_considered_as_int64(self):
"""
Tests that long fields are stored as long in mongo, even if long value
is small enough to be an int.
"""
class TestLongFieldConsideredAsInt64(Document):
some_long = LongField()
doc = TestLongFieldConsideredAsInt64(some_long=42).save()
db = get_db()
self.assertTrue(isinstance(db.test_long_field_considered_as_int64.find()[0]['some_long'], Int64))
self.assertTrue(isinstance(doc.some_long, (int, long,)))
class EmbeddedDocumentListFieldTestCase(unittest.TestCase):
@ -4115,19 +4131,6 @@ 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(db.test_document.find()[0]['some_long'], Int64))
self.assertTrue(isinstance(doc.some_long, (int, long,)))
if __name__ == '__main__':
unittest.main()