本文整理匯總了Python中mongomock.MongoClient方法的典型用法代碼示例。如果您正苦於以下問題:Python mongomock.MongoClient方法的具體用法?Python mongomock.MongoClient怎麽用?Python mongomock.MongoClient使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mongomock
的用法示例。
在下文中一共展示了mongomock.MongoClient方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: pymongo_client
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def pymongo_client(request):
"""Get a client to the mongo database"""
LOG.info("Get a mongomock client")
start_time = datetime.datetime.now()
mock_client = MongoClient()
def teardown():
print("\n")
LOG.info("Deleting database")
mock_client.drop_database(DATABASE)
LOG.info("Database deleted")
LOG.info("Time to run test:{}".format(datetime.datetime.now() - start_time))
request.addfinalizer(teardown)
return mock_client
示例2: test_aggregate
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def test_aggregate(self):
collection = mongomock.MongoClient().db.collection
objs = [
{
'test_id': '1',
'test_status': 'success'
},
{
'test_id': '2',
'test_status': 'failure'
},
{
'test_id': '3',
'test_status': 'success'
}
]
collection.insert(objs)
aggregate_query = [
{"$match": {'test_status': 'success'}}
]
results = self.hook.aggregate(collection, aggregate_query)
self.assertEqual(len(list(results)), 2)
示例3: test_logging_to_mongo
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def test_logging_to_mongo(self):
"""
Test the mongo log handler and logging to mongo
"""
assert ml.MongoClient is MockMongoClient
handler = ml.MongoHandler(level=logging.DEBUG)
self.assertIsInstance(handler.connection, MockMongoClient)
# Ensure there is nothing in the database.
self.assertEqual(handler.collection.count(), 0)
# Create the logging instance.
logger = logging.getLogger('test.mongo.logger.demo')
logger.setLevel(logging.INFO)
logger.addHandler(handler)
# Log a message
logger.info(tmsgf("This is a test of the mongo logger"))
# Ensure there is now a log message
self.assertEqual(handler.collection.count(), 1)
示例4: user_obj
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def user_obj(request, parsed_user):
"""Return a User object"""
_user_obj = build_user(parsed_user)
return _user_obj
#############################################################
##################### Adapter fixtures #####################
#############################################################
# We need to monkeypatch 'connect' function so the tests use a mongomock database
# @pytest.fixture(autouse=True)
# def no_connect(monkeypatch):
# # from scout.adapter.client import get_connection
# mongo = Mock(return_value=MongoClient())
# print('hej')
#
# monkeypatch.setattr('scout.adapter.client.get_connection', mongo)
示例5: real_pymongo_client
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def real_pymongo_client(request):
"""Get a client to the mongo database"""
LOG.info("Get a real pymongo client")
start_time = datetime.datetime.now()
mongo_client = pymongo.MongoClient()
def teardown():
print("\n")
LOG.info("Deleting database")
mongo_client.drop_database(REAL_DATABASE)
LOG.info("Database deleted")
LOG.info("Time to run test:{}".format(datetime.datetime.now() - start_time))
request.addfinalizer(teardown)
return mongo_client
示例6: test_connection
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def test_connection(monkeypatch):
def simple_mongo():
return mongomock.MongoClient()
monkeypatch.setattr(scout.adapter.client, "get_connection", simple_mongo)
client = scout.adapter.client.get_connection()
assert isinstance(client, mongomock.MongoClient)
# ##GIVEN a connection to a mongodatabase
# print('du')
# ##WHEN getting a mongo client
# ##THEN assert that the port is default
# print(client)
# assert False
# assert client.PORT == 27017
# def test_get_connection_uri(pymongo_client):
# ##GIVEN a connection to a mongodatabase
# uri = 'mongomock://'
# client = get_connection(uri=uri)
# ##WHEN getting a mongo client
# ##THEN assert that the port is default
# assert client.PORT == 27017
示例7: test_mongo_backend_package_used
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def test_mongo_backend_package_used():
import pymongo
import mongomock
from optimade.server.entry_collections import client
force_mongo_env_var = os.environ.get("OPTIMADE_CI_FORCE_MONGO", None)
if force_mongo_env_var is None:
return
if int(force_mongo_env_var) == 1:
assert issubclass(client.__class__, pymongo.MongoClient)
elif int(force_mongo_env_var) == 0:
assert issubclass(client.__class__, mongomock.MongoClient)
else:
raise Exception(
"The environment variable OPTIMADE_CI_FORCE_MONGO cannot be parsed as an int."
)
示例8: app
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def app():
"""Return api instance that uses mocked os.environ, ElasticSearch and MongoClient"""
test_env = {
'SCITRAN_CORE_DRONE_SECRET': SCITRAN_CORE_DRONE_SECRET,
'TERM': 'xterm', # enable terminal features - useful for pdb sessions
}
env_patch = mock.patch.dict(os.environ, test_env, clear=True)
env_patch.start()
es_patch = mock.patch('elasticsearch.Elasticsearch')
es_patch.start()
mongo_patch = mock.patch('pymongo.MongoClient', new=mongomock.MongoClient)
mongo_patch.start()
# NOTE db and log_db is created at import time in api.config
# reloading the module is needed to use the mocked MongoClient
reload(api.config)
yield api.web.start.app_factory()
mongo_patch.stop()
es_patch.stop()
env_patch.stop()
示例9: test_get_conn
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def test_get_conn(self):
self.assertEqual(self.hook.connection.port, 27017)
self.assertIsInstance(self.conn, pymongo.MongoClient)
示例10: test_insert_one
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def test_insert_one(self):
collection = mongomock.MongoClient().db.collection
obj = {'test_insert_one': 'test_value'}
self.hook.insert_one(collection, obj)
result_obj = collection.find_one(filter=obj)
self.assertEqual(obj, result_obj)
示例11: test_insert_many
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def test_insert_many(self):
collection = mongomock.MongoClient().db.collection
objs = [
{'test_insert_many_1': 'test_value'},
{'test_insert_many_2': 'test_value'}
]
self.hook.insert_many(collection, objs)
result_objs = list(collection.find())
self.assertEqual(len(result_objs), 2)
示例12: test_update_one
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def test_update_one(self):
collection = mongomock.MongoClient().db.collection
obj = {'_id': '1', 'field': 0}
collection.insert_one(obj)
filter_doc = obj
update_doc = {'$inc': {'field': 123}}
self.hook.update_one(collection, filter_doc, update_doc)
result_obj = collection.find_one(filter='1')
self.assertEqual(123, result_obj['field'])
示例13: test_update_one_with_upsert
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def test_update_one_with_upsert(self):
collection = mongomock.MongoClient().db.collection
filter_doc = {'_id': '1', 'field': 0}
update_doc = {'$inc': {'field': 123}}
self.hook.update_one(collection, filter_doc, update_doc, upsert=True)
result_obj = collection.find_one(filter='1')
self.assertEqual(123, result_obj['field'])
示例14: test_update_many_with_upsert
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def test_update_many_with_upsert(self):
collection = mongomock.MongoClient().db.collection
filter_doc = {'_id': '1', 'field': 0}
update_doc = {'$inc': {'field': 123}}
self.hook.update_many(collection, filter_doc, update_doc, upsert=True)
result_obj = collection.find_one(filter='1')
self.assertEqual(123, result_obj['field'])
示例15: test_replace_one
# 需要導入模塊: import mongomock [as 別名]
# 或者: from mongomock import MongoClient [as 別名]
def test_replace_one(self):
collection = mongomock.MongoClient().db.collection
obj1 = {'_id': '1', 'field': 'test_value_1'}
obj2 = {'_id': '2', 'field': 'test_value_2'}
collection.insert_many([obj1, obj2])
obj1['field'] = 'test_value_1_updated'
self.hook.replace_one(collection, obj1)
result_obj = collection.find_one(filter='1')
self.assertEqual('test_value_1_updated', result_obj['field'])
# Other document should stay intact
result_obj = collection.find_one(filter='2')
self.assertEqual('test_value_2', result_obj['field'])