def get_result(query, collection, projection, sort, limit, skip, exception_msg): try: if gen.global_prng.random() < 0.10: cur = collection.find(query, projection, batch_size=gen.global_prng.randint(2, 10)) else: cur = collection.find(query, projection) if sort is None: ret = [util.deep_convert_to_unordered(i) for i in cur] ret.sort(cmp=util.mongo_compare_unordered_dict_items) elif isinstance(collection, MongoCollection): ret = copy.deepcopy([i for i in cur]) for i, val in enumerate(ret): if '_id' in val: val['_id'] = 0 ret = util.MongoModelNondeterministicList(ret, sort, limit, skip, query, projection, collection.options) # print ret else: ret = [i for i in cur.sort(sort).skip(skip).limit(limit)] for i, val in enumerate(ret): if '_id' in val: val['_id'] = 0 # print '1====', i, ret[i] return ret except pymongo.errors.OperationFailure as e: exception_msg.append('Caught PyMongo error:\n\n' ' Collection: %s\n' % str(collection) + ' Exception: %s\n' % str(e) + ' Query: %s\n' % str(query) + ' Projection: %s\n' % str(projection) + ' Sort: %s\n' % str(sort) + ' Limit: %r\n' % limit + ' Skip: %r\n' % skip) except MongoModelException as e: exception_msg.append('Caught Mongo Model error:\n\n' ' Collection: %s\n' % str(collection) + ' Exception: %s\n' % str(e) + ' Query: %s\n' % str(query) + ' Projection: %s\n' % str(projection) + ' Sort: %s\n' % str(sort) + ' Limit: %r\n' % limit + ' Skip: %r\n' % skip) return list()
def test_non_deterministic_query(): # =====sort fields=====input data=====result=====expected data = [ ([('x', 1)], 0, 0, {}, ['{"x":1}', '{"x":2}'], ['{"x":1}', '{"x":2}'], True), ([('x', -1)], 0, 0, {}, ['{"x":1}', '{"x":2}'], ['{"x":2}', '{"x":1}'], True), ([('x', 1)], 0, 0, {}, ['{"x":3}', '{"x":1}', '{"x":2}'], ['{"x":1}', '{"x":2}', '{"x":3}'], True), ([('x', 1)], 0, 0, {}, ['{"a":3}', '{"x":1}', '{"x":2}'], ['{"a":3}', '{"x":1}', '{"x":2}'], True), ([('x', -1)], 0, 0, {}, ['{"a":3}', '{"x":1}', '{"x":2}'], ['{"x":2}', '{"x":1}', '{"a":3}'], True), ([('x', 1)], 0, 0, {}, ['{"a":3}', '{"x":1, "a":1}', '{"x":1, "a":2}'], ['{"a":3}', '{"x":1, "a":2}', '{"x":1, "a":1}'], True), ([('x', 1)], 0, 0, {}, ['{"x":1, "a":1}', '{"x":1, "a":2}', '{"x":1, "a":2}'], ['{"x":1, "a":2}', '{"x":1, "a":1}', '{"x":1, "a":1}'], False), # limit & skip ([('x', 1)], 1, 0, {}, ['{"x":1}', '{"x":2}'], ['{"x":1}'], True), ([('x', 1)], 1, 0, {}, ['{"x":1}', '{"x":2}'], ['{"x":2}'], False), ([('x', 1)], 0, 1, {}, ['{"x":1}', '{"x":2}'], ['{"x":2}'], True), ([('x', 1)], 0, 1, {}, ['{"x":1}', '{"x":2}'], ['{"x":1}'], False), ([('x', 1)], 0, 5, {}, ['{"x":1}', '{"x":2}'], [], True), ([('x', 1)], 1, 1, {}, ['{"x":1}', '{"x":2}', '{"x":3}'], ['{"x":2}'], True), # array ([('x', 1)], 0, 0, {}, ['{"a":3}', '{"x":1, "a":1}', '{"x":[2,1], "a":2}'], ['{"a":3}', '{"x":1, "a":1}', '{"x":[2,1], "a":2}'], True), ([('x', 1)], 0, 0, {}, ['{"a":3}', '{"x":1, "a":1}', '{"x":[2,1], "a":2}'], ['{"a":3}', '{"x":[2,1], "a":2}', '{"x":1, "a":1}'], True), ([('x', 1)], 0, 0, {}, ['{"a":3}', '{"x":1, "a":1}', '{"x":[2,1], "a":2}'], ['{"a":3}', '{"x":1, "a":1}', '{"x":[2,1], "a":2}'], True), ([('x', -1)], 0, 0, {}, ['{"a":3}', '{"x":1, "a":1}', '{"x":[2,1], "a":2}'], ['{"x":1, "a":1}', '{"x":[2,1], "a":2}', '{"a":3}'], False), ([('x', -1)], 0, 0, {}, ['{"a":3}', '{"x":1, "a":1}', '{"x":[2,1], "a":2}'], ['{"x":[2,1], "a":2}', '{"x":1, "a":1}', '{"a":3}'], True), ([('a', 1)], 0, 0, {}, ['{"a":3}', '{"a":[1,4]}', '{"a":1}', '{"a":[1]}', '{"a":[4,1]}'], [ '{"a":[1,4]}', '{"a":[4,1]}', '{"a":1}', '{"a":[1]}', '{"a":3}', ], True), ([('a', -1)], 0, 0, {}, ['{"a":3}', '{"a":[1,4]}', '{"a":1}', '{"a":[1]}', '{"a":[4,1]}'], [ '{"a":3}', '{"a":[1,4]}', '{"a":[4,1]}', '{"a":1}', '{"a":[1]}', ], False), ([('a', -1)], 0, 0, {}, ['{"a":3}', '{"a":[1,4]}', '{"a":1}', '{"a":[1]}', '{"a":[4,1]}'], [ '{"a":[4,1]}', '{"a":[1,4]}', '{"a":3}', '{"a":[1]}', '{"a":1}', ], True), ([('a', 1)], 0, 0, {}, ['{"a":3}', '{"a":[1,4]}', '{"a":[4,1]}', '{"a":1}', '{"a":[1]}', '{"b":[1]}'], [ '{"b":[1]}', '{"a":[1,4]}', '{"a":[4,1]}', '{"a":1}', '{"a":[1]}', '{"a":3}', ], True), # null, [] and {} ([('a', 1)], 0, 0, {}, [ '{"a":3}', '{"a":{}}', '{"a":[]}', '{"a":[1,4]}', '{"a":[4,1]}', '{"a":1}', '{"a":null}', '{"a":[1]}', '{"b":[1]}' ], [ '{"a":[]}', '{"a":null}', '{"b":[1]}', '{"a":[1,4]}', '{"a":[4,1]}', '{"a":1}', '{"a":[1]}', '{"a":3}', '{"a":{}}' ], True), # two sort fields ([('a', 1), ('b', 1)], 0, 0, {}, ['{"a":1, "b":1}', '{"a":1, "b":3}', '{"a":1, "b":2}'], ['{"a":1, "b":1}', '{"a":1, "b":2}', '{"a":1, "b":3}'], True), ([('a', 1), ('b', 1)], 0, 0, {}, ['{"a":2, "b":1}', '{"a":2, "b":3}', '{"a":2, "b":2}', '{"a":1, "b":6}', '{"a":1, "b":5}', '{"a":1, "b":4}'], ['{"a":1, "b":4}', '{"a":1, "b":5}', '{"a":1, "b":6}', '{"a":2, "b":1}', '{"a":2, "b":2}', '{"a":2, "b":3}'], True), ([('a', 1), ('b', -1)], 0, 0, {}, ['{"a":2, "b":1}', '{"a":2, "b":3}', '{"a":2, "b":2}', '{"a":1, "b":6}', '{"a":1, "b":5}', '{"a":1, "b":4}'], ['{"a":1, "b":6}', '{"a":1, "b":5}', '{"a":1, "b":4}', '{"a":2, "b":3}', '{"a":2, "b":2}', '{"a":2, "b":1}'], True), # with sort path ([('x.a', 1)], 0, 0, {}, ['{"x":{"a":3}}', '{"x":{"a":1}}', '{"x":{"a":2}}'], ['{"x":{"a":1}}', '{"x":{"a":2}}', '{"x":{"a":3}}'], True), ([('x.a', 1)], 0, 0, {}, ['{"x":{"a":3}}', '{"x":{"a":1}}', '{"x":{"a":null}}'], ['{"x":{"a":null}}', '{"x":{"a":1}}', '{"x":{"a":3}}'], True), ([('x.a', 1)], 0, 0, {}, ['{"x":{"a":3}}', '{"x":{"b":1}}', '{"x":{"a":null}}'], ['{"x":{"a":null}}', '{"x":{"b":1}}', '{"x":{"a":3}}'], True), ([('a.b', 1)], 0, 0, {}, ['{"a":[{"b":3}]}', '{"a":[{"b":[1,6]}]}'], ['{"a":[{"b":[1,6]}]}', '{"a":[{"b":3}]}'], True), ([('a.b', 1)], 0, 0, {}, ['{"a":[{"b":3}]}', '{"a":[{"b":[1,6]}]}'], ['{"a":[{"b":3}]}', '{"a":[{"b":[1,6]}]}'], False), ([('a.b', 1)], 0, 0, {}, ['{"a":[{"b":5}]}', '{"a":[{"b":[2,1]}]}'], ['{"a":[{"b":[2,1]}]}', '{"a":[{"b":5}]}'], True), ([('a.b', 1)], 0, 0, {}, ['{"a":[{"b":5}, {"b":null}]}', '{"a":[{"b":[2,1]}]}'], ['{"a":[{"b":5}, {"b":null}]}', '{"a":[{"b":[2,1]}]}'], True), ([('a.b', 1)], 0, 0, {}, ['{"a":[{"b":3},4]}', '{"a":[{"b":[1,6]}]}'], ['{"a":[{"b":3},4]}', '{"a":[{"b":[1,6]}]}'], True), ([('a.b', 1)], 0, 0, {}, ['{"a":[{"b":3},4]}', '{"a":[{"b":[1,6]}]}'], ['{"a":[{"b":[1,6]}]}', '{"a":[{"b":3},4]}'], False), ([('a.b', 1)], 0, 0, {}, ['{"a":[{"b":6},4]}', '{"a":[{"b":[1,3]}]}'], ['{"a":[{"b":6},4]}', '{"a":[{"b":[1,3]}]}'], True), ([('a.b', -1)], 0, 0, {}, ['{"a":[{"b":6},4]}', '{"a":[{"b":[1,3]}]}'], ['{"a":[{"b":6},4]}', '{"a":[{"b":[1,3]}]}'], True), ([('a.b', 1)], 0, 0, {}, ['{"a":[{"b":[3,4]}]}', '{"a":[{"b":[1,2]}]}'], ['{"a":[{"b":[1,2]}]}', '{"a":[{"b":[3,4]}]}'], True), ([('a.b', -1)], 0, 0, {}, ['{"a":[{"b":[3,4]}]}', '{"a":[{"b":[1,2]}]}'], ['{"a":[{"b":[3,4]}]}', '{"a":[{"b":[1,2]}]}'], True), # mongoDB special cases that not pass the test ([('2.D', 1), (2, -1)], 0, 0, {}, ['{"2": "e"}', '{"2": ["c", {"0": "e", "C": -68, "D": "a"}]}', '{"2": "c"}'], ['{"2": "e"}', '{"2": ["c", {"0": "e", "C": -68, "D": "a"}]}', '{"2": "c"}'], True), ] for idx, test_item in enumerate(data): print "\n========== test", idx, "==========" (sort, limit, skip, query, input_data, result, expected) = test_item input_data = util.generate_list_of_ordered_dict_from_json(input_data) result = util.generate_list_of_ordered_dict_from_json(result) nd_list = util.MongoModelNondeterministicList(input_data, sort, limit, skip, query) # print nd_list assert nd_list.compare(result) == expected, str(nd_list)