Merge branch 'single-work-op' of https://github.com/njoyce/mongoengine into 211
Conflicts: mongoengine/document.py tests/test_document.py
This commit is contained in:
commit
d90890c08e
@ -84,6 +84,17 @@ class BaseDocument(object):
|
|||||||
self._initialised = True
|
self._initialised = True
|
||||||
signals.post_init.send(self.__class__, document=self)
|
signals.post_init.send(self.__class__, document=self)
|
||||||
|
|
||||||
|
def __delattr__(self, *args, **kwargs):
|
||||||
|
"""Handle deletions of fields"""
|
||||||
|
field_name = args[0]
|
||||||
|
if field_name in self._fields:
|
||||||
|
default = self._fields[field_name].default
|
||||||
|
if callable(default):
|
||||||
|
default = default()
|
||||||
|
setattr(self, field_name, default)
|
||||||
|
else:
|
||||||
|
super(BaseDocument, self).__delattr__(*args, **kwargs)
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
# Handle dynamic data only if an initialised dynamic document
|
# Handle dynamic data only if an initialised dynamic document
|
||||||
if self._dynamic and not self._dynamic_lock:
|
if self._dynamic and not self._dynamic_lock:
|
||||||
|
@ -250,16 +250,16 @@ class Document(BaseDocument):
|
|||||||
return created
|
return created
|
||||||
|
|
||||||
upsert = self._created
|
upsert = self._created
|
||||||
|
update_query = {}
|
||||||
|
|
||||||
if updates:
|
if updates:
|
||||||
last_error = collection.update(select_dict,
|
update_query["$set"] = updates
|
||||||
{"$set": updates}, upsert=upsert, safe=safe,
|
|
||||||
**write_options)
|
|
||||||
created = is_new_object(last_error)
|
|
||||||
if removals:
|
if removals:
|
||||||
last_error = collection.update(select_dict,
|
update_query["$unset"] = removals
|
||||||
{"$unset": removals}, upsert=upsert, safe=safe,
|
if updates or removals:
|
||||||
**write_options)
|
last_error = collection.update(select_dict, update_query,
|
||||||
created = created or is_new_object(last_error)
|
upsert=upsert, safe=safe, **write_options)
|
||||||
|
created = is_new_object(last_error)
|
||||||
|
|
||||||
warn_cascade = not cascade and 'cascade' not in self._meta
|
warn_cascade = not cascade and 'cascade' not in self._meta
|
||||||
cascade = (self._meta.get('cascade', True)
|
cascade = (self._meta.get('cascade', True)
|
||||||
|
@ -18,7 +18,7 @@ from mongoengine.errors import (NotRegistered, InvalidDocumentError,
|
|||||||
from mongoengine.queryset import NULLIFY, Q
|
from mongoengine.queryset import NULLIFY, Q
|
||||||
from mongoengine.connection import get_db
|
from mongoengine.connection import get_db
|
||||||
from mongoengine.base import get_document
|
from mongoengine.base import get_document
|
||||||
from mongoengine.context_managers import switch_db
|
from mongoengine.context_managers import switch_db, query_counter
|
||||||
from mongoengine import signals
|
from mongoengine import signals
|
||||||
|
|
||||||
TEST_IMAGE_PATH = os.path.join(os.path.dirname(__file__),
|
TEST_IMAGE_PATH = os.path.join(os.path.dirname(__file__),
|
||||||
@ -971,6 +971,28 @@ class InstanceTest(unittest.TestCase):
|
|||||||
self.assertEqual(person.age, 21)
|
self.assertEqual(person.age, 21)
|
||||||
self.assertEqual(person.active, False)
|
self.assertEqual(person.active, False)
|
||||||
|
|
||||||
|
def test_set_unset_one_operation(self):
|
||||||
|
"""Ensure that $set and $unset actions are performed in the same
|
||||||
|
operation.
|
||||||
|
"""
|
||||||
|
class FooBar(Document):
|
||||||
|
foo = StringField(default=None)
|
||||||
|
bar = StringField(default=None)
|
||||||
|
|
||||||
|
FooBar.drop_collection()
|
||||||
|
|
||||||
|
# write an entity with a single prop
|
||||||
|
foo = FooBar(foo='foo').save()
|
||||||
|
|
||||||
|
self.assertEqual(foo.foo, 'foo')
|
||||||
|
del foo.foo
|
||||||
|
foo.bar = 'bar'
|
||||||
|
|
||||||
|
with query_counter() as q:
|
||||||
|
self.assertEqual(0, q)
|
||||||
|
foo.save()
|
||||||
|
self.assertEqual(1, q)
|
||||||
|
|
||||||
def test_save_only_changed_fields_recursive(self):
|
def test_save_only_changed_fields_recursive(self):
|
||||||
"""Ensure save only sets / unsets changed fields
|
"""Ensure save only sets / unsets changed fields
|
||||||
"""
|
"""
|
||||||
|
@ -260,7 +260,8 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
name='Test User', write_options=write_options)
|
name='Test User', write_options=write_options)
|
||||||
author.save(write_options=write_options)
|
author.save(write_options=write_options)
|
||||||
|
|
||||||
self.Person.objects.update(set__name='Ross', write_options=write_options)
|
self.Person.objects.update(set__name='Ross',
|
||||||
|
write_options=write_options)
|
||||||
|
|
||||||
author = self.Person.objects.first()
|
author = self.Person.objects.first()
|
||||||
self.assertEqual(author.name, 'Ross')
|
self.assertEqual(author.name, 'Ross')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user