def test_group_with_scope(self): db = self.db db.drop_collection("test") db.test.save({"a": 1}) db.test.save({"b": 1}) reduce_function = "function (obj, prev) { prev.count += inc_value; }" self.assertEqual(2, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 1}))[0]['count']) self.assertEqual(4, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 2}))[0]['count']) self.assertEqual(1, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 0.5}))[0]['count']) if version.at_least(db.connection, (1, 1)): self.assertEqual(2, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 1}), command=True)[0]['count']) self.assertEqual(4, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 2}), command=True)[0]['count']) self.assertEqual(1, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 0.5}), command=True)[0]['count'])
def test_distinct(self): if not version.at_least(self.db.connection(), (1, 1, 3, 1)): raise SkipTest() self.db.drop_collection("test") self.db.test.save({"a": 1}) self.db.test.save({"a": 2}) self.db.test.save({"a": 2}) self.db.test.save({"a": 2}) self.db.test.save({"a": 3}) distinct = self.db.test.find({"a": {"$lt": 3}}).distinct("a") distinct.sort() self.assertEqual([1, 2], distinct) self.db.drop_collection("test") self.db.test.save({"a": {"b": "a"}, "c": 12}) self.db.test.save({"a": {"b": "b"}, "c": 8}) self.db.test.save({"a": {"b": "c"}, "c": 12}) self.db.test.save({"a": {"b": "c"}, "c": 8}) distinct = self.db.test.find({"c": 8}).distinct("a.b") distinct.sort() self.assertEqual(["b", "c"], distinct)
def test_distinct(self): if not version.at_least(self.db.connection, (1, 1, 3, 1)): raise SkipTest() self.db.drop_collection("test") self.db.test.save({"a": 1}) self.db.test.save({"a": 2}) self.db.test.save({"a": 2}) self.db.test.save({"a": 2}) self.db.test.save({"a": 3}) distinct = self.db.test.find({"a": {"$lt": 3}}).distinct("a") distinct.sort() self.assertEqual([1, 2], distinct) self.db.drop_collection("test") self.db.test.save({"a": {"b": "a"}, "c": 12}) self.db.test.save({"a": {"b": "b"}, "c": 8}) self.db.test.save({"a": {"b": "c"}, "c": 12}) self.db.test.save({"a": {"b": "c"}, "c": 8}) distinct = self.db.test.find({"c": 8}).distinct("a.b") distinct.sort() self.assertEqual(["b", "c"], distinct)
def test_map_reduce(self): if not version.at_least(self.db.connection, (1, 1, 1)): raise SkipTest() db = self.db db.drop_collection("test") db.test.insert({"id": 1, "tags": ["dog", "cat"]}) db.test.insert({"id": 2, "tags": ["cat"]}) db.test.insert({"id": 3, "tags": ["mouse", "cat", "dog"]}) db.test.insert({"id": 4, "tags": []}) map = Code("function () {" " this.tags.forEach(function(z) {" " emit(z, 1);" " });" "}") reduce = Code( "function (key, values) {" " var total = 0;" " for (var i = 0; i < values.length; i++) {" " total += values[i];" " }" " return total;" "}" ) result = db.test.map_reduce(map, reduce) self.assertEqual(3, result.find_one({"_id": "cat"})["value"]) self.assertEqual(2, result.find_one({"_id": "dog"})["value"]) self.assertEqual(1, result.find_one({"_id": "mouse"})["value"]) full_result = db.test.map_reduce(map, reduce, full_response=True) self.assertEqual(6, full_result["counts"]["emit"]) result = db.test.map_reduce(map, reduce, limit=2) self.assertEqual(2, result.find_one({"_id": "cat"})["value"]) self.assertEqual(1, result.find_one({"_id": "dog"})["value"]) self.assertEqual(None, result.find_one({"_id": "mouse"}))
def test_count_with_fields(self): self.db.test.remove({}) self.db.test.save({"x": 1}) if not version.at_least(self.db.connection(), (1, 1, 3, -1)): for _ in self.db.test.find({}, ["a"]): self.fail() self.assertEqual(0, self.db.test.find({}, ["a"]).count()) else: self.assertEqual(1, self.db.test.find({}, ["a"]).count())
def test_count_with_fields(self): self.db.test.drop() self.db.test.save({"x": 1}) if not version.at_least(self.db.connection, (1, 1, 3, -1)): for _ in self.db.test.find({}, ["a"]): self.fail() self.assertEqual(0, self.db.test.find({}, ["a"]).count()) else: self.assertEqual(1, self.db.test.find({}, ["a"]).count())
def test_max_scan(self): if not version.at_least(self.db.connection, (1, 5, 1)): raise SkipTest() self.db.drop_collection("test") for _ in range(100): self.db.test.insert({}) self.assertEqual(100, len(list(self.db.test.find()))) self.assertEqual(50, len(list(self.db.test.find(max_scan=50)))) self.assertEqual(50, len(list(self.db.test.find() .max_scan(90).max_scan(50))))
def test_max_scan(self): if not version.at_least(self.db.connection, (1, 5, 1)): raise SkipTest() self.db.drop_collection("test") for _ in range(100): self.db.test.insert({}) self.assertEqual(100, len(list(self.db.test.find()))) self.assertEqual(50, len(list(self.db.test.find(max_scan=50)))) self.assertEqual( 50, len(list(self.db.test.find().max_scan(90).max_scan(50))))
def test_safe_remove(self): db = self.db db.drop_collection("test") db.create_collection("test", {"capped": True, "size": 1000}) db.test.insert({"x": 1}) self.assertEqual(1, db.test.count()) db.test.remove({"x": 1}) self.assertEqual(1, db.test.count()) if version.at_least(db.connection, (1, 1, 3, -1)): self.assertRaises(OperationFailure, db.test.remove, {"x": 1}, safe=True) else: # Just test that it doesn't blow up db.test.remove({"x": 1}, safe=True)
def test_multi_update(self): db = self.db if not version.at_least(db.connection, (1, 1, 3, -1)): raise SkipTest() db.drop_collection("test") db.test.save({"x": 4, "y": 3}) db.test.save({"x": 5, "y": 5}) db.test.save({"x": 4, "y": 4}) db.test.update({"x": 4}, {"$set": {"y": 5}}, multi=True) self.assertEqual(3, db.test.count()) for doc in db.test.find(): self.assertEqual(5, doc["y"])
def test_safe_update(self): db = self.db v113minus = version.at_least(db.connection, (1, 1, 3, -1)) db.drop_collection("test") db.test.create_index("x", unique=True) db.test.insert({"x": 5}) id = db.test.insert({"x": 4}) db.test.update({"_id": id}, {"$inc": {"x": 1}}) if v113minus: self.assert_(db.error()["err"].startswith("E11001")) else: self.assert_(db.error()["err"].startswith("E12011")) self.assertRaises(OperationFailure, db.test.update, {"_id": id}, {"$inc": {"x": 1}}, safe=True)
def test_group_with_scope(self): db = self.db db.drop_collection("test") db.test.save({"a": 1}) db.test.save({"b": 1}) reduce_function = "function (obj, prev) { prev.count += inc_value; }" self.assertEqual( 2, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 1}))[0]['count']) self.assertEqual( 4, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 2}))[0]['count']) self.assertEqual( 1, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 0.5}))[0]['count']) if version.at_least(db.connection(), (1, 1)): self.assertEqual( 2, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 1}), command=True)[0]['count']) self.assertEqual( 4, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 2}), command=True)[0]['count']) self.assertEqual( 1, db.test.group([], {}, {"count": 0}, Code(reduce_function, {"inc_value": 0.5}), command=True)[0]['count'])
def test_duplicate_key_error(self): db = self.db db.drop_collection("test") db.test.create_index("x", unique=True) db.test.insert({"_id": 1, "x": 1}, safe=True) db.test.insert({"_id": 2, "x": 2}, safe=True) expected_error = OperationFailure if version.at_least(db.connection, (1, 3)): expected_error = DuplicateKeyError self.assertRaises(expected_error, db.test.insert, {"_id": 1}, safe=True) self.assertRaises(expected_error, db.test.insert, {"x": 1}, safe=True) self.assertRaises(expected_error, db.test.save, {"x": 2}, safe=True) self.assertRaises(expected_error, db.test.update, {"x": 1}, {"$inc": {"x": 1}}, safe=True)
def test_count_with_limit_and_skip(self): if not version.at_least(self.db.connection(), (1, 1, 4, -1)): raise SkipTest() def check_len(cursor, length): self.assertEqual(len(list(cursor)), cursor.count(True)) self.assertEqual(length, cursor.count(True)) self.db.drop_collection("test") for i in range(100): self.db.test.save({"i": i}) check_len(self.db.test.find(), 100) check_len(self.db.test.find().limit(10), 10) check_len(self.db.test.find().limit(110), 100) check_len(self.db.test.find().skip(10), 90) check_len(self.db.test.find().skip(110), 0) check_len(self.db.test.find().limit(10).skip(10), 10) check_len(self.db.test.find()[10:20], 10) check_len(self.db.test.find().limit(10).skip(95), 5) check_len(self.db.test.find()[95:105], 5)
def test_count_with_limit_and_skip(self): if not version.at_least(self.db.connection, (1, 1, 4, -1)): raise SkipTest() def check_len(cursor, length): self.assertEqual(len(list(cursor)), cursor.count(True)) self.assertEqual(length, cursor.count(True)) self.db.drop_collection("test") for i in range(100): self.db.test.save({"i": i}) check_len(self.db.test.find(), 100) check_len(self.db.test.find().limit(10), 10) check_len(self.db.test.find().limit(110), 100) check_len(self.db.test.find().skip(10), 90) check_len(self.db.test.find().skip(110), 0) check_len(self.db.test.find().limit(10).skip(10), 10) check_len(self.db.test.find()[10:20], 10) check_len(self.db.test.find().limit(10).skip(95), 5) check_len(self.db.test.find()[95:105], 5)
def test_group(self): db = self.db db.drop_collection("test") def group_checker(args, expected, with_command=True): eval = db.test.group(*args) self.assertEqual(eval, expected) if with_command: cmd = db.test.group(*args, **{"command": True}) self.assertEqual(cmd, expected) args = [[], {}, {"count": 0}, "function (obj, prev) { prev.count++; }"] expected = [] group_checker(args, expected) db.test.save({"a": 2}) db.test.save({"b": 5}) db.test.save({"a": 1}) args = [[], {}, {"count": 0}, "function (obj, prev) { prev.count++; }"] expected = [{'count': 3}] group_checker(args, expected) args = [[], { "a": { "$gt": 1 } }, { "count": 0 }, "function (obj, prev) { prev.count++; }"] expected = [{'count': 1}] group_checker(args, expected) db.test.save({"a": 2, "b": 3}) args = [["a"], {}, { "count": 0 }, "function (obj, prev) { prev.count++; }"] # NOTE maybe we can't count on this ordering being right expected = [{ "a": 2, "count": 2 }, { "a": None, "count": 1 }, { "a": 1, "count": 1 }] group_checker(args, expected) # modifying finalize args = [["a"], {}, { "count": 0 }, "function (obj, prev) { prev.count++; }", "function(obj){obj.count++;}"] expected = [{ "a": 2, "count": 3 }, { "a": None, "count": 2 }, { "a": 1, "count": 2 }] group_checker(args, expected, with_command=version.at_least(db.connection(), (1, 1))) # returning finalize args = [["a"], {}, { "count": 0 }, "function (obj, prev) { prev.count++; }", "function(obj){ return obj.count;}"] expected = [ 2, # a:2 1, # a:None 1 ] # a:1 group_checker(args, expected, with_command=version.at_least(db.connection(), (1, 1))) self.assertRaises(OperationFailure, db.test.group, [], {}, {}, "5 ++ 5") self.assertRaises(OperationFailure, db.test.group, [], {}, {}, "5 ++ 5", command=True)
def test_group(self): db = self.db db.drop_collection("test") def group_checker(args, expected, with_command=True): eval = db.test.group(*args) self.assertEqual(eval, expected) if with_command: cmd = db.test.group(*args, **{"command": True}) self.assertEqual(cmd, expected) args = [[], {}, {"count": 0}, "function (obj, prev) { prev.count++; }"] expected = [] group_checker(args, expected) db.test.save({"a": 2}) db.test.save({"b": 5}) db.test.save({"a": 1}) args = [[], {}, {"count": 0}, "function (obj, prev) { prev.count++; }"] expected = [{'count': 3}] group_checker(args, expected) args = [[], {"a": {"$gt": 1}}, {"count": 0}, "function (obj, prev) { prev.count++; }"] expected = [{'count': 1}] group_checker(args, expected) db.test.save({"a": 2, "b": 3}) args = [["a"], {}, {"count": 0}, "function (obj, prev) { prev.count++; }"] # NOTE maybe we can't count on this ordering being right expected = [{"a": 2, "count": 2}, {"a": None, "count": 1}, {"a": 1, "count": 1}] group_checker(args, expected) # modifying finalize args = [["a"], {}, {"count": 0}, "function (obj, prev) { prev.count++; }", "function(obj){obj.count++;}"] expected = [{"a": 2, "count": 3}, {"a": None, "count": 2}, {"a": 1, "count": 2}] group_checker(args, expected, with_command=version.at_least(db.connection(), (1, 1))) # returning finalize args = [["a"], {}, {"count": 0}, "function (obj, prev) { prev.count++; }", "function(obj){ return obj.count;}"] expected = [2, # a:2 1, # a:None 1] # a:1 group_checker(args, expected, with_command=version.at_least(db.connection(), (1, 1))) self.assertRaises(OperationFailure, db.test.group, [], {}, {}, "5 ++ 5") self.assertRaises(OperationFailure, db.test.group, [], {}, {}, "5 ++ 5", command=True)