本文整理汇总了Python中mongoengine.connection.get_connection函数的典型用法代码示例。如果您正苦于以下问题:Python get_connection函数的具体用法?Python get_connection怎么用?Python get_connection使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_connection函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_connection_kwargs
def test_connection_kwargs(self):
"""Ensure that connection kwargs get passed to pymongo."""
connect('mongoenginetest', alias='t1', tz_aware=True)
conn = get_connection('t1')
self.assertTrue(get_tz_awareness(conn))
connect('mongoenginetest2', alias='t2')
conn = get_connection('t2')
self.assertFalse(get_tz_awareness(conn))
示例2: test_sharing_connections
def test_sharing_connections(self):
"""Ensure that connections are shared when the connection settings are exactly the same
"""
connect('mongoenginetest', alias='testdb1')
expected_connection = get_connection('testdb1')
connect('mongoenginetest', alias='testdb2')
actual_connection = get_connection('testdb2')
self.assertEqual(expected_connection, actual_connection)
示例3: test_connection_kwargs
def test_connection_kwargs(self):
"""Ensure that connection kwargs get passed to pymongo.
"""
connect("mongoenginetest", alias="t1", tz_aware=True)
conn = get_connection("t1")
self.assertTrue(conn.tz_aware)
connect("mongoenginetest2", alias="t2")
conn = get_connection("t2")
self.assertFalse(conn.tz_aware)
示例4: test_connect
def test_connect(self):
"""Ensure that the connect() method works properly."""
connect('mongoenginetest')
conn = get_connection()
self.assertIsInstance(conn, pymongo.mongo_client.MongoClient)
db = get_db()
self.assertIsInstance(db, pymongo.database.Database)
self.assertEqual(db.name, 'mongoenginetest')
connect('mongoenginetest2', alias='testdb')
conn = get_connection('testdb')
self.assertIsInstance(conn, pymongo.mongo_client.MongoClient)
示例5: disconnect
def disconnect(alias=DEFAULT_CONNECTION_NAME):
""" To disconnect pymongo connection.
Copied from mongoengine/connection.py to fix a bug in mongoengine source code,
('disconnect' method is removed from pymongo MongoClient in latest version.)
"""
global _connections
global _dbs
if alias in _connections:
get_connection(alias=alias).close()
del _connections[alias]
if alias in _dbs:
del _dbs[alias]
示例6: test_connect_in_mocking
def test_connect_in_mocking(self):
"""Ensure that the connect() method works properly in mocking.
"""
try:
import mongomock
except ImportError:
raise SkipTest('you need mongomock installed to run this testcase')
connect('mongoenginetest', host='mongomock://localhost')
conn = get_connection()
self.assertTrue(isinstance(conn, mongomock.MongoClient))
connect('mongoenginetest2', host='mongomock://localhost', alias='testdb')
conn = get_connection('testdb')
self.assertTrue(isinstance(conn, mongomock.MongoClient))
示例7: test_connect
def test_connect(self):
"""Ensure that the connect() method works properly.
"""
connect('mongoenginetest')
conn = get_connection()
self.assertTrue(isinstance(conn, pymongo.connection.Connection))
db = get_db()
self.assertTrue(isinstance(db, pymongo.database.Database))
self.assertEqual(db.name, 'mongoenginetest')
connect('mongoenginetest2', alias='testdb')
conn = get_connection('testdb')
self.assertTrue(isinstance(conn, pymongo.connection.Connection))
示例8: test_sharing_connections
def test_sharing_connections(self):
"""Ensure that connections are shared when the connection settings are exactly the same
"""
connect('mongoenginetests', alias='testdb1')
expected_connection = get_connection('testdb1')
connect('mongoenginetests', alias='testdb2')
actual_connection = get_connection('testdb2')
# Handle PyMongo 3+ Async Connection
if IS_PYMONGO_3:
# Ensure we are connected, throws ServerSelectionTimeoutError otherwise.
# Purposely not catching exception to fail test if thrown.
expected_connection.server_info()
self.assertEqual(expected_connection, actual_connection)
示例9: teardown_databases
def teardown_databases(self, *args, **kwargs):
for alias, params in self._iter_test_databases():
connection = get_connection(alias)
print("Dropping test database for alias '%s': %s" % (alias, params['name']))
connection.drop_database(params['name'])
disconnect(alias)
return super(TestRunner, self).teardown_databases(*args, **kwargs)
示例10: test_register_connection_defaults
def test_register_connection_defaults(self):
"""Ensure that defaults are used when the host and port are None.
"""
register_connection('testdb', 'mongoenginetest', host=None, port=None)
conn = get_connection('testdb')
self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))
示例11: teardown_databases
def teardown_databases(self, old_config, **kwargs):
from mongoengine.connection import get_connection, disconnect
connection = get_connection()
connection.drop_database(self.mongodb_name)
print 'Dropping mongo test database: ' + self.mongodb_name
disconnect()
super(MongoTestRunner, self).teardown_databases(old_config, **kwargs)
示例12: setUp
def setUp(self):
# データベースに接続
addr = '127.0.0.1'
port = 27017
connect('test', host=addr, port=port)
self.conn = get_connection()
self.db = get_db()
示例13: test_connect_uri
def test_connect_uri(self):
"""Ensure that the connect() method works properly with URIs."""
c = connect(db='mongoenginetest', alias='admin')
c.admin.system.users.remove({})
c.mongoenginetest.system.users.remove({})
c.admin.add_user("admin", "password")
c.admin.authenticate("admin", "password")
c.mongoenginetest.add_user("username", "password")
if not IS_PYMONGO_3:
self.assertRaises(
MongoEngineConnectionError, connect, 'testdb_uri_bad',
host='mongodb://test:[email protected]'
)
connect("testdb_uri", host='mongodb://username:[email protected]/mongoenginetest')
conn = get_connection()
self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))
db = get_db()
self.assertTrue(isinstance(db, pymongo.database.Database))
self.assertEqual(db.name, 'mongoenginetest')
c.admin.system.users.remove({})
c.mongoenginetest.system.users.remove({})
示例14: get_mongodb_version
def get_mongodb_version():
"""Return the version of the connected mongoDB (first 2 digits)
:return: tuple(int, int)
"""
version_list = get_connection().server_info()['versionArray'][:2] # e.g: (3, 2)
return tuple(version_list)
示例15: test_connect_uri
def test_connect_uri(self):
"""Ensure that the connect() method works properly with URIs."""
c = connect(db='mongoenginetest', alias='admin')
c.admin.system.users.delete_many({})
c.mongoenginetest.system.users.delete_many({})
c.admin.command("createUser", "admin", pwd="password", roles=["root"])
c.admin.authenticate("admin", "password")
c.admin.command("createUser", "username", pwd="password", roles=["dbOwner"])
if not IS_PYMONGO_3:
self.assertRaises(
MongoEngineConnectionError, connect, 'testdb_uri_bad',
host='mongodb://test:[email protected]'
)
connect("testdb_uri", host='mongodb://username:[email protected]/mongoenginetest')
conn = get_connection()
self.assertIsInstance(conn, pymongo.mongo_client.MongoClient)
db = get_db()
self.assertIsInstance(db, pymongo.database.Database)
self.assertEqual(db.name, 'mongoenginetest')
c.admin.system.users.delete_many({})
c.mongoenginetest.system.users.delete_many({})