Compare commits
4 Commits
fix-strict
...
batch-size
Author | SHA1 | Date | |
---|---|---|---|
|
34ba527e6d | ||
|
ea9027755f | ||
|
43668a93a2 | ||
|
15714ef855 |
@@ -438,7 +438,7 @@ class StrictDict(object):
|
||||
__slots__ = allowed_keys_tuple
|
||||
|
||||
def __repr__(self):
|
||||
return "{%s}" % ', '.join('"{0!s}": {0!r}'.format(k) for k in self.iterkeys())
|
||||
return "{%s}" % ', '.join('"{0!s}": {1!r}'.format(k, v) for k, v in self.items())
|
||||
|
||||
cls._classes[allowed_keys] = SpecificStrictDict
|
||||
return cls._classes[allowed_keys]
|
||||
|
@@ -82,6 +82,7 @@ class BaseQuerySet(object):
|
||||
self._limit = None
|
||||
self._skip = None
|
||||
self._hint = -1 # Using -1 as None is a valid value for hint
|
||||
self._batch_size = None
|
||||
self.only_fields = []
|
||||
self._max_time_ms = None
|
||||
|
||||
@@ -781,6 +782,19 @@ class BaseQuerySet(object):
|
||||
queryset._hint = index
|
||||
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):
|
||||
"""Return a list of distinct values for a given field.
|
||||
|
||||
@@ -1467,6 +1481,9 @@ class BaseQuerySet(object):
|
||||
if self._hint != -1:
|
||||
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
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
|
@@ -337,6 +337,34 @@ class QuerySetTest(unittest.TestCase):
|
||||
query = query.filter(boolfield=True)
|
||||
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):
|
||||
"""Test that passing write_concern works"""
|
||||
self.Person.drop_collection()
|
||||
|
@@ -1,5 +1,6 @@
|
||||
import unittest
|
||||
from mongoengine.base.datastructures import StrictDict, SemiStrictDict
|
||||
|
||||
from mongoengine.base.datastructures import StrictDict, SemiStrictDict
|
||||
|
||||
|
||||
class TestStrictDict(unittest.TestCase):
|
||||
@@ -13,9 +14,17 @@ class TestStrictDict(unittest.TestCase):
|
||||
d = self.dtype(a=1, b=1, c=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):
|
||||
self.assertRaises(AttributeError, lambda: self.dtype(a=1, b=2, d=3))
|
||||
|
||||
|
||||
def test_eq(self):
|
||||
d = self.dtype(a=1, b=1, c=1)
|
||||
dd = self.dtype(a=1, b=1, c=1)
|
||||
@@ -24,7 +33,7 @@ class TestStrictDict(unittest.TestCase):
|
||||
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)
|
||||
i = self.strict_dict_class(("a", "c", "b"))(a=1, b=1, c=2)
|
||||
|
||||
|
||||
self.assertEqual(d, dd)
|
||||
self.assertNotEqual(d, e)
|
||||
self.assertNotEqual(d, f)
|
||||
@@ -38,19 +47,19 @@ class TestStrictDict(unittest.TestCase):
|
||||
d.a = 1
|
||||
self.assertEqual(d.a, 1)
|
||||
self.assertRaises(AttributeError, lambda: d.b)
|
||||
|
||||
|
||||
def test_setattr_raises_on_nonexisting_attr(self):
|
||||
d = self.dtype()
|
||||
|
||||
def _f():
|
||||
d.x = 1
|
||||
self.assertRaises(AttributeError, _f)
|
||||
|
||||
|
||||
def test_setattr_getattr_special(self):
|
||||
d = self.strict_dict_class(["items"])
|
||||
d.items = 1
|
||||
self.assertEqual(d.items, 1)
|
||||
|
||||
|
||||
def test_get(self):
|
||||
d = self.dtype(a=1)
|
||||
self.assertEqual(d.get('a'), 1)
|
||||
@@ -88,7 +97,7 @@ class TestSemiSrictDict(TestStrictDict):
|
||||
def test_init_succeeds_with_nonexisting_attrs(self):
|
||||
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))
|
||||
|
||||
|
||||
def test_iter_with_nonexisting_attrs(self):
|
||||
d = self.dtype(a=1, b=1, c=1, x=2)
|
||||
self.assertEqual(list(d), ['a', 'b', 'c', 'x'])
|
||||
|
Reference in New Issue
Block a user