Merge branch 'master' into 0.8
Conflicts: AUTHORS docs/changelog.rst mongoengine/__init__.py mongoengine/base.py mongoengine/fields.py python-mongoengine.spec tests/test_document.py tests/test_fields.py tests/test_queryset.py
This commit is contained in:
@@ -17,6 +17,7 @@ from mongoengine.errors import (NotRegistered, InvalidDocumentError,
|
||||
InvalidQueryError)
|
||||
from mongoengine.queryset import NULLIFY, Q
|
||||
from mongoengine.connection import get_db
|
||||
from mongoengine.base import get_document
|
||||
|
||||
TEST_IMAGE_PATH = os.path.join(os.path.dirname(__file__), 'mongoengine.png')
|
||||
|
||||
@@ -281,7 +282,6 @@ class InstanceTest(unittest.TestCase):
|
||||
|
||||
User.drop_collection()
|
||||
|
||||
|
||||
def test_document_not_registered(self):
|
||||
|
||||
class Place(Document):
|
||||
@@ -306,6 +306,19 @@ class InstanceTest(unittest.TestCase):
|
||||
print Place.objects.all()
|
||||
self.assertRaises(NotRegistered, query_without_importing_nice_place)
|
||||
|
||||
def test_document_registry_regressions(self):
|
||||
|
||||
class Location(Document):
|
||||
name = StringField()
|
||||
meta = {'allow_inheritance': True}
|
||||
|
||||
class Area(Location):
|
||||
location = ReferenceField('Location', dbref=True)
|
||||
|
||||
Location.drop_collection()
|
||||
|
||||
self.assertEquals(Area, get_document("Area"))
|
||||
self.assertEquals(Area, get_document("Location.Area"))
|
||||
|
||||
def test_creation(self):
|
||||
"""Ensure that document may be created using keyword arguments.
|
||||
|
||||
@@ -1118,6 +1118,16 @@ class FieldTest(unittest.TestCase):
|
||||
p = Person.objects.get(name="Ross")
|
||||
self.assertEqual(p.parent, p1)
|
||||
|
||||
def test_dbref_to_mongo(self):
|
||||
class Person(Document):
|
||||
name = StringField()
|
||||
parent = ReferenceField('self', dbref=False)
|
||||
|
||||
p1 = Person._from_son({'name': "Yakxxx",
|
||||
'parent': "50a234ea469ac1eda42d347d"})
|
||||
mongoed = p1.to_mongo()
|
||||
self.assertTrue(isinstance(mongoed['parent'], ObjectId))
|
||||
|
||||
def test_objectid_reference_fields(self):
|
||||
|
||||
class Person(Document):
|
||||
@@ -2216,6 +2226,29 @@ class FieldTest(unittest.TestCase):
|
||||
c = self.db['mongoengine.counters'].find_one({'_id': 'person.id'})
|
||||
self.assertEqual(c['next'], 10)
|
||||
|
||||
def test_embedded_sequence_field(self):
|
||||
class Comment(EmbeddedDocument):
|
||||
id = SequenceField()
|
||||
content = StringField(required=True)
|
||||
|
||||
class Post(Document):
|
||||
title = StringField(required=True)
|
||||
comments = ListField(EmbeddedDocumentField(Comment))
|
||||
|
||||
self.db['mongoengine.counters'].drop()
|
||||
Post.drop_collection()
|
||||
|
||||
Post(title="MongoEngine",
|
||||
comments=[Comment(content="NoSQL Rocks"),
|
||||
Comment(content="MongoEngine Rocks")]).save()
|
||||
import ipdb; ipdb.set_trace();
|
||||
c = self.db['mongoengine.counters'].find_one({'_id': 'comment.id'})
|
||||
self.assertEqual(c['next'], 2)
|
||||
post = Post.objects.first()
|
||||
self.assertEqual(1, post.comments[0].id)
|
||||
self.assertEqual(2, post.comments[1].id)
|
||||
|
||||
|
||||
def test_generic_embedded_document(self):
|
||||
class Car(EmbeddedDocument):
|
||||
name = StringField()
|
||||
@@ -2339,6 +2372,18 @@ class FieldTest(unittest.TestCase):
|
||||
post.comments[1].content = 'here we go'
|
||||
post.validate()
|
||||
|
||||
def test_email_field_honors_regex(self):
|
||||
class User(Document):
|
||||
email = EmailField(regex=r'\w+@example.com')
|
||||
|
||||
# Fails regex validation
|
||||
user = User(email='me@foo.com')
|
||||
self.assertRaises(ValidationError, user.validate)
|
||||
|
||||
# Passes regex validation
|
||||
user = User(email='me@example.com')
|
||||
self.assertTrue(user.validate() is None)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
@@ -625,6 +625,10 @@ class QuerySetTest(unittest.TestCase):
|
||||
|
||||
self.assertRaises(OperationError, throw_operation_error)
|
||||
|
||||
# Test can insert new doc
|
||||
new_post = Blog(title="code", id=ObjectId())
|
||||
Blog.objects.insert(new_post)
|
||||
|
||||
# test handles other classes being inserted
|
||||
def throw_operation_error_wrong_doc():
|
||||
class Author(Document):
|
||||
@@ -1967,6 +1971,22 @@ class QuerySetTest(unittest.TestCase):
|
||||
ages = [p.age for p in self.Person.objects.order_by('-name')]
|
||||
self.assertEqual(ages, [30, 40, 20])
|
||||
|
||||
def test_order_by_chaining(self):
|
||||
"""Ensure that an order_by query chains properly and allows .only()
|
||||
"""
|
||||
self.Person(name="User A", age=20).save()
|
||||
self.Person(name="User B", age=40).save()
|
||||
self.Person(name="User C", age=30).save()
|
||||
|
||||
only_age = self.Person.objects.order_by('-age').only('age')
|
||||
|
||||
names = [p.name for p in only_age]
|
||||
ages = [p.age for p in only_age]
|
||||
|
||||
# The .only('age') clause should mean that all names are None
|
||||
self.assertEqual(names, [None, None, None])
|
||||
self.assertEqual(ages, [40, 30, 20])
|
||||
|
||||
def test_confirm_order_by_reference_wont_work(self):
|
||||
"""Ordering by reference is not possible. Use map / reduce.. or
|
||||
denormalise"""
|
||||
@@ -3761,5 +3781,38 @@ class QueryFieldListTest(unittest.TestCase):
|
||||
Test.objects(test='foo').update_one(upsert=True, set__test='foo')
|
||||
self.assertTrue('_cls' in Test._collection.find_one())
|
||||
|
||||
def test_as_pymongo(self):
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
class User(Document):
|
||||
id = ObjectIdField('_id')
|
||||
name = StringField()
|
||||
age = IntField()
|
||||
price = DecimalField()
|
||||
|
||||
User.drop_collection()
|
||||
User(name="Bob Dole", age=89, price=Decimal('1.11')).save()
|
||||
User(name="Barack Obama", age=51, price=Decimal('2.22')).save()
|
||||
|
||||
users = User.objects.only('name', 'price').as_pymongo()
|
||||
results = list(users)
|
||||
self.assertTrue(isinstance(results[0], dict))
|
||||
self.assertTrue(isinstance(results[1], dict))
|
||||
self.assertEqual(results[0]['name'], 'Bob Dole')
|
||||
self.assertEqual(results[0]['price'], '1.11')
|
||||
self.assertEqual(results[1]['name'], 'Barack Obama')
|
||||
self.assertEqual(results[1]['price'], '2.22')
|
||||
|
||||
# Test coerce_types
|
||||
users = User.objects.only('name', 'price').as_pymongo(coerce_types=True)
|
||||
results = list(users)
|
||||
self.assertTrue(isinstance(results[0], dict))
|
||||
self.assertTrue(isinstance(results[1], dict))
|
||||
self.assertEqual(results[0]['name'], 'Bob Dole')
|
||||
self.assertEqual(results[0]['price'], Decimal('1.11'))
|
||||
self.assertEqual(results[1]['name'], 'Barack Obama')
|
||||
self.assertEqual(results[1]['price'], Decimal('2.22'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user