Skip to content

Commit

Permalink
Make sure DocType objects can correctly pickle and unpickle
Browse files Browse the repository at this point in the history
Fixes #128, thanks brizzbane for the report!
  • Loading branch information
honzakral committed Apr 24, 2015
1 parent 07dc9ad commit 803bc25
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions elasticsearch_dsl/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ def __init__(self, meta=None, **kwargs):

super(DocType, self).__init__(**kwargs)

def __getstate__(self):
return (self._d_, self.meta._d_)

def __setstate__(self, state):
data, meta = state
super(AttrDict, self).__setattr__('_d_', data)
super(AttrDict, self).__setattr__('meta', ResultMeta(meta))

def __getattr__(self, name):
if name.startswith('_') and name[1:] in META_FIELDS:
return getattr(self.meta, name[1:])
Expand Down
11 changes: 11 additions & 0 deletions test_elasticsearch_dsl/test_document.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pickle
from hashlib import md5
from datetime import datetime

Expand Down Expand Up @@ -28,6 +29,16 @@ class MyMultiSubDoc(MyDoc2, MySubDoc):
class DocWithNested(document.DocType):
comments = field.Nested(properties={'title': field.String()})

def test_doc_type_can_be_correctly_pickled():
d = MyDoc(title='Hello World!', meta={'id': 42})
s = pickle.dumps(d)

d2 = pickle.loads(s)

assert d2 == d
assert 42 == d2.meta.id
assert 'Hello World!' == d2.title

def test_meta_is_accessible_even_on_empty_doc():
d = MyDoc()
d.meta
Expand Down

0 comments on commit 803bc25

Please sign in to comment.