Skip to content

Commit bd617bc

Browse files
author
Xin Dong
committed
Fix several bugs including:
- {} should NOT match anything other than an empty dictionary in some cases - indexes should be checked after all update operators
1 parent 2e6cd60 commit bd617bc

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

test/correctness/gen.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ def random_id_value():
183183
def random_value():
184184
r = global_prng.random()
185185
while True:
186-
# TODO re-enable None value generating
187186
# if (r < 0.1) and generator_options.test_nulls:
188-
# val = None
187+
# val = None
189188
if (r < 0.2):
190189
val = random_float()
191190
elif (r < 0.4):

test/correctness/mongo_model.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -835,11 +835,9 @@ def process_update_operator_pull_all(self, key, update_expression):
835835
raise MongoModelException("Cannot apply $pullAll to a non-array field.", code=10142)
836836

837837
def evaluate(self, query, document):
838-
if len(query) == 0:
839-
# If we made it here, the evaluate function must NOT be called from the `find` function, and thus
840-
# we can return false. Note in find() function, an empty query means maches all documents.
841-
return False
842838
acc = True
839+
if len(query) == 0:
840+
return len(document) == 0
843841
for field in query.keys():
844842
if field == '_id':
845843
tmp = OrderedDict()
@@ -960,21 +958,15 @@ def has_operator_expressions(doc):
960958

961959
def process_update_operator(self, key, update, new_doc=False):
962960
op_update = self.has_operator_expressions(update)
963-
old_data = None
964-
965-
try:
966-
old_data = deepcopy(self.data[key])
967-
if op_update:
968-
for k in update:
969-
if k == '$setOnInsert':
970-
self.mapUpdateOperator[k](key, update[k], new_doc=new_doc)
971-
elif k in self.mapUpdateOperator:
972-
self.mapUpdateOperator[k](key, update[k])
973-
else:
974-
self.replace(key, update)
975-
except MongoModelException as e:
976-
self.data[key] = old_data
977-
raise e
961+
old_data = deepcopy(self.data[key])
962+
if op_update:
963+
for k in update:
964+
if k == '$setOnInsert':
965+
self.mapUpdateOperator[k](key, update[k], new_doc=new_doc)
966+
elif k in self.mapUpdateOperator:
967+
self.mapUpdateOperator[k](key, update[k])
968+
else:
969+
self.replace(key, update)
978970

979971
def deep_transform_logical_operators(self, selector=None):
980972
new_selector = {}
@@ -1124,26 +1116,27 @@ def update(self, query, update, upsert, multi):
11241116
raise MongoModelException('multi update only works with $ operators', code=10158)
11251117
if isOperatorUpdate:
11261118
self.validate_update_object(update)
1127-
if len(query) == 0:
1128-
return
1129-
key = query.keys()[0]
11301119
any = False
11311120
old_data = deepcopy(self.data)
11321121
n = 0
11331122
try:
1123+
if len(query) == 0:
1124+
# Update all existing docs. And since the query is empty, do NOT do upsert.
1125+
any = True
1126+
for k in self.data.keys():
1127+
n +=1
1128+
self.process_update_operator(k, update)
1129+
if not multi:
1130+
break
1131+
key = query.keys()[0]
11341132
for k, item in self.data.iteritems():
11351133
if evaluate(key, query[key], item, self.options):
11361134
any = True
11371135
n += 1
11381136
# print "Result: ", item
1139-
if len(update) > 0:
1140-
self.process_update_operator(k, update)
1141-
if not multi:
1142-
return
1143-
else:
1144-
self.replace(k, update)
1145-
if not multi:
1146-
return
1137+
self.process_update_operator(k, update)
1138+
if not multi:
1139+
break
11471140
if any:
11481141
for index in self.indexes:
11491142
if not index.inError:
@@ -1184,7 +1177,7 @@ def update(self, query, update, upsert, multi):
11841177
if "_id" in query:
11851178
update["_id"] = query["_id"]
11861179
self.insert(update)
1187-
else:
1180+
elif not any:
11881181
# mongoDB raise an exception for the '$setOnInsert' update operator even if the upsert is False
11891182
if '$setOnInsert' in update.keys() and len(update['$setOnInsert']) == 0:
11901183
raise MongoModelException(

0 commit comments

Comments
 (0)