forked from MongoEngine/mongoengine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_pickable.py
More file actions
63 lines (43 loc) · 1.74 KB
/
Copy pathtest_pickable.py
File metadata and controls
63 lines (43 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pickle
from mongoengine import Document, IntField, StringField
from mongoengine.asynchronous import async_disconnect
from mongoengine.registry import _CollectionRegistry
from tests.asynchronous.utils import MongoDBAsyncTestCase, reset_async_connections
class Person(Document):
name = StringField()
age = IntField()
class TestQuerysetPickable(MongoDBAsyncTestCase):
"""
Test for adding pickling support for QuerySet instances
See issue https://github.com/MongoEngine/mongoengine/issues/442
"""
async def asyncSetUp(self):
await super().asyncSetUp()
self.john = await Person.aobjects.create(name="John", age=21)
async def asyncTearDown(self):
await Person.adrop_collection()
await async_disconnect()
await reset_async_connections()
_CollectionRegistry.clear()
async def test_picke_simple_qs(self):
qs = Person.aobjects.all()
pickle.dumps(qs)
async def _get_loaded(self, qs):
s = pickle.dumps(qs)
return pickle.loads(s)
async def test_unpickle(self):
qs = Person.aobjects.all()
loadedQs = await self._get_loaded(qs)
assert await qs.count() == await loadedQs.count()
# can update loadedQs
await loadedQs.update(age=23)
# check
assert (await Person.aobjects.first()).age == 23
async def test_pickle_support_filtration(self):
await Person.aobjects.create(name="Alice", age=22)
await Person.aobjects.create(name="Bob", age=23)
qs = Person.aobjects.filter(age__gte=22)
assert await qs.count() == 2
loaded = await self._get_loaded(qs)
assert await loaded.count() == 2
assert (await loaded.filter(name="Bob").first()).age == 23