##// END OF EJS Templates
jsonalchemy: add pickle support to mutation dict so that json...
dan -
r575:ec0a2507 default
parent child Browse files
Show More
@@ -0,0 +1,45 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2016-2016 RhodeCode GmbH
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 import pickle
22 import pytest
23
24 from rhodecode.lib.jsonalchemy import MutationDict, MutationList
25
26
27 def test_mutation_dict_is_picklable():
28 mutation_dict = MutationDict({'key1': 'value1', 'key2': 'value2'})
29 dumped = pickle.dumps(mutation_dict)
30 loaded = pickle.loads(dumped)
31 assert loaded == mutation_dict
32
33 def test_mutation_list_is_picklable():
34 mutation_list = MutationList(['a', 'b', 'c'])
35 dumped = pickle.dumps(mutation_list)
36 loaded = pickle.loads(dumped)
37 assert loaded == mutation_list
38
39 def test_mutation_dict_with_lists_is_picklable():
40 mutation_dict = MutationDict({
41 'key': MutationList(['values', MutationDict({'key': 'value'})])
42 })
43 dumped = pickle.dumps(mutation_dict)
44 loaded = pickle.loads(dumped)
45 assert loaded == mutation_dict
@@ -171,6 +171,14 b' class MutationDict(MutationObj, DictClas'
171 DictClass.__delitem__(self, key)
171 DictClass.__delitem__(self, key)
172 self.changed()
172 self.changed()
173
173
174 def __setstate__(self, state):
175 self.__dict__ = state
176
177 def __reduce_ex__(self, proto):
178 # support pickling of MutationDicts
179 d = dict(self)
180 return (self.__class__, (d, ))
181
174
182
175 class MutationList(MutationObj, list):
183 class MutationList(MutationObj, list):
176 @classmethod
184 @classmethod
General Comments 0
You need to be logged in to leave comments. Login now