Skip to content

Commit

Permalink
Fix tests and imports. issue #1253
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbsgilbs committed Mar 18, 2016
1 parent 87a2358 commit d651d0d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
7 changes: 6 additions & 1 deletion mongoengine/fields.py
Expand Up @@ -19,7 +19,12 @@

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,
Expand Down
31 changes: 17 additions & 14 deletions tests/fields/fields.py
Expand Up @@ -21,7 +21,10 @@
from decimal import Decimal

from bson import Binary, DBRef, ObjectId
from bson.int64 import Int64
try:
from bson.int64 import Int64
except ImportError:
Int64 = long

from mongoengine import *
from mongoengine.connection import get_db
Expand Down Expand Up @@ -3606,6 +3609,19 @@ def test():

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):

Expand Down Expand Up @@ -4115,19 +4131,6 @@ class CustomData(Document):
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()

0 comments on commit d651d0d

Please sign in to comment.