本文整理汇总了Python中test.utils.rs_or_single_client函数的典型用法代码示例。如果您正苦于以下问题:Python rs_or_single_client函数的具体用法?Python rs_or_single_client怎么用?Python rs_or_single_client使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了rs_or_single_client函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_auth_from_uri
def test_auth_from_uri(self):
self.client.admin.add_user("admin", "pass", roles=["root"])
self.addCleanup(self.client.admin.remove_user, "admin")
self.addCleanup(remove_all_users, self.client.pymongo_test)
self.client.pymongo_test.add_user("user", "pass", roles=["userAdmin", "readWrite"])
with self.assertRaises(OperationFailure):
connected(rs_or_single_client("mongodb://a:[email protected]%s:%d" % (host, port)))
# No error.
connected(rs_or_single_client_noauth("mongodb://admin:[email protected]%s:%d" % (host, port)))
# Wrong database.
uri = "mongodb://admin:[email protected]%s:%d/pymongo_test" % (host, port)
with self.assertRaises(OperationFailure):
connected(rs_or_single_client(uri))
# No error.
connected(rs_or_single_client_noauth("mongodb://user:[email protected]%s:%d/pymongo_test" % (host, port)))
# Auth with lazy connection.
rs_or_single_client(
"mongodb://user:[email protected]%s:%d/pymongo_test" % (host, port), connect=False
).pymongo_test.test.find_one()
# Wrong password.
bad_client = rs_or_single_client("mongodb://user:[email protected]%s:%d/pymongo_test" % (host, port), connect=False)
self.assertRaises(OperationFailure, bad_client.pymongo_test.test.find_one)
示例2: test_mongo_client
def test_mongo_client(self):
pair = client_context.pair
m = rs_or_single_client(w=0)
coll = m.pymongo_test.write_concern_test
coll.drop()
doc = {"_id": ObjectId()}
coll.insert_one(doc)
self.assertTrue(coll.insert_one(doc))
coll = coll.with_options(write_concern=WriteConcern(w=1))
self.assertRaises(OperationFailure, coll.insert_one, doc)
m = rs_or_single_client()
coll = m.pymongo_test.write_concern_test
new_coll = coll.with_options(write_concern=WriteConcern(w=0))
self.assertTrue(new_coll.insert_one(doc))
self.assertRaises(OperationFailure, coll.insert_one, doc)
m = rs_or_single_client("mongodb://%s/" % (pair,),
replicaSet=client_context.replica_set_name)
coll = m.pymongo_test.write_concern_test
self.assertRaises(OperationFailure, coll.insert_one, doc)
m = rs_or_single_client("mongodb://%s/?w=0" % (pair,),
replicaSet=client_context.replica_set_name)
coll = m.pymongo_test.write_concern_test
coll.insert_one(doc)
# Equality tests
direct = connected(single_client(w=0))
direct2 = connected(single_client("mongodb://%s/?w=0" % (pair,),
**self.credentials))
self.assertEqual(direct, direct2)
self.assertFalse(direct != direct2)
示例3: test_socket_timeout_ms_validation
def test_socket_timeout_ms_validation(self):
c = rs_or_single_client(socketTimeoutMS=10 * 1000)
self.assertEqual(10, get_pool(c).opts.socket_timeout)
c = connected(rs_or_single_client(socketTimeoutMS=None))
self.assertEqual(None, get_pool(c).opts.socket_timeout)
self.assertRaises(ValueError, rs_or_single_client, socketTimeoutMS=0)
self.assertRaises(ValueError, rs_or_single_client, socketTimeoutMS=-1)
self.assertRaises(ValueError, rs_or_single_client, socketTimeoutMS=1e10)
self.assertRaises(ValueError, rs_or_single_client, socketTimeoutMS="foo")
示例4: test_lazy_connect_w0
def test_lazy_connect_w0(self):
# Ensure that connect-on-demand works when the first operation is
# an unacknowledged write. This exercises _writable_max_wire_version().
# Use a separate collection to avoid races where we're still
# completing an operation on a collection while the next test begins.
client = rs_or_single_client(connect=False, w=0)
client.test_lazy_connect_w0.test.insert_one({})
client = rs_or_single_client(connect=False)
client.test_lazy_connect_w0.test.update_one({}, {'$set': {'x': 1}})
client = rs_or_single_client(connect=False)
client.test_lazy_connect_w0.test.delete_one({})
示例5: test_max_staleness_float
def test_max_staleness_float(self):
with self.assertRaises(TypeError) as ctx:
rs_or_single_client(maxStalenessSeconds=1.5,
readPreference="nearest")
self.assertIn("must be an integer", str(ctx.exception))
with warnings.catch_warnings(record=True) as ctx:
warnings.simplefilter("always")
client = MongoClient("mongodb://host/?maxStalenessSeconds=1.5"
"&readPreference=nearest")
# Option was ignored.
self.assertEqual(-1, client.read_preference.max_staleness)
self.assertIn("must be an integer", str(ctx[0]))
示例6: setUpClass
def setUpClass(cls):
cls.listener = EventListener()
cls.saved_listeners = monitoring._LISTENERS
monitoring._LISTENERS = monitoring._Listeners([], [], [], [])
cls.client = rs_or_single_client(event_listeners=[cls.listener])
cls.db = cls.client.pymongo_test
cls.collation = Collation('en_US')
示例7: test_init_disconnected
def test_init_disconnected(self):
c = rs_or_single_client(connect=False)
self.assertIsInstance(c.is_primary, bool)
self.assertIsInstance(c.is_mongos, bool)
self.assertIsInstance(c.max_pool_size, int)
self.assertIsInstance(c.nodes, frozenset)
self.assertEqual(c.codec_options, CodecOptions())
self.assertIsInstance(c.max_bson_size, int)
self.assertIsInstance(c.max_write_batch_size, int)
self.assertFalse(c.primary)
self.assertFalse(c.secondaries)
c.pymongo_test.command('ismaster') # Auto-connect.
if client_context.is_rs:
# The primary's host and port are from the replica set config.
self.assertIsNotNone(c.address)
else:
self.assertEqual(c.address, (host, port))
bad_host = "somedomainthatdoesntexist.org"
c = MongoClient(bad_host, port, connectTimeoutMS=1,
serverSelectionTimeoutMS=10)
self.assertRaises(ConnectionFailure, c.pymongo_test.test.find_one)
示例8: setUp
def setUp(self):
self.listener = SessionTestListener()
self.session_checker_listener = SessionTestListener()
self.client = rs_or_single_client(
event_listeners=[self.listener, self.session_checker_listener])
self.db = self.client.pymongo_test
self.initial_lsids = set(s['id'] for s in session_ids(self.client))
示例9: test_implicit_session_logout
def test_implicit_session_logout(self):
listener = SessionTestListener()
# Changing auth doesn't invalidate the session. Start as root.
client = rs_or_single_client(event_listeners=[listener])
db = client.pymongo_test
for name, f in [
('bulk_write', lambda: db.collection.bulk_write([InsertOne({})])),
('collection_names', db.collection_names),
('find_one', db.collection.find_one),
('aggregate', lambda: list(db.collection.aggregate([])))
]:
def sub_test():
listener.results.clear()
f()
for event in listener.results['started']:
self.assertIn(
'lsid', event.command,
"%s sent no lsid with %s" % (
name, event.command_name))
# We switch auth without clearing the pool of session ids. The
# server considers these to be new sessions since it's a new user.
# The old sessions time out on the server after 30 minutes.
client.admin.logout()
db.authenticate('second-user', 'pass')
sub_test()
db.logout()
client.admin.authenticate(db_user, db_pwd)
sub_test()
示例10: test_close_kills_cursor_synchronously
def test_close_kills_cursor_synchronously(self):
# Kill any cursors possibly queued up by previous tests.
gc.collect()
self.client._process_periodic_tasks()
listener = WhiteListEventListener("killCursors")
results = listener.results
client = rs_or_single_client(event_listeners=[listener])
self.addCleanup(client.close)
coll = client[self.db.name].test_close_kills_cursors
# Add some test data.
docs_inserted = 1000
coll.insert_many([{"i": i} for i in range(docs_inserted)])
results.clear()
# Close the cursor while it's still open on the server.
cursor = coll.find().batch_size(10)
self.assertTrue(bool(next(cursor)))
self.assertLess(cursor.retrieved, docs_inserted)
cursor.close()
# Test that the cursor was closed.
self.assertEqual(1, len(results["started"]))
self.assertEqual("killCursors", results["started"][0].command_name)
self.assertEqual(1, len(results["succeeded"]))
self.assertEqual("killCursors", results["succeeded"][0].command_name)
示例11: test_list_collection_names_filter
def test_list_collection_names_filter(self):
listener = OvertCommandListener()
results = listener.results
client = rs_or_single_client(event_listeners=[listener])
db = client[self.db.name]
db.capped.drop()
db.create_collection("capped", capped=True, size=4096)
db.capped.insert_one({})
db.non_capped.insert_one({})
self.addCleanup(client.drop_database, db.name)
# Should not send nameOnly.
for filter in ({'options.capped': True},
{'options.capped': True, 'name': 'capped'}):
results.clear()
names = db.list_collection_names(filter=filter)
self.assertEqual(names, ["capped"])
self.assertNotIn("nameOnly", results["started"][0].command)
# Should send nameOnly (except on 2.6).
for filter in (None, {}, {'name': {'$in': ['capped', 'non_capped']}}):
results.clear()
names = db.list_collection_names(filter=filter)
self.assertIn("capped", names)
self.assertIn("non_capped", names)
command = results["started"][0].command
if client_context.version >= (3, 0):
self.assertIn("nameOnly", command)
self.assertTrue(command["nameOnly"])
else:
self.assertNotIn("nameOnly", command)
示例12: test_errors
def test_errors(self):
with ignore_deprecations():
# We must call getlasterror, etc. on same socket as last operation.
db = rs_or_single_client(maxPoolSize=1).pymongo_test
db.reset_error_history()
self.assertEqual(None, db.error())
self.assertEqual(None, db.previous_error())
db.command("forceerror", check=False)
self.assertTrue(db.error())
self.assertTrue(db.previous_error())
db.command("forceerror", check=False)
self.assertTrue(db.error())
prev_error = db.previous_error()
self.assertEqual(prev_error["nPrev"], 1)
del prev_error["nPrev"]
prev_error.pop("lastOp", None)
error = db.error()
error.pop("lastOp", None)
# getLastError includes "connectionId" in recent
# server versions, getPrevError does not.
error.pop("connectionId", None)
self.assertEqual(error, prev_error)
db.test.find_one()
self.assertEqual(None, db.error())
self.assertTrue(db.previous_error())
self.assertEqual(db.previous_error()["nPrev"], 2)
db.reset_error_history()
self.assertEqual(None, db.error())
self.assertEqual(None, db.previous_error())
示例13: test_cursor_transfer
def test_cursor_transfer(self):
# This is just a test, don't try this at home...
client = rs_or_single_client()
db = client.pymongo_test
db.test.delete_many({})
db.test.insert_many([{'_id': i} for i in range(200)])
class CManager(CursorManager):
def __init__(self, client):
super(CManager, self).__init__(client)
def close(self, dummy, dummy2):
# Do absolutely nothing...
pass
client.set_cursor_manager(CManager)
docs = []
cursor = db.test.find().batch_size(10)
docs.append(next(cursor))
cursor.close()
docs.extend(cursor)
self.assertEqual(len(docs), 10)
cmd_cursor = {'id': cursor.cursor_id, 'firstBatch': []}
ccursor = CommandCursor(cursor.collection, cmd_cursor,
cursor.address, retrieved=cursor.retrieved)
docs.extend(ccursor)
self.assertEqual(len(docs), 200)
示例14: test_auth_network_error
def test_auth_network_error(self):
# Make sure there's no semaphore leak if we get a network error
# when authenticating a new socket with cached credentials.
# Get a client with one socket so we detect if it's leaked.
c = connected(rs_or_single_client(maxPoolSize=1,
waitQueueTimeoutMS=1))
# Simulate an authenticate() call on a different socket.
credentials = auth._build_credentials_tuple(
'DEFAULT', 'admin', db_user, db_pwd, {})
c._cache_credentials('test', credentials, connect=False)
# Cause a network error on the actual socket.
pool = get_pool(c)
socket_info = one(pool.sockets)
socket_info.sock.close()
# SocketInfo.check_auth logs in with the new credential, but gets a
# socket.error. Should be reraised as AutoReconnect.
self.assertRaises(AutoReconnect, c.test.collection.find_one)
# No semaphore leak, the pool is allowed to make a new socket.
c.test.collection.find_one()
示例15: test_retry_timeout_raises_original_error
def test_retry_timeout_raises_original_error(self):
"""A ServerSelectionTimeoutError on the retry attempt raises the
original error.
"""
listener = OvertCommandListener()
client = rs_or_single_client(
retryWrites=True, event_listeners=[listener])
self.addCleanup(client.close)
topology = client._topology
select_server = topology.select_server
def mock_select_server(*args, **kwargs):
server = select_server(*args, **kwargs)
def raise_error(*args, **kwargs):
raise ServerSelectionTimeoutError(
'No primary available for writes')
# Raise ServerSelectionTimeout on the retry attempt.
topology.select_server = raise_error
return server
for method, args, kwargs in retryable_single_statement_ops(
client.db.retryable_write_test):
msg = '%s(*%r, **%r)' % (method.__name__, args, kwargs)
listener.results.clear()
topology.select_server = mock_select_server
with self.assertRaises(ConnectionFailure, msg=msg):
method(*args, **kwargs)
self.assertEqual(len(listener.results['started']), 1, msg)