|
26 | 26 | import os.path |
27 | 27 | import random |
28 | 28 | import sys |
| 29 | +import pprint |
29 | 30 |
|
30 | 31 | import pymongo |
31 | 32 |
|
@@ -151,6 +152,8 @@ def diff_results(cA, rA, cB, rB): |
151 | 152 |
|
152 | 153 |
|
153 | 154 | def check_query(query, collection1, collection2, projection=None, sort=None, limit=0, skip=0): |
| 155 | + # if projection: |
| 156 | + # projection['_id'] = True |
154 | 157 | util.trace('debug', '\n==================================================') |
155 | 158 | util.trace('debug', 'query:', query) |
156 | 159 | util.trace('debug', 'sort:', sort) |
@@ -213,56 +216,59 @@ def check_query(query, collection1, collection2, projection=None, sort=None, lim |
213 | 216 | return False |
214 | 217 |
|
215 | 218 |
|
216 | | -def test_update(collections, verbose=False): |
| 219 | +def test_update(collection1, collection2, verbose=False): |
217 | 220 | okay = True |
218 | 221 | for i in range(1, 10): |
219 | | - update = gen.random_update(collections[0]) |
| 222 | + exceptionOne = None |
| 223 | + exceptionTwo = None |
| 224 | + update = gen.random_update(collection1) |
220 | 225 |
|
221 | 226 | util.trace('debug', '\n========== Update No.', i, '==========') |
222 | 227 | util.trace('debug', 'Query:', update['query']) |
223 | 228 | util.trace('debug', 'Update:', str(update['update'])) |
224 | 229 | util.trace('debug', 'Number results from collection: ', gen.count_query_results( |
225 | | - collections[0], update['query'])) |
226 | | - for item in collections[0].find(update['query']): |
| 230 | + collection1, update['query'])) |
| 231 | + for item in collection1.find(update['query']): |
227 | 232 | util.trace('debug', 'Find Result0:', item) |
228 | 233 |
|
229 | | - exception = [] |
230 | | - exception_msg = [] |
231 | | - for coll in collections: |
232 | | - try: |
233 | | - if verbose: |
234 | | - all = [x for x in coll.find(dict())] |
235 | | - for item in coll.find(update['query']): |
236 | | - print 'Before update doc:', item |
237 | | - print 'Before update coll size: ', len(all) |
238 | | - |
239 | | - coll.update(update['query'], update['update'], upsert=update['upsert'], multi=update['multi']) |
240 | | - |
241 | | - if verbose: |
242 | | - all = [x for x in coll.find(dict())] |
243 | | - for item in coll.find(update['query']): |
244 | | - print 'After update doc:', item |
245 | | - print 'After update coll size: ', len(all) |
246 | | - |
247 | | - except pymongo.errors.OperationFailure as e: |
248 | | - exception.append(e) |
249 | | - exception_msg.append( |
250 | | - util.join_n('Caught PyMongo error while attempting update: %s' % e[0], |
251 | | - 'Query: %s' % update['query'], 'Update: %s' % update['update'], |
252 | | - 'Upsert: {0}, Multi: {1}'.format(update['upsert'], update['multi']))) |
253 | | - except MongoModelException as e: |
254 | | - exception.append(e) |
255 | | - exception_msg.append( |
256 | | - util.join_n('Caught MongoModel error. Offending update(', str(update['query']), |
257 | | - str(update['update']), str(update['upsert']), str(update['multi']), ')')) |
258 | | - |
259 | | - if len(exception_msg) == 1: |
260 | | - print 'Update: ' + str(update['update']) |
261 | | - print '\033[91m', exception[0], '\033[0m' |
262 | | - print '\033[91m', exception_msg[0], '\033[0m' |
263 | | - return False |
| 234 | + try: |
| 235 | + if verbose: |
| 236 | + all = [x for x in collection1.find(dict())] |
| 237 | + for item in collection1.find(update['query']): |
| 238 | + print 'Before update doc:\n', pprint.pprint(item) |
| 239 | + print 'Before update collection1 size: ', len(all) |
| 240 | + collection1.update(update['query'], update['update'], upsert=update['upsert'], multi=update['multi']) |
| 241 | + except pymongo.errors.OperationFailure as e: |
| 242 | + exceptionOne = e |
| 243 | + except MongoModelException as e: |
| 244 | + exceptionOne = e |
| 245 | + try: |
| 246 | + if verbose: |
| 247 | + all = [x for x in collection2.find(dict())] |
| 248 | + for item in collection2.find(update['query']): |
| 249 | + print 'Before update doc:\n', pprint.pprint(item) |
| 250 | + print 'Before update collection2 size: ', len(all) |
| 251 | + collection2.update(update['query'], update['update'], upsert=update['upsert'], multi=update['multi']) |
| 252 | + except pymongo.errors.OperationFailure as e: |
| 253 | + exceptionTwo = e |
| 254 | + except MongoModelException as e: |
| 255 | + exceptionTwo = e |
| 256 | + |
| 257 | + if ((exceptionOne is None and exceptionTwo is None) |
| 258 | + or (exceptionOne is not None and exceptionTwo is not None)): |
| 259 | + # or (exceptionOne is not None and exceptionTwo is not None and exceptionOne.code == exceptionTwo.code)): |
| 260 | + # TODO re-enable the exact error check. |
| 261 | + pass |
| 262 | + else: |
| 263 | + print 'Unmatched result: ' |
| 264 | + print type(exceptionOne), ': ', str(exceptionOne) |
| 265 | + print type(exceptionTwo), ': ', str(exceptionTwo) |
| 266 | + okay = False |
| 267 | + ignored_exception_check(exceptionOne) |
| 268 | + ignored_exception_check(exceptionTwo) |
| 269 | + return okay |
264 | 270 |
|
265 | | - if not check_query(dict(), collections[0], collections[1]): |
| 271 | + if not check_query(dict(), collection1, collection2): |
266 | 272 | print 'Update: ' + str(update['update']) |
267 | 273 | return False |
268 | 274 |
|
@@ -393,8 +399,9 @@ def _run_operation_(op1, op2): |
393 | 399 | if not okay: |
394 | 400 | return (okay, fname, None) |
395 | 401 |
|
396 | | - if update_tests_enabled: |
397 | | - if not test_update([collection1, collection2], verbose): |
| 402 | + # if update_tests_enabled: |
| 403 | + if True: |
| 404 | + if not test_update(collection1, collection2, verbose): |
398 | 405 | okay = False |
399 | 406 | return (okay, fname, None) |
400 | 407 |
|
|
0 commit comments