本文整理汇总了Python中pymongo.connection.Connection.drop_database方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.drop_database方法的具体用法?Python Connection.drop_database怎么用?Python Connection.drop_database使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.connection.Connection
的用法示例。
在下文中一共展示了Connection.drop_database方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestConfig
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
class TestConfig(unittest.TestCase):
def setUp(self):
filename = dirname(__file__) + '/logging-test.config'
fileConfig(filename)
""" Create an empty database that could be used for logging """
self.db_name = '_mongolog_test'
self.collection_name = 'log_test'
self.conn = Connection('localhost')
self.conn.drop_database(self.db_name)
def tearDown(self):
""" Drop used database """
self.conn.drop_database(self.db_name)
def testLoggingFileConfiguration(self):
log = logging.getLogger('example')
log.debug('test')
r = self.conn[self.db_name][self.collection_name]
message = r.find_one({'level': 'debug', 'msg': 'test'})
self.assertEquals(message['msg'], 'test')
示例2: test_copy_db
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
def test_copy_db(self):
c = Connection(self.host, self.port)
self.assertTrue(c.in_request())
self.assertRaises(TypeError, c.copy_database, 4, "foo")
self.assertRaises(TypeError, c.copy_database, "foo", 4)
self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")
c.pymongo_test.test.drop()
c.drop_database("pymongo_test1")
c.drop_database("pymongo_test2")
c.pymongo_test.test.insert({"foo": "bar"})
# Due to SERVER-2329, databases may not disappear from a master in a
# master-slave pair
if not server_is_master_with_slave(c):
self.assertFalse("pymongo_test1" in c.database_names())
self.assertFalse("pymongo_test2" in c.database_names())
c.copy_database("pymongo_test", "pymongo_test1")
# copy_database() didn't accidentally end the request
self.assertTrue(c.in_request())
self.assertTrue("pymongo_test1" in c.database_names())
self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
c.end_request()
self.assertFalse(c.in_request())
c.copy_database("pymongo_test", "pymongo_test2", "%s:%d" % (self.host, self.port))
# copy_database() didn't accidentally restart the request
self.assertFalse(c.in_request())
self.assertTrue("pymongo_test2" in c.database_names())
self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"])
if version.at_least(c, (1, 3, 3, 1)):
c.drop_database("pymongo_test1")
c.pymongo_test.add_user("mike", "password")
self.assertRaises(
OperationFailure, c.copy_database, "pymongo_test", "pymongo_test1", username="foo", password="bar"
)
if not server_is_master_with_slave(c):
self.assertFalse("pymongo_test1" in c.database_names())
self.assertRaises(
OperationFailure, c.copy_database, "pymongo_test", "pymongo_test1", username="mike", password="bar"
)
if not server_is_master_with_slave(c):
self.assertFalse("pymongo_test1" in c.database_names())
if not is_mongos(c):
# See SERVER-6427
c.copy_database("pymongo_test", "pymongo_test1", username="mike", password="password")
self.assertTrue("pymongo_test1" in c.database_names())
self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
示例3: setUp
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
def setUp(self):
"""
set up DAS core module
"""
debug = 0
self.das = DASCore(debug=debug)
config = deepcopy(das_readconfig())
dburi = config['mongodb']['dburi']
connection = Connection(dburi)
connection.drop_database('das')
示例4: setUp
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
def setUp(self):
"""
set up DAS core module
"""
debug = 0
config = deepcopy(das_readconfig())
logger = PrintManager('TestDASMongocache', verbose=debug)
config['logger'] = logger
config['verbose'] = debug
dburi = config['mongodb']['dburi']
connection = Connection(dburi)
connection.drop_database('das')
dasmapping = DASMapping(config)
config['dasmapping'] = dasmapping
self.dasmongocache = DASMongocache(config)
示例5: generate_doc_content
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
def generate_doc_content():
generate_random_content()
conn = Connection()
conn.drop_database("peduli")
print("Adding users.", sep="")
for i in range(50):
create_new_user()
print(".", end="")
print(".\nAdding contents.", end="")
for i in range(500):
create_new_content(i)
print(".", end="")
sys.stdout.flush()
print(".")
示例6: test_copy_db
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
def test_copy_db(self):
c = Connection(self.host, self.port)
self.assertRaises(TypeError, c.copy_database, 4, "foo")
self.assertRaises(TypeError, c.copy_database, "foo", 4)
self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")
c.pymongo_test.test.drop()
c.drop_database("pymongo_test1")
c.drop_database("pymongo_test2")
c.pymongo_test.test.insert({"foo": "bar"})
self.assertFalse("pymongo_test1" in c.database_names())
self.assertFalse("pymongo_test2" in c.database_names())
c.copy_database("pymongo_test", "pymongo_test1")
self.assert_("pymongo_test1" in c.database_names())
self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
c.copy_database("pymongo_test", "pymongo_test2",
"%s:%s" % (self.host, self.port))
self.assert_("pymongo_test2" in c.database_names())
self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"])
if version.at_least(c, (1, 3, 3, 1)):
c.drop_database("pymongo_test1")
c.pymongo_test.add_user("mike", "password")
self.assertRaises(OperationFailure, c.copy_database,
"pymongo_test", "pymongo_test1",
username="foo", password="bar")
self.assertFalse("pymongo_test1" in c.database_names())
self.assertRaises(OperationFailure, c.copy_database,
"pymongo_test", "pymongo_test1",
username="mike", password="bar")
self.assertFalse("pymongo_test1" in c.database_names())
c.copy_database("pymongo_test", "pymongo_test1",
username="mike", password="password")
self.assert_("pymongo_test1" in c.database_names())
self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
示例7: TestDictConfig
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
class TestDictConfig(unittest.TestCase):
def setUp(self):
""" Create an empty database that could be used for logging """
self.db_name = '_mongolog_test_dict'
self.collection_name = 'log_test'
self.configDict = {
'version': 1,
'handlers': {
'mongo': {
'class': 'mongolog.handlers.MongoHandler',
'db': self.db_name,
'collection': self.collection_name,
'level': 'INFO'
}
},
'root': {
'handlers': ['mongo'],
'level': 'INFO'
}
}
self.conn = Connection('localhost')
self.conn.drop_database(self.db_name)
def testLoggingDictConfiguration(self):
dictConfig(self.configDict)
log = logging.getLogger('dict_example')
log.debug('testing dictionary config')
r = self.conn[self.db_name][self.collection_name]
message = r.find_one({'levelname':'DEBUG', 'msg':'dict_example'})
self.assertEquals(message, None,
"Logger put debug message in when info level handler requested")
log.info('dict_example')
message = r.find_one({'levelname':'INFO', 'msg':'dict_example'})
self.assertNotEquals(message, None,
"Logger didn't insert message into database")
self.assertEquals(message['msg'], 'dict_example',
"Logger didn't insert correct message into database")
def tearDown(self):
""" Drop used database """
self.conn.drop_database(self.db_name)
示例8: TestRootLoggerHandler
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
class TestRootLoggerHandler(unittest.TestCase):
"""
Test Handler attached to RootLogger
"""
def setUp(self):
""" Create an empty database that could be used for logging """
self.db_name = '_mongolog_test'
self.conn = Connection('localhost')
self.conn.drop_database(self.db_name)
self.db = self.conn[self.db_name]
self.collection = self.db['log']
def tearDown(self):
""" Drop used database """
self.conn.drop_database('_mongolog_test')
def testLogging(self):
""" Simple logging example """
log = logging.getLogger('')
log.setLevel(logging.DEBUG)
log.addHandler(MongoHandler(self.collection))
log.debug('test')
r = self.collection.find_one({'levelname':'DEBUG', 'msg':'test'})
self.assertEquals(r['msg'], 'test')
def testLoggingException(self):
""" Logging example with exception """
log = logging.getLogger('')
log.setLevel(logging.DEBUG)
log.addHandler(MongoHandler(self.collection))
try:
1/0
except ZeroDivisionError:
log.error('test zero division', exc_info=True)
r = self.collection.find_one({'levelname':'ERROR', 'msg':'test zero division'})
self.assertTrue(r['exc_info'].startswith('Traceback'))
示例9: test_drop_database
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
def test_drop_database(self):
connection = Connection(self.host, self.port)
self.assertRaises(TypeError, connection.drop_database, 5)
self.assertRaises(TypeError, connection.drop_database, None)
connection.pymongo_test.test.save({"dummy": u"object"})
dbs = connection.database_names()
self.assert_("pymongo_test" in dbs)
connection.drop_database("pymongo_test")
dbs = connection.database_names()
self.assert_("pymongo_test" not in dbs)
connection.pymongo_test.test.save({"dummy": u"object"})
dbs = connection.database_names()
self.assert_("pymongo_test" in dbs)
connection.drop_database(connection.pymongo_test)
dbs = connection.database_names()
self.assert_("pymongo_test" not in dbs)
示例10: TestMongosHighAvailability
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
class TestMongosHighAvailability(unittest.TestCase):
def setUp(self):
seed_list = ha_tools.create_sharded_cluster()
self.dbname = 'pymongo_mongos_ha'
self.conn = Connection(seed_list)
self.conn.drop_database(self.dbname)
def test_mongos_ha(self):
coll = self.conn[self.dbname].test
self.assertTrue(coll.insert({'foo': 'bar'}, safe=True))
first = '%s:%d' % (self.conn.host, self.conn.port)
ha_tools.kill_mongos(first)
# Fail first attempt
self.assertRaises(AutoReconnect, coll.count)
# Find new mongos
self.assertEqual(1, coll.count())
second = '%s:%d' % (self.conn.host, self.conn.port)
self.assertNotEqual(first, second)
ha_tools.kill_mongos(second)
# Fail first attempt
self.assertRaises(AutoReconnect, coll.count)
# Find new mongos
self.assertEqual(1, coll.count())
third = '%s:%d' % (self.conn.host, self.conn.port)
self.assertNotEqual(second, third)
ha_tools.kill_mongos(third)
# Fail first attempt
self.assertRaises(AutoReconnect, coll.count)
# We've killed all three, restart one.
ha_tools.restart_mongos(first)
# Find new mongos
self.assertEqual(1, coll.count())
def tearDown(self):
self.conn.drop_database(self.dbname)
ha_tools.kill_all_members()
示例11: TestAutoReferencePatch
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
class TestAutoReferencePatch(unittest.TestCase):
def setUp(self):
self.connection = Connection()
self.connection.drop_database("test_autoreference_patch")
self.db = self.connection["test_autoreference_patch"]
self.db.add_son_manipulator(NamespaceInjector())
self.db.add_son_manipulator(AutoReference(self.db))
messages = [{'title': i} for i in range(1000)]
self.db.messages.insert(messages)
user = {'name': 'tom', 'messages': messages}
self.db.users.save(user)
def test_patch(self):
start = time.time()
original = self.db.users.find_one()
print time.time() - start
import autoreference_patch
start = time.time()
patched = self.db.users.find_one()
print time.time() - start
self.assertEqual(original, patched)
示例12: test_drop_database
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
def test_drop_database(self):
connection = Connection(self.host, self.port)
self.assertRaises(TypeError, connection.drop_database, 5)
self.assertRaises(TypeError, connection.drop_database, None)
raise SkipTest("This test often fails due to SERVER-2329")
connection.pymongo_test.test.save({"dummy": u"object"})
dbs = connection.database_names()
self.assertTrue("pymongo_test" in dbs)
connection.drop_database("pymongo_test")
dbs = connection.database_names()
self.assertTrue("pymongo_test" not in dbs)
connection.pymongo_test.test.save({"dummy": u"object"})
dbs = connection.database_names()
self.assertTrue("pymongo_test" in dbs)
connection.drop_database(connection.pymongo_test)
dbs = connection.database_names()
self.assertTrue("pymongo_test" not in dbs)
示例13: setUp
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
def setUp(self):
"""
set up DAS core module
"""
debug = 0
self.db = 'test_mapping.db'
config = deepcopy(das_readconfig())
dburi = config['mongodb']['dburi']
logger = PrintManager('TestDASMapping', verbose=debug)
config['logger'] = logger
config['verbose'] = debug
dbname = 'test_mapping'
collname = 'db'
config['mappingdb'] = dict(dburi=dburi, dbname=dbname, collname=collname)
# add some maps to mapping db
conn = Connection(dburi)
conn.drop_database(dbname)
coll = conn[dbname][collname]
self.pmap = {"presentation": {"block":[{"ui": "Block name", "das": "block.name"},
{"ui": "Block size", "das": "block.size"}]}}
coll.insert(self.pmap)
self.mgr = DASMapping(config)
示例14: TestRootLoggerHandler
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
class TestRootLoggerHandler(unittest.TestCase):
"""
Test Handler attached to RootLogger
"""
def setUp(self):
""" Create an empty database that could be used for logging """
self.db_name = "_mongolog_test"
self.conn = Connection("localhost")
self.conn.drop_database(self.db_name)
self.db = self.conn[self.db_name]
self.collection = self.db["log"]
self.log = logging.getLogger("")
self.log.setLevel(logging.DEBUG)
self.handler = MongoHandler(self.collection)
self.log.addHandler(self.handler)
def tearDown(self):
""" Drop used database """
self.conn.drop_database(self.db_name)
self.log.removeHandler(self.handler)
def testLogging(self):
""" Simple logging example """
self.log.debug("test")
r = self.collection.find_one({"levelname": "DEBUG", "msg": "test"})
self.assertEquals(r["msg"], "test")
def testLoggingException(self):
""" Logging example with exception """
try:
1 / 0
except ZeroDivisionError:
self.log.error("test zero division", exc_info=True)
r = self.collection.find_one({"levelname": "ERROR", "msg": "test zero division"})
self.assertTrue(r["exc_info"].startswith("Traceback"))
def testQueryableMessages(self):
""" Logging example with dictionary """
self.log.info({"address": "340 N 12th St", "state": "PA", "country": "US"})
self.log.info({"address": "340 S 12th St", "state": "PA", "country": "US"})
self.log.info({"address": "1234 Market St", "state": "PA", "country": "US"})
cursor = self.collection.find({"levelname": "INFO", "msg.address": "340 N 12th St"})
self.assertEquals(cursor.count(), 1, "Expected query to return 1 " "message; it returned %d" % cursor.count())
self.assertEquals(cursor[0]["msg"]["address"], "340 N 12th St")
cursor = self.collection.find({"levelname": "INFO", "msg.state": "PA"})
self.assertEquals(cursor.count(), 3, "Didn't find all three documents")
def testFormatter(self):
formatString = "%(message)s from %(levelname)s"
self.handler.setFormatter(MongoFormatter(formatString))
self.log.info("%s within a message", "message")
document = self.collection.find_one()
self.assertEquals(document["message"], "message within a message from" " INFO")
def testNoneArgs(self):
""" Logging example with "None" as logging args """
self.log.info("This is a string %s with no args", None)
示例15: Connection
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import drop_database [as 别名]
Then do:
$ python auto_reference.py
"""
from pymongo.connection import Connection
from pymongo.son_manipulator import AutoReference, NamespaceInjector
# Make a connection to Mongo.
try:
connection = Connection("localhost", 27017)
except ConnectionFailure:
print "couldn't connect: be sure that Mongo is running on localhost:27017"
sys.exit(1)
# We need a database to use, but first make sure it's clean.
connection.drop_database("pymongo_examples")
db = connection["pymongo_examples"]
# Now we need to add the NamespaceInjector and AutoReference manipulators.
# These are what actually handle translating documents as the enter and exit
# MongoDB.
db.add_son_manipulator(NamespaceInjector()) # inject _ns
db.add_son_manipulator(AutoReference(db))
# Save a message to the database.
message = {"title": "foo"}
db.messages.save(message)
# Now save a user - the message will get automatically referenced (converted
# to a DBRef) since we've already saved it to the database.
user = {"name": "hello", "message": message}