Compare commits
1 Commits
batch-size
...
simpler-in
Author | SHA1 | Date | |
---|---|---|---|
|
ea82cb80f6 |
@@ -438,7 +438,7 @@ class StrictDict(object):
|
|||||||
__slots__ = allowed_keys_tuple
|
__slots__ = allowed_keys_tuple
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "{%s}" % ', '.join('"{0!s}": {1!r}'.format(k, v) for k, v in self.items())
|
return "{%s}" % ', '.join('"{0!s}": {0!r}'.format(k) for k in self.iterkeys())
|
||||||
|
|
||||||
cls._classes[allowed_keys] = SpecificStrictDict
|
cls._classes[allowed_keys] = SpecificStrictDict
|
||||||
return cls._classes[allowed_keys]
|
return cls._classes[allowed_keys]
|
||||||
|
@@ -121,7 +121,7 @@ class BaseDocument(object):
|
|||||||
else:
|
else:
|
||||||
self._data[key] = value
|
self._data[key] = value
|
||||||
|
|
||||||
# Set any get_<field>_display methods
|
# Set any get_fieldname_display methods
|
||||||
self.__set_field_display()
|
self.__set_field_display()
|
||||||
|
|
||||||
if self._dynamic:
|
if self._dynamic:
|
||||||
@@ -1005,18 +1005,19 @@ class BaseDocument(object):
|
|||||||
return '.'.join(parts)
|
return '.'.join(parts)
|
||||||
|
|
||||||
def __set_field_display(self):
|
def __set_field_display(self):
|
||||||
"""For each field that specifies choices, create a
|
"""Dynamically set the display value for a field with choices"""
|
||||||
get_<field>_display method.
|
for attr_name, field in self._fields.items():
|
||||||
"""
|
if field.choices:
|
||||||
fields_with_choices = [(n, f) for n, f in self._fields.items()
|
if self._dynamic:
|
||||||
if f.choices]
|
obj = self
|
||||||
for attr_name, field in fields_with_choices:
|
else:
|
||||||
setattr(self,
|
obj = type(self)
|
||||||
'get_%s_display' % attr_name,
|
setattr(obj,
|
||||||
partial(self.__get_field_display, field=field))
|
'get_%s_display' % attr_name,
|
||||||
|
partial(self.__get_field_display, field=field))
|
||||||
|
|
||||||
def __get_field_display(self, field):
|
def __get_field_display(self, field):
|
||||||
"""Return the display value for a choice field"""
|
"""Returns the display value for a choice field"""
|
||||||
value = getattr(self, field.name)
|
value = getattr(self, field.name)
|
||||||
if field.choices and isinstance(field.choices[0], (list, tuple)):
|
if field.choices and isinstance(field.choices[0], (list, tuple)):
|
||||||
return dict(field.choices).get(value, value)
|
return dict(field.choices).get(value, value)
|
||||||
|
@@ -577,7 +577,7 @@ class EmbeddedDocumentField(BaseField):
|
|||||||
return self.document_type._fields.get(member_name)
|
return self.document_type._fields.get(member_name)
|
||||||
|
|
||||||
def prepare_query_value(self, op, value):
|
def prepare_query_value(self, op, value):
|
||||||
if value is not None and not isinstance(value, self.document_type):
|
if not isinstance(value, self.document_type):
|
||||||
value = self.document_type._from_son(value)
|
value = self.document_type._from_son(value)
|
||||||
super(EmbeddedDocumentField, self).prepare_query_value(op, value)
|
super(EmbeddedDocumentField, self).prepare_query_value(op, value)
|
||||||
return self.to_mongo(value)
|
return self.to_mongo(value)
|
||||||
|
@@ -82,7 +82,6 @@ class BaseQuerySet(object):
|
|||||||
self._limit = None
|
self._limit = None
|
||||||
self._skip = None
|
self._skip = None
|
||||||
self._hint = -1 # Using -1 as None is a valid value for hint
|
self._hint = -1 # Using -1 as None is a valid value for hint
|
||||||
self._batch_size = None
|
|
||||||
self.only_fields = []
|
self.only_fields = []
|
||||||
self._max_time_ms = None
|
self._max_time_ms = None
|
||||||
|
|
||||||
@@ -782,19 +781,6 @@ class BaseQuerySet(object):
|
|||||||
queryset._hint = index
|
queryset._hint = index
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
def batch_size(self, size):
|
|
||||||
"""Limit the number of documents returned in a single batch (each
|
|
||||||
batch requires a round trip to the server).
|
|
||||||
|
|
||||||
See http://api.mongodb.com/python/current/api/pymongo/cursor.html#pymongo.cursor.Cursor.batch_size
|
|
||||||
for details.
|
|
||||||
|
|
||||||
:param size: desired size of each batch.
|
|
||||||
"""
|
|
||||||
queryset = self.clone()
|
|
||||||
queryset._batch_size = size
|
|
||||||
return queryset
|
|
||||||
|
|
||||||
def distinct(self, field):
|
def distinct(self, field):
|
||||||
"""Return a list of distinct values for a given field.
|
"""Return a list of distinct values for a given field.
|
||||||
|
|
||||||
@@ -947,14 +933,6 @@ class BaseQuerySet(object):
|
|||||||
queryset._ordering = queryset._get_order_by(keys)
|
queryset._ordering = queryset._get_order_by(keys)
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
def comment(self, text):
|
|
||||||
"""Add a comment to the query.
|
|
||||||
|
|
||||||
See https://docs.mongodb.com/manual/reference/method/cursor.comment/#cursor.comment
|
|
||||||
for details.
|
|
||||||
"""
|
|
||||||
return self._chainable_method("comment", text)
|
|
||||||
|
|
||||||
def explain(self, format=False):
|
def explain(self, format=False):
|
||||||
"""Return an explain plan record for the
|
"""Return an explain plan record for the
|
||||||
:class:`~mongoengine.queryset.QuerySet`\ 's cursor.
|
:class:`~mongoengine.queryset.QuerySet`\ 's cursor.
|
||||||
@@ -1481,9 +1459,6 @@ class BaseQuerySet(object):
|
|||||||
if self._hint != -1:
|
if self._hint != -1:
|
||||||
self._cursor_obj.hint(self._hint)
|
self._cursor_obj.hint(self._hint)
|
||||||
|
|
||||||
if self._batch_size is not None:
|
|
||||||
self._cursor_obj.batch_size(self._batch_size)
|
|
||||||
|
|
||||||
return self._cursor_obj
|
return self._cursor_obj
|
||||||
|
|
||||||
def __deepcopy__(self, memo):
|
def __deepcopy__(self, memo):
|
||||||
|
@@ -1047,7 +1047,7 @@ class FieldTest(unittest.TestCase):
|
|||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
def test_list_assignment(self):
|
def test_list_assignment(self):
|
||||||
"""Ensure that list field element assignment and slicing work
|
"""Ensure that list field element assignment and slicing work
|
||||||
"""
|
"""
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
info = ListField()
|
info = ListField()
|
||||||
@@ -1057,12 +1057,12 @@ class FieldTest(unittest.TestCase):
|
|||||||
post = BlogPost()
|
post = BlogPost()
|
||||||
post.info = ['e1', 'e2', 3, '4', 5]
|
post.info = ['e1', 'e2', 3, '4', 5]
|
||||||
post.save()
|
post.save()
|
||||||
|
|
||||||
post.info[0] = 1
|
post.info[0] = 1
|
||||||
post.save()
|
post.save()
|
||||||
post.reload()
|
post.reload()
|
||||||
self.assertEqual(post.info[0], 1)
|
self.assertEqual(post.info[0], 1)
|
||||||
|
|
||||||
post.info[1:3] = ['n2', 'n3']
|
post.info[1:3] = ['n2', 'n3']
|
||||||
post.save()
|
post.save()
|
||||||
post.reload()
|
post.reload()
|
||||||
@@ -1209,7 +1209,7 @@ class FieldTest(unittest.TestCase):
|
|||||||
self.assertEqual(simple.widgets, [4])
|
self.assertEqual(simple.widgets, [4])
|
||||||
|
|
||||||
def test_list_field_with_negative_indices(self):
|
def test_list_field_with_negative_indices(self):
|
||||||
|
|
||||||
class Simple(Document):
|
class Simple(Document):
|
||||||
widgets = ListField()
|
widgets = ListField()
|
||||||
|
|
||||||
@@ -1823,7 +1823,7 @@ class FieldTest(unittest.TestCase):
|
|||||||
'parent': "50a234ea469ac1eda42d347d"})
|
'parent': "50a234ea469ac1eda42d347d"})
|
||||||
mongoed = p1.to_mongo()
|
mongoed = p1.to_mongo()
|
||||||
self.assertTrue(isinstance(mongoed['parent'], ObjectId))
|
self.assertTrue(isinstance(mongoed['parent'], ObjectId))
|
||||||
|
|
||||||
def test_cached_reference_field_get_and_save(self):
|
def test_cached_reference_field_get_and_save(self):
|
||||||
"""
|
"""
|
||||||
Tests #1047: CachedReferenceField creates DBRefs on to_python, but can't save them on to_mongo
|
Tests #1047: CachedReferenceField creates DBRefs on to_python, but can't save them on to_mongo
|
||||||
@@ -1835,11 +1835,11 @@ class FieldTest(unittest.TestCase):
|
|||||||
class Ocorrence(Document):
|
class Ocorrence(Document):
|
||||||
person = StringField()
|
person = StringField()
|
||||||
animal = CachedReferenceField(Animal)
|
animal = CachedReferenceField(Animal)
|
||||||
|
|
||||||
Animal.drop_collection()
|
Animal.drop_collection()
|
||||||
Ocorrence.drop_collection()
|
Ocorrence.drop_collection()
|
||||||
|
|
||||||
Ocorrence(person="testte",
|
Ocorrence(person="testte",
|
||||||
animal=Animal(name="Leopard", tag="heavy").save()).save()
|
animal=Animal(name="Leopard", tag="heavy").save()).save()
|
||||||
p = Ocorrence.objects.get()
|
p = Ocorrence.objects.get()
|
||||||
p.person = 'new_testte'
|
p.person = 'new_testte'
|
||||||
@@ -3001,32 +3001,28 @@ class FieldTest(unittest.TestCase):
|
|||||||
('S', 'Small'), ('M', 'Medium'), ('L', 'Large'),
|
('S', 'Small'), ('M', 'Medium'), ('L', 'Large'),
|
||||||
('XL', 'Extra Large'), ('XXL', 'Extra Extra Large')))
|
('XL', 'Extra Large'), ('XXL', 'Extra Extra Large')))
|
||||||
style = StringField(max_length=3, choices=(
|
style = StringField(max_length=3, choices=(
|
||||||
('S', 'Small'), ('B', 'Baggy'), ('W', 'Wide')), default='W')
|
('S', 'Small'), ('B', 'Baggy'), ('W', 'wide')), default='S')
|
||||||
|
|
||||||
Shirt.drop_collection()
|
Shirt.drop_collection()
|
||||||
|
|
||||||
shirt1 = Shirt()
|
shirt = Shirt()
|
||||||
shirt2 = Shirt()
|
|
||||||
|
|
||||||
# Make sure get_<field>_display returns the default value (or None)
|
self.assertEqual(shirt.get_size_display(), None)
|
||||||
self.assertEqual(shirt1.get_size_display(), None)
|
self.assertEqual(shirt.get_style_display(), 'Small')
|
||||||
self.assertEqual(shirt1.get_style_display(), 'Wide')
|
|
||||||
|
|
||||||
shirt1.size = 'XXL'
|
shirt.size = "XXL"
|
||||||
shirt1.style = 'B'
|
shirt.style = "B"
|
||||||
shirt2.size = 'M'
|
self.assertEqual(shirt.get_size_display(), 'Extra Extra Large')
|
||||||
shirt2.style = 'S'
|
self.assertEqual(shirt.get_style_display(), 'Baggy')
|
||||||
self.assertEqual(shirt1.get_size_display(), 'Extra Extra Large')
|
|
||||||
self.assertEqual(shirt1.get_style_display(), 'Baggy')
|
|
||||||
self.assertEqual(shirt2.get_size_display(), 'Medium')
|
|
||||||
self.assertEqual(shirt2.get_style_display(), 'Small')
|
|
||||||
|
|
||||||
# Set as Z - an invalid choice
|
# Set as Z - an invalid choice
|
||||||
shirt1.size = 'Z'
|
shirt.size = "Z"
|
||||||
shirt1.style = 'Z'
|
shirt.style = "Z"
|
||||||
self.assertEqual(shirt1.get_size_display(), 'Z')
|
self.assertEqual(shirt.get_size_display(), 'Z')
|
||||||
self.assertEqual(shirt1.get_style_display(), 'Z')
|
self.assertEqual(shirt.get_style_display(), 'Z')
|
||||||
self.assertRaises(ValidationError, shirt1.validate)
|
self.assertRaises(ValidationError, shirt.validate)
|
||||||
|
|
||||||
|
Shirt.drop_collection()
|
||||||
|
|
||||||
def test_simple_choices_validation(self):
|
def test_simple_choices_validation(self):
|
||||||
"""Ensure that value is in a container of allowed values.
|
"""Ensure that value is in a container of allowed values.
|
||||||
|
@@ -337,36 +337,9 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
query = query.filter(boolfield=True)
|
query = query.filter(boolfield=True)
|
||||||
self.assertEqual(query.count(), 1)
|
self.assertEqual(query.count(), 1)
|
||||||
|
|
||||||
def test_batch_size(self):
|
|
||||||
"""Ensure that batch_size works."""
|
|
||||||
class A(Document):
|
|
||||||
s = StringField()
|
|
||||||
|
|
||||||
A.drop_collection()
|
|
||||||
|
|
||||||
for i in range(100):
|
|
||||||
A.objects.create(s=str(i))
|
|
||||||
|
|
||||||
# test iterating over the result set
|
|
||||||
cnt = 0
|
|
||||||
for a in A.objects.batch_size(10):
|
|
||||||
cnt += 1
|
|
||||||
self.assertEqual(cnt, 100)
|
|
||||||
|
|
||||||
# test chaining
|
|
||||||
qs = A.objects.all()
|
|
||||||
qs = qs.limit(10).batch_size(20).skip(91)
|
|
||||||
cnt = 0
|
|
||||||
for a in qs:
|
|
||||||
cnt += 1
|
|
||||||
self.assertEqual(cnt, 9)
|
|
||||||
|
|
||||||
# test invalid batch size
|
|
||||||
qs = A.objects.batch_size(-1)
|
|
||||||
self.assertRaises(ValueError, lambda: list(qs))
|
|
||||||
|
|
||||||
def test_update_write_concern(self):
|
def test_update_write_concern(self):
|
||||||
"""Test that passing write_concern works"""
|
"""Test that passing write_concern works"""
|
||||||
|
|
||||||
self.Person.drop_collection()
|
self.Person.drop_collection()
|
||||||
|
|
||||||
write_concern = {"fsync": True}
|
write_concern = {"fsync": True}
|
||||||
@@ -1266,8 +1239,7 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
self.assertFalse('$orderby' in q.get_ops()[0]['query'])
|
self.assertFalse('$orderby' in q.get_ops()[0]['query'])
|
||||||
|
|
||||||
def test_find_embedded(self):
|
def test_find_embedded(self):
|
||||||
"""Ensure that an embedded document is properly returned from
|
"""Ensure that an embedded document is properly returned from a query.
|
||||||
a query.
|
|
||||||
"""
|
"""
|
||||||
class User(EmbeddedDocument):
|
class User(EmbeddedDocument):
|
||||||
name = StringField()
|
name = StringField()
|
||||||
@@ -1278,31 +1250,16 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
|
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
BlogPost.objects.create(
|
post = BlogPost(content='Had a good coffee today...')
|
||||||
author=User(name='Test User'),
|
post.author = User(name='Test User')
|
||||||
content='Had a good coffee today...'
|
post.save()
|
||||||
)
|
|
||||||
|
|
||||||
result = BlogPost.objects.first()
|
result = BlogPost.objects.first()
|
||||||
self.assertTrue(isinstance(result.author, User))
|
self.assertTrue(isinstance(result.author, User))
|
||||||
self.assertEqual(result.author.name, 'Test User')
|
self.assertEqual(result.author.name, 'Test User')
|
||||||
|
|
||||||
def test_find_empty_embedded(self):
|
|
||||||
"""Ensure that you can save and find an empty embedded document."""
|
|
||||||
class User(EmbeddedDocument):
|
|
||||||
name = StringField()
|
|
||||||
|
|
||||||
class BlogPost(Document):
|
|
||||||
content = StringField()
|
|
||||||
author = EmbeddedDocumentField(User)
|
|
||||||
|
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
BlogPost.objects.create(content='Anonymous post...')
|
|
||||||
|
|
||||||
result = BlogPost.objects.get(author=None)
|
|
||||||
self.assertEqual(result.author, None)
|
|
||||||
|
|
||||||
def test_find_dict_item(self):
|
def test_find_dict_item(self):
|
||||||
"""Ensure that DictField items may be found.
|
"""Ensure that DictField items may be found.
|
||||||
"""
|
"""
|
||||||
@@ -2242,21 +2199,6 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
a.author.name for a in Author.objects.order_by('-author__age')]
|
a.author.name for a in Author.objects.order_by('-author__age')]
|
||||||
self.assertEqual(names, ['User A', 'User B', 'User C'])
|
self.assertEqual(names, ['User A', 'User B', 'User C'])
|
||||||
|
|
||||||
def test_comment(self):
|
|
||||||
"""Make sure adding a comment to the query works."""
|
|
||||||
class User(Document):
|
|
||||||
age = IntField()
|
|
||||||
|
|
||||||
with db_ops_tracker() as q:
|
|
||||||
adult = (User.objects.filter(age__gte=18)
|
|
||||||
.comment('looking for an adult')
|
|
||||||
.first())
|
|
||||||
ops = q.get_ops()
|
|
||||||
self.assertEqual(len(ops), 1)
|
|
||||||
op = ops[0]
|
|
||||||
self.assertEqual(op['query']['$query'], {'age': {'$gte': 18}})
|
|
||||||
self.assertEqual(op['query']['$comment'], 'looking for an adult')
|
|
||||||
|
|
||||||
def test_map_reduce(self):
|
def test_map_reduce(self):
|
||||||
"""Ensure map/reduce is both mapping and reducing.
|
"""Ensure map/reduce is both mapping and reducing.
|
||||||
"""
|
"""
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
from mongoengine.base.datastructures import StrictDict, SemiStrictDict
|
||||||
from mongoengine.base.datastructures import StrictDict, SemiStrictDict
|
|
||||||
|
|
||||||
|
|
||||||
class TestStrictDict(unittest.TestCase):
|
class TestStrictDict(unittest.TestCase):
|
||||||
@@ -14,17 +13,9 @@ class TestStrictDict(unittest.TestCase):
|
|||||||
d = self.dtype(a=1, b=1, c=1)
|
d = self.dtype(a=1, b=1, c=1)
|
||||||
self.assertEqual((d.a, d.b, d.c), (1, 1, 1))
|
self.assertEqual((d.a, d.b, d.c), (1, 1, 1))
|
||||||
|
|
||||||
def test_repr(self):
|
|
||||||
d = self.dtype(a=1, b=2, c=3)
|
|
||||||
self.assertEqual(repr(d), '{"a": 1, "b": 2, "c": 3}')
|
|
||||||
|
|
||||||
# make sure quotes are escaped properly
|
|
||||||
d = self.dtype(a='"', b="'", c="")
|
|
||||||
self.assertEqual(repr(d), '{"a": \'"\', "b": "\'", "c": \'\'}')
|
|
||||||
|
|
||||||
def test_init_fails_on_nonexisting_attrs(self):
|
def test_init_fails_on_nonexisting_attrs(self):
|
||||||
self.assertRaises(AttributeError, lambda: self.dtype(a=1, b=2, d=3))
|
self.assertRaises(AttributeError, lambda: self.dtype(a=1, b=2, d=3))
|
||||||
|
|
||||||
def test_eq(self):
|
def test_eq(self):
|
||||||
d = self.dtype(a=1, b=1, c=1)
|
d = self.dtype(a=1, b=1, c=1)
|
||||||
dd = self.dtype(a=1, b=1, c=1)
|
dd = self.dtype(a=1, b=1, c=1)
|
||||||
@@ -33,7 +24,7 @@ class TestStrictDict(unittest.TestCase):
|
|||||||
g = self.strict_dict_class(("a", "b", "c", "d"))(a=1, b=1, c=1, d=1)
|
g = self.strict_dict_class(("a", "b", "c", "d"))(a=1, b=1, c=1, d=1)
|
||||||
h = self.strict_dict_class(("a", "c", "b"))(a=1, b=1, c=1)
|
h = self.strict_dict_class(("a", "c", "b"))(a=1, b=1, c=1)
|
||||||
i = self.strict_dict_class(("a", "c", "b"))(a=1, b=1, c=2)
|
i = self.strict_dict_class(("a", "c", "b"))(a=1, b=1, c=2)
|
||||||
|
|
||||||
self.assertEqual(d, dd)
|
self.assertEqual(d, dd)
|
||||||
self.assertNotEqual(d, e)
|
self.assertNotEqual(d, e)
|
||||||
self.assertNotEqual(d, f)
|
self.assertNotEqual(d, f)
|
||||||
@@ -47,19 +38,19 @@ class TestStrictDict(unittest.TestCase):
|
|||||||
d.a = 1
|
d.a = 1
|
||||||
self.assertEqual(d.a, 1)
|
self.assertEqual(d.a, 1)
|
||||||
self.assertRaises(AttributeError, lambda: d.b)
|
self.assertRaises(AttributeError, lambda: d.b)
|
||||||
|
|
||||||
def test_setattr_raises_on_nonexisting_attr(self):
|
def test_setattr_raises_on_nonexisting_attr(self):
|
||||||
d = self.dtype()
|
d = self.dtype()
|
||||||
|
|
||||||
def _f():
|
def _f():
|
||||||
d.x = 1
|
d.x = 1
|
||||||
self.assertRaises(AttributeError, _f)
|
self.assertRaises(AttributeError, _f)
|
||||||
|
|
||||||
def test_setattr_getattr_special(self):
|
def test_setattr_getattr_special(self):
|
||||||
d = self.strict_dict_class(["items"])
|
d = self.strict_dict_class(["items"])
|
||||||
d.items = 1
|
d.items = 1
|
||||||
self.assertEqual(d.items, 1)
|
self.assertEqual(d.items, 1)
|
||||||
|
|
||||||
def test_get(self):
|
def test_get(self):
|
||||||
d = self.dtype(a=1)
|
d = self.dtype(a=1)
|
||||||
self.assertEqual(d.get('a'), 1)
|
self.assertEqual(d.get('a'), 1)
|
||||||
@@ -97,7 +88,7 @@ class TestSemiSrictDict(TestStrictDict):
|
|||||||
def test_init_succeeds_with_nonexisting_attrs(self):
|
def test_init_succeeds_with_nonexisting_attrs(self):
|
||||||
d = self.dtype(a=1, b=1, c=1, x=2)
|
d = self.dtype(a=1, b=1, c=1, x=2)
|
||||||
self.assertEqual((d.a, d.b, d.c, d.x), (1, 1, 1, 2))
|
self.assertEqual((d.a, d.b, d.c, d.x), (1, 1, 1, 2))
|
||||||
|
|
||||||
def test_iter_with_nonexisting_attrs(self):
|
def test_iter_with_nonexisting_attrs(self):
|
||||||
d = self.dtype(a=1, b=1, c=1, x=2)
|
d = self.dtype(a=1, b=1, c=1, x=2)
|
||||||
self.assertEqual(list(d), ['a', 'b', 'c', 'x'])
|
self.assertEqual(list(d), ['a', 'b', 'c', 'x'])
|
||||||
|
Reference in New Issue
Block a user