当前位置: 首页>>代码示例>>Python>>正文


Python Database.connection方法代码示例

本文整理汇总了Python中pymongo.database.Database.connection方法的典型用法代码示例。如果您正苦于以下问题:Python Database.connection方法的具体用法?Python Database.connection怎么用?Python Database.connection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pymongo.database.Database的用法示例。


在下文中一共展示了Database.connection方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestCursor

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import connection [as 别名]
class TestCursor(unittest.TestCase):
    def setUp(self):
        self.db = Database(get_connection(), "pymongo_test")

    def test_explain(self):
        a = self.db.test.find()
        b = a.explain()
        for _ in a:
            break
        c = a.explain()
        del b["millis"]
        b.pop("oldPlan", None)
        del c["millis"]
        c.pop("oldPlan", None)
        self.assertEqual(b, c)
        self.assert_("cursor" in b)

    def test_hint(self):
        db = self.db
        self.assertRaises(TypeError, db.test.find().hint, 5.5)
        db.test.remove({})
        db.test.drop_indexes()

        for i in range(100):
            db.test.insert({"num": i, "foo": i})

        self.assertRaises(OperationFailure, db.test.find({"num": 17, "foo": 17}).hint([("num", ASCENDING)]).explain)
        self.assertRaises(OperationFailure, db.test.find({"num": 17, "foo": 17}).hint([("foo", ASCENDING)]).explain)

        index = db.test.create_index("num")

        spec = [("num", ASCENDING)]
        self.assertEqual(db.test.find({}).explain()["cursor"], "BasicCursor")
        self.assertEqual(db.test.find({}).hint(spec).explain()["cursor"], "BtreeCursor %s" % index)
        self.assertEqual(db.test.find({}).hint(spec).hint(None).explain()["cursor"], "BasicCursor")
        self.assertRaises(OperationFailure, db.test.find({"num": 17, "foo": 17}).hint([("foo", ASCENDING)]).explain)

        a = db.test.find({"num": 17})
        a.hint(spec)
        for _ in a:
            break
        self.assertRaises(InvalidOperation, a.hint, spec)

        self.assertRaises(TypeError, db.test.find().hint, index)

    # This is deprecated - test that a warning is actually raised
    def test_slave_okay(self):
        db = self.db
        db.drop_collection("test")
        db.test.save({"x": 1})

        warnings.simplefilter("error")

        self.assertEqual(1, db.test.find().next()["x"])
        self.assertRaises(DeprecationWarning, db.test.find, slave_okay=True)
        self.assertRaises(DeprecationWarning, db.test.find, slave_okay=False)

        warnings.simplefilter("default")

    def test_limit(self):
        db = self.db

        self.assertRaises(TypeError, db.test.find().limit, None)
        self.assertRaises(TypeError, db.test.find().limit, "hello")
        self.assertRaises(TypeError, db.test.find().limit, 5.5)

        db.test.remove({})
        for i in range(100):
            db.test.save({"x": i})

        count = 0
        for _ in db.test.find():
            count += 1
        self.assertEqual(count, 100)

        count = 0
        for _ in db.test.find().limit(20):
            count += 1
        self.assertEqual(count, 20)

        count = 0
        for _ in db.test.find().limit(99):
            count += 1
        self.assertEqual(count, 99)

        count = 0
        for _ in db.test.find().limit(1):
            count += 1
        self.assertEqual(count, 1)

        count = 0
        for _ in db.test.find().limit(0):
            count += 1
        self.assertEqual(count, 100)

        count = 0
        for _ in db.test.find().limit(0).limit(50).limit(10):
            count += 1
        self.assertEqual(count, 10)

#.........这里部分代码省略.........
开发者ID:virtix,项目名称:mongo-python-driver,代码行数:103,代码来源:test_cursor.py


注:本文中的pymongo.database.Database.connection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。