本文整理汇总了Python中flask.ext.pymongo.MongoClient.drop_database方法的典型用法代码示例。如果您正苦于以下问题:Python MongoClient.drop_database方法的具体用法?Python MongoClient.drop_database怎么用?Python MongoClient.drop_database使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask.ext.pymongo.MongoClient
的用法示例。
在下文中一共展示了MongoClient.drop_database方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestMultiMongo
# 需要导入模块: from flask.ext.pymongo import MongoClient [as 别名]
# 或者: from flask.ext.pymongo.MongoClient import drop_database [as 别名]
class TestMultiMongo(TestBase):
def setUp(self):
super(TestMultiMongo, self).setUp()
self.setupDB2()
schema = {
'author': {'type': 'string'},
'title': {'type': 'string'},
}
settings = {
'schema': schema,
'mongo_prefix': 'MONGO1'
}
self.app.register_resource('works', settings)
def tearDown(self):
super(TestMultiMongo, self).tearDown()
self.dropDB2()
def setupDB2(self):
self.connection = MongoClient()
self.connection.drop_database(MONGO1_DBNAME)
self.connection[MONGO1_DBNAME].add_user(MONGO1_USERNAME,
MONGO1_PASSWORD)
self.bulk_insert2()
def dropDB2(self):
self.connection = MongoClient()
self.connection.drop_database(MONGO1_DBNAME)
self.connection.close()
def bulk_insert2(self):
_db = self.connection[MONGO1_DBNAME]
works = self.random_works(self.known_resource_count)
_db.works.insert(works)
self.work = _db.works.find_one()
self.connection.close()
def random_works(self, num):
works = []
for i in range(num):
dt = datetime.now()
work = {
'author': self.random_string(20),
'title': self.random_string(30),
eve.LAST_UPDATED: dt,
eve.DATE_CREATED: dt,
}
works.append(work)
return works
示例2: TestMinimal
# 需要导入模块: from flask.ext.pymongo import MongoClient [as 别名]
# 或者: from flask.ext.pymongo.MongoClient import drop_database [as 别名]
#.........这里部分代码省略.........
def assertResourceLink(self, links, resource):
self.assertTrue('self' in links)
link = links['self']
self.assertTrue('title' in link)
self.assertTrue('href' in link)
url = self.domain[resource]['url']
self.assertEqual(url, link['title'])
self.assertEqual("%s/%s" % (self._get_server_name(),
url),
link['href'])
def assertCollectionLink(self, links, resource):
self.assertTrue('collection' in links)
link = links['collection']
self.assertTrue('title' in link)
self.assertTrue('href' in link)
url = self.domain[resource]['url']
self.assertEqual(url, link['title'])
self.assertEqual("%s/%s" % (self._get_server_name(),
url), link['href'])
def assertNextLink(self, links, page):
self.assertTrue('next' in links)
link = links['next']
self.assertTrue('title' in link)
self.assertTrue('href' in link)
self.assertEqual('next page', link['title'])
self.assertTrue("page=%d" % page in link['href'])
def assertPrevLink(self, links, page):
self.assertTrue('prev' in links)
link = links['prev']
self.assertTrue('title' in link)
self.assertTrue('href' in link)
self.assertEqual('previous page', link['title'])
if page > 1:
self.assertTrue("page=%d" % page in link['href'])
def assertItemLink(self, links, item_id):
self.assertTrue('self' in links)
link = links['self']
# TODO we are too deep here to get a hold of the due title. Should fix.
self.assertTrue('title' in link)
self.assertTrue('href' in link)
self.assertTrue('/%s' % item_id in link['href'])
def assertLastLink(self, links, page):
if page:
self.assertTrue('last' in links)
link = links['last']
self.assertTrue('title' in link)
self.assertTrue('href' in link)
self.assertEqual('last page', link['title'])
self.assertTrue("page=%d" % page in link['href'])
else:
self.assertTrue('last' not in links)
def assert400(self, status):
self.assertEqual(status, 400)
def assert401(self, status):
self.assertEqual(status, 401)
def assert401or405(self, status):
self.assertTrue(status == 401 or 405)
def assert403(self, status):
self.assertEqual(status, 403)
def assert405(self, status):
self.assertEqual(status, 405)
def assert412(self, status):
self.assertEqual(status, 412)
def assert500(self, status):
self.assertEqual(status, 500)
def setupDB(self):
self.connection = MongoClient(MONGO_HOST, MONGO_PORT)
self.connection.drop_database(MONGO_DBNAME)
if MONGO_USERNAME:
self.connection[MONGO_DBNAME].add_user(MONGO_USERNAME,
MONGO_PASSWORD)
self.bulk_insert()
def bulk_insert(self):
pass
def _get_server_name(self):
server_name = self.app.config.get('SERVER_NAME', '')
url_protocol = self.app.config.get('URL_PROTOCOL', '')
if url_protocol:
server_name = '%s://%s' % (url_protocol, server_name)
return server_name
def dropDB(self):
self.connection = MongoClient(MONGO_HOST, MONGO_PORT)
self.connection.drop_database(MONGO_DBNAME)
self.connection.close()
示例3: TestBase
# 需要导入模块: from flask.ext.pymongo import MongoClient [as 别名]
# 或者: from flask.ext.pymongo.MongoClient import drop_database [as 别名]
class TestBase(TestMinimal):
DEVICES = 'devices'
DEVICE_EVENT = 'events/devices'
EVENTS = 'events'
PLACES = 'places'
SNAPSHOT = 'snapshot'
ACCOUNTS = 'accounts'
def setUp(self, settings_file=None, url_converters=None):
from ereuse_devicehub import default_settings as settings
self.set_settings(settings)
self.app = DeviceHub()
self.prepare()
@staticmethod
def set_settings(settings):
settings.MONGO_DBNAME = 'devicehubtest'
settings.DATABASES = 'dht1', 'dht2' # Some tests use 2 databases
settings.DHT1_DBNAME = 'dht1_'
settings.DHT2_DBNAME = 'dht2_'
settings.GRD_DEBUG = True # We do not want to actually fulfill GRD
settings.APP_NAME = 'DeviceHub'
settings.DEBUG = True
settings.TESTING = True
settings.LOG = True
settings.GRD = False
settings.AGENT_ACCOUNTS = {
'self': ('[email protected]', '12345')
}
def prepare(self):
self.MONGO_DBNAME = self.app.config['MONGO_DBNAME']
self.MONGO_HOST = self.app.config['MONGO_HOST']
self.MONGO_PORT = self.app.config['MONGO_PORT']
self.DATABASES = self.app.config['DATABASES']
self.connection = None
self.setupDB()
self.test_client = self.app.test_client()
self.domain = self.app.config['DOMAIN']
self.token = self._login()
self.auth_header = ('authorization', 'Basic ' + self.token)
def setupDB(self):
self.connection = MongoClient(self.MONGO_HOST, self.MONGO_PORT)
self.db = self.connection[self.MONGO_DBNAME]
self.drop_databases()
self.create_dummy_user()
self.create_self_machine_account()
# We call the method again as we have erased the DB
self.app.grd_submitter_caller = SubmitterCaller(self.app, GRDSubmitter)
# self.app.grd_submitter_caller.token = self.app.grd_submitter_caller.prepare_user(self.app)
# self.app.grd_submitter_caller.process = None
def create_dummy_user(self):
self.db.accounts.insert(
{
'email': "[email protected]",
'password': AccountDomain.encrypt_password('1234'),
'role': 'admin',
'token': 'NOFATDNNUB',
'databases': self.app.config['DATABASES'],
'defaultDatabase': self.app.config['DATABASES'][0],
'@type': 'Account'
}
)
self.account = self.db.accounts.find_one({'email': '[email protected]'})
def create_self_machine_account(self):
email, password = self.app.config['AGENT_ACCOUNTS']['self']
self.db.accounts.insert(
{
'role': 'superuser',
'token': 'QYADFBPNZZDFJEWAFGGF',
'databases': self.app.config['DATABASES'],
'@type': 'Account',
'email': email,
'password': AccountDomain.encrypt_password(password)
}
)
def tearDown(self):
self.dropDB()
del self.app
def drop_databases(self):
self.connection.drop_database(self.MONGO_DBNAME)
for database in self.DATABASES:
self.connection.drop_database(self.app.config[database.upper().replace('-', '') + '_DBNAME'])
def dropDB(self):
self.drop_databases()
self.connection.close()
def full(self, resource_name: str, resource: dict or str or ObjectId) -> dict:
return resource if type(resource) is dict else self.get(resource_name, '', str(resource))[0]
def select_database(self, url):
#.........这里部分代码省略.........
示例4: TestMinimal
# 需要导入模块: from flask.ext.pymongo import MongoClient [as 别名]
# 或者: from flask.ext.pymongo.MongoClient import drop_database [as 别名]
#.........这里部分代码省略.........
def assertHomeLink(self, links):
self.assertTrue('parent' in links)
link = links['parent']
self.assertTrue('title' in link)
self.assertTrue('href' in link)
self.assertEqual('home', link['title'])
self.assertEqual("%s" % self.app.config['SERVER_NAME'], link['href'])
def assertResourceLink(self, links, resource):
self.assertTrue('self' in links)
link = links['self']
self.assertTrue('title' in link)
self.assertTrue('href' in link)
url = self.domain[resource]['url']
self.assertEqual(url, link['title'])
self.assertEqual("%s/%s/" % (self.app.config['SERVER_NAME'], url),
link['href'])
def assertCollectionLink(self, links, resource):
self.assertTrue('collection' in links)
link = links['collection']
self.assertTrue('title' in link)
self.assertTrue('href' in link)
url = self.domain[resource]['url']
self.assertEqual(url, link['title'])
self.assertEqual("%s/%s/" % (self.app.config['SERVER_NAME'], url),
link['href'])
def assertNextLink(self, links, page):
self.assertTrue('next' in links)
link = links['next']
self.assertTrue('title' in link)
self.assertTrue('href' in link)
self.assertEqual('next page', link['title'])
self.assertTrue("page=%d" % page in link['href'])
def assertPrevLink(self, links, page):
self.assertTrue('prev' in links)
link = links['prev']
self.assertTrue('title' in link)
self.assertTrue('href' in link)
self.assertEqual('previous page', link['title'])
if page > 1:
self.assertTrue("page=%d" % page in link['href'])
def assertItemLink(self, links, item_id):
self.assertTrue('self' in links)
link = links['self']
#TODO we are too deep here to get a hold of the due title. Should fix.
self.assertTrue('title' in link)
self.assertTrue('href' in link)
self.assertTrue('/%s/' % item_id in link['href'])
def assertLastLink(self, links, page):
if page:
self.assertTrue('last' in links)
link = links['last']
self.assertTrue('title' in link)
self.assertTrue('href' in link)
self.assertEqual('last page', link['title'])
self.assertTrue("page=%d" % page in link['href'])
else:
self.assertTrue('last' not in links)
def assert400(self, status):
self.assertEqual(status, 400)
def assert401(self, status):
self.assertEqual(status, 401)
def assert401or405(self, status):
self.assertTrue(status == 401 or 405)
def assert403(self, status):
self.assertEqual(status, 403)
def assert405(self, status):
self.assertEqual(status, 405)
def assert412(self, status):
self.assertEqual(status, 412)
def assert500(self, status):
self.assertEqual(status, 500)
def setupDB(self):
self.connection = MongoClient(MONGO_HOST, MONGO_PORT)
self.connection.drop_database(MONGO_DBNAME)
if MONGO_USERNAME:
self.connection[MONGO_DBNAME].add_user(MONGO_USERNAME,
MONGO_PASSWORD)
self.bulk_insert()
def bulk_insert(self):
pass
def dropDB(self):
self.connection = MongoClient(MONGO_HOST, MONGO_PORT)
self.connection.drop_database(MONGO_DBNAME)
self.connection.close()
示例5: TestBase
# 需要导入模块: from flask.ext.pymongo import MongoClient [as 别名]
# 或者: from flask.ext.pymongo.MongoClient import drop_database [as 别名]
class TestBase(unittest.TestCase):
def setUp(self):
self.setupDB()
self.settings_file = 'eve/tests/test_settings.py'
self.app = Eve(settings=self.settings_file)
self.test_client = self.app.test_client()
self.domain = self.app.config['DOMAIN']
self.known_resource = 'contacts'
self.known_resource_url = ('/%s/' %
self.domain[self.known_resource]['url'])
self.empty_resource = 'empty'
self.empty_resource_url = '/%s/' % self.empty_resource
self.unknown_resource = 'unknown'
self.unknown_resource_url = '/%s/' % self.unknown_resource
self.unknown_item_id = '4f46445fc88e201858000000'
self.unknown_item_name = 'unknown'
self.unknown_item_id_url = ('/%s/%s/' %
(self.domain[self.known_resource]['url'],
self.unknown_item_id))
self.unknown_item_name_url = ('/%s/%s/' %
(self.domain[self.known_resource]['url'],
self.unknown_item_name))
self.readonly_resource = 'payments'
self.readonly_resource_url = (
'/%s/' % self.domain[self.readonly_resource]['url'])
self.different_resource = 'users'
self.different_resource_url = ('/%s/' %
self.domain[
self.different_resource]['url'])
def tearDown(self):
self.dropDB()
def assert200(self, status):
self.assertEqual(status, 200)
def assert301(self, status):
self.assertEqual(status, 301)
def assert404(self, status):
self.assertEqual(status, 404)
def assert304(self, status):
self.assertEqual(status, 304)
def setupDB(self):
self.connection = MongoClient()
self.connection.drop_database(MONGO_DBNAME)
self.connection[MONGO_DBNAME].add_user(MONGO_USERNAME, MONGO_PASSWORD)
self.bulk_insert()
def bulk_insert(self):
_db = self.connection[MONGO_DBNAME]
_db.contacts.insert(self.random_contacts(100))
_db.contacts.insert(self.random_users(2))
_db.payments.insert(self.random_payments(10))
_db.invoices.insert(self.random_invoices(1))
self.connection.close()
def dropDB(self):
self.connection = MongoClient()
self.connection.drop_database(MONGO_DBNAME)
self.connection.close()
def random_contacts(self, num, standard_date_fields=True):
schema = DOMAIN['contacts']['schema']
contacts = []
for i in range(num):
dt = datetime.now()
contact = {
'ref': self.random_string(schema['ref']['maxlength']),
'prog': i,
'role': random.choice(schema['role']['allowed']),
'rows': self.random_rows(random.randint(0, 5)),
'alist': self.random_list(random.randint(0, 5)),
'location': {
'address': 'address ' + self.random_string(5),
'city': 'city ' + self.random_string(3),
},
'born': datetime.today() + timedelta(
days=random.randint(-10, 10)),
'tid': ObjectId(),
}
if standard_date_fields:
contact[eve.LAST_UPDATED] = dt
contact[eve.DATE_CREATED] = dt
contacts.append(contact)
return contacts
def random_users(self, num):
users = self.random_contacts(num)
#.........这里部分代码省略.........
示例6: TestBase
# 需要导入模块: from flask.ext.pymongo import MongoClient [as 别名]
# 或者: from flask.ext.pymongo.MongoClient import drop_database [as 别名]
class TestBase(unittest.TestCase):
def setUp(self):
""" Prepare the test fixture """
self.setupDB()
self.app = run.app
self.domain = self.app.config['DOMAIN']
self.test_client = self.app.test_client()
self.valid_auth = [('Authorization', 'Basic dG9rZW4xOg==')]
self.headers = [('Content-Type', 'application/json')] + self.valid_auth
def tearDown(self):
self.dropDB()
def assert200(self, status):
self.assertEqual(status, 200)
def assert201(self, status):
self.assertEqual(status, 201)
def assert204(self, status):
self.assertEqual(status, 204)
def assert401(self, status):
self.assertEqual(status, 401)
def setupDB(self):
self.connection = MongoClient(MONGO_HOST, MONGO_PORT)
self.connection.drop_database(MONGO_DBNAME)
if MONGO_USERNAME:
self.connection[MONGO_DBNAME].add_user(MONGO_USERNAME,
MONGO_PASSWORD)
db = self.connection[MONGO_DBNAME]
self.accounts_insert(db)
self.company_insert(db)
db.connection.close()
def accounts_insert(self, db):
user_accounts = [
{'u': '[email protected]', 'p': 'pw1', 't': 'token1', 'r': ['app']},
{'u': '[email protected]', 'p': 'pw1', 't': 'token2', 'r': ['user']},
]
db.user_accounts.insert(user_accounts)
def company_insert(self, db):
company = {'n': 'test_company', 'p': 'pw1'}
self.company_id = str(db.companies.insert(company))
def dropDB(self):
self.connection = MongoClient(MONGO_HOST, MONGO_PORT)
self.connection.drop_database(MONGO_DBNAME)
self.connection.close()
def post(self, url, payload):
r = self.test_client.post(url, data=json.dumps(payload),
headers=self.headers)
return self.parse_response(r)
def put(self, url, payload, etag):
headers = self.headers + [('If-Match', etag)]
r = self.test_client.put(url, data=json.dumps(payload),
headers=headers)
return self.parse_response(r)
def delete(self, url, etag):
headers = self.headers + [('If-Match', etag)]
r = self.test_client.delete(url, headers=headers)
return self.parse_response(r)
def get(self, url):
r = self.test_client.get(url, headers=self.headers)
return self.parse_response(r)
def parse_response(self, r):
try:
v = json.loads(r.get_data())
except json.JSONDecodeError:
v = None
return v, r.status_code
示例7: TestMinimal
# 需要导入模块: from flask.ext.pymongo import MongoClient [as 别名]
# 或者: from flask.ext.pymongo.MongoClient import drop_database [as 别名]
#.........这里部分代码省略.........
self.config = self.app.config
self.DataMessage = {
'message_content': u'020f5be1018446df5314512d41b6028446df5' +
'339ecb6427438df53cee69f4255038446df538dc656427438df530dbe' +
'9442642adf53011aa641'
}
self.DeployMessage = {
'message_content': u'030f5b310026032229c58b436b4003e1b655'
}
def tearDown(self):
super(TestMinimal, self).tearDown()
self.dropDB()
self.app_context.pop()
del self.app
def assert200(self, status):
self.assertEqual(status, 200)
def assert201(self, status):
self.assertEqual(status, 201)
def assert301(self, status):
self.assertEqual(status, 301)
def assert404(self, status):
self.assertEqual(status, 404)
def assert304(self, status):
self.assertEqual(status, 304)
def get(self, resource, query='', item=None, headers=None):
if resource in self.domain:
resource = self.domain[resource]['url']
if item:
request = '/%s/%s%s' % (resource, item, query)
else:
request = '/%s%s' % (resource, query)
r = self.test_client.get(request)
return self.parse_response(r)
def post(self, url, data, headers=None, content_type='application/json'):
if headers is None:
headers = []
headers.append(('Content-Type', content_type))
r = self.test_client.post(url, data=json.dumps(data), headers=headers)
return self.parse_response(r)
def put(self, url, data, headers=None):
if headers is None:
headers = []
headers.append(('Content-Type', 'application/json'))
r = self.test_client.put(url, data=json.dumps(data), headers=headers)
return self.parse_response(r)
def patch(self, url, data, headers=None):
if headers is None:
headers = []
headers.append(('Content-Type', 'application/json'))
r = self.test_client.patch(url, data=json.dumps(data), headers=headers)
return self.parse_response(r)
def delete(self, url, headers=None):
r = self.test_client.delete(url, headers=headers)
return self.parse_response(r)
def bulk_insert(self):
pass
def parse_response(self, r):
try:
v = json.loads(r.get_data())
except json.JSONDecodeError:
v = None
return v, r.status_code
def setupDB(self):
settings = self.app.config['MONGODB_SETTINGS']
self.connection = MongoClient(settings['HOST'],
settings['PORT'])
self.connection.drop_database(settings['DB'])
if 'USERNAME' in settings:
self.connection[settings['DB']].add_user(
settings['USERNAME'], settings['PASSWORD'])
self.Pod = models.Pod
self.User = models.User
self.Message = models.Message
self.Notebook = models.Notebook
self.Sensor = models.Sensor
self.Data = models.Data
self.bulk_insert()
def dropDB(self):
settings = self.app.config['MONGODB_SETTINGS']
self.connection = MongoClient(
settings['HOST'],
settings['PORT'])
self.connection.drop_database(settings['DB'])
self.connection.close()
示例8: TestMinimal
# 需要导入模块: from flask.ext.pymongo import MongoClient [as 别名]
# 或者: from flask.ext.pymongo.MongoClient import drop_database [as 别名]
class TestMinimal(TestBase):
def setUp(self):
""" Prepare the test fixture
"""
self.setupDB()
super(TestMinimal, self).setUp()
self.app = Eve(settings='settings.py', auth=run.Auth)
self.test_client = self.app.test_client()
self.domain = self.app.config['DOMAIN']
def tearDown(self):
self.dropDB()
def assert200(self, status):
self.assertEqual(status, 200)
def assert301(self, status):
self.assertEqual(status, 301)
def assert404(self, status):
self.assertEqual(status, 404)
def assert304(self, status):
self.assertEqual(status, 304)
def assert400(self, status):
self.assertEqual(status, 400)
def assert401(self, status):
self.assertEqual(status, 401)
def assert401or405(self, status):
self.assertTrue(status == 401 or 405)
def assert403(self, status):
self.assertEqual(status, 403)
def assert405(self, status):
self.assertEqual(status, 405)
def assert412(self, status):
self.assertEqual(status, 412)
def assert500(self, status):
self.assertEqual(status, 500)
def setupDB(self):
self.connection = MongoClient(MONGO_HOST, MONGO_PORT)
self.connection.drop_database(MONGO_DBNAME)
if MONGO_USERNAME:
self.connection[MONGO_DBNAME].add_user(MONGO_USERNAME,
MONGO_PASSWORD)
self.bulk_insert()
def bulk_insert(self):
accounts = [
{'u': '[email protected]', 'p': 'pw1', 't': 'token1', 'r': ['app']},
{'u': '[email protected]', 'p': 'pw1', 't': 'token2', 'r': ['user']},
]
_db = self.connection[MONGO_DBNAME]
_db.accounts.insert(accounts)
self.connection.close()
def dropDB(self):
self.connection = MongoClient(MONGO_HOST, MONGO_PORT)
self.connection.drop_database(MONGO_DBNAME)
self.connection.close()