Tests use MongoClient instead of Connection PYTHON-451
This commit is contained in:
parent
a43f51e159
commit
d35bec12e5
@ -234,7 +234,7 @@ class Pool:
|
||||
- `pair`: optional (hostname, port) tuple
|
||||
"""
|
||||
# We use the pid here to avoid issues with fork / multiprocessing.
|
||||
# See test.test_connection:TestConnection.test_fork for an example of
|
||||
# See test.test_connection:TestClient.test_fork for an example of
|
||||
# what could go wrong otherwise
|
||||
if self.pid != os.getpid():
|
||||
self.reset()
|
||||
|
||||
@ -15,11 +15,11 @@
|
||||
"""Clean up databases after running `nosetests`.
|
||||
"""
|
||||
|
||||
from test.test_connection import get_connection
|
||||
from test.test_connection import get_client
|
||||
|
||||
|
||||
def teardown():
|
||||
c = get_connection()
|
||||
c = get_client()
|
||||
|
||||
c.drop_database("pymongo-pooling-tests")
|
||||
c.drop_database("pymongo_test")
|
||||
|
||||
@ -243,9 +243,9 @@ def create_sharded_cluster(num_routers=3):
|
||||
return None
|
||||
|
||||
# Add the shard
|
||||
conn = pymongo.Connection(host)
|
||||
client = pymongo.Connection(host)
|
||||
try:
|
||||
conn.admin.command({'addshard': shard_host})
|
||||
client.admin.command({'addshard': shard_host})
|
||||
except pymongo.errors.OperationFailure:
|
||||
# Already configured.
|
||||
pass
|
||||
|
||||
@ -116,7 +116,7 @@ class TestDirectConnection(unittest.TestCase):
|
||||
AutoReconnect, conn.pymongo_test.test.find_one)
|
||||
|
||||
# Since an attempt at an acknowledged write to a secondary from a
|
||||
# direct connection raises AutoReconnect('not master'), Connection
|
||||
# direct connection raises AutoReconnect('not master'), MongoClient
|
||||
# should do the same for unacknowledged writes.
|
||||
try:
|
||||
conn.pymongo_test.test.insert({}, safe=False)
|
||||
|
||||
@ -12,8 +12,8 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Minimal test of PyMongo in a WSGI application with ReplicaSetConnection, see
|
||||
bug PYTHON-353.
|
||||
"""Minimal test of PyMongo in a WSGI application with MongoReplicaSetClient,
|
||||
see bug PYTHON-353.
|
||||
"""
|
||||
|
||||
import os
|
||||
@ -26,16 +26,17 @@ repository_path = os.path.normpath(os.path.join(this_path, '..', '..'))
|
||||
sys.path.insert(0, repository_path)
|
||||
|
||||
import pymongo
|
||||
from pymongo.replica_set_connection import ReplicaSetConnection
|
||||
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
|
||||
|
||||
connection = ReplicaSetConnection(replicaSet='repl0')
|
||||
collection = connection.test.test
|
||||
# auto_start_request is part of the PYTHON-353 pathology
|
||||
client = MongoReplicaSetClient(replicaSet='repl0', auto_start_request=True)
|
||||
collection = client.test.test
|
||||
|
||||
ndocs = 20
|
||||
|
||||
collection.drop()
|
||||
collection.insert([{'i': i} for i in range(ndocs)], safe=True)
|
||||
connection.disconnect() # discard main thread's request socket
|
||||
collection.insert([{'i': i} for i in range(ndocs)])
|
||||
client.disconnect() # discard main thread's request socket
|
||||
|
||||
try:
|
||||
from mod_wsgi import version as mod_wsgi_version
|
||||
|
||||
@ -25,16 +25,17 @@ repository_path = os.path.normpath(os.path.join(this_path, '..', '..'))
|
||||
sys.path.insert(0, repository_path)
|
||||
|
||||
import pymongo
|
||||
from pymongo.connection import Connection
|
||||
from pymongo.mongo_client import MongoClient
|
||||
|
||||
connection = Connection()
|
||||
collection = connection.test.test
|
||||
# auto_start_request is part of the PYTHON-353 pathology
|
||||
client = MongoClient(auto_start_request=True)
|
||||
collection = client.test.test
|
||||
|
||||
ndocs = 20
|
||||
|
||||
collection.drop()
|
||||
collection.insert([{'i': i} for i in range(ndocs)], safe=True)
|
||||
connection.disconnect() # discard main thread's request socket
|
||||
collection.insert([{'i': i} for i in range(ndocs)])
|
||||
client.disconnect() # discard main thread's request socket
|
||||
|
||||
try:
|
||||
from mod_wsgi import version as mod_wsgi_version
|
||||
|
||||
@ -33,7 +33,7 @@ from bson.binary import *
|
||||
from bson.py3compat import b, binary_type
|
||||
from bson.son import SON
|
||||
from nose.plugins.skip import SkipTest
|
||||
from test.test_connection import get_connection
|
||||
from test.test_connection import get_client
|
||||
|
||||
|
||||
class TestBinary(unittest.TestCase):
|
||||
@ -157,12 +157,12 @@ class TestBinary(unittest.TestCase):
|
||||
self.assertEqual(data, encoded)
|
||||
|
||||
# Test insert and find
|
||||
conn = get_connection()
|
||||
conn.pymongo_test.drop_collection('java_uuid')
|
||||
coll = conn.pymongo_test.java_uuid
|
||||
client = get_client()
|
||||
client.pymongo_test.drop_collection('java_uuid')
|
||||
coll = client.pymongo_test.java_uuid
|
||||
coll.uuid_subtype = JAVA_LEGACY
|
||||
|
||||
coll.insert(docs, safe=True)
|
||||
coll.insert(docs)
|
||||
self.assertEqual(5, coll.count())
|
||||
for d in coll.find():
|
||||
self.assertEqual(d['newguid'], uuid.UUID(d['newguidstring']))
|
||||
@ -170,7 +170,7 @@ class TestBinary(unittest.TestCase):
|
||||
coll.uuid_subtype = OLD_UUID_SUBTYPE
|
||||
for d in coll.find():
|
||||
self.assertNotEqual(d['newguid'], d['newguidstring'])
|
||||
conn.pymongo_test.drop_collection('java_uuid')
|
||||
client.pymongo_test.drop_collection('java_uuid')
|
||||
|
||||
def test_legacy_csharp_uuid(self):
|
||||
if not should_test_uuid:
|
||||
@ -229,12 +229,12 @@ class TestBinary(unittest.TestCase):
|
||||
self.assertEqual(data, encoded)
|
||||
|
||||
# Test insert and find
|
||||
conn = get_connection()
|
||||
conn.pymongo_test.drop_collection('csharp_uuid')
|
||||
coll = conn.pymongo_test.csharp_uuid
|
||||
client = get_client()
|
||||
client.pymongo_test.drop_collection('csharp_uuid')
|
||||
coll = client.pymongo_test.csharp_uuid
|
||||
coll.uuid_subtype = CSHARP_LEGACY
|
||||
|
||||
coll.insert(docs, safe=True)
|
||||
coll.insert(docs)
|
||||
self.assertEqual(5, coll.count())
|
||||
for d in coll.find():
|
||||
self.assertEqual(d['newguid'], uuid.UUID(d['newguidstring']))
|
||||
@ -242,13 +242,13 @@ class TestBinary(unittest.TestCase):
|
||||
coll.uuid_subtype = OLD_UUID_SUBTYPE
|
||||
for d in coll.find():
|
||||
self.assertNotEqual(d['newguid'], d['newguidstring'])
|
||||
conn.pymongo_test.drop_collection('csharp_uuid')
|
||||
client.pymongo_test.drop_collection('csharp_uuid')
|
||||
|
||||
def test_uuid_queries(self):
|
||||
if not should_test_uuid:
|
||||
raise SkipTest("No uuid module")
|
||||
|
||||
c = get_connection()
|
||||
c = get_client()
|
||||
coll = c.pymongo_test.test
|
||||
coll.drop()
|
||||
|
||||
@ -308,5 +308,6 @@ class TestBinary(unittest.TestCase):
|
||||
for proto in xrange(pickle.HIGHEST_PROTOCOL + 1):
|
||||
self.assertEqual(uul, pickle.loads(pickle.dumps(uul, proto)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -46,7 +46,6 @@ from bson.max_key import MaxKey
|
||||
from bson.min_key import MinKey
|
||||
from bson.tz_util import (FixedOffset,
|
||||
utc)
|
||||
import pymongo
|
||||
|
||||
from test import qcheck
|
||||
|
||||
|
||||
@ -43,7 +43,7 @@ from pymongo.errors import (ConfigurationError,
|
||||
InvalidOperation,
|
||||
OperationFailure,
|
||||
TimeoutError)
|
||||
from test.test_connection import get_connection
|
||||
from test.test_connection import get_client
|
||||
from test.utils import is_mongos, joinall
|
||||
from test import (qcheck,
|
||||
version)
|
||||
@ -58,13 +58,13 @@ except ImportError:
|
||||
class TestCollection(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.connection = get_connection()
|
||||
self.db = self.connection.pymongo_test
|
||||
self.client = get_client()
|
||||
self.db = self.client.pymongo_test
|
||||
|
||||
def tearDown(self):
|
||||
self.db.drop_collection("test_large_limit")
|
||||
self.db = None
|
||||
self.connection = None
|
||||
self.client = None
|
||||
|
||||
def test_collection(self):
|
||||
self.assertRaises(TypeError, Collection, self.db, 5)
|
||||
@ -219,14 +219,14 @@ class TestCollection(unittest.TestCase):
|
||||
def test_ensure_unique_index_threaded(self):
|
||||
coll = self.db.test_unique_threaded
|
||||
coll.drop()
|
||||
coll.insert(({'foo': i} for i in xrange(10000)), safe=True)
|
||||
coll.insert(({'foo': i} for i in xrange(10000)))
|
||||
|
||||
class Indexer(threading.Thread):
|
||||
def run(self):
|
||||
try:
|
||||
coll.ensure_index('foo', unique=True)
|
||||
coll.insert({'foo': 'bar'}, safe=True)
|
||||
coll.insert({'foo': 'bar'}, safe=True)
|
||||
coll.insert({'foo': 'bar'})
|
||||
coll.insert({'foo': 'bar'})
|
||||
except OperationFailure:
|
||||
pass
|
||||
|
||||
@ -383,11 +383,11 @@ class TestCollection(unittest.TestCase):
|
||||
}, results[0])
|
||||
|
||||
def test_index_text(self):
|
||||
if not version.at_least(self.connection, (2, 3, 2)):
|
||||
if not version.at_least(self.client, (2, 3, 2)):
|
||||
raise SkipTest("Text search requires server >=2.3.2.")
|
||||
|
||||
self.connection.admin.command('setParameter', '*',
|
||||
textSearchEnabled=True)
|
||||
self.client.admin.command('setParameter', '*',
|
||||
textSearchEnabled=True)
|
||||
|
||||
db = self.db
|
||||
db.test.drop_indexes()
|
||||
@ -396,8 +396,8 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertTrue("weights" in index_info)
|
||||
db.test.drop_indexes()
|
||||
|
||||
self.connection.admin.command('setParameter', '*',
|
||||
textSearchEnabled=False)
|
||||
self.client.admin.command('setParameter', '*',
|
||||
textSearchEnabled=False)
|
||||
|
||||
def test_index_sparse(self):
|
||||
db = self.db
|
||||
@ -544,10 +544,10 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
def test_insert_find_one(self):
|
||||
db = self.db
|
||||
db.test.remove({}, safe=True)
|
||||
db.test.remove({})
|
||||
self.assertEqual(0, len(list(db.test.find())))
|
||||
doc = {"hello": u"world"}
|
||||
id = db.test.insert(doc, safe=True)
|
||||
id = db.test.insert(doc)
|
||||
self.assertEqual(1, len(list(db.test.find())))
|
||||
self.assertEqual(doc, db.test.find_one())
|
||||
self.assertEqual(doc["_id"], id)
|
||||
@ -560,8 +560,8 @@ class TestCollection(unittest.TestCase):
|
||||
doc_class = SON
|
||||
|
||||
def remove_insert_find_one(doc):
|
||||
db.test.remove({}, safe=True)
|
||||
db.test.insert(doc, safe=True)
|
||||
db.test.remove({})
|
||||
db.test.insert(doc)
|
||||
# SON equality is order sensitive.
|
||||
return db.test.find_one(as_class=doc_class) == doc.to_dict()
|
||||
|
||||
@ -678,15 +678,15 @@ class TestCollection(unittest.TestCase):
|
||||
db = self.db
|
||||
db.test.drop()
|
||||
|
||||
db.test.insert({"hello": "world"}, safe=True)
|
||||
db.test.insert({"hello": {"hello": "world"}}, safe=True)
|
||||
db.test.insert({"hello": "world"})
|
||||
db.test.insert({"hello": {"hello": "world"}})
|
||||
|
||||
self.assertRaises(InvalidDocument, db.test.insert, {"$hello": "world"})
|
||||
self.assertRaises(InvalidDocument, db.test.insert,
|
||||
{"hello": {"$hello": "world"}})
|
||||
|
||||
db.test.insert({"he$llo": "world"}, safe=True)
|
||||
db.test.insert({"hello": {"hello$": "world"}}, safe=True)
|
||||
db.test.insert({"he$llo": "world"})
|
||||
db.test.insert({"hello": {"hello$": "world"}})
|
||||
|
||||
self.assertRaises(InvalidDocument, db.test.insert,
|
||||
{".hello": "world"})
|
||||
@ -701,15 +701,13 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertRaises(InvalidDocument, db.test.insert,
|
||||
{"hello": {"hel.lo": "world"}})
|
||||
|
||||
db.test.update({"hello": "world"}, {"$inc": "hello"})
|
||||
|
||||
def test_insert_multiple(self):
|
||||
db = self.db
|
||||
db.drop_collection("test")
|
||||
doc1 = {"hello": u"world"}
|
||||
doc2 = {"hello": u"mike"}
|
||||
self.assertEqual(db.test.find().count(), 0)
|
||||
ids = db.test.insert([doc1, doc2], safe=True)
|
||||
ids = db.test.insert([doc1, doc2])
|
||||
self.assertEqual(db.test.find().count(), 2)
|
||||
self.assertEqual(doc1, db.test.find_one({"hello": u"world"}))
|
||||
self.assertEqual(doc2, db.test.find_one({"hello": u"mike"}))
|
||||
@ -727,15 +725,14 @@ class TestCollection(unittest.TestCase):
|
||||
def test_insert_multiple_with_duplicate(self):
|
||||
db = self.db
|
||||
db.drop_collection("test")
|
||||
db.safe = True
|
||||
db.test.ensure_index([('i', ASCENDING)], unique=True)
|
||||
|
||||
# No error
|
||||
db.test.insert([{'i': i} for i in range(5, 10)], safe=False)
|
||||
db.test.insert([{'i': i} for i in range(5, 10)], w=0)
|
||||
db.test.remove()
|
||||
|
||||
# No error
|
||||
db.test.insert([{'i': 1}] * 2, safe=False)
|
||||
db.test.insert([{'i': 1}] * 2, w=0)
|
||||
self.assertEqual(1, db.test.count())
|
||||
|
||||
self.assertRaises(
|
||||
@ -743,9 +740,8 @@ class TestCollection(unittest.TestCase):
|
||||
lambda: db.test.insert([{'i': 2}] * 2),
|
||||
)
|
||||
|
||||
# Test based on default setting as False
|
||||
db.drop_collection("test")
|
||||
db.safe = False
|
||||
db.write_concern['w'] = 0
|
||||
db.test.ensure_index([('i', ASCENDING)], unique=True)
|
||||
|
||||
# No error
|
||||
@ -755,13 +751,13 @@ class TestCollection(unittest.TestCase):
|
||||
# Implied safe
|
||||
self.assertRaises(
|
||||
DuplicateKeyError,
|
||||
lambda: db.test.insert([{'i': 2}] * 2, w=1),
|
||||
lambda: db.test.insert([{'i': 2}] * 2, j=True),
|
||||
)
|
||||
|
||||
# Explicit safe
|
||||
self.assertRaises(
|
||||
DuplicateKeyError,
|
||||
lambda: db.test.insert([{'i': 2}] * 2, safe=True),
|
||||
lambda: db.test.insert([{'i': 2}] * 2, w=1),
|
||||
)
|
||||
|
||||
# Misconfigured value for safe
|
||||
@ -816,17 +812,12 @@ class TestCollection(unittest.TestCase):
|
||||
# Safe mode
|
||||
self.db.test.create_index("hello", unique=True)
|
||||
# No exception, even though we duplicate the first doc's "hello" value
|
||||
self.db.test.save(
|
||||
{'_id': 'explicit_id', 'hello': 'world'},
|
||||
safe=False
|
||||
)
|
||||
self.db.test.save({'_id': 'explicit_id', 'hello': 'world'}, w=0)
|
||||
|
||||
self.assertRaises(
|
||||
DuplicateKeyError,
|
||||
self.db.test.save,
|
||||
{'_id': 'explicit_id', 'hello': 'world'},
|
||||
safe=True
|
||||
)
|
||||
{'_id': 'explicit_id', 'hello': 'world'})
|
||||
|
||||
def test_save_with_invalid_key(self):
|
||||
self.db.drop_collection("test")
|
||||
@ -851,7 +842,7 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
db.test.save({"hello": "world"})
|
||||
db.test.save({"hello": "mike"})
|
||||
db.test.save({"hello": "world"})
|
||||
db.test.save({"hello": "world"}, w=0)
|
||||
self.assertTrue(db.error())
|
||||
|
||||
def test_duplicate_key_error(self):
|
||||
@ -860,18 +851,18 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
db.test.create_index("x", unique=True)
|
||||
|
||||
db.test.insert({"_id": 1, "x": 1}, safe=True)
|
||||
db.test.insert({"_id": 2, "x": 2}, safe=True)
|
||||
db.test.insert({"_id": 1, "x": 1})
|
||||
db.test.insert({"_id": 2, "x": 2})
|
||||
|
||||
# No error
|
||||
db.test.insert({"_id": 1, "x": 1}, safe=False)
|
||||
db.test.save({"_id": 1, "x": 1}, safe=False)
|
||||
db.test.insert({"_id": 2, "x": 2}, safe=False)
|
||||
db.test.save({"_id": 2, "x": 2}, safe=False)
|
||||
db.test.insert({"_id": 1, "x": 1})
|
||||
db.test.save({"_id": 1, "x": 1})
|
||||
db.test.insert({"_id": 2, "x": 2})
|
||||
db.test.save({"_id": 2, "x": 2})
|
||||
db.test.insert({"_id": 1, "x": 1}, w=0)
|
||||
db.test.save({"_id": 1, "x": 1}, w=0)
|
||||
db.test.insert({"_id": 2, "x": 2}, w=0)
|
||||
db.test.save({"_id": 2, "x": 2}, w=0)
|
||||
|
||||
# But all those statements didn't do anything
|
||||
self.assertEqual(2, db.test.count())
|
||||
@ -881,15 +872,15 @@ class TestCollection(unittest.TestCase):
|
||||
expected_error = DuplicateKeyError
|
||||
|
||||
self.assertRaises(expected_error,
|
||||
db.test.insert, {"_id": 1}, safe=True)
|
||||
db.test.insert, {"_id": 1})
|
||||
self.assertRaises(expected_error,
|
||||
db.test.insert, {"x": 1}, safe=True)
|
||||
db.test.insert, {"x": 1})
|
||||
|
||||
self.assertRaises(expected_error,
|
||||
db.test.save, {"x": 2}, safe=True)
|
||||
db.test.save, {"x": 2})
|
||||
self.assertRaises(expected_error,
|
||||
db.test.update, {"x": 1},
|
||||
{"$inc": {"x": 1}}, safe=True)
|
||||
{"$inc": {"x": 1}})
|
||||
|
||||
def test_continue_on_error(self):
|
||||
db = self.db
|
||||
@ -906,31 +897,31 @@ class TestCollection(unittest.TestCase):
|
||||
docs.append({"four": 4})
|
||||
docs.append({"five": 5})
|
||||
|
||||
db.test.insert(docs, manipulate=False)
|
||||
db.test.insert(docs, manipulate=False, w=0)
|
||||
self.assertEqual(11000, db.error()['code'])
|
||||
self.assertEqual(1, db.test.count())
|
||||
|
||||
db.test.insert(docs, manipulate=False, continue_on_error=True)
|
||||
db.test.insert(docs, manipulate=False, continue_on_error=True, w=0)
|
||||
self.assertEqual(11000, db.error()['code'])
|
||||
self.assertEqual(4, db.test.count())
|
||||
|
||||
db.drop_collection("test")
|
||||
oid = db.test.insert({"_id": oid, "one": 1})
|
||||
oid = db.test.insert({"_id": oid, "one": 1}, w=0)
|
||||
self.assertEqual(1, db.test.count())
|
||||
docs[0].pop("_id")
|
||||
docs[2]["_id"] = oid
|
||||
|
||||
db.test.insert(docs, manipulate=False)
|
||||
db.test.insert(docs, manipulate=False, w=0)
|
||||
self.assertEqual(11000, db.error()['code'])
|
||||
self.assertEqual(3, db.test.count())
|
||||
|
||||
db.test.insert(docs, manipulate=False, continue_on_error=True)
|
||||
db.test.insert(docs, manipulate=False, continue_on_error=True, w=0)
|
||||
self.assertEqual(11000, db.error()['code'])
|
||||
self.assertEqual(6, db.test.count())
|
||||
|
||||
def test_error_code(self):
|
||||
try:
|
||||
self.db.test.update({}, {"$thismodifierdoesntexist": 1}, safe=True)
|
||||
self.db.test.update({}, {"$thismodifierdoesntexist": 1})
|
||||
self.fail()
|
||||
except OperationFailure, e:
|
||||
if version.at_least(self.db.connection, (1, 3)):
|
||||
@ -943,15 +934,14 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.insert({"hello": {"a": 4, "b": 5}})
|
||||
db.test.insert({"hello": {"a": 7, "b": 2}})
|
||||
db.test.insert({"hello": {"a": 4, "b": 10}})
|
||||
self.assertFalse(db.error())
|
||||
|
||||
db.drop_collection("test")
|
||||
db.test.create_index("hello.a", unique=True)
|
||||
|
||||
db.test.insert({"hello": {"a": 4, "b": 5}})
|
||||
db.test.insert({"hello": {"a": 7, "b": 2}})
|
||||
db.test.insert({"hello": {"a": 4, "b": 10}})
|
||||
self.assertTrue(db.error())
|
||||
self.assertRaises(DuplicateKeyError,
|
||||
db.test.insert, {"hello": {"a": 4, "b": 10}})
|
||||
|
||||
def test_safe_insert(self):
|
||||
db = self.db
|
||||
@ -959,10 +949,10 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
a = {"hello": "world"}
|
||||
db.test.insert(a)
|
||||
db.test.insert(a)
|
||||
db.test.insert(a, w=0)
|
||||
self.assertTrue("E11000" in db.error()["err"])
|
||||
|
||||
self.assertRaises(OperationFailure, db.test.insert, a, safe=True)
|
||||
self.assertRaises(OperationFailure, db.test.insert, a)
|
||||
|
||||
def test_update(self):
|
||||
db = self.db
|
||||
@ -995,7 +985,7 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertEqual(5, doc["y"])
|
||||
|
||||
self.assertEqual(2, db.test.update({"x": 4}, {"$set": {"y": 6}},
|
||||
multi=True, safe=True)["n"])
|
||||
multi=True)["n"])
|
||||
|
||||
def test_upsert(self):
|
||||
db = self.db
|
||||
@ -1018,7 +1008,9 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.insert({"x": 5})
|
||||
id = db.test.insert({"x": 4})
|
||||
|
||||
self.assertEqual(None, db.test.update({"_id": id}, {"$inc": {"x": 1}}))
|
||||
self.assertEqual(
|
||||
None, db.test.update({"_id": id}, {"$inc": {"x": 1}}, w=0))
|
||||
|
||||
if v19:
|
||||
self.assertTrue(db.error()["err"].startswith("E11000"))
|
||||
elif v113minus:
|
||||
@ -1027,19 +1019,17 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertTrue(db.error()["err"].startswith("E12011"))
|
||||
|
||||
self.assertRaises(OperationFailure, db.test.update,
|
||||
{"_id": id}, {"$inc": {"x": 1}}, safe=True)
|
||||
{"_id": id}, {"$inc": {"x": 1}})
|
||||
|
||||
self.assertEqual(1, db.test.update({"_id": id},
|
||||
{"$inc": {"x": 2}},
|
||||
safe=True)["n"])
|
||||
{"$inc": {"x": 2}})["n"])
|
||||
|
||||
self.assertEqual(0, db.test.update({"_id": "foo"},
|
||||
{"$inc": {"x": 2}},
|
||||
safe=True)["n"])
|
||||
{"$inc": {"x": 2}})["n"])
|
||||
|
||||
def test_update_with_invalid_keys(self):
|
||||
self.db.drop_collection("test")
|
||||
self.assertTrue(self.db.test.insert({"hello": "world"}, safe=True))
|
||||
self.assertTrue(self.db.test.insert({"hello": "world"}))
|
||||
doc = self.db.test.find_one()
|
||||
doc['a.b'] = 'c'
|
||||
|
||||
@ -1056,13 +1046,13 @@ class TestCollection(unittest.TestCase):
|
||||
# Modify shouldn't check keys...
|
||||
self.assertTrue(self.db.test.update({"hello": "world"},
|
||||
{"$set": {"foo.bar": "baz"}},
|
||||
upsert=True, safe=True))
|
||||
upsert=True))
|
||||
|
||||
# I know this seems like testing the server but I'd like to be notified
|
||||
# by CI if the server's behavior changes here.
|
||||
doc = SON([("$set", {"foo.bar": "bim"}), ("hello", "world")])
|
||||
self.assertRaises(OperationFailure, self.db.test.update,
|
||||
{"hello": "world"}, doc, upsert=True, safe=True)
|
||||
{"hello": "world"}, doc, upsert=True)
|
||||
|
||||
# This is going to cause keys to be checked and raise InvalidDocument.
|
||||
# That's OK assuming the server's behavior in the previous assert
|
||||
@ -1070,11 +1060,11 @@ class TestCollection(unittest.TestCase):
|
||||
# '$' in update won't be good enough anymore.
|
||||
doc = SON([("hello", "world"), ("$set", {"foo.bar": "bim"})])
|
||||
self.assertRaises(InvalidDocument, self.db.test.update,
|
||||
{"hello": "world"}, doc, upsert=True, safe=True)
|
||||
{"hello": "world"}, doc, upsert=True)
|
||||
|
||||
# Replace with empty document
|
||||
self.assertNotEqual(0, self.db.test.update({"hello": "world"},
|
||||
{}, safe=True)['n'])
|
||||
{})['n'])
|
||||
|
||||
def test_safe_save(self):
|
||||
db = self.db
|
||||
@ -1082,11 +1072,11 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.create_index("hello", unique=True)
|
||||
|
||||
db.test.save({"hello": "world"})
|
||||
db.test.save({"hello": "world"})
|
||||
db.test.save({"hello": "world"}, w=0)
|
||||
self.assertTrue("E11000" in db.error()["err"])
|
||||
|
||||
self.assertRaises(OperationFailure, db.test.save,
|
||||
{"hello": "world"}, safe=True)
|
||||
{"hello": "world"})
|
||||
|
||||
def test_safe_remove(self):
|
||||
db = self.db
|
||||
@ -1096,29 +1086,29 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.insert({"x": 1})
|
||||
self.assertEqual(1, db.test.count())
|
||||
|
||||
self.assertEqual(None, db.test.remove({"x": 1}))
|
||||
self.assertEqual(None, db.test.remove({"x": 1}, w=0))
|
||||
self.assertEqual(1, db.test.count())
|
||||
|
||||
if version.at_least(db.connection, (1, 1, 3, -1)):
|
||||
self.assertRaises(OperationFailure, db.test.remove,
|
||||
{"x": 1}, safe=True)
|
||||
{"x": 1})
|
||||
else: # Just test that it doesn't blow up
|
||||
db.test.remove({"x": 1}, safe=True)
|
||||
db.test.remove({"x": 1})
|
||||
|
||||
db.drop_collection("test")
|
||||
db.test.insert({"x": 1})
|
||||
db.test.insert({"x": 1})
|
||||
self.assertEqual(2, db.test.remove({}, safe=True)["n"])
|
||||
self.assertEqual(0, db.test.remove({}, safe=True)["n"])
|
||||
self.assertEqual(2, db.test.remove({})["n"])
|
||||
self.assertEqual(0, db.test.remove({})["n"])
|
||||
|
||||
def test_last_error_options(self):
|
||||
if not version.at_least(self.connection, (1, 5, 1)):
|
||||
if not version.at_least(self.client, (1, 5, 1)):
|
||||
raise SkipTest("getLastError options require MongoDB >= 1.5.1")
|
||||
|
||||
# XXX: Fix this if we ever have a replica set unittest env.
|
||||
# mongo >=1.7.6 errors with 'norepl' when w=2+
|
||||
# and we aren't replicated.
|
||||
if not version.at_least(self.connection, (1, 7, 6)):
|
||||
if not version.at_least(self.client, (1, 7, 6)):
|
||||
self.assertRaises(TimeoutError, self.db.test.save,
|
||||
{"x": 1}, w=2, wtimeout=1)
|
||||
self.assertRaises(TimeoutError, self.db.test.insert,
|
||||
@ -1134,11 +1124,11 @@ class TestCollection(unittest.TestCase):
|
||||
self.db.test.update({"x": 1}, {"y": 2}, w=1, wtimeout=1)
|
||||
|
||||
def test_manual_last_error(self):
|
||||
self.db.test.save({"x": 1})
|
||||
self.db.test.save({"x": 1}, w=0)
|
||||
# XXX: Fix this if we ever have a replica set unittest env.
|
||||
# mongo >=1.7.6 errors with 'norepl' when w=2+
|
||||
# and we aren't replicated
|
||||
if not version.at_least(self.connection, (1, 7, 6)):
|
||||
if not version.at_least(self.client, (1, 7, 6)):
|
||||
self.assertRaises(TimeoutError, self.db.command,
|
||||
"getlasterror", w=2, wtimeout=1)
|
||||
self.db.command("getlasterror", w=1, wtimeout=1)
|
||||
@ -1284,7 +1274,7 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
for i in range(2000):
|
||||
doc = {"x": i, "y": "mongomongo" * 1000}
|
||||
db.test_large_limit.insert(doc, safe=True)
|
||||
db.test_large_limit.insert(doc)
|
||||
|
||||
# Wait for insert to complete; often mysteriously failing in Jenkins
|
||||
st = time.time()
|
||||
@ -1448,7 +1438,7 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertTrue("_id" in doc)
|
||||
|
||||
def test_save_adds_id(self):
|
||||
doc = {"hello": "world"}
|
||||
doc = {"hello": "jesse"}
|
||||
self.db.test.save(doc)
|
||||
self.assertTrue("_id" in doc)
|
||||
|
||||
@ -1464,8 +1454,6 @@ class TestCollection(unittest.TestCase):
|
||||
self.db.drop_collection("test")
|
||||
|
||||
test = self.db.test
|
||||
test.safe = True
|
||||
|
||||
test.save({"a": 1})
|
||||
test.save({"a": 2})
|
||||
test.save({"a": 2})
|
||||
@ -1528,14 +1516,12 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertRaises(InvalidDocument, self.db.test.insert,
|
||||
[{"x": 1}, {"foo": "x" * max_size}])
|
||||
self.db.test.insert([{"foo": "x" * half_size},
|
||||
{"foo": "x" * half_size}], safe=True)
|
||||
{"foo": "x" * half_size}])
|
||||
|
||||
self.db.test.insert({"bar": "x"})
|
||||
self.assertRaises(InvalidDocument, self.db.test.update,
|
||||
{"bar": "x"}, {"bar": "x" * (max_size - 14)},
|
||||
safe=True)
|
||||
self.db.test.update({"bar": "x"}, {"bar": "x" * (max_size - 15)},
|
||||
safe=True)
|
||||
{"bar": "x"}, {"bar": "x" * (max_size - 14)})
|
||||
self.db.test.update({"bar": "x"}, {"bar": "x" * (max_size - 15)})
|
||||
|
||||
def test_map_reduce(self):
|
||||
if not version.at_least(self.db.connection, (1, 1, 1)):
|
||||
@ -1605,7 +1591,7 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertEqual(3, result.find_one({"_id": "cat"})["value"])
|
||||
self.assertEqual(2, result.find_one({"_id": "dog"})["value"])
|
||||
self.assertEqual(1, result.find_one({"_id": "mouse"})["value"])
|
||||
self.connection.drop_database('mrtestdb')
|
||||
self.client.drop_database('mrtestdb')
|
||||
|
||||
full_result = db.test.map_reduce(map, reduce,
|
||||
out='mrunittests', full_response=True)
|
||||
@ -1727,7 +1713,7 @@ class TestCollection(unittest.TestCase):
|
||||
c = self.db.test
|
||||
c.drop()
|
||||
for j in xrange(5):
|
||||
c.insert({'j': j, 'i': 0}, safe=True)
|
||||
c.insert({'j': j, 'i': 0})
|
||||
|
||||
sort={'j': DESCENDING}
|
||||
self.assertEqual(4, c.find_and_modify({},
|
||||
@ -1829,7 +1815,7 @@ class TestCollection(unittest.TestCase):
|
||||
son['foo'] += 2
|
||||
return son
|
||||
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.add_son_manipulator(IncByTwo())
|
||||
c = db.test
|
||||
c.drop()
|
||||
@ -1843,7 +1829,7 @@ class TestCollection(unittest.TestCase):
|
||||
if not have_uuid:
|
||||
raise SkipTest("No uuid module")
|
||||
|
||||
coll = self.connection.pymongo_test.uuid
|
||||
coll = self.client.pymongo_test.uuid
|
||||
coll.drop()
|
||||
|
||||
def change_subtype(collection, subtype):
|
||||
@ -1857,7 +1843,7 @@ class TestCollection(unittest.TestCase):
|
||||
# Test basic query
|
||||
uu = uuid.uuid4()
|
||||
# Insert as binary subtype 3
|
||||
coll.insert({'uu': uu}, safe=True)
|
||||
coll.insert({'uu': uu})
|
||||
self.assertEqual(uu, coll.find_one({'uu': uu})['uu'])
|
||||
coll.uuid_subtype = UUID_SUBTYPE
|
||||
self.assertEqual(UUID_SUBTYPE, coll.uuid_subtype)
|
||||
@ -1878,13 +1864,13 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertEqual(0, coll.count())
|
||||
|
||||
# Test save
|
||||
coll.insert({'_id': uu, 'i': 0}, safe=True)
|
||||
coll.insert({'_id': uu, 'i': 0})
|
||||
self.assertEqual(1, coll.count())
|
||||
self.assertEqual(1, coll.find({'_id': uu}).count())
|
||||
self.assertEqual(0, coll.find_one({'_id': uu})['i'])
|
||||
doc = coll.find_one({'_id': uu})
|
||||
doc['i'] = 1
|
||||
coll.save(doc, safe=True)
|
||||
coll.save(doc)
|
||||
self.assertEqual(1, coll.find_one({'_id': uu})['i'])
|
||||
|
||||
# Test update
|
||||
@ -1909,7 +1895,7 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertEqual(5, coll.find_one({'_id': uu})['i'])
|
||||
|
||||
# Test command
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
no_obj_error = "No matching object found"
|
||||
result = db.command('findAndModify', 'uuid',
|
||||
allowable_errors=[no_obj_error],
|
||||
@ -1980,5 +1966,6 @@ class TestCollection(unittest.TestCase):
|
||||
coll.group([], {"_id": uu},
|
||||
{"count": 0}, reduce))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -25,9 +25,7 @@ from nose.plugins.skip import SkipTest
|
||||
|
||||
from bson.objectid import ObjectId
|
||||
from bson.son import SON
|
||||
from pymongo.connection import Connection
|
||||
from pymongo.mongo_client import MongoClient
|
||||
from pymongo.replica_set_connection import ReplicaSetConnection
|
||||
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
|
||||
from pymongo.errors import ConfigurationError, OperationFailure
|
||||
from test.utils import drop_collections
|
||||
@ -62,18 +60,18 @@ class TestCommon(unittest.TestCase):
|
||||
|
||||
warnings.simplefilter("ignore")
|
||||
|
||||
c = Connection(pair)
|
||||
c = MongoClient(pair)
|
||||
self.assertFalse(c.slave_okay)
|
||||
self.assertFalse(c.safe)
|
||||
self.assertTrue(c.safe)
|
||||
self.assertEqual({}, c.get_lasterror_options())
|
||||
db = c.pymongo_test
|
||||
db.drop_collection("test")
|
||||
self.assertFalse(db.slave_okay)
|
||||
self.assertFalse(db.safe)
|
||||
self.assertTrue(db.safe)
|
||||
self.assertEqual({}, db.get_lasterror_options())
|
||||
coll = db.test
|
||||
self.assertFalse(coll.slave_okay)
|
||||
self.assertFalse(coll.safe)
|
||||
self.assertTrue(coll.safe)
|
||||
self.assertEqual({}, coll.get_lasterror_options())
|
||||
cursor = coll.find()
|
||||
self.assertFalse(cursor._Cursor__slave_okay)
|
||||
@ -81,10 +79,10 @@ class TestCommon(unittest.TestCase):
|
||||
self.assertTrue(cursor._Cursor__slave_okay)
|
||||
|
||||
# Setting any safe operations overrides explicit safe
|
||||
self.assertTrue(Connection(host, port, wtimeout=1000, safe=False).safe)
|
||||
self.assertTrue(MongoClient(host, port, wtimeout=1000, safe=False).safe)
|
||||
|
||||
c = Connection(pair, slaveok=True, w='majority',
|
||||
wtimeout=300, fsync=True, j=True)
|
||||
c = MongoClient(pair, slaveok=True, w='majority',
|
||||
wtimeout=300, fsync=True, j=True)
|
||||
self.assertTrue(c.slave_okay)
|
||||
self.assertTrue(c.safe)
|
||||
d = {'w': 'majority', 'wtimeout': 300, 'fsync': True, 'j': True}
|
||||
@ -102,14 +100,14 @@ class TestCommon(unittest.TestCase):
|
||||
cursor = coll.find(slave_okay=False)
|
||||
self.assertFalse(cursor._Cursor__slave_okay)
|
||||
|
||||
c = Connection('mongodb://%s/?'
|
||||
c = MongoClient('mongodb://%s/?'
|
||||
'w=2;wtimeoutMS=300;fsync=true;'
|
||||
'journal=true' % (pair,))
|
||||
self.assertTrue(c.safe)
|
||||
d = {'w': 2, 'wtimeout': 300, 'fsync': True, 'j': True}
|
||||
self.assertEqual(d, c.get_lasterror_options())
|
||||
|
||||
c = Connection('mongodb://%s/?'
|
||||
c = MongoClient('mongodb://%s/?'
|
||||
'slaveok=true;w=1;wtimeout=300;'
|
||||
'fsync=true;j=true' % (pair,))
|
||||
self.assertTrue(c.slave_okay)
|
||||
@ -202,7 +200,7 @@ class TestCommon(unittest.TestCase):
|
||||
warnings.resetwarnings()
|
||||
|
||||
def test_write_concern(self):
|
||||
c = Connection(pair)
|
||||
c = MongoClient(pair)
|
||||
|
||||
self.assertEqual({}, c.write_concern)
|
||||
wc = {'w': 2, 'wtimeout': 1000}
|
||||
@ -216,7 +214,7 @@ class TestCommon(unittest.TestCase):
|
||||
self.assertEqual(wc, c.write_concern)
|
||||
|
||||
wc = {'w': 3, 'wtimeout': 1000}
|
||||
c = Connection(w=3, wtimeout=1000)
|
||||
c = MongoClient(w=3, wtimeout=1000)
|
||||
self.assertEqual(wc, c.write_concern)
|
||||
wc = {'w': 2, 'wtimeout': 1000}
|
||||
c.write_concern = wc
|
||||
@ -246,39 +244,6 @@ class TestCommon(unittest.TestCase):
|
||||
c.write_concern = [('foo', 'bar')]
|
||||
self.assertRaises(ConfigurationError, f)
|
||||
|
||||
def test_connection(self):
|
||||
c = Connection(pair)
|
||||
coll = c.pymongo_test.write_concern_test
|
||||
coll.drop()
|
||||
doc = {"_id": ObjectId()}
|
||||
coll.insert(doc)
|
||||
self.assertTrue(coll.insert(doc, safe=False))
|
||||
self.assertTrue(coll.insert(doc, w=0))
|
||||
self.assertTrue(coll.insert(doc))
|
||||
self.assertRaises(OperationFailure, coll.insert, doc, safe=True)
|
||||
self.assertRaises(OperationFailure, coll.insert, doc, w=1)
|
||||
|
||||
c = Connection(pair, safe=True)
|
||||
coll = c.pymongo_test.write_concern_test
|
||||
self.assertTrue(coll.insert(doc, safe=False))
|
||||
self.assertTrue(coll.insert(doc, w=0))
|
||||
self.assertRaises(OperationFailure, coll.insert, doc)
|
||||
self.assertRaises(OperationFailure, coll.insert, doc, safe=True)
|
||||
self.assertRaises(OperationFailure, coll.insert, doc, w=1)
|
||||
|
||||
c = Connection("mongodb://%s/" % (pair,))
|
||||
self.assertFalse(c.safe)
|
||||
coll = c.pymongo_test.write_concern_test
|
||||
self.assertTrue(coll.insert(doc))
|
||||
c = Connection("mongodb://%s/?safe=true" % (pair,))
|
||||
self.assertTrue(c.safe)
|
||||
coll = c.pymongo_test.write_concern_test
|
||||
self.assertRaises(OperationFailure, coll.insert, doc)
|
||||
|
||||
# Equality tests
|
||||
self.assertEqual(c, Connection("mongodb://%s/?safe=true" % (pair,)))
|
||||
self.assertFalse(c != Connection("mongodb://%s/?safe=true" % (pair,)))
|
||||
|
||||
def test_mongo_client(self):
|
||||
m = MongoClient(pair, w=0)
|
||||
coll = m.pymongo_test.write_concern_test
|
||||
@ -312,43 +277,8 @@ class TestCommon(unittest.TestCase):
|
||||
self.assertEqual(m, MongoClient("mongodb://%s/?w=0" % (pair,)))
|
||||
self.assertFalse(m != MongoClient("mongodb://%s/?w=0" % (pair,)))
|
||||
|
||||
def test_replica_set_connection(self):
|
||||
c = Connection(pair)
|
||||
ismaster = c.admin.command('ismaster')
|
||||
if 'setName' in ismaster:
|
||||
setname = str(ismaster.get('setName'))
|
||||
else:
|
||||
raise SkipTest("Not connected to a replica set.")
|
||||
c = ReplicaSetConnection(pair, replicaSet=setname)
|
||||
coll = c.pymongo_test.write_concern_test
|
||||
coll.drop()
|
||||
doc = {"_id": ObjectId()}
|
||||
coll.insert(doc)
|
||||
self.assertTrue(coll.insert(doc, safe=False))
|
||||
self.assertTrue(coll.insert(doc, w=0))
|
||||
self.assertTrue(coll.insert(doc))
|
||||
self.assertRaises(OperationFailure, coll.insert, doc, safe=True)
|
||||
self.assertRaises(OperationFailure, coll.insert, doc, w=1)
|
||||
|
||||
c = ReplicaSetConnection(pair, replicaSet=setname, safe=True)
|
||||
coll = c.pymongo_test.write_concern_test
|
||||
self.assertTrue(coll.insert(doc, safe=False))
|
||||
self.assertTrue(coll.insert(doc, w=0))
|
||||
self.assertRaises(OperationFailure, coll.insert, doc)
|
||||
self.assertRaises(OperationFailure, coll.insert, doc, safe=True)
|
||||
self.assertRaises(OperationFailure, coll.insert, doc, w=1)
|
||||
|
||||
c = ReplicaSetConnection("mongodb://%s/?replicaSet=%s" % (pair, setname))
|
||||
self.assertFalse(c.safe)
|
||||
coll = c.pymongo_test.write_concern_test
|
||||
self.assertTrue(coll.insert(doc))
|
||||
c = ReplicaSetConnection("mongodb://%s/?replicaSet=%s;safe=true" % (pair, setname))
|
||||
self.assertTrue(c.safe)
|
||||
coll = c.pymongo_test.write_concern_test
|
||||
self.assertRaises(OperationFailure, coll.insert, doc)
|
||||
|
||||
def test_mongo_replica_set_client(self):
|
||||
c = Connection(pair)
|
||||
c = MongoClient(pair)
|
||||
ismaster = c.admin.command('ismaster')
|
||||
if 'setName' in ismaster:
|
||||
setname = str(ismaster.get('setName'))
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Test the connection module."""
|
||||
"""Test the mongo_client module."""
|
||||
|
||||
import datetime
|
||||
import os
|
||||
@ -30,7 +30,7 @@ from nose.plugins.skip import SkipTest
|
||||
|
||||
from bson.son import SON
|
||||
from bson.tz_util import utc
|
||||
from pymongo.connection import Connection
|
||||
from pymongo.mongo_client import MongoClient
|
||||
from pymongo.database import Database
|
||||
from pymongo.pool import SocketInfo
|
||||
from pymongo import thread_util
|
||||
@ -50,130 +50,134 @@ host = os.environ.get("DB_IP", "localhost")
|
||||
port = int(os.environ.get("DB_PORT", 27017))
|
||||
|
||||
|
||||
def get_connection(*args, **kwargs):
|
||||
return Connection(host, port, *args, **kwargs)
|
||||
def get_client(*args, **kwargs):
|
||||
return MongoClient(host, port, *args, **kwargs)
|
||||
|
||||
|
||||
class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
class TestClient(unittest.TestCase, TestRequestMixin):
|
||||
def setUp(self):
|
||||
self.host = os.environ.get("DB_IP", "localhost")
|
||||
self.port = int(os.environ.get("DB_PORT", 27017))
|
||||
|
||||
def test_types(self):
|
||||
self.assertRaises(TypeError, Connection, 1)
|
||||
self.assertRaises(TypeError, Connection, 1.14)
|
||||
self.assertRaises(TypeError, Connection, "localhost", "27017")
|
||||
self.assertRaises(TypeError, Connection, "localhost", 1.14)
|
||||
self.assertRaises(TypeError, Connection, "localhost", [])
|
||||
self.assertRaises(TypeError, MongoClient, 1)
|
||||
self.assertRaises(TypeError, MongoClient, 1.14)
|
||||
self.assertRaises(TypeError, MongoClient, "localhost", "27017")
|
||||
self.assertRaises(TypeError, MongoClient, "localhost", 1.14)
|
||||
self.assertRaises(TypeError, MongoClient, "localhost", [])
|
||||
|
||||
self.assertRaises(ConfigurationError, Connection, [])
|
||||
self.assertRaises(ConfigurationError, MongoClient, [])
|
||||
|
||||
def test_constants(self):
|
||||
Connection.HOST = self.host
|
||||
Connection.PORT = self.port
|
||||
self.assertTrue(Connection())
|
||||
MongoClient.HOST = self.host
|
||||
MongoClient.PORT = self.port
|
||||
self.assertTrue(MongoClient())
|
||||
|
||||
Connection.HOST = "somedomainthatdoesntexist.org"
|
||||
Connection.PORT = 123456789
|
||||
assertRaisesExactly(ConnectionFailure, Connection, connectTimeoutMS=600)
|
||||
self.assertTrue(Connection(self.host, self.port))
|
||||
MongoClient.HOST = "somedomainthatdoesntexist.org"
|
||||
MongoClient.PORT = 123456789
|
||||
assertRaisesExactly(
|
||||
ConnectionFailure, MongoClient, connectTimeoutMS=600)
|
||||
self.assertTrue(MongoClient(self.host, self.port))
|
||||
|
||||
Connection.HOST = self.host
|
||||
Connection.PORT = self.port
|
||||
self.assertTrue(Connection())
|
||||
MongoClient.HOST = self.host
|
||||
MongoClient.PORT = self.port
|
||||
self.assertTrue(MongoClient())
|
||||
|
||||
def test_connect(self):
|
||||
# Check that the exception is a ConnectionFailure, not a subclass like
|
||||
# AutoReconnect
|
||||
assertRaisesExactly(
|
||||
ConnectionFailure, Connection,
|
||||
ConnectionFailure, MongoClient,
|
||||
"somedomainthatdoesntexist.org", connectTimeoutMS=600)
|
||||
|
||||
assertRaisesExactly(
|
||||
ConnectionFailure, Connection, self.host, 123456789)
|
||||
ConnectionFailure, MongoClient, self.host, 123456789)
|
||||
|
||||
self.assertTrue(Connection(self.host, self.port))
|
||||
self.assertTrue(MongoClient(self.host, self.port))
|
||||
|
||||
def test_equality(self):
|
||||
connection = Connection(self.host, self.port)
|
||||
self.assertEqual(connection, Connection(self.host, self.port))
|
||||
client = MongoClient(self.host, self.port)
|
||||
self.assertEqual(client, MongoClient(self.host, self.port))
|
||||
# Explicity test inequality
|
||||
self.assertFalse(connection != Connection(self.host, self.port))
|
||||
self.assertFalse(client != MongoClient(self.host, self.port))
|
||||
|
||||
def test_host_w_port(self):
|
||||
self.assertTrue(Connection("%s:%d" % (self.host, self.port)))
|
||||
assertRaisesExactly(ConnectionFailure, Connection,
|
||||
self.assertTrue(MongoClient("%s:%d" % (self.host, self.port)))
|
||||
assertRaisesExactly(ConnectionFailure, MongoClient,
|
||||
"%s:1234567" % (self.host,), self.port)
|
||||
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(Connection(self.host, self.port)),
|
||||
"Connection('%s', %d)" % (self.host, self.port))
|
||||
self.assertEqual(repr(MongoClient(self.host, self.port)),
|
||||
"MongoClient('%s', %d)" % (self.host, self.port))
|
||||
|
||||
def test_getters(self):
|
||||
self.assertEqual(Connection(self.host, self.port).host, self.host)
|
||||
self.assertEqual(Connection(self.host, self.port).port, self.port)
|
||||
self.assertEqual(MongoClient(self.host, self.port).host, self.host)
|
||||
self.assertEqual(MongoClient(self.host, self.port).port, self.port)
|
||||
self.assertEqual(set([(self.host, self.port)]),
|
||||
Connection(self.host, self.port).nodes)
|
||||
MongoClient(self.host, self.port).nodes)
|
||||
|
||||
def test_use_greenlets(self):
|
||||
self.assertFalse(Connection(self.host, self.port).use_greenlets)
|
||||
self.assertFalse(MongoClient(self.host, self.port).use_greenlets)
|
||||
if thread_util.have_greenlet:
|
||||
self.assertTrue(
|
||||
Connection(
|
||||
MongoClient(
|
||||
self.host, self.port, use_greenlets=True).use_greenlets)
|
||||
|
||||
def test_get_db(self):
|
||||
connection = Connection(self.host, self.port)
|
||||
client = MongoClient(self.host, self.port)
|
||||
|
||||
def make_db(base, name):
|
||||
return base[name]
|
||||
|
||||
self.assertRaises(InvalidName, make_db, connection, "")
|
||||
self.assertRaises(InvalidName, make_db, connection, "te$t")
|
||||
self.assertRaises(InvalidName, make_db, connection, "te.t")
|
||||
self.assertRaises(InvalidName, make_db, connection, "te\\t")
|
||||
self.assertRaises(InvalidName, make_db, connection, "te/t")
|
||||
self.assertRaises(InvalidName, make_db, connection, "te st")
|
||||
self.assertRaises(InvalidName, make_db, client, "")
|
||||
self.assertRaises(InvalidName, make_db, client, "te$t")
|
||||
self.assertRaises(InvalidName, make_db, client, "te.t")
|
||||
self.assertRaises(InvalidName, make_db, client, "te\\t")
|
||||
self.assertRaises(InvalidName, make_db, client, "te/t")
|
||||
self.assertRaises(InvalidName, make_db, client, "te st")
|
||||
|
||||
self.assertTrue(isinstance(connection.test, Database))
|
||||
self.assertEqual(connection.test, connection["test"])
|
||||
self.assertEqual(connection.test, Database(connection, "test"))
|
||||
self.assertTrue(isinstance(client.test, Database))
|
||||
self.assertEqual(client.test, client["test"])
|
||||
self.assertEqual(client.test, Database(client, "test"))
|
||||
|
||||
def test_database_names(self):
|
||||
connection = Connection(self.host, self.port)
|
||||
client = MongoClient(self.host, self.port)
|
||||
|
||||
connection.pymongo_test.test.save({"dummy": u"object"})
|
||||
connection.pymongo_test_mike.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test_mike.test.save({"dummy": u"object"})
|
||||
|
||||
dbs = connection.database_names()
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
self.assertTrue("pymongo_test_mike" in dbs)
|
||||
|
||||
def test_drop_database(self):
|
||||
connection = Connection(self.host, self.port)
|
||||
client = MongoClient(self.host, self.port)
|
||||
|
||||
self.assertRaises(TypeError, connection.drop_database, 5)
|
||||
self.assertRaises(TypeError, connection.drop_database, None)
|
||||
self.assertRaises(TypeError, client.drop_database, 5)
|
||||
self.assertRaises(TypeError, client.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()
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
connection.drop_database("pymongo_test")
|
||||
dbs = connection.database_names()
|
||||
client.drop_database("pymongo_test")
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" not in dbs)
|
||||
|
||||
connection.pymongo_test.test.save({"dummy": u"object"})
|
||||
dbs = connection.database_names()
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
connection.drop_database(connection.pymongo_test)
|
||||
dbs = connection.database_names()
|
||||
client.drop_database(client.pymongo_test)
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" not in dbs)
|
||||
|
||||
def test_copy_db(self):
|
||||
c = Connection(self.host, self.port)
|
||||
self.assertTrue(c.in_request())
|
||||
c = MongoClient(self.host, self.port)
|
||||
# We test copy twice; once starting in a request and once not. In
|
||||
# either case the copy should succeed (because it starts a request
|
||||
# internally) and should leave us in the same state as before the copy.
|
||||
c.start_request()
|
||||
|
||||
self.assertRaises(TypeError, c.copy_database, 4, "foo")
|
||||
self.assertRaises(TypeError, c.copy_database, "foo", 4)
|
||||
@ -235,15 +239,15 @@ class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
|
||||
|
||||
def test_iteration(self):
|
||||
connection = Connection(self.host, self.port)
|
||||
client = MongoClient(self.host, self.port)
|
||||
|
||||
def iterate():
|
||||
[a for a in connection]
|
||||
[a for a in client]
|
||||
|
||||
self.assertRaises(TypeError, iterate)
|
||||
|
||||
def test_disconnect(self):
|
||||
c = Connection(self.host, self.port)
|
||||
c = MongoClient(self.host, self.port)
|
||||
coll = c.pymongo_test.bar
|
||||
|
||||
c.disconnect()
|
||||
@ -257,19 +261,19 @@ class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
coll.count()
|
||||
|
||||
def test_from_uri(self):
|
||||
c = Connection(self.host, self.port)
|
||||
c = MongoClient(self.host, self.port)
|
||||
|
||||
self.assertEqual(c, Connection("mongodb://%s:%d" %
|
||||
self.assertEqual(c, MongoClient("mongodb://%s:%d" %
|
||||
(self.host, self.port)))
|
||||
|
||||
self.assertTrue(Connection("mongodb://%s:%d" %
|
||||
self.assertTrue(MongoClient("mongodb://%s:%d" %
|
||||
(self.host, self.port),
|
||||
slave_okay=True).slave_okay)
|
||||
self.assertTrue(Connection("mongodb://%s:%d/?slaveok=true;w=2" %
|
||||
self.assertTrue(MongoClient("mongodb://%s:%d/?slaveok=true;w=2" %
|
||||
(self.host, self.port)).slave_okay)
|
||||
|
||||
def test_auth_from_uri(self):
|
||||
c = Connection(self.host, self.port)
|
||||
c = MongoClient(self.host, self.port)
|
||||
# Sharded auth not supported before MongoDB 2.0
|
||||
if is_mongos(c) and not version.at_least(c, (2, 0, 0)):
|
||||
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
|
||||
@ -280,21 +284,21 @@ class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
c.admin.authenticate("admin", "pass")
|
||||
c.pymongo_test.add_user("user", "pass")
|
||||
|
||||
self.assertRaises(ConfigurationError, Connection,
|
||||
self.assertRaises(ConfigurationError, MongoClient,
|
||||
"mongodb://foo:bar@%s:%d" % (self.host, self.port))
|
||||
self.assertRaises(ConfigurationError, Connection,
|
||||
self.assertRaises(ConfigurationError, MongoClient,
|
||||
"mongodb://admin:bar@%s:%d" % (self.host, self.port))
|
||||
self.assertRaises(ConfigurationError, Connection,
|
||||
self.assertRaises(ConfigurationError, MongoClient,
|
||||
"mongodb://user:pass@%s:%d" % (self.host, self.port))
|
||||
Connection("mongodb://admin:pass@%s:%d" % (self.host, self.port))
|
||||
MongoClient("mongodb://admin:pass@%s:%d" % (self.host, self.port))
|
||||
|
||||
self.assertRaises(ConfigurationError, Connection,
|
||||
self.assertRaises(ConfigurationError, MongoClient,
|
||||
"mongodb://admin:pass@%s:%d/pymongo_test" %
|
||||
(self.host, self.port))
|
||||
self.assertRaises(ConfigurationError, Connection,
|
||||
self.assertRaises(ConfigurationError, MongoClient,
|
||||
"mongodb://user:foo@%s:%d/pymongo_test" %
|
||||
(self.host, self.port))
|
||||
Connection("mongodb://user:pass@%s:%d/pymongo_test" %
|
||||
MongoClient("mongodb://user:pass@%s:%d/pymongo_test" %
|
||||
(self.host, self.port))
|
||||
|
||||
c.admin.system.users.remove({})
|
||||
@ -304,28 +308,28 @@ class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
if not hasattr(socket, "AF_UNIX"):
|
||||
raise SkipTest("UNIX-sockets are not supported on this system")
|
||||
if (sys.platform == 'darwin' and
|
||||
server_started_with_auth(Connection(self.host, self.port))):
|
||||
server_started_with_auth(MongoClient(self.host, self.port))):
|
||||
raise SkipTest("SERVER-8492")
|
||||
|
||||
mongodb_socket = '/tmp/mongodb-27017.sock'
|
||||
if not os.access(mongodb_socket, os.R_OK):
|
||||
raise SkipTest("Socket file is not accessable")
|
||||
|
||||
self.assertTrue(Connection("mongodb://%s" % mongodb_socket))
|
||||
self.assertTrue(MongoClient("mongodb://%s" % mongodb_socket))
|
||||
|
||||
connection = Connection("mongodb://%s" % mongodb_socket)
|
||||
connection.pymongo_test.test.save({"dummy": "object"}, safe=True)
|
||||
client = MongoClient("mongodb://%s" % mongodb_socket)
|
||||
client.pymongo_test.test.save({"dummy": "object"})
|
||||
|
||||
# Confirm we can read via the socket
|
||||
dbs = connection.database_names()
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
|
||||
# Confirm it fails with a missing socket
|
||||
self.assertRaises(ConnectionFailure, Connection,
|
||||
self.assertRaises(ConnectionFailure, MongoClient,
|
||||
"mongodb:///tmp/none-existent.sock")
|
||||
|
||||
def test_fork(self):
|
||||
# Test using a connection before and after a fork.
|
||||
# Test using a client before and after a fork.
|
||||
if sys.platform == "win32":
|
||||
raise SkipTest("Can't fork on windows")
|
||||
|
||||
@ -334,16 +338,16 @@ class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
except ImportError:
|
||||
raise SkipTest("No multiprocessing module")
|
||||
|
||||
db = Connection(self.host, self.port).pymongo_test
|
||||
db = MongoClient(self.host, self.port).pymongo_test
|
||||
|
||||
# Failure occurs if the connection is used before the fork
|
||||
# Failure occurs if the client is used before the fork
|
||||
db.test.find_one()
|
||||
db.connection.end_request()
|
||||
|
||||
def loop(pipe):
|
||||
while True:
|
||||
try:
|
||||
db.test.insert({"a": "b"}, safe=True)
|
||||
db.test.insert({"a": "b"})
|
||||
for _ in db.test.find():
|
||||
pass
|
||||
except:
|
||||
@ -384,7 +388,7 @@ class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
pass
|
||||
|
||||
def test_document_class(self):
|
||||
c = Connection(self.host, self.port)
|
||||
c = MongoClient(self.host, self.port)
|
||||
db = c.pymongo_test
|
||||
db.test.insert({"x": 1})
|
||||
|
||||
@ -398,7 +402,7 @@ class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
self.assertTrue(isinstance(db.test.find_one(), SON))
|
||||
self.assertFalse(isinstance(db.test.find_one(as_class=dict), SON))
|
||||
|
||||
c = Connection(self.host, self.port, document_class=SON)
|
||||
c = MongoClient(self.host, self.port, document_class=SON)
|
||||
db = c.pymongo_test
|
||||
|
||||
self.assertEqual(SON, c.document_class)
|
||||
@ -412,37 +416,43 @@ class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
self.assertFalse(isinstance(db.test.find_one(), SON))
|
||||
|
||||
def test_timeouts(self):
|
||||
conn = Connection(self.host, self.port, connectTimeoutMS=10500)
|
||||
self.assertEqual(10.5, conn._MongoClient__pool.conn_timeout)
|
||||
conn = Connection(self.host, self.port, socketTimeoutMS=10500)
|
||||
self.assertEqual(10.5, conn._MongoClient__pool.net_timeout)
|
||||
client = MongoClient(self.host, self.port, connectTimeoutMS=10500)
|
||||
self.assertEqual(10.5, client._MongoClient__pool.conn_timeout)
|
||||
client = MongoClient(self.host, self.port, socketTimeoutMS=10500)
|
||||
self.assertEqual(10.5, client._MongoClient__pool.net_timeout)
|
||||
|
||||
def test_network_timeout_validation(self):
|
||||
c = get_connection(network_timeout=10)
|
||||
c = get_client(socketTimeoutMS=10 * 1000)
|
||||
self.assertEqual(10, c._MongoClient__net_timeout)
|
||||
|
||||
c = get_connection(network_timeout=None)
|
||||
c = get_client(socketTimeoutMS=None)
|
||||
self.assertEqual(None, c._MongoClient__net_timeout)
|
||||
|
||||
self.assertRaises(ConfigurationError,
|
||||
get_connection, network_timeout=0)
|
||||
get_client, socketTimeoutMS=0)
|
||||
|
||||
self.assertRaises(ConfigurationError,
|
||||
get_connection, network_timeout=-1)
|
||||
get_client, socketTimeoutMS=-1)
|
||||
|
||||
self.assertRaises(ConfigurationError,
|
||||
get_connection, network_timeout=1e10)
|
||||
get_client, socketTimeoutMS=1e10)
|
||||
|
||||
self.assertRaises(ConfigurationError,
|
||||
get_connection, network_timeout='foo')
|
||||
get_client, socketTimeoutMS='foo')
|
||||
|
||||
# network_timeout is gone from MongoClient, remains in deprecated
|
||||
# Connection
|
||||
self.assertRaises(ConfigurationError,
|
||||
get_client, network_timeout=10)
|
||||
|
||||
def test_network_timeout(self):
|
||||
no_timeout = Connection(self.host, self.port)
|
||||
no_timeout = MongoClient(self.host, self.port)
|
||||
timeout_sec = 1
|
||||
timeout = Connection(self.host, self.port, network_timeout=timeout_sec)
|
||||
timeout = MongoClient(
|
||||
self.host, self.port, socketTimeoutMS=1000 * timeout_sec)
|
||||
|
||||
no_timeout.pymongo_test.drop_collection("test")
|
||||
no_timeout.pymongo_test.test.insert({"x": 1}, safe=True)
|
||||
no_timeout.pymongo_test.test.insert({"x": 1})
|
||||
|
||||
# A $where clause that takes a second longer than the timeout
|
||||
where_func = delay(timeout_sec + 1)
|
||||
@ -461,14 +471,14 @@ class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
no_timeout.pymongo_test, 0.1)
|
||||
|
||||
def test_tz_aware(self):
|
||||
self.assertRaises(ConfigurationError, Connection, tz_aware='foo')
|
||||
self.assertRaises(ConfigurationError, MongoClient, tz_aware='foo')
|
||||
|
||||
aware = Connection(self.host, self.port, tz_aware=True)
|
||||
naive = Connection(self.host, self.port)
|
||||
aware = MongoClient(self.host, self.port, tz_aware=True)
|
||||
naive = MongoClient(self.host, self.port)
|
||||
aware.pymongo_test.drop_collection("test")
|
||||
|
||||
now = datetime.datetime.utcnow()
|
||||
aware.pymongo_test.test.insert({"x": now}, safe=True)
|
||||
aware.pymongo_test.test.insert({"x": now})
|
||||
|
||||
self.assertEqual(None, naive.pymongo_test.test.find_one()["x"].tzinfo)
|
||||
self.assertEqual(utc, aware.pymongo_test.test.find_one()["x"].tzinfo)
|
||||
@ -478,29 +488,29 @@ class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
|
||||
def test_ipv6(self):
|
||||
try:
|
||||
connection = Connection("[::1]")
|
||||
client = MongoClient("[::1]")
|
||||
except:
|
||||
# Either mongod was started without --ipv6
|
||||
# or the OS doesn't support it (or both).
|
||||
raise SkipTest("No IPv6")
|
||||
|
||||
# Try a few simple things
|
||||
connection = Connection("mongodb://[::1]:%d" % (self.port,))
|
||||
connection = Connection("mongodb://[::1]:%d/"
|
||||
client = MongoClient("mongodb://[::1]:%d" % (self.port,))
|
||||
client = MongoClient("mongodb://[::1]:%d/"
|
||||
"?slaveOk=true" % (self.port,))
|
||||
connection = Connection("[::1]:%d,"
|
||||
client = MongoClient("[::1]:%d,"
|
||||
"localhost:%d" % (self.port, self.port))
|
||||
connection = Connection("localhost:%d,"
|
||||
client = MongoClient("localhost:%d,"
|
||||
"[::1]:%d" % (self.port, self.port))
|
||||
connection.pymongo_test.test.save({"dummy": u"object"})
|
||||
connection.pymongo_test_bernie.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test_bernie.test.save({"dummy": u"object"})
|
||||
|
||||
dbs = connection.database_names()
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
self.assertTrue("pymongo_test_bernie" in dbs)
|
||||
|
||||
def test_fsync_lock_unlock(self):
|
||||
c = get_connection()
|
||||
c = get_client()
|
||||
if is_mongos(c):
|
||||
raise SkipTest('fsync/lock not supported by mongos')
|
||||
|
||||
@ -530,39 +540,39 @@ class TestConnection(unittest.TestCase, TestRequestMixin):
|
||||
|
||||
import contextlib
|
||||
|
||||
conn = get_connection(auto_start_request=False)
|
||||
conn.pymongo_test.drop_collection("test")
|
||||
conn.pymongo_test.test.insert({"foo": "bar"})
|
||||
client = get_client(auto_start_request=False)
|
||||
client.pymongo_test.drop_collection("test")
|
||||
client.pymongo_test.test.insert({"foo": "bar"})
|
||||
|
||||
# The socket used for the previous commands has been returned to the
|
||||
# pool
|
||||
self.assertEqual(1, len(conn._MongoClient__pool.sockets))
|
||||
self.assertEqual(1, len(client._MongoClient__pool.sockets))
|
||||
|
||||
# We need exec here because if the Python version is less than 2.6
|
||||
# these with-statements won't even compile.
|
||||
exec """
|
||||
with contextlib.closing(conn):
|
||||
self.assertEqual("bar", conn.pymongo_test.test.find_one()["foo"])
|
||||
self.assertEqual(0, len(conn._MongoClient__pool.sockets))
|
||||
with contextlib.closing(client):
|
||||
self.assertEqual("bar", client.pymongo_test.test.find_one()["foo"])
|
||||
self.assertEqual(0, len(client._MongoClient__pool.sockets))
|
||||
"""
|
||||
|
||||
exec """
|
||||
with get_connection() as connection:
|
||||
self.assertEqual("bar", connection.pymongo_test.test.find_one()["foo"])
|
||||
# Calling conn.close() has reset the pool
|
||||
self.assertEqual(0, len(connection._MongoClient__pool.sockets))
|
||||
with get_client() as client:
|
||||
self.assertEqual("bar", client.pymongo_test.test.find_one()["foo"])
|
||||
# Calling client.close() has reset the pool
|
||||
self.assertEqual(0, len(client._MongoClient__pool.sockets))
|
||||
"""
|
||||
|
||||
def test_with_start_request(self):
|
||||
conn = get_connection(auto_start_request=False)
|
||||
pool = conn._MongoClient__pool
|
||||
client = get_client()
|
||||
pool = client._MongoClient__pool
|
||||
|
||||
# No request started
|
||||
self.assertNoRequest(pool)
|
||||
self.assertDifferentSock(pool)
|
||||
|
||||
# Start a request
|
||||
request_context_mgr = conn.start_request()
|
||||
request_context_mgr = client.start_request()
|
||||
self.assertTrue(
|
||||
isinstance(request_context_mgr, object)
|
||||
)
|
||||
@ -581,8 +591,8 @@ self.assertEqual(0, len(connection._MongoClient__pool.sockets))
|
||||
# We need exec here because if the Python version is less than 2.6
|
||||
# these with-statements won't even compile.
|
||||
exec """
|
||||
with conn.start_request() as request:
|
||||
self.assertEqual(conn, request.connection)
|
||||
with client.start_request() as request:
|
||||
self.assertEqual(client, request.connection)
|
||||
self.assertNoSocketYet(pool)
|
||||
self.assertSameSock(pool)
|
||||
self.assertRequestSocket(pool)
|
||||
@ -596,66 +606,66 @@ with conn.start_request() as request:
|
||||
for bad_horrible_value in (None, 5, 'hi!'):
|
||||
self.assertRaises(
|
||||
(TypeError, ConfigurationError),
|
||||
lambda: get_connection(auto_start_request=bad_horrible_value)
|
||||
lambda: get_client(auto_start_request=bad_horrible_value)
|
||||
)
|
||||
|
||||
# auto_start_request should default to True
|
||||
conn = get_connection()
|
||||
self.assertTrue(conn.auto_start_request)
|
||||
self.assertTrue(conn.in_request())
|
||||
pool = conn._MongoClient__pool
|
||||
# auto_start_request should default to False
|
||||
client = get_client()
|
||||
self.assertFalse(client.auto_start_request)
|
||||
|
||||
# Request started already, just from Connection constructor - it's a
|
||||
# bit weird, but Connection does some socket stuff when it initializes
|
||||
client = get_client(auto_start_request=True)
|
||||
self.assertTrue(client.auto_start_request)
|
||||
self.assertTrue(client.in_request())
|
||||
pool = client._MongoClient__pool
|
||||
|
||||
# Request started already, just from MongoClient constructor - it's a
|
||||
# bit weird, but MongoClient does some socket stuff when it initializes
|
||||
# and it ends up with a request socket
|
||||
self.assertRequestSocket(pool)
|
||||
self.assertSameSock(pool)
|
||||
|
||||
conn.end_request()
|
||||
client.end_request()
|
||||
self.assertNoRequest(pool)
|
||||
self.assertDifferentSock(pool)
|
||||
|
||||
# Trigger auto_start_request
|
||||
conn.pymongo_test.test.find_one()
|
||||
client.pymongo_test.test.find_one()
|
||||
self.assertRequestSocket(pool)
|
||||
self.assertSameSock(pool)
|
||||
|
||||
def test_nested_request(self):
|
||||
# auto_start_request is True
|
||||
conn = get_connection()
|
||||
pool = conn._MongoClient__pool
|
||||
self.assertTrue(conn.in_request())
|
||||
# auto_start_request is False
|
||||
client = get_client()
|
||||
pool = client._MongoClient__pool
|
||||
self.assertFalse(client.in_request())
|
||||
|
||||
# Start and end request - we're still in "outer" original request
|
||||
conn.start_request()
|
||||
self.assertInRequestAndSameSock(conn, pool)
|
||||
conn.end_request()
|
||||
self.assertInRequestAndSameSock(conn, pool)
|
||||
# Start and end request
|
||||
client.start_request()
|
||||
self.assertInRequestAndSameSock(client, pool)
|
||||
client.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
|
||||
# Double-nesting
|
||||
conn.start_request()
|
||||
conn.start_request()
|
||||
conn.end_request()
|
||||
conn.end_request()
|
||||
self.assertInRequestAndSameSock(conn, pool)
|
||||
|
||||
# Finally, end original request
|
||||
conn.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
client.start_request()
|
||||
client.start_request()
|
||||
client.end_request()
|
||||
self.assertInRequestAndSameSock(client, pool)
|
||||
client.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
|
||||
# Extra end_request calls have no effect - count stays at zero
|
||||
conn.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
client.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
|
||||
conn.start_request()
|
||||
self.assertInRequestAndSameSock(conn, pool)
|
||||
conn.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
client.start_request()
|
||||
self.assertInRequestAndSameSock(client, pool)
|
||||
client.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
|
||||
def test_request_threads(self):
|
||||
conn = get_connection(auto_start_request=False)
|
||||
pool = conn._MongoClient__pool
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
client = get_client(auto_start_request=False)
|
||||
pool = client._MongoClient__pool
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
|
||||
started_request, ended_request = threading.Event(), threading.Event()
|
||||
checked_request = threading.Event()
|
||||
@ -664,15 +674,15 @@ with conn.start_request() as request:
|
||||
# Starting a request in one thread doesn't put the other thread in a
|
||||
# request
|
||||
def f():
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
conn.start_request()
|
||||
self.assertInRequestAndSameSock(conn, pool)
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
client.start_request()
|
||||
self.assertInRequestAndSameSock(client, pool)
|
||||
started_request.set()
|
||||
checked_request.wait()
|
||||
checked_request.clear()
|
||||
self.assertInRequestAndSameSock(conn, pool)
|
||||
conn.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
self.assertInRequestAndSameSock(client, pool)
|
||||
client.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
ended_request.set()
|
||||
checked_request.wait()
|
||||
thread_done[0] = True
|
||||
@ -682,15 +692,15 @@ with conn.start_request() as request:
|
||||
t.start()
|
||||
# It doesn't matter in what order the main thread or t initially get
|
||||
# to started_request.set() / wait(); by waiting here we ensure that t
|
||||
# has called conn.start_request() before we assert on the next line.
|
||||
# has called client.start_request() before we assert on the next line.
|
||||
started_request.wait()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
checked_request.set()
|
||||
ended_request.wait()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
checked_request.set()
|
||||
t.join()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
self.assertTrue(thread_done[0], "Thread didn't complete")
|
||||
|
||||
def test_interrupt_signal(self):
|
||||
@ -701,9 +711,9 @@ with conn.start_request() as request:
|
||||
# PYTHON-294 can't actually occur in Jython.
|
||||
raise SkipTest("Can't test interrupts in Jython")
|
||||
|
||||
# Test fix for PYTHON-294 -- make sure Connection closes its
|
||||
# Test fix for PYTHON-294 -- make sure MongoClient closes its
|
||||
# socket if it gets an interrupt while waiting to recv() from it.
|
||||
c = get_connection()
|
||||
c = get_client()
|
||||
db = c.pymongo_test
|
||||
|
||||
# A $where clause which takes 1.5 sec to execute
|
||||
@ -711,7 +721,7 @@ with conn.start_request() as request:
|
||||
|
||||
# Need exactly 1 document so find() will execute its $where clause once
|
||||
db.drop_collection('foo')
|
||||
db.foo.insert({'_id': 1}, safe=True)
|
||||
db.foo.insert({'_id': 1})
|
||||
|
||||
def interrupter():
|
||||
# Raises KeyboardInterrupt in the main thread
|
||||
@ -740,37 +750,37 @@ with conn.start_request() as request:
|
||||
)
|
||||
|
||||
def test_operation_failure_without_request(self):
|
||||
# Ensure Connection doesn't close socket after it gets an error
|
||||
# Ensure MongoClient doesn't close socket after it gets an error
|
||||
# response to getLastError. PYTHON-395.
|
||||
c = get_connection(auto_start_request=False)
|
||||
c = get_client()
|
||||
pool = c._MongoClient__pool
|
||||
self.assertEqual(1, len(pool.sockets))
|
||||
old_sock_info = iter(pool.sockets).next()
|
||||
c.pymongo_test.test.drop()
|
||||
c.pymongo_test.test.insert({'_id': 'foo'}, safe=True)
|
||||
c.pymongo_test.test.insert({'_id': 'foo'})
|
||||
self.assertRaises(
|
||||
OperationFailure,
|
||||
c.pymongo_test.test.insert, {'_id': 'foo'}, safe=True)
|
||||
c.pymongo_test.test.insert, {'_id': 'foo'})
|
||||
|
||||
self.assertEqual(1, len(pool.sockets))
|
||||
new_sock_info = iter(pool.sockets).next()
|
||||
self.assertEqual(old_sock_info, new_sock_info)
|
||||
|
||||
def test_operation_failure_with_request(self):
|
||||
# Ensure Connection doesn't close socket after it gets an error
|
||||
# Ensure MongoClient doesn't close socket after it gets an error
|
||||
# response to getLastError. PYTHON-395.
|
||||
c = get_connection(auto_start_request=True)
|
||||
c = get_client(auto_start_request=True)
|
||||
pool = c._MongoClient__pool
|
||||
|
||||
# Connection has reserved a socket for this thread
|
||||
# MongoClient has reserved a socket for this thread
|
||||
self.assertTrue(isinstance(pool._get_request_state(), SocketInfo))
|
||||
|
||||
old_sock_info = pool._get_request_state()
|
||||
c.pymongo_test.test.drop()
|
||||
c.pymongo_test.test.insert({'_id': 'foo'}, safe=True)
|
||||
c.pymongo_test.test.insert({'_id': 'foo'})
|
||||
self.assertRaises(
|
||||
OperationFailure,
|
||||
c.pymongo_test.test.insert, {'_id': 'foo'}, safe=True)
|
||||
c.pymongo_test.test.insert, {'_id': 'foo'})
|
||||
|
||||
# OperationFailure doesn't affect the request socket
|
||||
self.assertEqual(old_sock_info, pool._get_request_state())
|
||||
|
||||
@ -31,13 +31,13 @@ from pymongo.database import Database
|
||||
from pymongo.errors import (InvalidOperation,
|
||||
OperationFailure)
|
||||
from test import version
|
||||
from test.test_connection import get_connection
|
||||
from test.test_connection import get_client
|
||||
|
||||
|
||||
class TestCursor(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.db = Database(get_connection(), "pymongo_test")
|
||||
self.db = Database(get_client(), "pymongo_test")
|
||||
|
||||
def test_explain(self):
|
||||
a = self.db.test.find()
|
||||
@ -716,21 +716,21 @@ class TestCursor(unittest.TestCase):
|
||||
|
||||
cursor = db.test.find(tailable=True)
|
||||
|
||||
db.test.insert({"x": 1}, safe=True)
|
||||
db.test.insert({"x": 1})
|
||||
count = 0
|
||||
for doc in cursor:
|
||||
count += 1
|
||||
self.assertEqual(1, doc["x"])
|
||||
self.assertEqual(1, count)
|
||||
|
||||
db.test.insert({"x": 2}, safe=True)
|
||||
db.test.insert({"x": 2})
|
||||
count = 0
|
||||
for doc in cursor:
|
||||
count += 1
|
||||
self.assertEqual(2, doc["x"])
|
||||
self.assertEqual(1, count)
|
||||
|
||||
db.test.insert({"x": 3}, safe=True)
|
||||
db.test.insert({"x": 3})
|
||||
count = 0
|
||||
for doc in cursor:
|
||||
count += 1
|
||||
|
||||
@ -42,46 +42,46 @@ from pymongo.son_manipulator import (AutoReference,
|
||||
ObjectIdShuffler)
|
||||
from test import version
|
||||
from test.utils import is_mongos, server_started_with_auth
|
||||
from test.test_connection import get_connection
|
||||
from test.test_connection import get_client
|
||||
|
||||
|
||||
class TestDatabase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.connection = get_connection()
|
||||
self.client = get_client()
|
||||
|
||||
def test_name(self):
|
||||
self.assertRaises(TypeError, Database, self.connection, 4)
|
||||
self.assertRaises(InvalidName, Database, self.connection, "my db")
|
||||
self.assertRaises(InvalidName, Database, self.connection, "my\x00db")
|
||||
self.assertRaises(TypeError, Database, self.client, 4)
|
||||
self.assertRaises(InvalidName, Database, self.client, "my db")
|
||||
self.assertRaises(InvalidName, Database, self.client, "my\x00db")
|
||||
self.assertRaises(InvalidName, Database,
|
||||
self.connection, u"my\u0000db")
|
||||
self.assertEqual("name", Database(self.connection, "name").name)
|
||||
self.client, u"my\u0000db")
|
||||
self.assertEqual("name", Database(self.client, "name").name)
|
||||
|
||||
def test_equality(self):
|
||||
self.assertNotEqual(Database(self.connection, "test"),
|
||||
Database(self.connection, "mike"))
|
||||
self.assertEqual(Database(self.connection, "test"),
|
||||
Database(self.connection, "test"))
|
||||
self.assertNotEqual(Database(self.client, "test"),
|
||||
Database(self.client, "mike"))
|
||||
self.assertEqual(Database(self.client, "test"),
|
||||
Database(self.client, "test"))
|
||||
|
||||
# Explicitly test inequality
|
||||
self.assertFalse(Database(self.connection, "test") !=
|
||||
Database(self.connection, "test"))
|
||||
self.assertFalse(Database(self.client, "test") !=
|
||||
Database(self.client, "test"))
|
||||
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(Database(self.connection, "pymongo_test")),
|
||||
"Database(%r, %s)" % (self.connection,
|
||||
self.assertEqual(repr(Database(self.client, "pymongo_test")),
|
||||
"Database(%r, %s)" % (self.client,
|
||||
repr(u"pymongo_test")))
|
||||
|
||||
def test_get_coll(self):
|
||||
db = Database(self.connection, "pymongo_test")
|
||||
db = Database(self.client, "pymongo_test")
|
||||
self.assertEqual(db.test, db["test"])
|
||||
self.assertEqual(db.test, Collection(db, "test"))
|
||||
self.assertNotEqual(db.test, Collection(db, "mike"))
|
||||
self.assertEqual(db.test.mike, db["test.mike"])
|
||||
|
||||
def test_create_collection(self):
|
||||
db = Database(self.connection, "pymongo_test")
|
||||
db = Database(self.client, "pymongo_test")
|
||||
|
||||
db.test.insert({"hello": "world"})
|
||||
self.assertRaises(CollectionInvalid, db.create_collection, "test")
|
||||
@ -104,7 +104,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
|
||||
|
||||
def test_collection_names(self):
|
||||
db = Database(self.connection, "pymongo_test")
|
||||
db = Database(self.client, "pymongo_test")
|
||||
db.test.save({"dummy": u"object"})
|
||||
db.test.mike.save({"dummy": u"object"})
|
||||
|
||||
@ -115,7 +115,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertTrue("$" not in coll)
|
||||
|
||||
def test_drop_collection(self):
|
||||
db = Database(self.connection, "pymongo_test")
|
||||
db = Database(self.client, "pymongo_test")
|
||||
|
||||
self.assertRaises(TypeError, db.drop_collection, 5)
|
||||
self.assertRaises(TypeError, db.drop_collection, None)
|
||||
@ -144,7 +144,7 @@ class TestDatabase(unittest.TestCase):
|
||||
db.drop_collection(db.test.doesnotexist)
|
||||
|
||||
def test_validate_collection(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
|
||||
self.assertRaises(TypeError, db.validate_collection, 5)
|
||||
self.assertRaises(TypeError, db.validate_collection, None)
|
||||
@ -164,9 +164,9 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertTrue(db.validate_collection(db.test, True, True))
|
||||
|
||||
def test_profiling_levels(self):
|
||||
if is_mongos(self.connection):
|
||||
if is_mongos(self.client):
|
||||
raise SkipTest('profile is not supported by mongos')
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
self.assertEqual(db.profiling_level(), OFF) # default
|
||||
|
||||
self.assertRaises(ValueError, db.set_profiling_level, 5.5)
|
||||
@ -194,9 +194,9 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual(100, db.command("profile", -1)['slowms'])
|
||||
|
||||
def test_profiling_info(self):
|
||||
if is_mongos(self.connection):
|
||||
if is_mongos(self.client):
|
||||
raise SkipTest('profile is not supported by mongos')
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
|
||||
db.set_profiling_level(ALL)
|
||||
db.test.find()
|
||||
@ -207,7 +207,7 @@ class TestDatabase(unittest.TestCase):
|
||||
|
||||
# Check if we're going to fail because of SERVER-4754, in which
|
||||
# profiling info isn't collected if mongod was started with --auth
|
||||
if server_started_with_auth(self.connection):
|
||||
if server_started_with_auth(self.client):
|
||||
raise SkipTest(
|
||||
"We need SERVER-4754 fixed for the rest of this test to pass"
|
||||
)
|
||||
@ -228,7 +228,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertTrue(isinstance(info[0]["ts"], datetime.datetime))
|
||||
|
||||
def test_iteration(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
|
||||
def iterate():
|
||||
[a for a in db]
|
||||
@ -236,9 +236,9 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertRaises(TypeError, iterate)
|
||||
|
||||
def test_errors(self):
|
||||
if is_mongos(self.connection):
|
||||
if is_mongos(self.client):
|
||||
raise SkipTest('getpreverror not supported by mongos')
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
|
||||
db.reset_error_history()
|
||||
self.assertEqual(None, db.error())
|
||||
@ -271,12 +271,12 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual(None, db.previous_error())
|
||||
|
||||
def test_command(self):
|
||||
db = self.connection.admin
|
||||
db = self.client.admin
|
||||
|
||||
self.assertEqual(db.command("buildinfo"), db.command({"buildinfo": 1}))
|
||||
|
||||
def test_last_status(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
|
||||
db.test.remove({})
|
||||
db.test.save({"i": 1})
|
||||
@ -302,10 +302,10 @@ class TestDatabase(unittest.TestCase):
|
||||
u"81e0e2364499209f466e75926a162d73")
|
||||
|
||||
def test_authenticate_add_remove_user(self):
|
||||
if (is_mongos(self.connection) and not
|
||||
version.at_least(self.connection, (2, 0, 0))):
|
||||
if (is_mongos(self.client) and not
|
||||
version.at_least(self.client, (2, 0, 0))):
|
||||
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.system.users.remove({})
|
||||
db.remove_user("mike")
|
||||
|
||||
@ -345,72 +345,74 @@ class TestDatabase(unittest.TestCase):
|
||||
db.logout()
|
||||
|
||||
def test_authenticate_and_safe(self):
|
||||
if (is_mongos(self.connection) and not
|
||||
version.at_least(self.connection, (2, 0, 0))):
|
||||
if (is_mongos(self.client) and not
|
||||
version.at_least(self.client, (2, 0, 0))):
|
||||
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
|
||||
db = self.connection.auth_test
|
||||
db = self.client.auth_test
|
||||
db.system.users.remove({})
|
||||
db.add_user("bernie", "password")
|
||||
db.authenticate("bernie", "password")
|
||||
|
||||
db.test.remove({})
|
||||
self.assertTrue(db.test.insert({"bim": "baz"}, safe=True))
|
||||
self.assertTrue(db.test.insert({"bim": "baz"}))
|
||||
self.assertEqual(1, db.test.count())
|
||||
|
||||
self.assertEqual(1,
|
||||
db.test.update({"bim": "baz"},
|
||||
{"$set": {"bim": "bar"}},
|
||||
safe=True).get('n'))
|
||||
{"$set": {"bim": "bar"}}).get('n'))
|
||||
|
||||
self.assertEqual(1,
|
||||
db.test.remove({}, safe=True).get('n'))
|
||||
db.test.remove({}).get('n'))
|
||||
|
||||
self.assertEqual(0, db.test.count())
|
||||
self.connection.drop_database("auth_test")
|
||||
self.client.drop_database("auth_test")
|
||||
|
||||
|
||||
def test_authenticate_and_request(self):
|
||||
if (is_mongos(self.connection) and not
|
||||
version.at_least(self.connection, (2, 0, 0))):
|
||||
if (is_mongos(self.client) and not
|
||||
version.at_least(self.client, (2, 0, 0))):
|
||||
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
|
||||
|
||||
# Database.authenticate() needs to be in a request - check that it
|
||||
# always runs in a request, and that it restores the request state
|
||||
# (in or not in a request) properly when it's finished.
|
||||
self.assertTrue(self.connection.auto_start_request)
|
||||
db = self.connection.pymongo_test
|
||||
self.assertFalse(self.client.auto_start_request)
|
||||
db = self.client.pymongo_test
|
||||
db.system.users.remove({})
|
||||
db.remove_user("mike")
|
||||
db.add_user("mike", "password")
|
||||
self.assertTrue(self.connection.in_request())
|
||||
self.assertFalse(self.client.in_request())
|
||||
self.assertTrue(db.authenticate("mike", "password"))
|
||||
self.assertTrue(self.connection.in_request())
|
||||
self.assertFalse(self.client.in_request())
|
||||
|
||||
no_request_cx = get_connection(auto_start_request=False)
|
||||
no_request_db = no_request_cx.pymongo_test
|
||||
self.assertFalse(no_request_cx.in_request())
|
||||
self.assertTrue(no_request_db.authenticate("mike", "password"))
|
||||
self.assertFalse(no_request_cx.in_request())
|
||||
request_cx = get_client(auto_start_request=True)
|
||||
request_db = request_cx.pymongo_test
|
||||
self.assertTrue(request_cx.in_request())
|
||||
self.assertTrue(request_db.authenticate("mike", "password"))
|
||||
self.assertTrue(request_cx.in_request())
|
||||
|
||||
# just make sure there are no exceptions here
|
||||
db.logout()
|
||||
no_request_db.logout()
|
||||
db.collection.find_one()
|
||||
request_db.logout()
|
||||
request_db.collection.find_one()
|
||||
|
||||
def test_authenticate_multiple(self):
|
||||
conn = get_connection()
|
||||
if (is_mongos(conn) and not
|
||||
version.at_least(self.connection, (2, 0, 0))):
|
||||
client = get_client()
|
||||
if (is_mongos(client) and not
|
||||
version.at_least(self.client, (2, 0, 0))):
|
||||
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
|
||||
if not server_started_with_auth(conn):
|
||||
if not server_started_with_auth(client):
|
||||
raise SkipTest("Authentication is not enabled on server")
|
||||
|
||||
# Setup
|
||||
users_db = conn.pymongo_test
|
||||
admin_db = conn.admin
|
||||
other_db = conn.pymongo_test1
|
||||
users_db.system.users.remove(safe=True)
|
||||
admin_db.system.users.remove(safe=True)
|
||||
users_db.test.remove(safe=True)
|
||||
other_db.test.remove(safe=True)
|
||||
users_db = client.pymongo_test
|
||||
admin_db = client.admin
|
||||
other_db = client.pymongo_test1
|
||||
users_db.system.users.remove()
|
||||
admin_db.system.users.remove()
|
||||
users_db.test.remove()
|
||||
other_db.test.remove()
|
||||
|
||||
admin_db.add_user('admin', 'pass')
|
||||
self.assertTrue(admin_db.authenticate('admin', 'pass'))
|
||||
@ -432,26 +434,26 @@ class TestDatabase(unittest.TestCase):
|
||||
admin_db.authenticate('ro-admin', 'pass')
|
||||
self.assertEqual(0, other_db.test.count())
|
||||
self.assertRaises(OperationFailure,
|
||||
other_db.test.insert, {}, safe=True)
|
||||
other_db.test.insert, {})
|
||||
|
||||
# Force close all sockets
|
||||
conn.disconnect()
|
||||
client.disconnect()
|
||||
|
||||
# We should still be able to write to the regular user's db
|
||||
self.assertTrue(users_db.test.remove(safe=True))
|
||||
self.assertTrue(users_db.test.remove())
|
||||
# And read from other dbs...
|
||||
self.assertEqual(0, other_db.test.count())
|
||||
# But still not write to other dbs...
|
||||
self.assertRaises(OperationFailure,
|
||||
other_db.test.insert, {}, safe=True)
|
||||
other_db.test.insert, {})
|
||||
|
||||
# Cleanup
|
||||
admin_db.logout()
|
||||
users_db.logout()
|
||||
self.assertTrue(admin_db.authenticate('admin', 'pass'))
|
||||
self.assertTrue(admin_db.system.users.remove(safe=True))
|
||||
self.assertTrue(admin_db.system.users.remove())
|
||||
self.assertEqual(0, admin_db.system.users.count())
|
||||
self.assertTrue(users_db.system.users.remove(safe=True))
|
||||
self.assertTrue(users_db.system.users.remove())
|
||||
|
||||
def test_id_ordering(self):
|
||||
# PyMongo attempts to have _id show up first
|
||||
@ -460,10 +462,8 @@ class TestDatabase(unittest.TestCase):
|
||||
# guarantee any particular order. This will never
|
||||
# work right in Jython or Python >= 3.3 with
|
||||
# hash randomization enabled.
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.test.remove({})
|
||||
|
||||
db.test.insert({"hello": "world", "_id": 5})
|
||||
db.test.insert(SON([("hello", "world"),
|
||||
("_id", 5)]))
|
||||
|
||||
@ -482,7 +482,7 @@ class TestDatabase(unittest.TestCase):
|
||||
break
|
||||
|
||||
def test_deref(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.test.remove({})
|
||||
|
||||
self.assertRaises(TypeError, db.dereference, 5)
|
||||
@ -504,7 +504,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual(obj, db.dereference(DBRef("test", 4)))
|
||||
|
||||
def test_eval(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.test.remove({})
|
||||
|
||||
self.assertRaises(TypeError, db.eval, None)
|
||||
@ -531,7 +531,7 @@ class TestDatabase(unittest.TestCase):
|
||||
|
||||
# TODO some of these tests belong in the collection level testing.
|
||||
def test_save_find_one(self):
|
||||
db = Database(self.connection, "pymongo_test")
|
||||
db = Database(self.client, "pymongo_test")
|
||||
db.test.remove({})
|
||||
|
||||
a_doc = SON({"hello": u"world"})
|
||||
@ -558,13 +558,13 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual(count, 1)
|
||||
|
||||
def test_long(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.test.remove({})
|
||||
db.test.save({"x": 9223372036854775807L})
|
||||
self.assertEqual(9223372036854775807L, db.test.find_one()["x"])
|
||||
|
||||
def test_remove(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.test.remove({})
|
||||
|
||||
one = db.test.save({"x": 1})
|
||||
@ -598,7 +598,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertFalse(db.test.find_one())
|
||||
|
||||
def test_save_a_bunch(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.test.remove({})
|
||||
|
||||
for i in xrange(1000):
|
||||
@ -616,7 +616,7 @@ class TestDatabase(unittest.TestCase):
|
||||
break
|
||||
|
||||
def test_auto_ref_and_deref(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.add_son_manipulator(AutoReference(db))
|
||||
db.add_son_manipulator(NamespaceInjector())
|
||||
|
||||
@ -645,7 +645,7 @@ class TestDatabase(unittest.TestCase):
|
||||
|
||||
# some stuff the user marc wanted to be able to do, make sure it works
|
||||
def test_marc(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.add_son_manipulator(AutoReference(db))
|
||||
db.add_son_manipulator(NamespaceInjector())
|
||||
|
||||
@ -668,7 +668,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual("bar", db.users.find_one()["messages"][1]["title"])
|
||||
|
||||
def test_system_js(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.system.js.remove()
|
||||
|
||||
self.assertEqual(0, db.system.js.count())
|
||||
@ -701,7 +701,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual(5, db.system_js.no_param())
|
||||
|
||||
def test_system_js_list(self):
|
||||
db = self.connection.pymongo_test
|
||||
db = self.client.pymongo_test
|
||||
db.system.js.remove()
|
||||
self.assertEqual([], db.system_js.list())
|
||||
|
||||
@ -715,7 +715,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual(["bar"], db.system_js.list())
|
||||
|
||||
def test_manipulator_properties(self):
|
||||
db = self.connection.foo
|
||||
db = self.client.foo
|
||||
self.assertEqual(['ObjectIdInjector'], db.incoming_manipulators)
|
||||
self.assertEqual([], db.incoming_copying_manipulators)
|
||||
self.assertEqual([], db.outgoing_manipulators)
|
||||
|
||||
@ -18,14 +18,14 @@ import unittest
|
||||
import sys
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from pymongo import Connection
|
||||
from pymongo import MongoClient
|
||||
from pymongo.errors import PyMongoError
|
||||
|
||||
|
||||
class TestErrors(unittest.TestCase):
|
||||
|
||||
def test_base_exception(self):
|
||||
self.assertRaises(PyMongoError, Connection, port=0)
|
||||
self.assertRaises(PyMongoError, MongoClient, port=0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@ -35,14 +35,14 @@ from gridfs.grid_file import (DEFAULT_CHUNK_SIZE,
|
||||
GridOut)
|
||||
from gridfs.errors import (NoFile,
|
||||
UnsupportedAPI)
|
||||
from test.test_connection import get_connection
|
||||
from test.test_connection import get_client
|
||||
from test import qcheck
|
||||
|
||||
|
||||
class TestGridFile(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.db = get_connection().pymongo_test
|
||||
self.db = get_client().pymongo_test
|
||||
self.db.fs.files.remove({})
|
||||
self.db.fs.chunks.remove({})
|
||||
|
||||
|
||||
@ -19,11 +19,10 @@
|
||||
import sys
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from gridfs.grid_file import GridIn
|
||||
from pymongo.connection import Connection
|
||||
from pymongo.mongo_client import MongoClient
|
||||
from pymongo.errors import AutoReconnect
|
||||
from pymongo.read_preferences import ReadPreference
|
||||
from test.test_replica_set_connection import TestConnectionReplicaSetBase
|
||||
from test.test_replica_set_connection import TestReplicaSetClientBase
|
||||
|
||||
import datetime
|
||||
import unittest
|
||||
@ -34,7 +33,7 @@ import gridfs
|
||||
from bson.py3compat import b, StringIO
|
||||
from gridfs.errors import (FileExists,
|
||||
NoFile)
|
||||
from test.test_connection import get_connection
|
||||
from test.test_connection import get_client
|
||||
from test.utils import joinall
|
||||
|
||||
|
||||
@ -73,7 +72,7 @@ class JustRead(threading.Thread):
|
||||
class TestGridfs(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.db = get_connection().pymongo_test
|
||||
self.db = get_client().pymongo_test
|
||||
self.db.drop_collection("fs.files")
|
||||
self.db.drop_collection("fs.chunks")
|
||||
self.db.drop_collection("alt.files")
|
||||
@ -271,7 +270,7 @@ class TestGridfs(unittest.TestCase):
|
||||
self.assertEqual(b("hello world"), self.fs.get(oid).read())
|
||||
|
||||
def test_file_exists(self):
|
||||
db = get_connection(w=1).pymongo_test
|
||||
db = get_client(w=1).pymongo_test
|
||||
fs = gridfs.GridFS(db)
|
||||
|
||||
oid = fs.put(b("hello"))
|
||||
@ -351,21 +350,6 @@ class TestGridfs(unittest.TestCase):
|
||||
self.db.fs.files.find({'filename':'test'}).count()
|
||||
)
|
||||
|
||||
|
||||
class TestGridfsRequest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
# TODO: merge this into TestGridfs as we update all tests to use
|
||||
# MongoClient instead of Connection
|
||||
from pymongo.mongo_client import MongoClient
|
||||
from test.test_connection import host, port
|
||||
|
||||
# MongoClient defaults to w=1, auto_start_request=False
|
||||
self.db = MongoClient(host, port, w=0).pymongo_test
|
||||
self.db.drop_collection("fs.files")
|
||||
self.db.drop_collection("fs.chunks")
|
||||
self.fs = gridfs.GridFS(self.db)
|
||||
|
||||
def test_gridfs_request(self):
|
||||
self.assertFalse(self.db.connection.in_request())
|
||||
self.fs.put(b("hello world"))
|
||||
@ -373,9 +357,9 @@ class TestGridfsRequest(unittest.TestCase):
|
||||
self.assertFalse(self.db.connection.in_request())
|
||||
|
||||
|
||||
class TestGridfsReplicaSet(TestConnectionReplicaSetBase):
|
||||
class TestGridfsReplicaSet(TestReplicaSetClientBase):
|
||||
def test_gridfs_replica_set(self):
|
||||
rsc = self._get_connection(
|
||||
rsc = self._get_client(
|
||||
w=self.w, wtimeout=5000,
|
||||
read_preference=ReadPreference.SECONDARY)
|
||||
|
||||
@ -389,14 +373,14 @@ class TestGridfsReplicaSet(TestConnectionReplicaSetBase):
|
||||
|
||||
def test_gridfs_secondary(self):
|
||||
primary_host, primary_port = self.primary
|
||||
primary_connection = Connection(primary_host, primary_port)
|
||||
primary_connection = MongoClient(primary_host, primary_port)
|
||||
|
||||
secondary_host, secondary_port = self.secondaries[0]
|
||||
for secondary_connection in [
|
||||
Connection(secondary_host, secondary_port, slave_okay=True),
|
||||
Connection(secondary_host, secondary_port,
|
||||
read_preference=ReadPreference.SECONDARY),
|
||||
]:
|
||||
MongoClient(secondary_host, secondary_port, slave_okay=True),
|
||||
MongoClient(secondary_host, secondary_port,
|
||||
read_preference=ReadPreference.SECONDARY),
|
||||
]:
|
||||
primary_connection.pymongo_test.drop_collection("fs.files")
|
||||
primary_connection.pymongo_test.drop_collection("fs.chunks")
|
||||
|
||||
@ -408,7 +392,7 @@ class TestGridfsReplicaSet(TestConnectionReplicaSetBase):
|
||||
self.assertRaises(AutoReconnect, fs.put, b('foo'))
|
||||
|
||||
def tearDown(self):
|
||||
rsc = self._get_connection()
|
||||
rsc = self._get_client()
|
||||
rsc.pymongo_test.drop_collection('fs.files')
|
||||
rsc.pymongo_test.drop_collection('fs.chunks')
|
||||
rsc.close()
|
||||
|
||||
@ -35,7 +35,7 @@ from bson.objectid import ObjectId
|
||||
from bson.timestamp import Timestamp
|
||||
from bson.tz_util import utc
|
||||
|
||||
from test.test_connection import get_connection
|
||||
from test.test_connection import get_client
|
||||
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
@ -46,7 +46,7 @@ class TestJsonUtil(unittest.TestCase):
|
||||
if not json_util.json_lib:
|
||||
raise SkipTest("No json or simplejson module")
|
||||
|
||||
self.db = get_connection().pymongo_test
|
||||
self.db = get_client().pymongo_test
|
||||
|
||||
def round_tripped(self, doc):
|
||||
return json_util.loads(json_util.dumps(doc))
|
||||
|
||||
99
test/test_legacy_connections.py
Normal file
99
test/test_legacy_connections.py
Normal file
@ -0,0 +1,99 @@
|
||||
# Copyright 2013 10gen, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Test deprecated client classes Connection and ReplicaSetConnection."""
|
||||
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
from bson import ObjectId
|
||||
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
import pymongo
|
||||
from pymongo.connection import Connection
|
||||
from pymongo.replica_set_connection import ReplicaSetConnection
|
||||
from pymongo.errors import ConfigurationError
|
||||
|
||||
from test.test_replica_set_connection import TestReplicaSetClientBase
|
||||
from test.test_connection import host, port
|
||||
|
||||
|
||||
class TestConnection(unittest.TestCase):
|
||||
def test_connection(self):
|
||||
c = Connection(host, port)
|
||||
self.assertTrue(c.auto_start_request)
|
||||
self.assertFalse(c.slave_okay)
|
||||
self.assertFalse(c.safe)
|
||||
self.assertEqual({}, c.get_lasterror_options())
|
||||
|
||||
# Connection's writes are unacknowledged by default
|
||||
doc = {"_id": ObjectId()}
|
||||
coll = c.pymongo_test.write_concern_test
|
||||
coll.drop()
|
||||
coll.insert(doc)
|
||||
coll.insert(doc)
|
||||
|
||||
c = Connection("mongodb://%s:%s/?safe=true" % (host, port))
|
||||
self.assertTrue(c.safe)
|
||||
|
||||
# Connection's network_timeout argument is translated into
|
||||
# socketTimeoutMS
|
||||
self.assertEqual(123, Connection(
|
||||
host, port, network_timeout=123)._MongoClient__net_timeout)
|
||||
|
||||
for network_timeout in 'foo', 0, -1:
|
||||
self.assertRaises(ConfigurationError,
|
||||
Connection, host, port, network_timeout=network_timeout)
|
||||
|
||||
def test_connection_alias(self):
|
||||
# Testing that pymongo module imports connection.Connection
|
||||
self.assertEqual(Connection, pymongo.Connection)
|
||||
|
||||
|
||||
class TestReplicaSetConnection(TestReplicaSetClientBase):
|
||||
def test_replica_set_connection(self):
|
||||
c = ReplicaSetConnection(host, port, replicaSet=self.name)
|
||||
self.assertTrue(c.auto_start_request)
|
||||
self.assertFalse(c.slave_okay)
|
||||
self.assertFalse(c.safe)
|
||||
self.assertEqual({}, c.get_lasterror_options())
|
||||
|
||||
# ReplicaSetConnection's writes are unacknowledged by default
|
||||
doc = {"_id": ObjectId()}
|
||||
coll = c.pymongo_test.write_concern_test
|
||||
coll.drop()
|
||||
coll.insert(doc)
|
||||
coll.insert(doc)
|
||||
|
||||
c = ReplicaSetConnection("mongodb://%s:%s/?replicaSet=%s&safe=true" % (
|
||||
host, port, self.name))
|
||||
|
||||
self.assertTrue(c.safe)
|
||||
|
||||
# ReplicaSetConnection's network_timeout argument is translated into
|
||||
# socketTimeoutMS
|
||||
self.assertEqual(123, ReplicaSetConnection(
|
||||
host, port, replicaSet=self.name, network_timeout=123
|
||||
)._MongoReplicaSetClient__net_timeout)
|
||||
|
||||
for network_timeout in 'foo', 0, -1:
|
||||
self.assertRaises(ConfigurationError,
|
||||
ReplicaSetConnection, host, port, replicaSet=self.name,
|
||||
network_timeout=network_timeout)
|
||||
|
||||
def test_replica_set_connection_alias(self):
|
||||
# Testing that pymongo module imports ReplicaSetConnection
|
||||
self.assertEqual(ReplicaSetConnection, pymongo.ReplicaSetConnection)
|
||||
|
||||
@ -31,7 +31,7 @@ from pymongo.errors import ConnectionFailure, InvalidName
|
||||
from pymongo.errors import CollectionInvalid, OperationFailure
|
||||
from pymongo.errors import AutoReconnect
|
||||
from pymongo.database import Database
|
||||
from pymongo.connection import Connection
|
||||
from pymongo.mongo_client import MongoClient
|
||||
from pymongo.collection import Collection
|
||||
from pymongo.master_slave_connection import MasterSlaveConnection
|
||||
from test.utils import TestRequestMixin
|
||||
@ -44,18 +44,18 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
self.host = host
|
||||
self.port = port
|
||||
|
||||
self.master = Connection(host, port)
|
||||
self.master = MongoClient(host, port)
|
||||
|
||||
self.slaves = []
|
||||
try:
|
||||
self.slaves.append(Connection(os.environ.get("DB_IP2", host),
|
||||
self.slaves.append(MongoClient(os.environ.get("DB_IP2", host),
|
||||
int(os.environ.get("DB_PORT2", 27018)),
|
||||
read_preference=ReadPreference.SECONDARY))
|
||||
except ConnectionFailure:
|
||||
pass
|
||||
|
||||
try:
|
||||
self.slaves.append(Connection(os.environ.get("DB_IP3", host),
|
||||
self.slaves.append(MongoClient(os.environ.get("DB_IP3", host),
|
||||
int(os.environ.get("DB_PORT3", 27019)),
|
||||
read_preference=ReadPreference.SECONDARY))
|
||||
except ConnectionFailure:
|
||||
@ -64,14 +64,14 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
if not self.slaves:
|
||||
raise SkipTest("Not connected to master-slave set")
|
||||
|
||||
self.connection = MasterSlaveConnection(self.master, self.slaves)
|
||||
self.db = self.connection.pymongo_test
|
||||
self.client = MasterSlaveConnection(self.master, self.slaves)
|
||||
self.db = self.client.pymongo_test
|
||||
|
||||
def tearDown(self):
|
||||
try:
|
||||
self.db.test.drop_indexes()
|
||||
except Exception:
|
||||
# Tests like test_disconnect can monkey with the connection in ways
|
||||
# Tests like test_disconnect can monkey with the client in ways
|
||||
# that make this fail
|
||||
pass
|
||||
|
||||
@ -83,41 +83,41 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
self.assertRaises(TypeError, MasterSlaveConnection, self.master, [1])
|
||||
|
||||
def test_use_greenlets(self):
|
||||
self.assertFalse(self.connection.use_greenlets)
|
||||
self.assertFalse(self.client.use_greenlets)
|
||||
|
||||
if thread_util.have_greenlet:
|
||||
master = Connection(self.host, self.port, use_greenlets=True)
|
||||
master = MongoClient(self.host, self.port, use_greenlets=True)
|
||||
slaves = [
|
||||
Connection(slave.host, slave.port, use_greenlets=True)
|
||||
MongoClient(slave.host, slave.port, use_greenlets=True)
|
||||
for slave in self.slaves]
|
||||
|
||||
self.assertTrue(
|
||||
MasterSlaveConnection(master, slaves).use_greenlets)
|
||||
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(self.connection),
|
||||
self.assertEqual(repr(self.client),
|
||||
"MasterSlaveConnection(%r, %r)" %
|
||||
(self.master, self.slaves))
|
||||
|
||||
def test_disconnect(self):
|
||||
class Connection(object):
|
||||
class MongoClient(object):
|
||||
def __init__(self):
|
||||
self._disconnects = 0
|
||||
|
||||
def disconnect(self):
|
||||
self._disconnects += 1
|
||||
|
||||
self.connection._MasterSlaveConnection__master = Connection()
|
||||
self.connection._MasterSlaveConnection__slaves = [Connection(),
|
||||
Connection()]
|
||||
self.client._MasterSlaveConnection__master = MongoClient()
|
||||
self.client._MasterSlaveConnection__slaves = [MongoClient(),
|
||||
MongoClient()]
|
||||
|
||||
self.connection.disconnect()
|
||||
self.client.disconnect()
|
||||
self.assertEqual(1,
|
||||
self.connection._MasterSlaveConnection__master._disconnects)
|
||||
self.client._MasterSlaveConnection__master._disconnects)
|
||||
self.assertEqual(1,
|
||||
self.connection._MasterSlaveConnection__slaves[0]._disconnects)
|
||||
self.client._MasterSlaveConnection__slaves[0]._disconnects)
|
||||
self.assertEqual(1,
|
||||
self.connection._MasterSlaveConnection__slaves[1]._disconnects)
|
||||
self.client._MasterSlaveConnection__slaves[1]._disconnects)
|
||||
|
||||
def test_continue_until_slave_works(self):
|
||||
class Slave(object):
|
||||
@ -146,9 +146,9 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
NotRandomList.last_idx = idx
|
||||
return self._items.pop(0)
|
||||
|
||||
self.connection._MasterSlaveConnection__slaves = NotRandomList()
|
||||
self.client._MasterSlaveConnection__slaves = NotRandomList()
|
||||
|
||||
response = self.connection._send_message_with_response('message')
|
||||
response = self.client._send_message_with_response('message')
|
||||
self.assertEqual((NotRandomList.last_idx, 'sent'), response)
|
||||
self.assertNotEqual(-1, NotRandomList.last_idx)
|
||||
self.assertEqual(3, Slave.calls)
|
||||
@ -177,10 +177,10 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
def __getitem__(self, idx):
|
||||
return self._items.pop(0)
|
||||
|
||||
self.connection._MasterSlaveConnection__slaves = NotRandomList()
|
||||
self.client._MasterSlaveConnection__slaves = NotRandomList()
|
||||
|
||||
self.assertRaises(AutoReconnect,
|
||||
self.connection._send_message_with_response, 'message')
|
||||
self.client._send_message_with_response, 'message')
|
||||
self.assertEqual(4, Slave.calls)
|
||||
|
||||
def test_get_db(self):
|
||||
@ -188,57 +188,57 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
def make_db(base, name):
|
||||
return base[name]
|
||||
|
||||
self.assertRaises(InvalidName, make_db, self.connection, "")
|
||||
self.assertRaises(InvalidName, make_db, self.connection, "te$t")
|
||||
self.assertRaises(InvalidName, make_db, self.connection, "te.t")
|
||||
self.assertRaises(InvalidName, make_db, self.connection, "te\\t")
|
||||
self.assertRaises(InvalidName, make_db, self.connection, "te/t")
|
||||
self.assertRaises(InvalidName, make_db, self.connection, "te st")
|
||||
self.assertRaises(InvalidName, make_db, self.client, "")
|
||||
self.assertRaises(InvalidName, make_db, self.client, "te$t")
|
||||
self.assertRaises(InvalidName, make_db, self.client, "te.t")
|
||||
self.assertRaises(InvalidName, make_db, self.client, "te\\t")
|
||||
self.assertRaises(InvalidName, make_db, self.client, "te/t")
|
||||
self.assertRaises(InvalidName, make_db, self.client, "te st")
|
||||
|
||||
self.assertTrue(isinstance(self.connection.test, Database))
|
||||
self.assertEqual(self.connection.test, self.connection["test"])
|
||||
self.assertEqual(self.connection.test, Database(self.connection,
|
||||
self.assertTrue(isinstance(self.client.test, Database))
|
||||
self.assertEqual(self.client.test, self.client["test"])
|
||||
self.assertEqual(self.client.test, Database(self.client,
|
||||
"test"))
|
||||
|
||||
def test_database_names(self):
|
||||
self.connection.pymongo_test.test.save({"dummy": u"object"})
|
||||
self.connection.pymongo_test_mike.test.save({"dummy": u"object"})
|
||||
self.client.pymongo_test.test.save({"dummy": u"object"})
|
||||
self.client.pymongo_test_mike.test.save({"dummy": u"object"})
|
||||
|
||||
dbs = self.connection.database_names()
|
||||
dbs = self.client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
self.assertTrue("pymongo_test_mike" in dbs)
|
||||
|
||||
def test_drop_database(self):
|
||||
self.assertRaises(TypeError, self.connection.drop_database, 5)
|
||||
self.assertRaises(TypeError, self.connection.drop_database, None)
|
||||
self.assertRaises(TypeError, self.client.drop_database, 5)
|
||||
self.assertRaises(TypeError, self.client.drop_database, None)
|
||||
|
||||
raise SkipTest("This test often fails due to SERVER-2329")
|
||||
|
||||
self.connection.pymongo_test.test.save({"dummy": u"object"}, safe=True)
|
||||
dbs = self.connection.database_names()
|
||||
self.client.pymongo_test.test.save({"dummy": u"object"})
|
||||
dbs = self.client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
self.connection.drop_database("pymongo_test")
|
||||
dbs = self.connection.database_names()
|
||||
self.client.drop_database("pymongo_test")
|
||||
dbs = self.client.database_names()
|
||||
self.assertTrue("pymongo_test" not in dbs)
|
||||
|
||||
self.connection.pymongo_test.test.save({"dummy": u"object"})
|
||||
dbs = self.connection.database_names()
|
||||
self.client.pymongo_test.test.save({"dummy": u"object"})
|
||||
dbs = self.client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
self.connection.drop_database(self.connection.pymongo_test)
|
||||
dbs = self.connection.database_names()
|
||||
self.client.drop_database(self.client.pymongo_test)
|
||||
dbs = self.client.database_names()
|
||||
self.assertTrue("pymongo_test" not in dbs)
|
||||
|
||||
def test_iteration(self):
|
||||
|
||||
def iterate():
|
||||
[a for a in self.connection]
|
||||
[a for a in self.client]
|
||||
|
||||
self.assertRaises(TypeError, iterate)
|
||||
|
||||
def test_insert_find_one_in_request(self):
|
||||
count = 0
|
||||
for i in range(100):
|
||||
self.connection.start_request()
|
||||
self.client.start_request()
|
||||
self.db.test.remove({})
|
||||
self.db.test.insert({"x": i})
|
||||
try:
|
||||
@ -246,44 +246,43 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
count += 1
|
||||
except:
|
||||
count += 1
|
||||
self.connection.end_request()
|
||||
self.client.end_request()
|
||||
self.assertFalse(count)
|
||||
|
||||
def test_nested_request(self):
|
||||
conn = self.connection
|
||||
client = self.client
|
||||
|
||||
def assertRequest(in_request):
|
||||
self.assertEqual(in_request, conn.in_request())
|
||||
self.assertEqual(in_request, conn.master.in_request())
|
||||
self.assertEqual(in_request, client.in_request())
|
||||
self.assertEqual(in_request, client.master.in_request())
|
||||
|
||||
# MasterSlaveConnection is special, alas - it has no auto_start_request
|
||||
# and it begins *not* in a request. When it's in a request, it sends
|
||||
# all queries to primary.
|
||||
self.assertFalse(conn.in_request())
|
||||
self.assertTrue(conn.master.in_request())
|
||||
conn.master.end_request()
|
||||
self.assertFalse(client.in_request())
|
||||
self.assertFalse(client.master.in_request())
|
||||
|
||||
# Start and end request
|
||||
conn.start_request()
|
||||
client.start_request()
|
||||
assertRequest(True)
|
||||
conn.end_request()
|
||||
client.end_request()
|
||||
assertRequest(False)
|
||||
|
||||
# Double-nesting
|
||||
conn.start_request()
|
||||
conn.start_request()
|
||||
conn.end_request()
|
||||
client.start_request()
|
||||
client.start_request()
|
||||
client.end_request()
|
||||
assertRequest(True)
|
||||
conn.end_request()
|
||||
client.end_request()
|
||||
assertRequest(False)
|
||||
|
||||
def test_request_threads(self):
|
||||
conn = self.connection
|
||||
client = self.client
|
||||
|
||||
# In a request, all ops go through master
|
||||
pool = conn.master._MongoClient__pool
|
||||
conn.master.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
pool = client.master._MongoClient__pool
|
||||
client.master.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
|
||||
started_request, ended_request = threading.Event(), threading.Event()
|
||||
checked_request = threading.Event()
|
||||
@ -292,15 +291,15 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
# Starting a request in one thread doesn't put the other thread in a
|
||||
# request
|
||||
def f():
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
conn.start_request()
|
||||
self.assertInRequestAndSameSock(conn, pool)
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
client.start_request()
|
||||
self.assertInRequestAndSameSock(client, pool)
|
||||
started_request.set()
|
||||
checked_request.wait()
|
||||
checked_request.clear()
|
||||
self.assertInRequestAndSameSock(conn, pool)
|
||||
conn.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
self.assertInRequestAndSameSock(client, pool)
|
||||
client.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
ended_request.set()
|
||||
checked_request.wait()
|
||||
thread_done[0] = True
|
||||
@ -309,18 +308,18 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
t.setDaemon(True)
|
||||
t.start()
|
||||
started_request.wait()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
checked_request.set()
|
||||
ended_request.wait()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
checked_request.set()
|
||||
t.join()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pool)
|
||||
self.assertNotInRequestAndDifferentSock(client, pool)
|
||||
self.assertTrue(thread_done[0], "Thread didn't complete")
|
||||
|
||||
# This was failing because commands were being sent to the slaves
|
||||
def test_create_collection(self):
|
||||
self.connection.pymongo_test.test.drop()
|
||||
self.client.pymongo_test.test.drop()
|
||||
|
||||
collection = self.db.create_collection('test')
|
||||
self.assertTrue(isinstance(collection, Collection))
|
||||
@ -329,12 +328,12 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
|
||||
# Believe this was failing for the same reason...
|
||||
def test_unique_index(self):
|
||||
self.connection.pymongo_test.test.drop()
|
||||
self.client.pymongo_test.test.drop()
|
||||
self.db.test.create_index('username', unique=True)
|
||||
|
||||
self.db.test.save({'username': 'mike'}, safe=True)
|
||||
self.db.test.save({'username': 'mike'})
|
||||
self.assertRaises(OperationFailure,
|
||||
self.db.test.save, {'username': 'mike'}, safe=True)
|
||||
self.db.test.save, {'username': 'mike'})
|
||||
|
||||
# NOTE this test is non-deterministic, but I expect
|
||||
# some failures unless the db is pulling instantaneously...
|
||||
@ -367,7 +366,7 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
self.assertFalse(count)
|
||||
|
||||
def test_kill_cursor_explicit(self):
|
||||
c = self.connection
|
||||
c = self.client
|
||||
c.slave_okay = True
|
||||
db = c.pymongo_test
|
||||
|
||||
@ -417,27 +416,26 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
self.assertRaises(OperationFailure, lambda: list(cursor2))
|
||||
|
||||
def test_base_object(self):
|
||||
c = self.connection
|
||||
c = self.client
|
||||
self.assertFalse(c.slave_okay)
|
||||
self.assertTrue(bool(c.read_preference))
|
||||
self.assertFalse(c.safe)
|
||||
self.assertTrue(c.safe)
|
||||
self.assertEqual({}, c.get_lasterror_options())
|
||||
db = c.pymongo_test
|
||||
self.assertFalse(db.slave_okay)
|
||||
self.assertTrue(bool(c.read_preference))
|
||||
self.assertFalse(db.safe)
|
||||
self.assertTrue(db.safe)
|
||||
self.assertEqual({}, db.get_lasterror_options())
|
||||
coll = db.test
|
||||
coll.drop()
|
||||
self.assertFalse(coll.slave_okay)
|
||||
self.assertTrue(bool(c.read_preference))
|
||||
self.assertFalse(coll.safe)
|
||||
self.assertTrue(coll.safe)
|
||||
self.assertEqual({}, coll.get_lasterror_options())
|
||||
cursor = coll.find()
|
||||
self.assertFalse(cursor._Cursor__slave_okay)
|
||||
self.assertTrue(bool(cursor._Cursor__read_preference))
|
||||
|
||||
c.safe = True
|
||||
w = 1 + len(self.slaves)
|
||||
wtimeout=10000 # Wait 10 seconds for replication to complete
|
||||
c.set_lasterror_options(w=w, wtimeout=wtimeout)
|
||||
@ -466,19 +464,18 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
coll.remove({'foo': 'bar'})
|
||||
self.assertEqual(0, coll.find({'foo': 'bar'}).count())
|
||||
|
||||
# Set self.connection back to defaults
|
||||
c.safe = False
|
||||
c.unset_lasterror_options()
|
||||
self.assertFalse(self.connection.slave_okay)
|
||||
self.assertTrue(bool(self.connection.read_preference))
|
||||
self.assertFalse(self.connection.safe)
|
||||
self.assertEqual({}, self.connection.get_lasterror_options())
|
||||
self.assertFalse(self.client.slave_okay)
|
||||
self.assertTrue(bool(self.client.read_preference))
|
||||
self.assertFalse(self.client.safe)
|
||||
self.assertEqual({}, self.client.get_lasterror_options())
|
||||
|
||||
def test_document_class(self):
|
||||
c = MasterSlaveConnection(self.master, self.slaves)
|
||||
db = c.pymongo_test
|
||||
w = 1 + len(self.slaves)
|
||||
db.test.insert({"x": 1}, safe=True, w=w)
|
||||
db.test.insert({"x": 1}, w=w)
|
||||
|
||||
self.assertEqual(dict, c.document_class)
|
||||
self.assertTrue(isinstance(db.test.find_one(), dict))
|
||||
@ -505,23 +502,23 @@ class TestMasterSlaveConnection(unittest.TestCase, TestRequestMixin):
|
||||
|
||||
def test_tz_aware(self):
|
||||
dt = datetime.datetime.utcnow()
|
||||
conn = MasterSlaveConnection(self.master, self.slaves)
|
||||
self.assertEqual(False, conn.tz_aware)
|
||||
db = conn.pymongo_test
|
||||
client = MasterSlaveConnection(self.master, self.slaves)
|
||||
self.assertEqual(False, client.tz_aware)
|
||||
db = client.pymongo_test
|
||||
w = 1 + len(self.slaves)
|
||||
db.tztest.insert({'dt': dt}, safe=True, w=w)
|
||||
db.tztest.insert({'dt': dt}, w=w)
|
||||
self.assertEqual(None, db.tztest.find_one()['dt'].tzinfo)
|
||||
|
||||
conn = MasterSlaveConnection(self.master, self.slaves, tz_aware=True)
|
||||
self.assertEqual(True, conn.tz_aware)
|
||||
db = conn.pymongo_test
|
||||
db.tztest.insert({'dt': dt}, safe=True, w=w)
|
||||
client = MasterSlaveConnection(self.master, self.slaves, tz_aware=True)
|
||||
self.assertEqual(True, client.tz_aware)
|
||||
db = client.pymongo_test
|
||||
db.tztest.insert({'dt': dt}, w=w)
|
||||
self.assertEqual(utc, db.tztest.find_one()['dt'].tzinfo)
|
||||
|
||||
conn = MasterSlaveConnection(self.master, self.slaves, tz_aware=False)
|
||||
self.assertEqual(False, conn.tz_aware)
|
||||
db = conn.pymongo_test
|
||||
db.tztest.insert({'dt': dt}, safe=True, w=w)
|
||||
client = MasterSlaveConnection(self.master, self.slaves, tz_aware=False)
|
||||
self.assertEqual(False, client.tz_aware)
|
||||
db = client.pymongo_test
|
||||
db.tztest.insert({'dt': dt}, w=w)
|
||||
self.assertEqual(None, db.tztest.find_one()['dt'].tzinfo)
|
||||
|
||||
|
||||
|
||||
@ -22,14 +22,12 @@ localhost:27017 and localhost:27018 by default.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import warnings
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from pymongo.errors import ConnectionFailure
|
||||
from pymongo.connection import Connection
|
||||
from pymongo.mongo_client import MongoClient
|
||||
|
||||
skip_tests = True
|
||||
|
||||
@ -53,42 +51,43 @@ class TestPaired(unittest.TestCase):
|
||||
from nose.plugins.skip import SkipTest
|
||||
raise SkipTest()
|
||||
|
||||
|
||||
def test_connect(self):
|
||||
self.skip()
|
||||
self.assertRaises(ConnectionFailure, Connection,
|
||||
[self.bad, self.bad])
|
||||
|
||||
connection = Connection([self.left, self.right])
|
||||
self.assertTrue(connection)
|
||||
# Use a timeout so the test is reasonably fast
|
||||
self.assertRaises(ConnectionFailure, MongoClient,
|
||||
[self.bad, self.bad], connectTimeoutMS=500)
|
||||
|
||||
host = connection.host
|
||||
port = connection.port
|
||||
client = MongoClient([self.left, self.right])
|
||||
self.assertTrue(client)
|
||||
|
||||
connection = Connection([self.right, self.left])
|
||||
self.assertTrue(connection)
|
||||
self.assertEqual(host, connection.host)
|
||||
self.assertEqual(port, connection.port)
|
||||
host = client.host
|
||||
port = client.port
|
||||
|
||||
slave = self.left == (host, port) and self.right or self.left
|
||||
self.assertRaises(ConnectionFailure, Connection,
|
||||
[slave, self.bad])
|
||||
self.assertRaises(ConnectionFailure, Connection,
|
||||
[self.bad, slave])
|
||||
client = MongoClient([self.right, self.left], connectTimeoutMS=500)
|
||||
self.assertTrue(client)
|
||||
self.assertEqual(host, client.host)
|
||||
self.assertEqual(port, client.port)
|
||||
|
||||
def test_repr(self):
|
||||
self.skip()
|
||||
connection = Connection([self.left, self.right])
|
||||
if self.left == '%s:%s' % (host, port):
|
||||
slave = self.right
|
||||
else:
|
||||
slave = self.left
|
||||
|
||||
self.assertEqual(repr(connection),
|
||||
"Connection(['%s', '%s'])" %
|
||||
(self.left, self.right))
|
||||
# Refuse to connect if master absent
|
||||
self.assertRaises(ConnectionFailure, MongoClient,
|
||||
[slave, self.bad], connectTimeoutMS=500)
|
||||
self.assertRaises(ConnectionFailure, MongoClient,
|
||||
[self.bad, slave], connectTimeoutMS=500)
|
||||
|
||||
# No error
|
||||
MongoClient([self.left, self.bad], connectTimeoutMS=500)
|
||||
|
||||
def test_basic(self):
|
||||
self.skip()
|
||||
connection = Connection([self.left, self.right])
|
||||
client = MongoClient([self.left, self.right])
|
||||
|
||||
db = connection.pymongo_test
|
||||
db = client.pymongo_test
|
||||
|
||||
db.drop_collection("test")
|
||||
a = {"x": 1}
|
||||
@ -97,14 +96,14 @@ class TestPaired(unittest.TestCase):
|
||||
|
||||
def test_end_request(self):
|
||||
self.skip()
|
||||
connection = Connection([self.left, self.right])
|
||||
db = connection.pymongo_test
|
||||
client = MongoClient([self.left, self.right])
|
||||
db = client.pymongo_test
|
||||
|
||||
for _ in range(100):
|
||||
db.test.remove({})
|
||||
db.test.insert({})
|
||||
self.assertTrue(db.test.find_one())
|
||||
connection.end_request()
|
||||
client.end_request()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -41,8 +41,8 @@ class TestPoolingThreads(_TestPooling, unittest.TestCase):
|
||||
raise SkipTest("No multiprocessing module")
|
||||
|
||||
coll = self.c.pymongo_test.test
|
||||
coll.remove(safe=True)
|
||||
coll.insert({'_id': 1}, safe=True)
|
||||
coll.remove()
|
||||
coll.insert({'_id': 1})
|
||||
coll.find_one()
|
||||
self.assert_pool_size(1)
|
||||
self.c.start_request()
|
||||
@ -102,8 +102,8 @@ class TestPoolingThreads(_TestPooling, unittest.TestCase):
|
||||
self.assertEqual(sock_ids[0], sock_ids[1])
|
||||
|
||||
def test_pool_with_fork(self):
|
||||
# Test that separate Connections have separate Pools, and that the
|
||||
# driver can create a new Connection after forking
|
||||
# Test that separate MongoClients have separate Pools, and that the
|
||||
# driver can create a new MongoClient after forking
|
||||
if sys.platform == "win32":
|
||||
raise SkipTest("Can't test forking on Windows")
|
||||
|
||||
@ -112,15 +112,15 @@ class TestPoolingThreads(_TestPooling, unittest.TestCase):
|
||||
except ImportError:
|
||||
raise SkipTest("No multiprocessing module")
|
||||
|
||||
a = self.get_connection(auto_start_request=False)
|
||||
a.pymongo_test.test.remove(safe=True)
|
||||
a.pymongo_test.test.insert({'_id':1}, safe=True)
|
||||
a = self.get_client(auto_start_request=False)
|
||||
a.pymongo_test.test.remove()
|
||||
a.pymongo_test.test.insert({'_id':1})
|
||||
a.pymongo_test.test.find_one()
|
||||
self.assertEqual(1, len(a._MongoClient__pool.sockets))
|
||||
a_sock = one(a._MongoClient__pool.sockets)
|
||||
|
||||
def loop(pipe):
|
||||
c = self.get_connection(auto_start_request=False)
|
||||
c = self.get_client(auto_start_request=False)
|
||||
self.assertEqual(1,len(c._MongoClient__pool.sockets))
|
||||
c.pymongo_test.test.find_one()
|
||||
self.assertEqual(1,len(c._MongoClient__pool.sockets))
|
||||
|
||||
@ -28,11 +28,11 @@ sys.path[0:0] = [""]
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
import pymongo.pool
|
||||
from pymongo.connection import Connection
|
||||
from pymongo.mongo_client import MongoClient
|
||||
from pymongo.pool import Pool, NO_REQUEST, NO_SOCKET_YET, SocketInfo
|
||||
from pymongo.errors import ConfigurationError
|
||||
from test import version
|
||||
from test.test_connection import get_connection, host, port
|
||||
from test.test_connection import get_client, host, port
|
||||
from test.utils import delay, is_mongos, one
|
||||
|
||||
N = 50
|
||||
@ -53,11 +53,11 @@ except ImportError:
|
||||
|
||||
|
||||
class MongoThread(object):
|
||||
"""A thread, or a greenlet, that uses a Connection"""
|
||||
"""A thread, or a greenlet, that uses a MongoClient"""
|
||||
def __init__(self, test_case):
|
||||
self.use_greenlets = test_case.use_greenlets
|
||||
self.connection = test_case.c
|
||||
self.db = self.connection[DB]
|
||||
self.client = test_case.c
|
||||
self.db = self.client[DB]
|
||||
self.ut = test_case
|
||||
self.passed = False
|
||||
|
||||
@ -96,7 +96,7 @@ class SaveAndFind(MongoThread):
|
||||
def run_mongo_thread(self):
|
||||
for _ in xrange(N):
|
||||
rand = random.randint(0, N)
|
||||
_id = self.db.sf.save({"x": rand}, safe=True)
|
||||
_id = self.db.sf.save({"x": rand})
|
||||
self.ut.assertEqual(rand, self.db.sf.find_one(_id)["x"])
|
||||
|
||||
|
||||
@ -104,40 +104,39 @@ class Unique(MongoThread):
|
||||
|
||||
def run_mongo_thread(self):
|
||||
for _ in xrange(N):
|
||||
self.connection.start_request()
|
||||
self.db.unique.insert({})
|
||||
self.ut.assertEqual(None, self.db.error())
|
||||
self.connection.end_request()
|
||||
self.client.start_request()
|
||||
self.db.unique.insert({}) # no error
|
||||
self.client.end_request()
|
||||
|
||||
|
||||
class NonUnique(MongoThread):
|
||||
|
||||
def run_mongo_thread(self):
|
||||
for _ in xrange(N):
|
||||
self.connection.start_request()
|
||||
self.db.unique.insert({"_id": "jesse"})
|
||||
self.client.start_request()
|
||||
self.db.unique.insert({"_id": "jesse"}, w=0)
|
||||
self.ut.assertNotEqual(None, self.db.error())
|
||||
self.connection.end_request()
|
||||
self.client.end_request()
|
||||
|
||||
|
||||
class Disconnect(MongoThread):
|
||||
|
||||
def run_mongo_thread(self):
|
||||
for _ in xrange(N):
|
||||
self.connection.disconnect()
|
||||
self.client.disconnect()
|
||||
|
||||
|
||||
class NoRequest(MongoThread):
|
||||
|
||||
def run_mongo_thread(self):
|
||||
self.connection.start_request()
|
||||
self.client.start_request()
|
||||
errors = 0
|
||||
for _ in xrange(N):
|
||||
self.db.unique.insert({"_id": "jesse"})
|
||||
self.db.unique.insert({"_id": "jesse"}, w=0)
|
||||
if not self.db.error():
|
||||
errors += 1
|
||||
|
||||
self.connection.end_request()
|
||||
self.client.end_request()
|
||||
self.ut.assertEqual(0, errors)
|
||||
|
||||
|
||||
@ -149,7 +148,7 @@ def run_cases(ut, cases):
|
||||
and gevent.version_info[0] < 1
|
||||
):
|
||||
# Gevent 0.13.6 bug on Mac, Greenlet.join() hangs if more than
|
||||
# about 35 Greenlets share a Connection. Apparently fixed in
|
||||
# about 35 Greenlets share a MongoClient. Apparently fixed in
|
||||
# recent Gevent development.
|
||||
nruns = 5
|
||||
|
||||
@ -172,27 +171,27 @@ class OneOp(MongoThread):
|
||||
super(OneOp, self).__init__(ut)
|
||||
|
||||
def run_mongo_thread(self):
|
||||
pool = self.connection._MongoClient__pool
|
||||
pool = self.client._MongoClient__pool
|
||||
assert len(pool.sockets) == 1, "Expected 1 socket, found %d" % (
|
||||
len(pool.sockets)
|
||||
)
|
||||
|
||||
sock_info = one(pool.sockets)
|
||||
|
||||
self.connection.start_request()
|
||||
self.client.start_request()
|
||||
|
||||
# start_request() hasn't yet moved the socket from the general pool into
|
||||
# the request
|
||||
assert len(pool.sockets) == 1
|
||||
assert one(pool.sockets) == sock_info
|
||||
|
||||
self.connection[DB].test.find_one()
|
||||
self.client[DB].test.find_one()
|
||||
|
||||
# find_one() causes the socket to be used in the request, so now it's
|
||||
# bound to this thread
|
||||
assert len(pool.sockets) == 0
|
||||
assert pool._get_request_state() == sock_info
|
||||
self.connection.end_request()
|
||||
self.client.end_request()
|
||||
|
||||
# The socket is back in the pool
|
||||
assert len(pool.sockets) == 1
|
||||
@ -211,9 +210,9 @@ class CreateAndReleaseSocket(MongoThread):
|
||||
self.lock = threading.Lock()
|
||||
self.ready = threading.Event()
|
||||
|
||||
def __init__(self, ut, connection, start_request, end_request, rendevous):
|
||||
def __init__(self, ut, client, start_request, end_request, rendevous):
|
||||
super(CreateAndReleaseSocket, self).__init__(ut)
|
||||
self.connection = connection
|
||||
self.client = client
|
||||
self.start_request = start_request
|
||||
self.end_request = end_request
|
||||
self.rendevous = rendevous
|
||||
@ -224,10 +223,10 @@ class CreateAndReleaseSocket(MongoThread):
|
||||
# lots of simultaneous connections, to ensure that Pool obeys its
|
||||
# max_size configuration and closes extra sockets as they're returned.
|
||||
for i in range(self.start_request):
|
||||
self.connection.start_request()
|
||||
self.client.start_request()
|
||||
|
||||
# Use a socket
|
||||
self.connection[DB].test.find_one()
|
||||
self.client[DB].test.find_one()
|
||||
|
||||
# Don't finish until all threads reach this point
|
||||
r = self.rendevous
|
||||
@ -243,11 +242,11 @@ class CreateAndReleaseSocket(MongoThread):
|
||||
assert r.ready.isSet(), "Rendezvous timed out"
|
||||
|
||||
for i in range(self.end_request):
|
||||
self.connection.end_request()
|
||||
self.client.end_request()
|
||||
|
||||
|
||||
class _TestPoolingBase(object):
|
||||
"""Base class for all connection-pool tests. Doesn't inherit from
|
||||
"""Base class for all client-pool tests. Doesn't inherit from
|
||||
unittest.TestCase, and its name is prefixed with "_" to avoid being
|
||||
run by nose. Real tests double-inherit from this base and from TestCase.
|
||||
"""
|
||||
@ -260,18 +259,18 @@ class _TestPoolingBase(object):
|
||||
|
||||
# Note we don't do patch_thread() or patch_all() - we're
|
||||
# testing here that patch_thread() is unnecessary for
|
||||
# the connection pool to work properly.
|
||||
# the client pool to work properly.
|
||||
monkey.patch_socket()
|
||||
|
||||
self.c = self.get_connection(auto_start_request=False)
|
||||
self.c = self.get_client(auto_start_request=False)
|
||||
|
||||
# reset the db
|
||||
db = self.c[DB]
|
||||
db.unique.drop()
|
||||
db.test.drop()
|
||||
db.unique.insert({"_id": "jesse"}, safe=True)
|
||||
db.unique.insert({"_id": "jesse"})
|
||||
|
||||
db.test.insert([{} for i in range(10)], safe=True)
|
||||
db.test.insert([{} for i in range(10)])
|
||||
|
||||
def tearDown(self):
|
||||
self.c.close()
|
||||
@ -279,10 +278,10 @@ class _TestPoolingBase(object):
|
||||
# Undo patch
|
||||
reload(socket)
|
||||
|
||||
def get_connection(self, *args, **kwargs):
|
||||
def get_client(self, *args, **kwargs):
|
||||
opts = kwargs.copy()
|
||||
opts['use_greenlets'] = self.use_greenlets
|
||||
return get_connection(*args, **opts)
|
||||
return get_client(*args, **opts)
|
||||
|
||||
def get_pool(self, *args, **kwargs):
|
||||
kwargs['use_greenlets'] = self.use_greenlets
|
||||
@ -313,23 +312,23 @@ class _TestPooling(_TestPoolingBase):
|
||||
"""Basic pool tests, to be run both with threads and with greenlets."""
|
||||
def test_max_pool_size_validation(self):
|
||||
self.assertRaises(
|
||||
ConfigurationError, Connection, host=host, port=port,
|
||||
ConfigurationError, MongoClient, host=host, port=port,
|
||||
max_pool_size=-1
|
||||
)
|
||||
|
||||
self.assertRaises(
|
||||
ConfigurationError, Connection, host=host, port=port,
|
||||
ConfigurationError, MongoClient, host=host, port=port,
|
||||
max_pool_size='foo'
|
||||
)
|
||||
|
||||
c = Connection(host=host, port=port, max_pool_size=100)
|
||||
c = MongoClient(host=host, port=port, max_pool_size=100)
|
||||
self.assertEqual(c.max_pool_size, 100)
|
||||
|
||||
def test_no_disconnect(self):
|
||||
run_cases(self, [NoRequest, NonUnique, Unique, SaveAndFind])
|
||||
|
||||
def test_simple_disconnect(self):
|
||||
# Connection just created, expect 1 free socket
|
||||
# MongoClient just created, expect 1 free socket
|
||||
self.assert_pool_size(1)
|
||||
self.assert_no_request()
|
||||
|
||||
@ -391,8 +390,8 @@ class _TestPooling(_TestPoolingBase):
|
||||
self.assert_pool_size(1)
|
||||
|
||||
def test_multiple_connections(self):
|
||||
a = self.get_connection(auto_start_request=False)
|
||||
b = self.get_connection(auto_start_request=False)
|
||||
a = self.get_client(auto_start_request=False)
|
||||
b = self.get_client(auto_start_request=False)
|
||||
self.assertEqual(1, len(a._MongoClient__pool.sockets))
|
||||
self.assertEqual(1, len(b._MongoClient__pool.sockets))
|
||||
|
||||
@ -673,10 +672,10 @@ class _TestMaxPoolSize(_TestPoolingBase):
|
||||
with greenlets.
|
||||
"""
|
||||
def _test_max_pool_size(self, start_request, end_request):
|
||||
c = self.get_connection(max_pool_size=4, auto_start_request=False)
|
||||
c = self.get_client(max_pool_size=4, auto_start_request=False)
|
||||
# If you increase nthreads over about 35, note a
|
||||
# Gevent 0.13.6 bug on Mac, Greenlet.join() hangs if more than
|
||||
# about 35 Greenlets share a Connection. Apparently fixed in
|
||||
# about 35 Greenlets share a MongoClient. Apparently fixed in
|
||||
# recent Gevent development.
|
||||
nthreads = 10
|
||||
|
||||
@ -760,14 +759,14 @@ class _TestPoolSocketSharing(_TestPoolingBase):
|
||||
gr1: get results
|
||||
gr0: get results
|
||||
"""
|
||||
cx = get_connection(
|
||||
cx = get_client(
|
||||
use_greenlets=self.use_greenlets,
|
||||
auto_start_request=False
|
||||
)
|
||||
|
||||
db = cx.pymongo_test
|
||||
db.test.remove(safe=True)
|
||||
db.test.insert({'_id': 1}, safe=True)
|
||||
db.test.remove()
|
||||
db.test.insert({'_id': 1})
|
||||
|
||||
history = []
|
||||
|
||||
|
||||
@ -28,11 +28,12 @@ class TestPyMongo(unittest.TestCase):
|
||||
self.host = os.environ.get("DB_IP", "localhost")
|
||||
self.port = int(os.environ.get("DB_PORT", 27017))
|
||||
|
||||
def test_connection_alias(self):
|
||||
c = pymongo.Connection(self.host, self.port)
|
||||
self.assertTrue(c)
|
||||
def test_mongo_client_alias(self):
|
||||
# Testing that pymongo module imports mongo_client.MongoClient
|
||||
c = pymongo.MongoClient(self.host, self.port)
|
||||
self.assertEqual(c.host, self.host)
|
||||
self.assertEqual(c.port, self.port)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -24,53 +24,53 @@ sys.path[0:0] = [""]
|
||||
|
||||
from bson.son import SON
|
||||
from pymongo.cursor import _QUERY_OPTIONS
|
||||
from pymongo.replica_set_connection import ReplicaSetConnection
|
||||
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
|
||||
from pymongo.read_preferences import (ReadPreference, modes, MovingAverage,
|
||||
secondary_ok_commands)
|
||||
from pymongo.errors import ConfigurationError
|
||||
|
||||
from test.test_replica_set_connection import TestConnectionReplicaSetBase
|
||||
from test.test_connection import get_connection, host, port
|
||||
from test.test_replica_set_connection import TestReplicaSetClientBase
|
||||
from test.test_connection import get_client, host, port
|
||||
from test import version, utils
|
||||
|
||||
|
||||
class TestReadPreferencesBase(TestConnectionReplicaSetBase):
|
||||
class TestReadPreferencesBase(TestReplicaSetClientBase):
|
||||
def setUp(self):
|
||||
super(TestReadPreferencesBase, self).setUp()
|
||||
# Insert some data so we can use cursors in read_from_which_host
|
||||
c = self._get_connection()
|
||||
c = self._get_client()
|
||||
c.pymongo_test.test.drop()
|
||||
c.pymongo_test.test.insert([{'_id': i} for i in range(10)], w=self.w)
|
||||
|
||||
def tearDown(self):
|
||||
super(TestReadPreferencesBase, self).tearDown()
|
||||
c = self._get_connection()
|
||||
c = self._get_client()
|
||||
c.pymongo_test.test.drop()
|
||||
|
||||
def read_from_which_host(self, connection):
|
||||
"""Do a find() on the connection and return which host was used
|
||||
def read_from_which_host(self, client):
|
||||
"""Do a find() on the client and return which host was used
|
||||
"""
|
||||
cursor = connection.pymongo_test.test.find()
|
||||
cursor = client.pymongo_test.test.find()
|
||||
cursor.next()
|
||||
return cursor._Cursor__connection_id
|
||||
|
||||
def read_from_which_kind(self, connection):
|
||||
"""Do a find() on the connection and return 'primary' or 'secondary'
|
||||
depending on which the connection used.
|
||||
def read_from_which_kind(self, client):
|
||||
"""Do a find() on the client and return 'primary' or 'secondary'
|
||||
depending on which the client used.
|
||||
"""
|
||||
connection_id = self.read_from_which_host(connection)
|
||||
if connection_id == connection.primary:
|
||||
connection_id = self.read_from_which_host(client)
|
||||
if connection_id == client.primary:
|
||||
return 'primary'
|
||||
elif connection_id in connection.secondaries:
|
||||
elif connection_id in client.secondaries:
|
||||
return 'secondary'
|
||||
else:
|
||||
self.fail(
|
||||
'Cursor used connection id %s, expected either primary '
|
||||
'%s or secondaries %s' % (
|
||||
connection_id, connection.primary, connection.secondaries))
|
||||
connection_id, client.primary, client.secondaries))
|
||||
|
||||
def assertReadsFrom(self, expected, **kwargs):
|
||||
c = self._get_connection(**kwargs)
|
||||
c = self._get_client(**kwargs)
|
||||
used = self.read_from_which_kind(c)
|
||||
self.assertEqual(expected, used, 'Cursor used %s, expected %s' % (
|
||||
expected, used))
|
||||
@ -80,52 +80,52 @@ class TestReadPreferences(TestReadPreferencesBase):
|
||||
def test_mode_validation(self):
|
||||
# 'modes' are imported from read_preferences.py
|
||||
for mode in modes:
|
||||
self.assertEqual(mode, self._get_connection(
|
||||
self.assertEqual(mode, self._get_client(
|
||||
read_preference=mode).read_preference)
|
||||
|
||||
self.assertRaises(ConfigurationError, self._get_connection,
|
||||
self.assertRaises(ConfigurationError, self._get_client,
|
||||
read_preference='foo')
|
||||
|
||||
def test_tag_sets_validation(self):
|
||||
# Can't use tags with PRIMARY
|
||||
self.assertRaises(ConfigurationError, self._get_connection,
|
||||
self.assertRaises(ConfigurationError, self._get_client,
|
||||
tag_sets=[{'k': 'v'}])
|
||||
|
||||
# ... but empty tag sets are ok with PRIMARY
|
||||
self.assertEqual([{}], self._get_connection(tag_sets=[{}]).tag_sets)
|
||||
self.assertEqual([{}], self._get_client(tag_sets=[{}]).tag_sets)
|
||||
|
||||
S = ReadPreference.SECONDARY
|
||||
self.assertEqual([{}], self._get_connection(read_preference=S).tag_sets)
|
||||
self.assertEqual([{}], self._get_client(read_preference=S).tag_sets)
|
||||
|
||||
self.assertEqual([{'k': 'v'}], self._get_connection(
|
||||
self.assertEqual([{'k': 'v'}], self._get_client(
|
||||
read_preference=S, tag_sets=[{'k': 'v'}]).tag_sets)
|
||||
|
||||
self.assertEqual([{'k': 'v'}, {}], self._get_connection(
|
||||
self.assertEqual([{'k': 'v'}, {}], self._get_client(
|
||||
read_preference=S, tag_sets=[{'k': 'v'}, {}]).tag_sets)
|
||||
|
||||
self.assertRaises(ConfigurationError, self._get_connection,
|
||||
self.assertRaises(ConfigurationError, self._get_client,
|
||||
read_preference=S, tag_sets=[])
|
||||
|
||||
# One dict not ok, must be a list of dicts
|
||||
self.assertRaises(ConfigurationError, self._get_connection,
|
||||
self.assertRaises(ConfigurationError, self._get_client,
|
||||
read_preference=S, tag_sets={'k': 'v'})
|
||||
|
||||
self.assertRaises(ConfigurationError, self._get_connection,
|
||||
self.assertRaises(ConfigurationError, self._get_client,
|
||||
read_preference=S, tag_sets='foo')
|
||||
|
||||
self.assertRaises(ConfigurationError, self._get_connection,
|
||||
self.assertRaises(ConfigurationError, self._get_client,
|
||||
read_preference=S, tag_sets=['foo'])
|
||||
|
||||
def test_latency_validation(self):
|
||||
self.assertEqual(17, self._get_connection(
|
||||
self.assertEqual(17, self._get_client(
|
||||
secondary_acceptable_latency_ms=17
|
||||
).secondary_acceptable_latency_ms)
|
||||
|
||||
self.assertEqual(42, self._get_connection(
|
||||
self.assertEqual(42, self._get_client(
|
||||
secondaryAcceptableLatencyMS=42
|
||||
).secondary_acceptable_latency_ms)
|
||||
|
||||
self.assertEqual(666, self._get_connection(
|
||||
self.assertEqual(666, self._get_client(
|
||||
secondaryacceptablelatencyms=666
|
||||
).secondary_acceptable_latency_ms)
|
||||
|
||||
@ -136,7 +136,7 @@ class TestReadPreferences(TestReadPreferencesBase):
|
||||
def test_primary_with_tags(self):
|
||||
# Tags not allowed with PRIMARY
|
||||
self.assertRaises(ConfigurationError,
|
||||
self._get_connection, tag_sets=[{'dc': 'ny'}])
|
||||
self._get_client, tag_sets=[{'dc': 'ny'}])
|
||||
|
||||
def test_primary_preferred(self):
|
||||
self.assertReadsFrom('primary',
|
||||
@ -159,7 +159,7 @@ class TestReadPreferences(TestReadPreferencesBase):
|
||||
def test_nearest(self):
|
||||
# With high secondaryAcceptableLatencyMS, expect to read from any
|
||||
# member
|
||||
c = self._get_connection(
|
||||
c = self._get_client(
|
||||
read_preference=ReadPreference.NEAREST,
|
||||
secondaryAcceptableLatencyMS=10000, # 10 seconds
|
||||
auto_start_request=False)
|
||||
@ -187,7 +187,7 @@ class TestReadPreferences(TestReadPreferencesBase):
|
||||
" but didn't use %s\nlatencies: %s" % (not_used, latencies))
|
||||
|
||||
|
||||
class ReadPrefTester(ReplicaSetConnection):
|
||||
class ReadPrefTester(MongoReplicaSetClient):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.has_read_from = set()
|
||||
super(ReadPrefTester, self).__init__(*args, **kwargs)
|
||||
@ -199,7 +199,7 @@ class ReadPrefTester(ReplicaSetConnection):
|
||||
member, *args, **kwargs)
|
||||
|
||||
|
||||
class TestCommandAndReadPreference(TestConnectionReplicaSetBase):
|
||||
class TestCommandAndReadPreference(TestReplicaSetClientBase):
|
||||
def setUp(self):
|
||||
super(TestCommandAndReadPreference, self).setUp()
|
||||
|
||||
@ -216,18 +216,18 @@ class TestCommandAndReadPreference(TestConnectionReplicaSetBase):
|
||||
|
||||
# We create a lot of collections and indexes in these tests, so drop
|
||||
# the database
|
||||
self._get_connection().drop_database('pymongo_test')
|
||||
self._get_client().drop_database('pymongo_test')
|
||||
super(TestCommandAndReadPreference, self).tearDown()
|
||||
|
||||
def executed_on_which_member(self, connection, fn, *args, **kwargs):
|
||||
connection.has_read_from.clear()
|
||||
def executed_on_which_member(self, client, fn, *args, **kwargs):
|
||||
client.has_read_from.clear()
|
||||
fn(*args, **kwargs)
|
||||
self.assertEqual(1, len(connection.has_read_from))
|
||||
member, = connection.has_read_from
|
||||
self.assertEqual(1, len(client.has_read_from))
|
||||
member, = client.has_read_from
|
||||
return member
|
||||
|
||||
def assertExecutedOn(self, state, connection, fn, *args, **kwargs):
|
||||
member = self.executed_on_which_member(connection, fn, *args, **kwargs)
|
||||
def assertExecutedOn(self, state, client, fn, *args, **kwargs):
|
||||
member = self.executed_on_which_member(client, fn, *args, **kwargs)
|
||||
if state == 'primary':
|
||||
self.assertTrue(member.is_primary)
|
||||
elif state == 'secondary':
|
||||
@ -475,7 +475,7 @@ class TestMovingAverage(unittest.TestCase):
|
||||
|
||||
class TestMongosConnection(unittest.TestCase):
|
||||
def test_mongos_connection(self):
|
||||
c = get_connection()
|
||||
c = get_client()
|
||||
is_mongos = utils.is_mongos(c)
|
||||
|
||||
# Test default mode, PRIMARY
|
||||
@ -511,9 +511,9 @@ class TestMongosConnection(unittest.TestCase):
|
||||
for tag_sets in (
|
||||
None, [{}]
|
||||
):
|
||||
# Create a connection e.g. with read_preference=NEAREST or
|
||||
# Create a client e.g. with read_preference=NEAREST or
|
||||
# slave_okay=True
|
||||
c = get_connection(tag_sets=tag_sets, **{kwarg: value})
|
||||
c = get_client(tag_sets=tag_sets, **{kwarg: value})
|
||||
|
||||
self.assertEqual(is_mongos, c.is_mongos)
|
||||
cursor = c.pymongo_test.test.find()
|
||||
@ -557,11 +557,11 @@ class TestMongosConnection(unittest.TestCase):
|
||||
# real read preference
|
||||
self.assertRaises(
|
||||
ConfigurationError,
|
||||
get_connection, tag_sets=tag_sets, **{kwarg: value})
|
||||
get_client, tag_sets=tag_sets, **{kwarg: value})
|
||||
|
||||
continue
|
||||
|
||||
c = get_connection(tag_sets=tag_sets, **{kwarg: value})
|
||||
c = get_client(tag_sets=tag_sets, **{kwarg: value})
|
||||
|
||||
self.assertEqual(is_mongos, c.is_mongos)
|
||||
cursor = c.pymongo_test.test.find()
|
||||
@ -574,7 +574,7 @@ class TestMongosConnection(unittest.TestCase):
|
||||
'$readPreference' in cursor._Cursor__query_spec())
|
||||
|
||||
def test_only_secondary_ok_commands_have_read_prefs(self):
|
||||
c = get_connection(read_preference=ReadPreference.SECONDARY)
|
||||
c = get_client(read_preference=ReadPreference.SECONDARY)
|
||||
is_mongos = utils.is_mongos(c)
|
||||
if not is_mongos:
|
||||
raise SkipTest("Only mongos have read_prefs added to the spec")
|
||||
|
||||
@ -32,9 +32,9 @@ from nose.plugins.skip import SkipTest
|
||||
|
||||
from bson.son import SON
|
||||
from bson.tz_util import utc
|
||||
from pymongo.connection import Connection
|
||||
from pymongo.mongo_client import MongoClient
|
||||
from pymongo.read_preferences import ReadPreference
|
||||
from pymongo.replica_set_connection import ReplicaSetConnection
|
||||
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
|
||||
from pymongo.mongo_replica_set_client import _partition_node, have_gevent
|
||||
from pymongo.database import Database
|
||||
from pymongo.pool import SocketInfo
|
||||
@ -53,26 +53,26 @@ host = os.environ.get("DB_IP", 'localhost')
|
||||
port = int(os.environ.get("DB_PORT", 27017))
|
||||
pair = '%s:%d' % (host, port)
|
||||
|
||||
class TestReplicaSetConnectionAgainstStandalone(unittest.TestCase):
|
||||
"""This is a funny beast -- we want to run tests for ReplicaSetConnection
|
||||
class TestReplicaSetClientAgainstStandalone(unittest.TestCase):
|
||||
"""This is a funny beast -- we want to run tests for MongoReplicaSetClient
|
||||
but only if the database at DB_IP and DB_PORT is a standalone.
|
||||
"""
|
||||
def setUp(self):
|
||||
conn = Connection(pair)
|
||||
response = conn.admin.command('ismaster')
|
||||
client = MongoClient(pair)
|
||||
response = client.admin.command('ismaster')
|
||||
if 'setName' in response:
|
||||
raise SkipTest("Connected to a replica set, not a standalone mongod")
|
||||
|
||||
def test_connect(self):
|
||||
self.assertRaises(ConfigurationError, ReplicaSetConnection,
|
||||
self.assertRaises(ConfigurationError, MongoReplicaSetClient,
|
||||
pair, replicaSet='anything',
|
||||
connectTimeoutMS=600)
|
||||
|
||||
|
||||
class TestConnectionReplicaSetBase(unittest.TestCase):
|
||||
class TestReplicaSetClientBase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
conn = Connection(pair)
|
||||
response = conn.admin.command('ismaster')
|
||||
client = MongoClient(pair)
|
||||
response = client.admin.command('ismaster')
|
||||
if 'setName' in response:
|
||||
self.name = str(response['setName'])
|
||||
self.w = len(response['hosts'])
|
||||
@ -81,7 +81,7 @@ class TestConnectionReplicaSetBase(unittest.TestCase):
|
||||
self.arbiters = set([_partition_node(h)
|
||||
for h in response.get("arbiters", [])])
|
||||
|
||||
repl_set_status = conn.admin.command('replSetGetStatus')
|
||||
repl_set_status = client.admin.command('replSetGetStatus')
|
||||
primary_info = [
|
||||
m for m in repl_set_status['members']
|
||||
if m['stateStr'] == 'PRIMARY'
|
||||
@ -95,30 +95,30 @@ class TestConnectionReplicaSetBase(unittest.TestCase):
|
||||
else:
|
||||
raise SkipTest("Not connected to a replica set")
|
||||
|
||||
def _get_connection(self, **kwargs):
|
||||
return ReplicaSetConnection(pair,
|
||||
def _get_client(self, **kwargs):
|
||||
return MongoReplicaSetClient(pair,
|
||||
replicaSet=self.name,
|
||||
**kwargs)
|
||||
|
||||
class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
def test_connect(self):
|
||||
assertRaisesExactly(ConnectionFailure, ReplicaSetConnection,
|
||||
assertRaisesExactly(ConnectionFailure, MongoReplicaSetClient,
|
||||
"somedomainthatdoesntexist.org:27017",
|
||||
replicaSet=self.name,
|
||||
connectTimeoutMS=600)
|
||||
self.assertRaises(ConfigurationError, ReplicaSetConnection,
|
||||
self.assertRaises(ConfigurationError, MongoReplicaSetClient,
|
||||
pair, replicaSet='fdlksjfdslkjfd')
|
||||
self.assertTrue(ReplicaSetConnection(pair, replicaSet=self.name))
|
||||
self.assertTrue(MongoReplicaSetClient(pair, replicaSet=self.name))
|
||||
|
||||
def test_repr(self):
|
||||
connection = self._get_connection()
|
||||
self.assertEqual(repr(connection),
|
||||
"ReplicaSetConnection(%r)" % (["%s:%d" % n
|
||||
client = self._get_client()
|
||||
self.assertEqual(repr(client),
|
||||
"MongoReplicaSetClient(%r)" % (["%s:%d" % n
|
||||
for n in
|
||||
self.hosts],))
|
||||
|
||||
def test_properties(self):
|
||||
c = ReplicaSetConnection(pair, replicaSet=self.name)
|
||||
c = MongoReplicaSetClient(pair, replicaSet=self.name)
|
||||
c.admin.command('ping')
|
||||
self.assertEqual(c.primary, self.primary)
|
||||
self.assertEqual(c.hosts, self.hosts)
|
||||
@ -127,13 +127,13 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
self.assertEqual(c.document_class, dict)
|
||||
self.assertEqual(c.tz_aware, False)
|
||||
|
||||
# Make sure RSC's properties are copied to Database and Collection
|
||||
# Make sure MRSC's properties are copied to Database and Collection
|
||||
for obj in c, c.pymongo_test, c.pymongo_test.test:
|
||||
self.assertEqual(obj.read_preference, ReadPreference.PRIMARY)
|
||||
self.assertEqual(obj.tag_sets, [{}])
|
||||
self.assertEqual(obj.secondary_acceptable_latency_ms, 15)
|
||||
self.assertEqual(obj.slave_okay, False)
|
||||
self.assertEqual(obj.safe, False)
|
||||
self.assertEqual(obj.write_concern, {})
|
||||
|
||||
cursor = c.pymongo_test.test.find()
|
||||
self.assertEqual(
|
||||
@ -144,9 +144,9 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
c.close()
|
||||
|
||||
tag_sets = [{'dc': 'la', 'rack': '2'}, {'foo': 'bar'}]
|
||||
c = ReplicaSetConnection(pair, replicaSet=self.name, max_pool_size=25,
|
||||
c = MongoReplicaSetClient(pair, replicaSet=self.name, max_pool_size=25,
|
||||
document_class=SON, tz_aware=True,
|
||||
slaveOk=False, safe=True,
|
||||
slaveOk=False,
|
||||
read_preference=ReadPreference.SECONDARY,
|
||||
tag_sets=copy.deepcopy(tag_sets),
|
||||
secondary_acceptable_latency_ms=77)
|
||||
@ -191,32 +191,32 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
|
||||
def test_use_greenlets(self):
|
||||
self.assertFalse(
|
||||
ReplicaSetConnection(pair, replicaSet=self.name).use_greenlets)
|
||||
MongoReplicaSetClient(pair, replicaSet=self.name).use_greenlets)
|
||||
|
||||
if have_gevent:
|
||||
self.assertTrue(ReplicaSetConnection(
|
||||
self.assertTrue(MongoReplicaSetClient(
|
||||
pair, replicaSet=self.name, use_greenlets=True).use_greenlets)
|
||||
|
||||
def test_get_db(self):
|
||||
connection = self._get_connection()
|
||||
client = self._get_client()
|
||||
|
||||
def make_db(base, name):
|
||||
return base[name]
|
||||
|
||||
self.assertRaises(InvalidName, make_db, connection, "")
|
||||
self.assertRaises(InvalidName, make_db, connection, "te$t")
|
||||
self.assertRaises(InvalidName, make_db, connection, "te.t")
|
||||
self.assertRaises(InvalidName, make_db, connection, "te\\t")
|
||||
self.assertRaises(InvalidName, make_db, connection, "te/t")
|
||||
self.assertRaises(InvalidName, make_db, connection, "te st")
|
||||
self.assertRaises(InvalidName, make_db, client, "")
|
||||
self.assertRaises(InvalidName, make_db, client, "te$t")
|
||||
self.assertRaises(InvalidName, make_db, client, "te.t")
|
||||
self.assertRaises(InvalidName, make_db, client, "te\\t")
|
||||
self.assertRaises(InvalidName, make_db, client, "te/t")
|
||||
self.assertRaises(InvalidName, make_db, client, "te st")
|
||||
|
||||
self.assertTrue(isinstance(connection.test, Database))
|
||||
self.assertEqual(connection.test, connection["test"])
|
||||
self.assertEqual(connection.test, Database(connection, "test"))
|
||||
connection.close()
|
||||
self.assertTrue(isinstance(client.test, Database))
|
||||
self.assertEqual(client.test, client["test"])
|
||||
self.assertEqual(client.test, Database(client, "test"))
|
||||
client.close()
|
||||
|
||||
def test_auto_reconnect_exception_when_read_preference_is_secondary(self):
|
||||
c = self._get_connection()
|
||||
c = self._get_client()
|
||||
db = c.pymongo_test
|
||||
|
||||
def raise_socket_error(*args, **kwargs):
|
||||
@ -232,7 +232,7 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
socket.socket.sendall = old_sendall
|
||||
|
||||
def test_operations(self):
|
||||
c = self._get_connection()
|
||||
c = self._get_client()
|
||||
|
||||
# Check explicitly for a case we've commonly hit in tests:
|
||||
# a replica set is started with a tiny oplog, a previous
|
||||
@ -247,9 +247,9 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
)
|
||||
|
||||
db = c.pymongo_test
|
||||
db.test.remove({}, safe=True)
|
||||
db.test.remove({})
|
||||
self.assertEqual(0, db.test.count())
|
||||
db.test.insert({'foo': 'x'}, safe=True, w=self.w, wtimeout=10000)
|
||||
db.test.insert({'foo': 'x'}, w=self.w, wtimeout=10000)
|
||||
self.assertEqual(1, db.test.count())
|
||||
|
||||
cursor = db.test.find()
|
||||
@ -265,46 +265,49 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
self.assertTrue(cursor._Cursor__connection_id in c.secondaries)
|
||||
|
||||
self.assertEqual(1, db.test.count())
|
||||
db.test.remove({}, safe=True)
|
||||
db.test.remove({})
|
||||
self.assertEqual(0, db.test.count())
|
||||
db.test.drop()
|
||||
c.close()
|
||||
|
||||
def test_database_names(self):
|
||||
connection = self._get_connection()
|
||||
client = self._get_client()
|
||||
|
||||
connection.pymongo_test.test.save({"dummy": u"object"})
|
||||
connection.pymongo_test_mike.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test_mike.test.save({"dummy": u"object"})
|
||||
|
||||
dbs = connection.database_names()
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
self.assertTrue("pymongo_test_mike" in dbs)
|
||||
connection.close()
|
||||
client.close()
|
||||
|
||||
def test_drop_database(self):
|
||||
connection = self._get_connection()
|
||||
client = self._get_client()
|
||||
|
||||
self.assertRaises(TypeError, connection.drop_database, 5)
|
||||
self.assertRaises(TypeError, connection.drop_database, None)
|
||||
self.assertRaises(TypeError, client.drop_database, 5)
|
||||
self.assertRaises(TypeError, client.drop_database, None)
|
||||
|
||||
connection.pymongo_test.test.save({"dummy": u"object"})
|
||||
dbs = connection.database_names()
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
connection.drop_database("pymongo_test")
|
||||
dbs = connection.database_names()
|
||||
client.drop_database("pymongo_test")
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" not in dbs)
|
||||
|
||||
connection.pymongo_test.test.save({"dummy": u"object"})
|
||||
dbs = connection.database_names()
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
connection.drop_database(connection.pymongo_test)
|
||||
dbs = connection.database_names()
|
||||
client.drop_database(client.pymongo_test)
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" not in dbs)
|
||||
connection.close()
|
||||
client.close()
|
||||
|
||||
def test_copy_db(self):
|
||||
c = self._get_connection()
|
||||
self.assertTrue(c.in_request())
|
||||
c = self._get_client()
|
||||
# We test copy twice; once starting in a request and once not. In
|
||||
# either case the copy should succeed (because it starts a request
|
||||
# internally) and should leave us in the same state as before the copy.
|
||||
c.start_request()
|
||||
|
||||
self.assertRaises(TypeError, c.copy_database, 4, "foo")
|
||||
self.assertRaises(TypeError, c.copy_database, "foo", 4)
|
||||
@ -362,16 +365,16 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
c.close()
|
||||
|
||||
def test_iteration(self):
|
||||
connection = self._get_connection()
|
||||
client = self._get_client()
|
||||
|
||||
def iterate():
|
||||
[a for a in connection]
|
||||
[a for a in client]
|
||||
|
||||
self.assertRaises(TypeError, iterate)
|
||||
connection.close()
|
||||
client.close()
|
||||
|
||||
def test_disconnect(self):
|
||||
c = self._get_connection()
|
||||
c = self._get_client()
|
||||
coll = c.pymongo_test.bar
|
||||
|
||||
c.disconnect()
|
||||
@ -385,7 +388,7 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
coll.count()
|
||||
|
||||
def test_fork(self):
|
||||
"""Test using a connection before and after a fork.
|
||||
"""Test using a client before and after a fork.
|
||||
"""
|
||||
if sys.platform == "win32":
|
||||
raise SkipTest("Can't fork on Windows")
|
||||
@ -395,16 +398,16 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
except ImportError:
|
||||
raise SkipTest("No multiprocessing module")
|
||||
|
||||
db = self._get_connection().pymongo_test
|
||||
db = self._get_client().pymongo_test
|
||||
|
||||
# Failure occurs if the connection is used before the fork
|
||||
# Failure occurs if the client is used before the fork
|
||||
db.test.find_one()
|
||||
#db.connection.end_request()
|
||||
|
||||
def loop(pipe):
|
||||
while True:
|
||||
try:
|
||||
db.test.insert({"a": "b"}, safe=True)
|
||||
db.test.insert({"a": "b"})
|
||||
for _ in db.test.find():
|
||||
pass
|
||||
except:
|
||||
@ -448,7 +451,7 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
db.connection.close()
|
||||
|
||||
def test_document_class(self):
|
||||
c = self._get_connection()
|
||||
c = self._get_client()
|
||||
db = c.pymongo_test
|
||||
db.test.insert({"x": 1})
|
||||
|
||||
@ -463,7 +466,7 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
self.assertFalse(isinstance(db.test.find_one(as_class=dict), SON))
|
||||
c.close()
|
||||
|
||||
c = self._get_connection(document_class=SON)
|
||||
c = self._get_client(document_class=SON)
|
||||
db = c.pymongo_test
|
||||
|
||||
self.assertEqual(SON, c.document_class)
|
||||
@ -478,31 +481,36 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
c.close()
|
||||
|
||||
def test_network_timeout_validation(self):
|
||||
c = self._get_connection(network_timeout=10)
|
||||
c = self._get_client(socketTimeoutMS=10 * 1000)
|
||||
self.assertEqual(10, c._MongoReplicaSetClient__net_timeout)
|
||||
|
||||
c = self._get_connection(network_timeout=None)
|
||||
c = self._get_client(socketTimeoutMS=None)
|
||||
self.assertEqual(None, c._MongoReplicaSetClient__net_timeout)
|
||||
|
||||
self.assertRaises(ConfigurationError,
|
||||
self._get_connection, network_timeout=0)
|
||||
self._get_client, socketTimeoutMS=0)
|
||||
|
||||
self.assertRaises(ConfigurationError,
|
||||
self._get_connection, network_timeout=-1)
|
||||
self._get_client, socketTimeoutMS=-1)
|
||||
|
||||
self.assertRaises(ConfigurationError,
|
||||
self._get_connection, network_timeout=1e10)
|
||||
self._get_client, socketTimeoutMS=1e10)
|
||||
|
||||
self.assertRaises(ConfigurationError,
|
||||
self._get_connection, network_timeout='foo')
|
||||
self._get_client, socketTimeoutMS='foo')
|
||||
|
||||
# network_timeout is gone from MongoReplicaSetClient, remains in
|
||||
# deprecated ReplicaSetConnection
|
||||
self.assertRaises(ConfigurationError,
|
||||
self._get_client, network_timeout=10)
|
||||
|
||||
def test_network_timeout(self):
|
||||
no_timeout = self._get_connection()
|
||||
no_timeout = self._get_client()
|
||||
timeout_sec = 1
|
||||
timeout = self._get_connection(socketTimeoutMS=timeout_sec*1000)
|
||||
timeout = self._get_client(socketTimeoutMS=timeout_sec*1000)
|
||||
|
||||
no_timeout.pymongo_test.drop_collection("test")
|
||||
no_timeout.pymongo_test.test.insert({"x": 1}, safe=True)
|
||||
no_timeout.pymongo_test.test.insert({"x": 1})
|
||||
|
||||
# A $where clause that takes a second longer than the timeout
|
||||
where_func = delay(1 + timeout_sec)
|
||||
@ -523,15 +531,15 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
timeout.close()
|
||||
|
||||
def test_tz_aware(self):
|
||||
self.assertRaises(ConfigurationError, ReplicaSetConnection,
|
||||
self.assertRaises(ConfigurationError, MongoReplicaSetClient,
|
||||
tz_aware='foo', replicaSet=self.name)
|
||||
|
||||
aware = self._get_connection(tz_aware=True)
|
||||
naive = self._get_connection()
|
||||
aware = self._get_client(tz_aware=True)
|
||||
naive = self._get_client()
|
||||
aware.pymongo_test.drop_collection("test")
|
||||
|
||||
now = datetime.datetime.utcnow()
|
||||
aware.pymongo_test.test.insert({"x": now}, safe=True)
|
||||
aware.pymongo_test.test.insert({"x": now})
|
||||
time.sleep(1)
|
||||
|
||||
self.assertEqual(None, naive.pymongo_test.test.find_one()["x"].tzinfo)
|
||||
@ -542,7 +550,7 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
|
||||
def test_ipv6(self):
|
||||
try:
|
||||
connection = ReplicaSetConnection("[::1]:%d" % (port,),
|
||||
client = MongoReplicaSetClient("[::1]:%d" % (port,),
|
||||
replicaSet=self.name)
|
||||
except:
|
||||
# Either mongod was started without --ipv6
|
||||
@ -550,26 +558,26 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
raise SkipTest("No IPv6")
|
||||
|
||||
# Try a few simple things
|
||||
connection = ReplicaSetConnection("mongodb://[::1]:%d" % (port,),
|
||||
client = MongoReplicaSetClient("mongodb://[::1]:%d" % (port,),
|
||||
replicaSet=self.name)
|
||||
connection = ReplicaSetConnection("mongodb://[::1]:%d/?safe=true;"
|
||||
client = MongoReplicaSetClient("mongodb://[::1]:%d/?safe=true;"
|
||||
"replicaSet=%s" % (port, self.name))
|
||||
connection = ReplicaSetConnection("[::1]:%d,localhost:"
|
||||
client = MongoReplicaSetClient("[::1]:%d,localhost:"
|
||||
"%d" % (port, port),
|
||||
replicaSet=self.name)
|
||||
connection = ReplicaSetConnection("localhost:%d,[::1]:"
|
||||
client = MongoReplicaSetClient("localhost:%d,[::1]:"
|
||||
"%d" % (port, port),
|
||||
replicaSet=self.name)
|
||||
connection.pymongo_test.test.save({"dummy": u"object"})
|
||||
connection.pymongo_test_bernie.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test_bernie.test.save({"dummy": u"object"})
|
||||
|
||||
dbs = connection.database_names()
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
self.assertTrue("pymongo_test_bernie" in dbs)
|
||||
connection.close()
|
||||
client.close()
|
||||
|
||||
def _test_kill_cursor_explicit(self, read_pref):
|
||||
c = self._get_connection(read_preference=read_pref)
|
||||
c = self._get_client(read_preference=read_pref)
|
||||
db = c.pymongo_test
|
||||
db.drop_collection("test")
|
||||
|
||||
@ -619,9 +627,9 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
if sys.platform.startswith('java'):
|
||||
raise SkipTest("Can't test interrupts in Jython")
|
||||
|
||||
# Test fix for PYTHON-294 -- make sure Connection closes its
|
||||
# socket if it gets an interrupt while waiting to recv() from it.
|
||||
c = self._get_connection()
|
||||
# Test fix for PYTHON-294 -- make sure client closes its socket if it
|
||||
# gets an interrupt while waiting to recv() from it.
|
||||
c = self._get_client()
|
||||
db = c.pymongo_test
|
||||
|
||||
# A $where clause which takes 1.5 sec to execute
|
||||
@ -629,7 +637,7 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
|
||||
# Need exactly 1 document so find() will execute its $where clause once
|
||||
db.drop_collection('foo')
|
||||
db.foo.insert({'_id': 1}, safe=True)
|
||||
db.foo.insert({'_id': 1})
|
||||
|
||||
old_signal_handler = None
|
||||
|
||||
@ -680,17 +688,17 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
signal.signal(signal.SIGALRM, old_signal_handler)
|
||||
|
||||
def test_operation_failure_without_request(self):
|
||||
# Ensure ReplicaSetConnection doesn't close socket after it gets an
|
||||
# Ensure MongoReplicaSetClient doesn't close socket after it gets an
|
||||
# error response to getLastError. PYTHON-395.
|
||||
c = self._get_connection(auto_start_request=False)
|
||||
c = self._get_client(auto_start_request=False)
|
||||
pool = c._MongoReplicaSetClient__members[self.primary].pool
|
||||
self.assertEqual(1, len(pool.sockets))
|
||||
old_sock_info = iter(pool.sockets).next()
|
||||
c.pymongo_test.test.drop()
|
||||
c.pymongo_test.test.insert({'_id': 'foo'}, safe=True)
|
||||
c.pymongo_test.test.insert({'_id': 'foo'})
|
||||
self.assertRaises(
|
||||
OperationFailure,
|
||||
c.pymongo_test.test.insert, {'_id': 'foo'}, safe=True)
|
||||
c.pymongo_test.test.insert, {'_id': 'foo'})
|
||||
|
||||
self.assertEqual(1, len(pool.sockets))
|
||||
new_sock_info = iter(pool.sockets).next()
|
||||
@ -699,21 +707,21 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
c.close()
|
||||
|
||||
def test_operation_failure_with_request(self):
|
||||
# Ensure ReplicaSetConnection doesn't close socket after it gets an
|
||||
# Ensure MongoReplicaSetClient doesn't close socket after it gets an
|
||||
# error response to getLastError. PYTHON-395.
|
||||
c = self._get_connection(auto_start_request=True)
|
||||
c = self._get_client(auto_start_request=True)
|
||||
c.pymongo_test.test.find_one()
|
||||
pool = c._MongoReplicaSetClient__members[self.primary].pool
|
||||
|
||||
# Connection has reserved a socket for this thread
|
||||
# Client reserved a socket for this thread
|
||||
self.assertTrue(isinstance(pool._get_request_state(), SocketInfo))
|
||||
|
||||
old_sock_info = pool._get_request_state()
|
||||
c.pymongo_test.test.drop()
|
||||
c.pymongo_test.test.insert({'_id': 'foo'}, safe=True)
|
||||
c.pymongo_test.test.insert({'_id': 'foo'})
|
||||
self.assertRaises(
|
||||
OperationFailure,
|
||||
c.pymongo_test.test.insert, {'_id': 'foo'}, safe=True)
|
||||
c.pymongo_test.test.insert, {'_id': 'foo'})
|
||||
|
||||
# OperationFailure doesn't affect the request socket
|
||||
self.assertEqual(old_sock_info, pool._get_request_state())
|
||||
@ -723,24 +731,25 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
for bad_horrible_value in (None, 5, 'hi!'):
|
||||
self.assertRaises(
|
||||
(TypeError, ConfigurationError),
|
||||
lambda: self._get_connection(auto_start_request=bad_horrible_value)
|
||||
lambda: self._get_client(auto_start_request=bad_horrible_value)
|
||||
)
|
||||
|
||||
conn = self._get_connection()
|
||||
self.assertTrue(conn.auto_start_request)
|
||||
client = self._get_client(auto_start_request=True)
|
||||
self.assertTrue(client.auto_start_request)
|
||||
pools = [mongo.pool for mongo in
|
||||
conn._MongoReplicaSetClient__members.values()]
|
||||
client._MongoReplicaSetClient__members.values()]
|
||||
|
||||
primary_pool = conn._MongoReplicaSetClient__members[conn.primary].pool
|
||||
self.assertTrue(conn.auto_start_request)
|
||||
self.assertTrue(conn.in_request())
|
||||
self.assertInRequestAndSameSock(client, pools)
|
||||
|
||||
primary_pool = \
|
||||
client._MongoReplicaSetClient__members[client.primary].pool
|
||||
|
||||
# Trigger the RSC to actually start a request on primary pool
|
||||
conn.pymongo_test.test.find_one()
|
||||
client.pymongo_test.test.find_one()
|
||||
self.assertTrue(primary_pool.in_request())
|
||||
|
||||
# Trigger the RSC to actually start a request on secondary pool
|
||||
cursor = conn.pymongo_test.test.find(
|
||||
cursor = client.pymongo_test.test.find(
|
||||
read_preference=ReadPreference.SECONDARY)
|
||||
try:
|
||||
cursor.next()
|
||||
@ -749,75 +758,77 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
pass
|
||||
|
||||
secondary = cursor._Cursor__connection_id
|
||||
secondary_pool = conn._MongoReplicaSetClient__members[secondary].pool
|
||||
secondary_pool = client._MongoReplicaSetClient__members[secondary].pool
|
||||
self.assertTrue(secondary_pool.in_request())
|
||||
|
||||
conn.end_request()
|
||||
self.assertFalse(conn.in_request())
|
||||
client.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(client, pools)
|
||||
for pool in pools:
|
||||
self.assertFalse(pool.in_request())
|
||||
conn.start_request()
|
||||
self.assertTrue(conn.in_request())
|
||||
conn.close()
|
||||
client.start_request()
|
||||
self.assertInRequestAndSameSock(client, pools)
|
||||
client.close()
|
||||
|
||||
conn = self._get_connection(auto_start_request=False)
|
||||
self.assertFalse(conn.in_request())
|
||||
conn.start_request()
|
||||
self.assertTrue(conn.in_request())
|
||||
conn.end_request()
|
||||
self.assertFalse(conn.in_request())
|
||||
conn.close()
|
||||
client = self._get_client()
|
||||
pools = [mongo.pool for mongo in
|
||||
client._MongoReplicaSetClient__members.values()]
|
||||
|
||||
self.assertNotInRequestAndDifferentSock(client, pools)
|
||||
client.start_request()
|
||||
self.assertInRequestAndSameSock(client, pools)
|
||||
client.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(client, pools)
|
||||
client.close()
|
||||
|
||||
def test_nested_request(self):
|
||||
# auto_start_request is True
|
||||
conn = self._get_connection()
|
||||
client = self._get_client(auto_start_request=True)
|
||||
try:
|
||||
pools = [member.pool for member in
|
||||
conn._MongoReplicaSetClient__members.values()]
|
||||
self.assertTrue(conn.in_request())
|
||||
client._MongoReplicaSetClient__members.values()]
|
||||
self.assertTrue(client.in_request())
|
||||
|
||||
# Start and end request - we're still in "outer" original request
|
||||
conn.start_request()
|
||||
self.assertInRequestAndSameSock(conn, pools)
|
||||
conn.end_request()
|
||||
self.assertInRequestAndSameSock(conn, pools)
|
||||
client.start_request()
|
||||
self.assertInRequestAndSameSock(client, pools)
|
||||
client.end_request()
|
||||
self.assertInRequestAndSameSock(client, pools)
|
||||
|
||||
# Double-nesting
|
||||
conn.start_request()
|
||||
conn.start_request()
|
||||
client.start_request()
|
||||
client.start_request()
|
||||
self.assertEqual(
|
||||
3, conn._MongoReplicaSetClient__request_counter.get())
|
||||
3, client._MongoReplicaSetClient__request_counter.get())
|
||||
|
||||
for pool in pools:
|
||||
# MRSC only called start_request() once per pool, although its
|
||||
# own counter is 2.
|
||||
self.assertEqual(1, pool._request_counter.get())
|
||||
|
||||
conn.end_request()
|
||||
conn.end_request()
|
||||
self.assertInRequestAndSameSock(conn, pools)
|
||||
client.end_request()
|
||||
client.end_request()
|
||||
self.assertInRequestAndSameSock(client, pools)
|
||||
|
||||
self.assertEqual(
|
||||
1, conn._MongoReplicaSetClient__request_counter.get())
|
||||
1, client._MongoReplicaSetClient__request_counter.get())
|
||||
|
||||
for pool in pools:
|
||||
self.assertEqual(1, pool._request_counter.get())
|
||||
|
||||
# Finally, end original request
|
||||
conn.end_request()
|
||||
client.end_request()
|
||||
for pool in pools:
|
||||
self.assertFalse(pool.in_request())
|
||||
|
||||
self.assertNotInRequestAndDifferentSock(conn, pools)
|
||||
self.assertNotInRequestAndDifferentSock(client, pools)
|
||||
finally:
|
||||
conn.close()
|
||||
client.close()
|
||||
|
||||
def test_request_threads(self):
|
||||
conn = self._get_connection(auto_start_request=False)
|
||||
client = self._get_client()
|
||||
try:
|
||||
pools = [member.pool for member in
|
||||
conn._MongoReplicaSetClient__members.values()]
|
||||
self.assertNotInRequestAndDifferentSock(conn, pools)
|
||||
client._MongoReplicaSetClient__members.values()]
|
||||
self.assertNotInRequestAndDifferentSock(client, pools)
|
||||
|
||||
started_request, ended_request = threading.Event(), threading.Event()
|
||||
checked_request = threading.Event()
|
||||
@ -826,15 +837,15 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
# Starting a request in one thread doesn't put the other thread in a
|
||||
# request
|
||||
def f():
|
||||
self.assertNotInRequestAndDifferentSock(conn, pools)
|
||||
conn.start_request()
|
||||
self.assertInRequestAndSameSock(conn, pools)
|
||||
self.assertNotInRequestAndDifferentSock(client, pools)
|
||||
client.start_request()
|
||||
self.assertInRequestAndSameSock(client, pools)
|
||||
started_request.set()
|
||||
checked_request.wait()
|
||||
checked_request.clear()
|
||||
self.assertInRequestAndSameSock(conn, pools)
|
||||
conn.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pools)
|
||||
self.assertInRequestAndSameSock(client, pools)
|
||||
client.end_request()
|
||||
self.assertNotInRequestAndDifferentSock(client, pools)
|
||||
ended_request.set()
|
||||
checked_request.wait()
|
||||
thread_done[0] = True
|
||||
@ -843,86 +854,85 @@ class TestConnection(TestConnectionReplicaSetBase, TestRequestMixin):
|
||||
t.setDaemon(True)
|
||||
t.start()
|
||||
started_request.wait()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pools)
|
||||
self.assertNotInRequestAndDifferentSock(client, pools)
|
||||
checked_request.set()
|
||||
ended_request.wait()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pools)
|
||||
self.assertNotInRequestAndDifferentSock(client, pools)
|
||||
checked_request.set()
|
||||
t.join()
|
||||
self.assertNotInRequestAndDifferentSock(conn, pools)
|
||||
self.assertNotInRequestAndDifferentSock(client, pools)
|
||||
self.assertTrue(thread_done[0], "Thread didn't complete")
|
||||
finally:
|
||||
conn.close()
|
||||
client.close()
|
||||
|
||||
def test_schedule_refresh(self):
|
||||
# Monitor thread starts waiting for _refresh_interval, 30 seconds
|
||||
conn = self._get_connection()
|
||||
client = self._get_client()
|
||||
|
||||
# Reconnect if necessary
|
||||
conn.pymongo_test.test.find_one()
|
||||
client.pymongo_test.test.find_one()
|
||||
|
||||
secondaries = conn.secondaries
|
||||
secondaries = client.secondaries
|
||||
for secondary in secondaries:
|
||||
conn._MongoReplicaSetClient__members[secondary].up = False
|
||||
client._MongoReplicaSetClient__members[secondary].up = False
|
||||
|
||||
conn._MongoReplicaSetClient__members[conn.primary].up = False
|
||||
client._MongoReplicaSetClient__members[client.primary].up = False
|
||||
|
||||
# Wake up monitor thread
|
||||
conn._MongoReplicaSetClient__schedule_refresh()
|
||||
client._MongoReplicaSetClient__schedule_refresh()
|
||||
|
||||
# Refresh interval is 30 seconds; scheduling a refresh tells the
|
||||
# monitor thread / greenlet to start a refresh now. We still need to
|
||||
# sleep a few seconds for it to complete.
|
||||
time.sleep(5)
|
||||
for secondary in secondaries:
|
||||
self.assertTrue(conn._MongoReplicaSetClient__members[secondary].up,
|
||||
"ReplicaSetConnection didn't detect secondary is up")
|
||||
self.assertTrue(client._MongoReplicaSetClient__members[secondary].up,
|
||||
"MongoReplicaSetClient didn't detect secondary is up")
|
||||
|
||||
self.assertTrue(conn._MongoReplicaSetClient__members[conn.primary].up,
|
||||
"ReplicaSetConnection didn't detect primary is up")
|
||||
self.assertTrue(client._MongoReplicaSetClient__members[client.primary].up,
|
||||
"MongoReplicaSetClient didn't detect primary is up")
|
||||
|
||||
conn.close()
|
||||
client.close()
|
||||
|
||||
def test_pinned_member(self):
|
||||
latency = 1000 * 1000
|
||||
conn = self._get_connection(
|
||||
auto_start_request=False, secondary_acceptable_latency_ms=latency)
|
||||
client = self._get_client(secondary_acceptable_latency_ms=latency)
|
||||
|
||||
host = read_from_which_host(conn, ReadPreference.SECONDARY)
|
||||
self.assertTrue(host in conn.secondaries)
|
||||
host = read_from_which_host(client, ReadPreference.SECONDARY)
|
||||
self.assertTrue(host in client.secondaries)
|
||||
|
||||
# No pinning since we're not in a request
|
||||
assertReadFromAll(
|
||||
self, conn, conn.secondaries,
|
||||
self, client, client.secondaries,
|
||||
ReadPreference.SECONDARY, None, latency)
|
||||
|
||||
assertReadFromAll(
|
||||
self, conn, list(conn.secondaries) + [conn.primary],
|
||||
self, client, list(client.secondaries) + [client.primary],
|
||||
ReadPreference.NEAREST, None, latency)
|
||||
|
||||
conn.start_request()
|
||||
host = read_from_which_host(conn, ReadPreference.SECONDARY)
|
||||
self.assertTrue(host in conn.secondaries)
|
||||
assertReadFrom(self, conn, host, ReadPreference.SECONDARY)
|
||||
client.start_request()
|
||||
host = read_from_which_host(client, ReadPreference.SECONDARY)
|
||||
self.assertTrue(host in client.secondaries)
|
||||
assertReadFrom(self, client, host, ReadPreference.SECONDARY)
|
||||
|
||||
# Changing any part of read preference (mode, tag_sets, latency)
|
||||
# unpins the current host and pins to a new one
|
||||
primary = conn.primary
|
||||
assertReadFrom(self, conn, primary, ReadPreference.PRIMARY_PREFERRED)
|
||||
primary = client.primary
|
||||
assertReadFrom(self, client, primary, ReadPreference.PRIMARY_PREFERRED)
|
||||
|
||||
host = read_from_which_host(conn, ReadPreference.NEAREST)
|
||||
assertReadFrom(self, conn, host, ReadPreference.NEAREST)
|
||||
host = read_from_which_host(client, ReadPreference.NEAREST)
|
||||
assertReadFrom(self, client, host, ReadPreference.NEAREST)
|
||||
|
||||
assertReadFrom(self, conn, primary, ReadPreference.PRIMARY_PREFERRED)
|
||||
assertReadFrom(self, client, primary, ReadPreference.PRIMARY_PREFERRED)
|
||||
|
||||
host = read_from_which_host(conn, ReadPreference.SECONDARY_PREFERRED)
|
||||
self.assertTrue(host in conn.secondaries)
|
||||
assertReadFrom(self, conn, host, ReadPreference.SECONDARY_PREFERRED)
|
||||
host = read_from_which_host(client, ReadPreference.SECONDARY_PREFERRED)
|
||||
self.assertTrue(host in client.secondaries)
|
||||
assertReadFrom(self, client, host, ReadPreference.SECONDARY_PREFERRED)
|
||||
|
||||
# Unpin
|
||||
conn.end_request()
|
||||
client.end_request()
|
||||
assertReadFromAll(
|
||||
self, conn, list(conn.secondaries) + [conn.primary],
|
||||
self, client, list(client.secondaries) + [client.primary],
|
||||
ReadPreference.NEAREST, None, latency)
|
||||
|
||||
|
||||
|
||||
@ -26,14 +26,14 @@ from pymongo.son_manipulator import (NamespaceInjector,
|
||||
ObjectIdInjector,
|
||||
ObjectIdShuffler,
|
||||
SONManipulator)
|
||||
from test.test_connection import get_connection
|
||||
from test.test_connection import get_client
|
||||
from test import qcheck
|
||||
|
||||
|
||||
class TestSONManipulator(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.db = Database(get_connection(), "pymongo_test")
|
||||
self.db = Database(get_client(), "pymongo_test")
|
||||
|
||||
def test_basic(self):
|
||||
manip = SONManipulator()
|
||||
|
||||
@ -20,7 +20,7 @@ sys.path[0:0] = [""]
|
||||
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
from pymongo import Connection, ReplicaSetConnection
|
||||
from pymongo import MongoClient, MongoReplicaSetClient
|
||||
from pymongo.errors import ConfigurationError, ConnectionFailure
|
||||
|
||||
have_ssl = True
|
||||
@ -38,15 +38,15 @@ class TestSSL(unittest.TestCase):
|
||||
"with SSL and socket timeouts.")
|
||||
|
||||
def test_config_ssl(self):
|
||||
self.assertRaises(ConfigurationError, Connection, ssl='foo')
|
||||
self.assertRaises(TypeError, Connection, ssl=0)
|
||||
self.assertRaises(TypeError, Connection, ssl=5.5)
|
||||
self.assertRaises(TypeError, Connection, ssl=[])
|
||||
self.assertRaises(ConfigurationError, MongoClient, ssl='foo')
|
||||
self.assertRaises(TypeError, MongoClient, ssl=0)
|
||||
self.assertRaises(TypeError, MongoClient, ssl=5.5)
|
||||
self.assertRaises(TypeError, MongoClient, ssl=[])
|
||||
|
||||
self.assertRaises(ConfigurationError, ReplicaSetConnection, ssl='foo')
|
||||
self.assertRaises(TypeError, ReplicaSetConnection, ssl=0)
|
||||
self.assertRaises(TypeError, ReplicaSetConnection, ssl=5.5)
|
||||
self.assertRaises(TypeError, ReplicaSetConnection, ssl=[])
|
||||
self.assertRaises(ConfigurationError, MongoReplicaSetClient, ssl='foo')
|
||||
self.assertRaises(TypeError, MongoReplicaSetClient, ssl=0)
|
||||
self.assertRaises(TypeError, MongoReplicaSetClient, ssl=5.5)
|
||||
self.assertRaises(TypeError, MongoReplicaSetClient, ssl=[])
|
||||
|
||||
def test_no_ssl(self):
|
||||
if have_ssl:
|
||||
@ -56,28 +56,28 @@ class TestSSL(unittest.TestCase):
|
||||
)
|
||||
|
||||
self.assertRaises(ConfigurationError,
|
||||
Connection, ssl=True)
|
||||
MongoClient, ssl=True)
|
||||
self.assertRaises(ConfigurationError,
|
||||
ReplicaSetConnection, ssl=True)
|
||||
MongoReplicaSetClient, ssl=True)
|
||||
|
||||
def test_simple_ops(self):
|
||||
if not have_ssl:
|
||||
raise SkipTest("The ssl module is not available.")
|
||||
|
||||
try:
|
||||
conn = Connection(connectTimeoutMS=100, ssl=True)
|
||||
client = MongoClient(connectTimeoutMS=100, ssl=True)
|
||||
# MongoDB not configured for SSL?
|
||||
except ConnectionFailure:
|
||||
raise SkipTest("No mongod available over SSL")
|
||||
response = conn.admin.command('ismaster')
|
||||
response = client.admin.command('ismaster')
|
||||
if 'setName' in response:
|
||||
conn = ReplicaSetConnection(replicaSet=response['setName'],
|
||||
client = MongoReplicaSetClient(replicaSet=response['setName'],
|
||||
w=len(response['hosts']),
|
||||
ssl=True)
|
||||
|
||||
db = conn.pymongo_ssl_test
|
||||
db = client.pymongo_ssl_test
|
||||
db.test.drop()
|
||||
self.assertTrue(db.test.insert({'ssl': True}, safe=True))
|
||||
self.assertTrue(db.test.insert({'ssl': True}))
|
||||
self.assertTrue(db.test.find_one()['ssl'])
|
||||
|
||||
|
||||
|
||||
@ -21,22 +21,22 @@ import traceback
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
from test.utils import server_started_with_auth, joinall, RendezvousThread
|
||||
from test.test_connection import get_connection
|
||||
from pymongo.connection import Connection
|
||||
from pymongo.replica_set_connection import ReplicaSetConnection
|
||||
from test.test_connection import get_client
|
||||
from pymongo.mongo_client import MongoClient
|
||||
from pymongo.replica_set_connection import MongoReplicaSetClient
|
||||
from pymongo.pool import SocketInfo, _closed
|
||||
from pymongo.errors import AutoReconnect, OperationFailure
|
||||
|
||||
|
||||
def get_pool(connection):
|
||||
if isinstance(connection, Connection):
|
||||
return connection._MongoClient__pool
|
||||
elif isinstance(connection, ReplicaSetConnection):
|
||||
writer = connection._MongoReplicaSetClient__writer
|
||||
pools = connection._MongoReplicaSetClient__members
|
||||
def get_pool(client):
|
||||
if isinstance(client, MongoClient):
|
||||
return client._MongoClient__pool
|
||||
elif isinstance(client, MongoReplicaSetClient):
|
||||
writer = client._MongoReplicaSetClient__writer
|
||||
pools = client._MongoReplicaSetClient__members
|
||||
return pools[writer].pool
|
||||
else:
|
||||
raise TypeError(str(connection))
|
||||
raise TypeError(str(client))
|
||||
|
||||
|
||||
class AutoAuthenticateThreads(threading.Thread):
|
||||
@ -51,7 +51,7 @@ class AutoAuthenticateThreads(threading.Thread):
|
||||
def run(self):
|
||||
try:
|
||||
for i in xrange(self.num):
|
||||
self.coll.insert({'num':i}, safe=True)
|
||||
self.coll.insert({'num':i})
|
||||
self.coll.find_one({'num':i})
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
@ -86,7 +86,7 @@ class Insert(threading.Thread):
|
||||
error = True
|
||||
|
||||
try:
|
||||
self.collection.insert({"test": "insert"}, safe=True)
|
||||
self.collection.insert({"test": "insert"})
|
||||
error = False
|
||||
except:
|
||||
if not self.expect_exception:
|
||||
@ -111,7 +111,7 @@ class Update(threading.Thread):
|
||||
|
||||
try:
|
||||
self.collection.update({"test": "unique"},
|
||||
{"$set": {"test": "update"}}, safe=True)
|
||||
{"$set": {"test": "update"}})
|
||||
error = False
|
||||
except:
|
||||
if not self.expect_exception:
|
||||
@ -178,26 +178,26 @@ class BaseTestThreads(object):
|
||||
test_threads_replica_set_connection.py, which imports this module.)
|
||||
"""
|
||||
def setUp(self):
|
||||
self.db = self._get_connection().pymongo_test
|
||||
self.db = self._get_client().pymongo_test
|
||||
|
||||
def tearDown(self):
|
||||
# Clear connection reference so that RSC's monitor thread
|
||||
# Clear client reference so that RSC's monitor thread
|
||||
# dies.
|
||||
self.db = None
|
||||
|
||||
def _get_connection(self):
|
||||
def _get_client(self):
|
||||
"""
|
||||
Intended for overriding in TestThreadsReplicaSet. This method
|
||||
returns a Connection here, and a ReplicaSetConnection in
|
||||
returns a MongoClient here, and a MongoReplicaSetClient in
|
||||
test_threads_replica_set_connection.py.
|
||||
"""
|
||||
# Regular test connection
|
||||
return get_connection()
|
||||
# Regular test client
|
||||
return get_client()
|
||||
|
||||
def test_threading(self):
|
||||
self.db.drop_collection("test")
|
||||
for i in xrange(1000):
|
||||
self.db.test.save({"x": i}, safe=True)
|
||||
self.db.test.save({"x": i})
|
||||
|
||||
threads = []
|
||||
for i in range(10):
|
||||
@ -249,7 +249,7 @@ class BaseTestThreads(object):
|
||||
# PYTHON-345, we need to make sure that threads' request sockets are
|
||||
# closed by disconnect().
|
||||
#
|
||||
# 1. Create a connection with auto_start_request=True
|
||||
# 1. Create a client with auto_start_request=True
|
||||
# 2. Start N threads and do a find() in each to get a request socket
|
||||
# 3. Pause all threads
|
||||
# 4. In the main thread close all sockets, including threads' request
|
||||
@ -260,9 +260,8 @@ class BaseTestThreads(object):
|
||||
#
|
||||
# If we've fixed PYTHON-345, then only one AutoReconnect is raised,
|
||||
# and all the threads get new request sockets.
|
||||
cx = self.db.connection
|
||||
self.assertTrue(cx.auto_start_request)
|
||||
collection = self.db.pymongo_test
|
||||
cx = get_client(auto_start_request=True)
|
||||
collection = cx.db.pymongo_test
|
||||
|
||||
# acquire a request socket for the main thread
|
||||
collection.find_one()
|
||||
@ -297,7 +296,7 @@ class BaseTestThreads(object):
|
||||
# ... and close it:
|
||||
request_sock.close()
|
||||
|
||||
# Doing an operation on the connection raises an AutoReconnect and
|
||||
# Doing an operation on the client raises an AutoReconnect and
|
||||
# resets the pool behind the scenes
|
||||
self.assertRaises(AutoReconnect, collection.find_one)
|
||||
|
||||
@ -318,48 +317,48 @@ class BaseTestThreadsAuth(object):
|
||||
nose imports this module, and once when nose imports
|
||||
test_threads_replica_set_connection.py, which imports this module.)
|
||||
"""
|
||||
def _get_connection(self):
|
||||
def _get_client(self):
|
||||
"""
|
||||
Intended for overriding in TestThreadsAuthReplicaSet. This method
|
||||
returns a Connection here, and a ReplicaSetConnection in
|
||||
returns a MongoClient here, and a MongoReplicaSetClient in
|
||||
test_threads_replica_set_connection.py.
|
||||
"""
|
||||
# Regular test connection
|
||||
return get_connection()
|
||||
# Regular test client
|
||||
return get_client()
|
||||
|
||||
def setUp(self):
|
||||
conn = self._get_connection()
|
||||
if not server_started_with_auth(conn):
|
||||
client = self._get_client()
|
||||
if not server_started_with_auth(client):
|
||||
raise SkipTest("Authentication is not enabled on server")
|
||||
self.conn = conn
|
||||
self.conn.admin.system.users.remove({})
|
||||
self.conn.admin.add_user('admin-user', 'password')
|
||||
self.conn.admin.authenticate("admin-user", "password")
|
||||
self.conn.auth_test.system.users.remove({})
|
||||
self.conn.auth_test.add_user("test-user", "password")
|
||||
self.client = client
|
||||
self.client.admin.system.users.remove({})
|
||||
self.client.admin.add_user('admin-user', 'password')
|
||||
self.client.admin.authenticate("admin-user", "password")
|
||||
self.client.auth_test.system.users.remove({})
|
||||
self.client.auth_test.add_user("test-user", "password")
|
||||
|
||||
def tearDown(self):
|
||||
# Remove auth users from databases
|
||||
self.conn.admin.authenticate("admin-user", "password")
|
||||
self.conn.admin.system.users.remove({})
|
||||
self.conn.auth_test.system.users.remove({})
|
||||
self.conn.drop_database('auth_test')
|
||||
# Clear connection reference so that RSC's monitor thread
|
||||
self.client.admin.authenticate("admin-user", "password")
|
||||
self.client.admin.system.users.remove({})
|
||||
self.client.auth_test.system.users.remove({})
|
||||
self.client.drop_database('auth_test')
|
||||
# Clear client reference so that RSC's monitor thread
|
||||
# dies.
|
||||
self.conn = None
|
||||
self.client = None
|
||||
|
||||
def test_auto_auth_login(self):
|
||||
conn = self._get_connection()
|
||||
self.assertRaises(OperationFailure, conn.auth_test.test.find_one)
|
||||
client = self._get_client()
|
||||
self.assertRaises(OperationFailure, client.auth_test.test.find_one)
|
||||
|
||||
# Admin auth
|
||||
conn = self._get_connection()
|
||||
conn.admin.authenticate("admin-user", "password")
|
||||
client = self._get_client()
|
||||
client.admin.authenticate("admin-user", "password")
|
||||
|
||||
nthreads = 10
|
||||
threads = []
|
||||
for _ in xrange(nthreads):
|
||||
t = AutoAuthenticateThreads(conn.auth_test.test, 100)
|
||||
t = AutoAuthenticateThreads(client.auth_test.test, 100)
|
||||
t.start()
|
||||
threads.append(t)
|
||||
|
||||
@ -369,12 +368,12 @@ class BaseTestThreadsAuth(object):
|
||||
self.assertTrue(t.success)
|
||||
|
||||
# Database-specific auth
|
||||
conn = self._get_connection()
|
||||
conn.auth_test.authenticate("test-user", "password")
|
||||
client = self._get_client()
|
||||
client.auth_test.authenticate("test-user", "password")
|
||||
|
||||
threads = []
|
||||
for _ in xrange(nthreads):
|
||||
t = AutoAuthenticateThreads(conn.auth_test.test, 100)
|
||||
t = AutoAuthenticateThreads(client.auth_test.test, 100)
|
||||
t.start()
|
||||
threads.append(t)
|
||||
|
||||
|
||||
@ -16,54 +16,50 @@
|
||||
|
||||
import unittest
|
||||
|
||||
from pymongo.replica_set_connection import ReplicaSetConnection
|
||||
from test.test_threads import BaseTestThreads, BaseTestThreadsAuth
|
||||
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
|
||||
|
||||
from test.test_replica_set_connection import (TestConnectionReplicaSetBase,
|
||||
from test.test_threads import BaseTestThreads, BaseTestThreadsAuth
|
||||
from test.test_replica_set_connection import (TestReplicaSetClientBase,
|
||||
pair)
|
||||
|
||||
|
||||
class TestThreadsReplicaSet(TestConnectionReplicaSetBase, BaseTestThreads):
|
||||
class TestThreadsReplicaSet(TestReplicaSetClientBase, BaseTestThreads):
|
||||
def setUp(self):
|
||||
"""
|
||||
Prepare to test all the same things that TestThreads tests, but do it
|
||||
with a replica-set connection
|
||||
with a replica-set client
|
||||
"""
|
||||
TestConnectionReplicaSetBase.setUp(self)
|
||||
TestReplicaSetClientBase.setUp(self)
|
||||
BaseTestThreads.setUp(self)
|
||||
|
||||
def tearDown(self):
|
||||
TestConnectionReplicaSetBase.tearDown(self)
|
||||
TestReplicaSetClientBase.tearDown(self)
|
||||
BaseTestThreads.tearDown(self)
|
||||
|
||||
def _get_connection(self):
|
||||
"""
|
||||
Override TestThreads, so its tests run on a ReplicaSetConnection
|
||||
instead of a regular Connection.
|
||||
"""
|
||||
return ReplicaSetConnection(pair, replicaSet=self.name)
|
||||
def _get_client(self, **kwargs):
|
||||
return TestReplicaSetClientBase._get_client(self, **kwargs)
|
||||
|
||||
|
||||
class TestThreadsAuthReplicaSet(TestConnectionReplicaSetBase, BaseTestThreadsAuth):
|
||||
class TestThreadsAuthReplicaSet(TestReplicaSetClientBase, BaseTestThreadsAuth):
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
Prepare to test all the same things that TestThreads tests, but do it
|
||||
with a replica-set connection
|
||||
with a replica-set client
|
||||
"""
|
||||
TestConnectionReplicaSetBase.setUp(self)
|
||||
TestReplicaSetClientBase.setUp(self)
|
||||
BaseTestThreadsAuth.setUp(self)
|
||||
|
||||
def tearDown(self):
|
||||
TestConnectionReplicaSetBase.tearDown(self)
|
||||
TestReplicaSetClientBase.tearDown(self)
|
||||
BaseTestThreadsAuth.tearDown(self)
|
||||
|
||||
def _get_connection(self):
|
||||
def _get_client(self):
|
||||
"""
|
||||
Override TestThreadsAuth, so its tests run on a ReplicaSetConnection
|
||||
instead of a regular Connection.
|
||||
Override TestThreadsAuth, so its tests run on a MongoReplicaSetClient
|
||||
instead of a regular MongoClient.
|
||||
"""
|
||||
return ReplicaSetConnection(pair, replicaSet=self.name)
|
||||
return MongoReplicaSetClient(pair, replicaSet=self.name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -32,17 +32,17 @@ def delay(sec):
|
||||
while (d > (new Date())) { }; return true;
|
||||
}''' % sec
|
||||
|
||||
def get_command_line(connection):
|
||||
command_line = connection.admin.command('getCmdLineOpts')
|
||||
def get_command_line(client):
|
||||
command_line = client.admin.command('getCmdLineOpts')
|
||||
assert command_line['ok'] == 1, "getCmdLineOpts() failed"
|
||||
return command_line['argv']
|
||||
|
||||
def server_started_with_auth(connection):
|
||||
argv = get_command_line(connection)
|
||||
def server_started_with_auth(client):
|
||||
argv = get_command_line(client)
|
||||
return '--auth' in argv or '--keyFile' in argv
|
||||
|
||||
def server_is_master_with_slave(connection):
|
||||
return '--master' in get_command_line(connection)
|
||||
def server_is_master_with_slave(client):
|
||||
return '--master' in get_command_line(client)
|
||||
|
||||
def drop_collections(db):
|
||||
for coll in db.collection_names():
|
||||
@ -55,15 +55,15 @@ def joinall(threads):
|
||||
t.join(300)
|
||||
assert not t.isAlive(), "Thread %s hung" % t
|
||||
|
||||
def is_mongos(conn):
|
||||
res = conn.admin.command('ismaster')
|
||||
def is_mongos(client):
|
||||
res = client.admin.command('ismaster')
|
||||
return res.get('msg', '') == 'isdbgrid'
|
||||
|
||||
def assertRaisesExactly(cls, fn, *args, **kwargs):
|
||||
"""
|
||||
Unlike the standard assertRaises, this checks that a function raises a
|
||||
specific class of exception, and not a subclass. E.g., check that
|
||||
Connection() raises ConnectionFailure but not its subclass, AutoReconnect.
|
||||
MongoClient() raises ConnectionFailure but not its subclass, AutoReconnect.
|
||||
"""
|
||||
try:
|
||||
fn(*args, **kwargs)
|
||||
@ -189,11 +189,11 @@ def read_from_which_host(
|
||||
tag_sets=None,
|
||||
secondary_acceptable_latency_ms=15
|
||||
):
|
||||
"""Read from a ReplicaSetConnection with the given Read Preference mode,
|
||||
"""Read from a MongoReplicaSetClient with the given Read Preference mode,
|
||||
tags, and acceptable latency. Return the 'host:port' which was read from.
|
||||
|
||||
:Parameters:
|
||||
- `rsc`: A ReplicaSetConnection
|
||||
- `rsc`: A MongoReplicaSetClient
|
||||
- `mode`: A ReadPreference
|
||||
- `tag_sets`: List of dicts of tags for data-center-aware reads
|
||||
- `secondary_acceptable_latency_ms`: a float
|
||||
@ -224,7 +224,7 @@ def assertReadFrom(testcase, rsc, member, *args, **kwargs):
|
||||
|
||||
:Parameters:
|
||||
- `testcase`: A unittest.TestCase
|
||||
- `rsc`: A ReplicaSetConnection
|
||||
- `rsc`: A MongoReplicaSetClient
|
||||
- `member`: A host:port expected to be used
|
||||
- `mode`: A ReadPreference
|
||||
- `tag_sets` (optional): List of dicts of tags for data-center-aware reads
|
||||
@ -240,7 +240,7 @@ def assertReadFromAll(testcase, rsc, members, *args, **kwargs):
|
||||
|
||||
:Parameters:
|
||||
- `testcase`: A unittest.TestCase
|
||||
- `rsc`: A ReplicaSetConnection
|
||||
- `rsc`: A MongoReplicaSetClient
|
||||
- `members`: Sequence of host:port expected to be used
|
||||
- `mode`: A ReadPreference
|
||||
- `tag_sets` (optional): List of dicts of tags for data-center-aware reads
|
||||
@ -255,15 +255,15 @@ def assertReadFromAll(testcase, rsc, members, *args, **kwargs):
|
||||
|
||||
class TestRequestMixin(object):
|
||||
"""Inherit from this class and from unittest.TestCase to get some
|
||||
convenient methods for testing connection-pools and requests
|
||||
convenient methods for testing connection pools and requests
|
||||
"""
|
||||
def get_sock(self, pool):
|
||||
# Connection calls Pool.get_socket((host, port)), whereas RSC sets
|
||||
# MongoClient calls Pool.get_socket((host, port)), whereas RSC sets
|
||||
# Pool.pair at construction-time and just calls Pool.get_socket().
|
||||
# Deal with either case so we can use TestRequestMixin to test pools
|
||||
# from Connection and from RSC.
|
||||
# from MongoClient and from RSC.
|
||||
if not pool.pair:
|
||||
# self is test_connection.TestConnection
|
||||
# self is test_connection.TestClient
|
||||
self.assertTrue(hasattr(self, 'host') and hasattr(self, 'port'))
|
||||
sock_info = pool.get_socket((self.host, self.port))
|
||||
else:
|
||||
@ -293,16 +293,18 @@ class TestRequestMixin(object):
|
||||
def assertRequestSocket(self, pool):
|
||||
self.assertTrue(isinstance(pool._get_request_state(), SocketInfo))
|
||||
|
||||
def assertInRequestAndSameSock(self, conn, pools):
|
||||
self.assertTrue(conn.in_request())
|
||||
def assertInRequestAndSameSock(self, client, pools):
|
||||
self.assertTrue(client.in_request())
|
||||
if not isinstance(pools, list):
|
||||
pools = [pools]
|
||||
for pool in pools:
|
||||
self.assertTrue(pool.in_request())
|
||||
self.assertSameSock(pool)
|
||||
|
||||
def assertNotInRequestAndDifferentSock(self, conn, pools):
|
||||
self.assertFalse(conn.in_request())
|
||||
def assertNotInRequestAndDifferentSock(self, client, pools):
|
||||
self.assertFalse(client.in_request())
|
||||
if not isinstance(pools, list):
|
||||
pools = [pools]
|
||||
for pool in pools:
|
||||
self.assertFalse(pool.in_request())
|
||||
self.assertDifferentSock(pool)
|
||||
|
||||
@ -47,9 +47,9 @@ def _parse_version_string(version_string):
|
||||
|
||||
|
||||
# Note this is probably broken for very old versions of the database...
|
||||
def version(connection):
|
||||
return _parse_version_string(connection.server_info()["version"])
|
||||
def version(client):
|
||||
return _parse_version_string(client.server_info()["version"])
|
||||
|
||||
|
||||
def at_least(connection, min_version):
|
||||
return version(connection) >= tuple(_padded(min_version, 4))
|
||||
def at_least(client, min_version):
|
||||
return version(client) >= tuple(_padded(min_version, 4))
|
||||
|
||||
@ -21,7 +21,7 @@ sys.path[0:0] = [""]
|
||||
import datetime
|
||||
import cProfile
|
||||
|
||||
from pymongo import connection
|
||||
from pymongo import mongo_client
|
||||
from pymongo import ASCENDING
|
||||
|
||||
trials = 2
|
||||
@ -93,8 +93,7 @@ def timed(name, function, args=[], setup=None):
|
||||
|
||||
|
||||
def main():
|
||||
connection._TIMEOUT = 60 # jack up the timeout
|
||||
c = connection.Connection()
|
||||
c = mongo_client.MongoClient(connectTimeoutMS=60*1000) # jack up timeout
|
||||
c.drop_database("benchmark")
|
||||
db = c.benchmark
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user