@@ -69,6 +69,158 @@ def test_insert_many(self):
69
69
result_objs = [result for result in result_objs ]
70
70
self .assertEqual (len (result_objs ), 2 )
71
71
72
+ @unittest .skipIf (mongomock is None , 'mongomock package not present' )
73
+ def test_update_one (self ):
74
+ collection = mongomock .MongoClient ().db .collection
75
+ obj = {'_id' : '1' , 'field' : 0 }
76
+ collection .insert_one (obj )
77
+
78
+ filter_doc = obj
79
+ update_doc = {'$inc' : {'field' : 123 }}
80
+
81
+ self .hook .update_one (collection , filter_doc , update_doc )
82
+
83
+ result_obj = collection .find_one (filter = '1' )
84
+ self .assertEqual (123 , result_obj ['field' ])
85
+
86
+ @unittest .skipIf (mongomock is None , 'mongomock package not present' )
87
+ def test_update_one_with_upsert (self ):
88
+ collection = mongomock .MongoClient ().db .collection
89
+
90
+ filter_doc = {'_id' : '1' , 'field' : 0 }
91
+ update_doc = {'$inc' : {'field' : 123 }}
92
+
93
+ self .hook .update_one (collection , filter_doc , update_doc , upsert = True )
94
+
95
+ result_obj = collection .find_one (filter = '1' )
96
+ self .assertEqual (123 , result_obj ['field' ])
97
+
98
+ @unittest .skipIf (mongomock is None , 'mongomock package not present' )
99
+ def test_update_many (self ):
100
+ collection = mongomock .MongoClient ().db .collection
101
+ obj1 = {'_id' : '1' , 'field' : 0 }
102
+ obj2 = {'_id' : '2' , 'field' : 0 }
103
+ collection .insert_many ([obj1 , obj2 ])
104
+
105
+ filter_doc = {'field' : 0 }
106
+ update_doc = {'$inc' : {'field' : 123 }}
107
+
108
+ self .hook .update_many (collection , filter_doc , update_doc )
109
+
110
+ result_obj = collection .find_one (filter = '1' )
111
+ self .assertEqual (123 , result_obj ['field' ])
112
+
113
+ result_obj = collection .find_one (filter = '2' )
114
+ self .assertEqual (123 , result_obj ['field' ])
115
+
116
+ @unittest .skipIf (mongomock is None , 'mongomock package not present' )
117
+ def test_update_many_with_upsert (self ):
118
+ collection = mongomock .MongoClient ().db .collection
119
+
120
+ filter_doc = {'_id' : '1' , 'field' : 0 }
121
+ update_doc = {'$inc' : {'field' : 123 }}
122
+
123
+ self .hook .update_many (collection , filter_doc , update_doc , upsert = True )
124
+
125
+ result_obj = collection .find_one (filter = '1' )
126
+ self .assertEqual (123 , result_obj ['field' ])
127
+
128
+ @unittest .skipIf (mongomock is None , 'mongomock package not present' )
129
+ def test_replace_one (self ):
130
+ collection = mongomock .MongoClient ().db .collection
131
+ obj1 = {'_id' : '1' , 'field' : 'test_value_1' }
132
+ obj2 = {'_id' : '2' , 'field' : 'test_value_2' }
133
+ collection .insert_many ([obj1 , obj2 ])
134
+
135
+ obj1 ['field' ] = 'test_value_1_updated'
136
+ self .hook .replace_one (collection , obj1 )
137
+
138
+ result_obj = collection .find_one (filter = '1' )
139
+ self .assertEqual ('test_value_1_updated' , result_obj ['field' ])
140
+
141
+ # Other document should stay intact
142
+ result_obj = collection .find_one (filter = '2' )
143
+ self .assertEqual ('test_value_2' , result_obj ['field' ])
144
+
145
+ @unittest .skipIf (mongomock is None , 'mongomock package not present' )
146
+ def test_replace_one_with_filter (self ):
147
+ collection = mongomock .MongoClient ().db .collection
148
+ obj1 = {'_id' : '1' , 'field' : 'test_value_1' }
149
+ obj2 = {'_id' : '2' , 'field' : 'test_value_2' }
150
+ collection .insert_many ([obj1 , obj2 ])
151
+
152
+ obj1 ['field' ] = 'test_value_1_updated'
153
+ self .hook .replace_one (collection , obj1 , {'field' : 'test_value_1' })
154
+
155
+ result_obj = collection .find_one (filter = '1' )
156
+ self .assertEqual ('test_value_1_updated' , result_obj ['field' ])
157
+
158
+ # Other document should stay intact
159
+ result_obj = collection .find_one (filter = '2' )
160
+ self .assertEqual ('test_value_2' , result_obj ['field' ])
161
+
162
+ @unittest .skipIf (mongomock is None , 'mongomock package not present' )
163
+ def test_replace_one_with_upsert (self ):
164
+ collection = mongomock .MongoClient ().db .collection
165
+
166
+ obj = {'_id' : '1' , 'field' : 'test_value_1' }
167
+ self .hook .replace_one (collection , obj , upsert = True )
168
+
169
+ result_obj = collection .find_one (filter = '1' )
170
+ self .assertEqual ('test_value_1' , result_obj ['field' ])
171
+
172
+ @unittest .skipIf (mongomock is None , 'mongomock package not present' )
173
+ def test_replace_many (self ):
174
+ collection = mongomock .MongoClient ().db .collection
175
+ obj1 = {'_id' : '1' , 'field' : 'test_value_1' }
176
+ obj2 = {'_id' : '2' , 'field' : 'test_value_2' }
177
+ collection .insert_many ([obj1 , obj2 ])
178
+
179
+ obj1 ['field' ] = 'test_value_1_updated'
180
+ obj2 ['field' ] = 'test_value_2_updated'
181
+ self .hook .replace_many (collection , [obj1 , obj2 ])
182
+
183
+ result_obj = collection .find_one (filter = '1' )
184
+ self .assertEqual ('test_value_1_updated' , result_obj ['field' ])
185
+
186
+ result_obj = collection .find_one (filter = '2' )
187
+ self .assertEqual ('test_value_2_updated' , result_obj ['field' ])
188
+
189
+ @unittest .skipIf (mongomock is None , 'mongomock package not present' )
190
+ def test_replace_many_with_upsert (self ):
191
+ collection = mongomock .MongoClient ().db .collection
192
+ obj1 = {'_id' : '1' , 'field' : 'test_value_1' }
193
+ obj2 = {'_id' : '2' , 'field' : 'test_value_2' }
194
+
195
+ self .hook .replace_many (collection , [obj1 , obj2 ], upsert = True )
196
+
197
+ result_obj = collection .find_one (filter = '1' )
198
+ self .assertEqual ('test_value_1' , result_obj ['field' ])
199
+
200
+ result_obj = collection .find_one (filter = '2' )
201
+ self .assertEqual ('test_value_2' , result_obj ['field' ])
202
+
203
+ @unittest .skipIf (mongomock is None , 'mongomock package not present' )
204
+ def test_delete_one (self ):
205
+ collection = mongomock .MongoClient ().db .collection
206
+ obj = {'_id' : '1' }
207
+ collection .insert_one (obj )
208
+
209
+ self .hook .delete_one (collection , {'_id' : '1' })
210
+
211
+ self .assertEqual (0 , collection .count ())
212
+
213
+ @unittest .skipIf (mongomock is None , 'mongomock package not present' )
214
+ def test_delete_many (self ):
215
+ collection = mongomock .MongoClient ().db .collection
216
+ obj1 = {'_id' : '1' , 'field' : 'value' }
217
+ obj2 = {'_id' : '2' , 'field' : 'value' }
218
+ collection .insert_many ([obj1 , obj2 ])
219
+
220
+ self .hook .delete_many (collection , {'field' : 'value' })
221
+
222
+ self .assertEqual (0 , collection .count ())
223
+
72
224
@unittest .skipIf (mongomock is None , 'mongomock package not present' )
73
225
def test_find_one (self ):
74
226
collection = mongomock .MongoClient ().db .collection
0 commit comments