コード例 #1
0
def test_update(collection1, collection2, verbose=False):
    for i in range(1, 10):
        exceptionOne = None
        exceptionTwo = None
        update = gen.random_update(collection1)

        util.trace('debug', '\n========== Update No.', i, '==========')
        util.trace('debug', 'Query:', update['query'])
        util.trace('debug', 'Update:', str(update['update']))
        util.trace('debug', 'Number results from collection: ',
                   gen.count_query_results(collection1, update['query']))
        for item in collection1.find(update['query']):
            util.trace('debug', 'Find Result1:', item)
        for item in collection2.find(update['query']):
            util.trace('debug', 'Find Result2:', item)

        try:
            if verbose:
                all = [x for x in collection1.find(dict())]
                for item in collection1.find(update['query']):
                    print '[{}] Before update doc:{}'.format(
                        type(collection1), item)
                print 'Before update collection1 size: ', len(all)
            collection1.update(update['query'],
                               update['update'],
                               upsert=update['upsert'],
                               multi=update['multi'])
        except pymongo.errors.OperationFailure as e:
            exceptionOne = e
        except MongoModelException as e:
            exceptionOne = e
        try:
            if verbose:
                all = [x for x in collection2.find(dict())]
                for item in collection2.find(update['query']):
                    print '[{}]Before update doc:{}'.format(
                        type(collection2), item)
                print 'Before update collection2 size: ', len(all)
            collection2.update(update['query'],
                               update['update'],
                               upsert=update['upsert'],
                               multi=update['multi'])
        except pymongo.errors.OperationFailure as e:
            exceptionTwo = e
        except MongoModelException as e:
            exceptionTwo = e

        if (exceptionOne is None and exceptionTwo is None):
            # happy case, proceed to consistency check
            pass
        elif exceptionOne is not None and exceptionTwo is not None:
            # or (exceptionOne is not None and exceptionTwo is not None and exceptionOne.code == exceptionTwo.code)):
            # TODO re-enable the exact error check.
            # TODO re-enable consistency check when failure happened
            return (True, True)
        else:
            print 'Unmatched result: '
            print type(exceptionOne), ': ', str(exceptionOne)
            print type(exceptionTwo), ': ', str(exceptionTwo)
            ignored_exception_check(exceptionOne)
            ignored_exception_check(exceptionTwo)
            return (False, False)

        if not check_query(dict(), collection1, collection2):
            return (False, False)

    return (True, False)
コード例 #2
0
def test_update(collections, verbose=False):
    okay = True
    for i in range(1, 10):
        update = gen.random_update(collections[0])

        util.trace('debug', '\n========== Update No.', i, '==========')
        util.trace('debug', 'Query:', update['query'])
        util.trace('debug', 'Update:', str(update['update']))
        util.trace('debug', 'Number results from collection: ',
                   gen.count_query_results(collections[0], update['query']))
        for item in transactional_shim.find(collections[0], update['query']):
            util.trace('debug', 'Find Result0:', item)

        exception = []
        exception_msg = []
        for coll in collections:
            try:
                if verbose:
                    all = [x for x in coll.find(dict())]
                    for item in transactional_shim.find(coll, update['query']):
                        print 'Before update doc:', item
                    print 'Before update coll size: ', len(all)

                transactional_shim.update(coll,
                                          update['query'],
                                          update['update'],
                                          upsert=update['upsert'],
                                          multi=update['multi'])

                if verbose:
                    all = [x for x in coll.find(dict())]
                    for item in coll.find(update['query']):
                        print 'After update doc:', item
                    print 'After update coll size: ', len(all)

            except pymongo.errors.OperationFailure as e:
                exception.append(e)
                exception_msg.append(
                    util.join_n(
                        'Caught PyMongo error while attempting update: %s' %
                        e[0], 'Query: %s' % update['query'],
                        'Update: %s' % update['update'],
                        'Upsert: {0}, Multi: {1}'.format(
                            update['upsert'], update['multi'])))
            except MongoModelException as e:
                exception.append(e)
                exception_msg.append(
                    util.join_n('Caught MongoModel error. Offending update(',
                                str(update['query']), str(update['update']),
                                str(update['upsert']), str(update['multi']),
                                ')'))

        if len(exception_msg) == 1:
            print 'Update: ' + str(update['update'])
            print '\033[91m', exception[0], '\033[0m'
            print '\033[91m', exception_msg[0], '\033[0m'
            return False

        if not check_query(dict(), collections[0], collections[1]):
            print 'Update: ' + str(update['update'])
            return False

    return okay