File tree 3 files changed +23
-2
lines changed
3 files changed +23
-2
lines changed Original file line number Diff line number Diff line change
1
+ test :
2
+ nosetests
Original file line number Diff line number Diff line change @@ -141,15 +141,19 @@ def insert(self, data):
141
141
if isinstance (data , list ):
142
142
return [self ._insert (element ) for element in data ]
143
143
return self ._insert (data )
144
+
144
145
def _insert (self , data ):
145
- if not '_id' in data :
146
+ if '_id' not in data :
146
147
data ['_id' ] = ObjectId ()
147
148
object_id = data ['_id' ]
148
149
if object_id in self ._documents :
149
150
raise DuplicateKeyError ("Duplicate Key Error" , 11000 )
150
- self ._documents [object_id ] = copy . deepcopy (data )
151
+ self ._documents [object_id ] = self . _internalize_dict (data )
151
152
return object_id
152
153
154
+ def _internalize_dict (self , d ):
155
+ return dict ((k , copy .deepcopy (v )) for k , v in iteritems (d ))
156
+
153
157
def _has_key (self , doc , key ):
154
158
return key in doc
155
159
Original file line number Diff line number Diff line change @@ -88,6 +88,21 @@ def test__getting_collection_via_getattr(self):
88
88
self .assertIs (col1 , self .db ['some_collection_here' ])
89
89
self .assertIsInstance (col1 , mongomock .Collection )
90
90
91
+ def test__save_class_deriving_from_dict (self ):
92
+ # See https://github.com/vmalloc/mongomock/issues/52
93
+ class Document (dict ):
94
+ def __init__ (self , collection ):
95
+ self .collection = collection
96
+ super (Document , self ).__init__ ()
97
+ self .save ()
98
+
99
+ def save (self ):
100
+ self .collection .save (self )
101
+
102
+ doc = Document (self .db .collection )
103
+ self .assertIn ("_id" , doc )
104
+ self .assertNotIn ("collection" , doc )
105
+
91
106
def test__getting_collection_via_getitem (self ):
92
107
col1 = self .db ['some_collection_here' ]
93
108
col2 = self .db ['some_collection_here' ]
You can’t perform that action at this time.
0 commit comments