PEP8 cleanups.

This commit is contained in:
behackett 2011-04-06 14:57:17 -07:00
parent 7af6746d77
commit 2ed05bec70
26 changed files with 347 additions and 191 deletions

View File

@ -56,6 +56,7 @@ MIN_INT32 = -2147483648
MAX_INT64 = 9223372036854775807
MIN_INT64 = -9223372036854775808
def _get_int(data, as_class=None, tz_aware=False, unsigned=False):
format = unsigned and "I" or "i"
try:

View File

@ -20,6 +20,7 @@ dictionary."""
import copy
class SON(dict):
"""SON data.

View File

@ -20,9 +20,11 @@ from sphinx import addnodes
from sphinx.util.compat import (Directive,
make_admonition)
class mongodoc(nodes.Admonition, nodes.Element):
pass
class mongoref(nodes.reference):
pass
@ -62,8 +64,8 @@ class MongodocDirective(Directive):
return make_admonition(mongodoc, self.name,
['See general MongoDB documentation'],
self.options, self.content, self.lineno,
self.content_offset, self.block_text, self.state,
self.state_machine)
self.content_offset, self.block_text,
self.state, self.state_machine)
def process_mongodoc_nodes(app, doctree, fromdocname):

View File

@ -933,8 +933,8 @@ class Collection(object):
- `reduce`: reduce function (as a JavaScript string)
- `out` (required): output collection name
- `merge_output` (optional): Merge output into `out`. If the same
key exists in both the result set and the existing output collection,
the new key will overwrite the existing key
key exists in both the result set and the existing output
collection, the new key will overwrite the existing key
- `reduce_output` (optional): If documents exist for a given key
in the result set and in the existing output collection, then a
reduce operation (using the specified reduce function) will be
@ -962,7 +962,8 @@ class Collection(object):
raise TypeError("'out' must be an instance of basestring")
if merge_output and reduce_output:
raise InvalidOperation("Can't do both merge and re-reduce of output.")
raise InvalidOperation("Can't do both merge"
" and re-reduce of output.")
if merge_output:
out_conf = {"merge": out}
@ -1056,9 +1057,12 @@ class Collection(object):
raise ValueError("Can't do both update and remove")
# No need to include empty args
if query: kwargs['query'] = query
if update: kwargs['update'] = update
if upsert: kwargs['upsert'] = upsert
if query:
kwargs['query'] = query
if update:
kwargs['update'] = update
if upsert:
kwargs['upsert'] = upsert
no_obj_error = "No matching object found"
@ -1070,7 +1074,7 @@ class Collection(object):
return None
else:
# Should never get here b/c of allowable_errors
raise ValueError("Unexpected Error: %s"%out)
raise ValueError("Unexpected Error: %s" % (out,))
return out.get('value')

View File

@ -134,7 +134,6 @@ def _parse_uri(uri, default_port=27017):
elif raw_options.find("="):
options = dict([raw_options.split("=")])
return (host_list, db, username, password, collection, options)
@ -170,7 +169,6 @@ class _Pool(threading.local):
if not hasattr(self, "sockets"):
self.sockets = []
def socket(self):
# We use the pid here to avoid issues with fork / multiprocessing.
# See test.test_connection:TestConnection.test_fork for an example of
@ -335,7 +333,7 @@ class Connection(object): # TODO support auth for pooling
# TODO - Support using other options like w and fsync from URI
self.__options = options
# TODO - Support setting the collection from URI as the Java driver does
# TODO - Support setting the collection from URI like the Java driver
self.__collection = collection
self.__cursor_manager = CursorManager(self)

View File

@ -23,6 +23,7 @@ from pymongo.connection import Connection
from pymongo.database import Database
from pymongo.errors import AutoReconnect
class MasterSlaveConnection(object):
"""A master-slave connection to Mongo.
"""

View File

@ -79,12 +79,12 @@ Please ask in the user forums for help.
if sys.platform == 'win32' and sys.version_info > (2, 6):
# 2.6's distutils.msvc9compiler can raise an IOError when failing to
# find the compiler
build_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError,
IOError)
# 2.6's distutils.msvc9compiler can raise an IOError when failing to
# find the compiler
build_errors = (CCompilerError, DistutilsExecError,
DistutilsPlatformError, IOError)
else:
build_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError)
build_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError)
class custom_build_ext(build_ext):
@ -110,7 +110,8 @@ although they do result in significant speed improvements.
print e
print self.warning_message % ("Extension modules",
"There was an issue with your "
"platform configuration - see above.")
"platform configuration "
"- see above.")
def build_extension(self, ext):
if sys.version_info[:3] >= (2, 4, 0):
@ -118,7 +119,8 @@ although they do result in significant speed improvements.
build_ext.build_extension(self, ext)
except build_errors, e:
print e
print self.warning_message % ("The %s extension module" % ext.name,
print self.warning_message % ("The %s extension "
"module" % ext.name,
"Above is the ouput showing how "
"the compilation failed.")
else:

View File

@ -17,6 +17,7 @@
from test_connection import get_connection
def teardown():
c = get_connection()

View File

@ -168,9 +168,9 @@ def gen_mongo_dict(depth, ref=True):
gen_range(0, 10)), SON)
def simplify(case): # TODO this is a hack
def simplify(case): # TODO this is a hack
if isinstance(case, SON) and "$ref" not in case:
simplified = SON(case) # make a copy!
simplified = SON(case) # make a copy!
if random.choice([True, False]):
# delete
if not len(simplified.keys()):

View File

@ -48,6 +48,7 @@ from bson.tz_util import (FixedOffset,
import pymongo
import qcheck
class TestBSON(unittest.TestCase):
def setUp(self):
@ -175,7 +176,7 @@ class TestBSON(unittest.TestCase):
helper({"ref": DBRef("coll", 5, foo="bar", bar=4)})
helper({"ref": DBRef("coll", 5, "foo")})
helper({"ref": DBRef("coll", 5, "foo", foo="bar")})
helper({"ref": Timestamp(1,2)})
helper({"ref": Timestamp(1, 2)})
helper({"foo": MinKey()})
helper({"foo": MaxKey()})
@ -186,15 +187,18 @@ class TestBSON(unittest.TestCase):
qcheck.gen_mongo_dict(3))
def test_aware_datetime(self):
aware = datetime.datetime(1993, 4, 4, 2, tzinfo=FixedOffset(555, "SomeZone"))
aware = datetime.datetime(1993, 4, 4, 2,
tzinfo=FixedOffset(555, "SomeZone"))
as_utc = (aware - aware.utcoffset()).replace(tzinfo=utc)
self.assertEqual(datetime.datetime(1993, 4, 3, 16, 45, tzinfo=utc), as_utc)
self.assertEqual(datetime.datetime(1993, 4, 3, 16, 45, tzinfo=utc),
as_utc)
after = BSON.encode({"date": aware}).decode(tz_aware=True)["date"]
self.assertEqual(utc, after.tzinfo)
self.assertEqual(as_utc, after)
def test_naive_decode(self):
aware = datetime.datetime(1993, 4, 4, 2, tzinfo=FixedOffset(555, "SomeZone"))
aware = datetime.datetime(1993, 4, 4, 2,
tzinfo=FixedOffset(555, "SomeZone"))
naive_utc = (aware - aware.utcoffset()).replace(tzinfo=None)
self.assertEqual(datetime.datetime(1993, 4, 3, 16, 45), naive_utc)
after = BSON.encode({"date": aware}).decode()["date"]
@ -208,7 +212,7 @@ class TestBSON(unittest.TestCase):
def test_bad_encode(self):
self.assertRaises(InvalidStringData, BSON.encode,
{"lalala": '\xf4\xe0\xf0\xe1\xc0 Color Touch'})
evil_list = {'a' : []}
evil_list = {'a': []}
evil_list['a'].append(evil_list)
evil_dict = {}
evil_dict['a'] = evil_dict
@ -217,10 +221,12 @@ class TestBSON(unittest.TestCase):
def test_overflow(self):
self.assert_(BSON.encode({"x": 9223372036854775807L}))
self.assertRaises(OverflowError, BSON.encode, {"x": 9223372036854775808L})
self.assertRaises(OverflowError, BSON.encode,
{"x": 9223372036854775808L})
self.assert_(BSON.encode({"x": -9223372036854775808L}))
self.assertRaises(OverflowError, BSON.encode, {"x": -9223372036854775809L})
self.assertRaises(OverflowError, BSON.encode,
{"x": -9223372036854775809L})
def test_small_long_encode_decode(self):
encoded1 = BSON.encode({'x': 256})
@ -282,8 +288,10 @@ class TestBSON(unittest.TestCase):
self.assertRaises(InvalidDocument, BSON.encode, {"\x00": "a"})
self.assertRaises(InvalidDocument, BSON.encode, {u"\x00": "a"})
self.assertRaises(InvalidDocument, BSON.encode, {"a": re.compile("ab\x00c")})
self.assertRaises(InvalidDocument, BSON.encode, {"a": re.compile(u"ab\x00c")})
self.assertRaises(InvalidDocument, BSON.encode,
{"a": re.compile("ab\x00c")})
self.assertRaises(InvalidDocument, BSON.encode,
{"a": re.compile(u"ab\x00c")})
def test_move_id(self):
self.assertEqual("\x19\x00\x00\x00\x02_id\x00\x02\x00\x00\x00a\x00"
@ -295,7 +303,8 @@ class TestBSON(unittest.TestCase):
"\x03b\x00"
"\x19\x00\x00\x00\x02a\x00\x02\x00\x00\x00a\x00"
"\x02_id\x00\x02\x00\x00\x00a\x00\x00\x00",
BSON.encode(SON([("b", SON([("a", "a"), ("_id", "a")])),
BSON.encode(SON([("b",
SON([("a", "a"), ("_id", "a")])),
("_id", "b")])))
def test_dates(self):
@ -324,12 +333,15 @@ class TestBSON(unittest.TestCase):
# make sure we can serialize subclasses of native Python types.
class _myint(int):
pass
class _myfloat(float):
pass
class _myunicode(unicode):
pass
d = {'a' : _myint(42), 'b' : _myfloat(63.9),
'c' : _myunicode('hello world')
d = {'a': _myint(42), 'b': _myfloat(63.9),
'c': _myunicode('hello world')
}
d2 = BSON.encode(d).decode()
for key, value in d2.iteritems():

View File

@ -115,8 +115,8 @@ class TestCollection(unittest.TestCase):
.find({"ns": u"pymongo_test.test"})])
db.test.drop()
db.test.insert({'a':1})
db.test.insert({'a':1})
db.test.insert({'a': 1})
db.test.insert({'a': 1})
self.assertRaises(DuplicateKeyError, db.test.create_index,
'a', unique=True)
@ -222,7 +222,7 @@ class TestCollection(unittest.TestCase):
db = self.db
db.test.drop_indexes()
db.test.remove({})
db.test.save({}) # create collection
db.test.save({}) # create collection
self.assertEqual(len(db.test.index_information()), 1)
self.assert_("_id_" in db.test.index_information())
@ -237,8 +237,10 @@ class TestCollection(unittest.TestCase):
[("hello", ASCENDING)])
self.assertEqual(len(db.test.index_information()), 3)
self.assertEqual([("hello", DESCENDING), ("world", ASCENDING)],
db.test.index_information()["hello_-1_world_1"]["key"])
self.assertEqual(True, db.test.index_information()["hello_-1_world_1"]["unique"])
db.test.index_information()["hello_-1_world_1"]["key"]
)
self.assertEqual(True,
db.test.index_information()["hello_-1_world_1"]["unique"])
def test_field_selection(self):
db = self.db
@ -326,7 +328,8 @@ class TestCollection(unittest.TestCase):
self.assert_("extra thing" in db.test.find({}).next())
self.assert_("x" in db.test.find({}, ["x", "mike"]).next())
self.assert_("mike" in db.test.find({}, ["x", "mike"]).next())
self.assertFalse("extra thing" in db.test.find({}, ["x", "mike"]).next())
self.assertFalse("extra thing" in db.test.find({},
["x", "mike"]).next())
self.assertFalse("x" in db.test.find({}, ["mike"]).next())
self.assert_("mike" in db.test.find({}, ["mike"]).next())
self.assertFalse("extra thing" in db.test.find({}, ["mike"]).next())
@ -337,9 +340,9 @@ class TestCollection(unittest.TestCase):
db.test.insert({"x": [1, 2, 3], "mike": "awesome"})
self.assertEqual([1,2,3], db.test.find_one()["x"])
self.assertEqual([1, 2, 3], db.test.find_one()["x"])
if version.at_least(db.connection, (1, 5, 1)):
self.assertEqual([2,3],
self.assertEqual([2, 3],
db.test.find_one(fields={"x": {"$slice":
-2}})["x"])
self.assert_("x" not in db.test.find_one(fields={"x": 0}))
@ -456,7 +459,8 @@ class TestCollection(unittest.TestCase):
db.drop_collection("test")
self.assertEqual(db.test.find().count(), 0)
ids = db.test.insert(itertools.imap(lambda x: {"hello": "world"}, itertools.repeat(None, 10)))
ids = db.test.insert(itertools.imap(lambda x: {"hello": "world"},
itertools.repeat(None, 10)))
self.assertEqual(db.test.find().count(), 10)
def test_save(self):
@ -505,7 +509,8 @@ class TestCollection(unittest.TestCase):
self.assertRaises(expected_error,
db.test.save, {"x": 2}, safe=True)
self.assertRaises(expected_error,
db.test.update, {"x": 1}, {"$inc": {"x": 1}}, safe=True)
db.test.update, {"x": 1},
{"$inc": {"x": 1}}, safe=True)
def test_error_code(self):
try:
@ -515,7 +520,6 @@ class TestCollection(unittest.TestCase):
if version.at_least(self.db.connection, (1, 3)):
self.assertEqual(10147, e.code)
def test_index_on_subfield(self):
db = self.db
db.drop_collection("test")
@ -606,9 +610,13 @@ class TestCollection(unittest.TestCase):
self.assertRaises(OperationFailure, db.test.update,
{"_id": id}, {"$inc": {"x": 1}}, safe=True)
self.assertEqual(1, db.test.update({"_id": id}, {"$inc": {"x": 2}}, safe=True)["n"])
self.assertEqual(1, db.test.update({"_id": id},
{"$inc": {"x": 2}},
safe=True)["n"])
self.assertEqual(0, db.test.update({"_id": "foo"}, {"$inc": {"x": 2}}, safe=True)["n"])
self.assertEqual(0, db.test.update({"_id": "foo"},
{"$inc": {"x": 2}},
safe=True)["n"])
def test_safe_save(self):
db = self.db
@ -619,7 +627,8 @@ class TestCollection(unittest.TestCase):
db.test.save({"hello": "world"})
self.assert_("E11000" in db.error()["err"])
self.assertRaises(OperationFailure, db.test.save, {"hello": "world"}, safe=True)
self.assertRaises(OperationFailure, db.test.save,
{"hello": "world"}, safe=True)
def test_safe_remove(self):
db = self.db
@ -633,8 +642,9 @@ class TestCollection(unittest.TestCase):
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)
else: # Just test that it doesn't blow up
self.assertRaises(OperationFailure, db.test.remove,
{"x": 1}, safe=True)
else: # Just test that it doesn't blow up
db.test.remove({"x": 1}, safe=True)
db.drop_collection("test")
@ -651,10 +661,14 @@ class TestCollection(unittest.TestCase):
# 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)):
self.assertRaises(TimeoutError, self.db.test.save, {"x": 1}, w=2, wtimeout=1)
self.assertRaises(TimeoutError, self.db.test.insert, {"x": 1}, w=2, wtimeout=1)
self.assertRaises(TimeoutError, self.db.test.update, {"x": 1}, {"y": 2}, w=2, wtimeout=1)
self.assertRaises(TimeoutError, self.db.test.remove, {"x": 1}, {"y": 2}, w=2, wtimeout=1)
self.assertRaises(TimeoutError, self.db.test.save,
{"x": 1}, w=2, wtimeout=1)
self.assertRaises(TimeoutError, self.db.test.insert,
{"x": 1}, w=2, wtimeout=1)
self.assertRaises(TimeoutError, self.db.test.update,
{"x": 1}, {"y": 2}, w=2, wtimeout=1)
self.assertRaises(TimeoutError, self.db.test.remove,
{"x": 1}, {"y": 2}, w=2, wtimeout=1)
self.db.test.save({"x": 1}, w=1, wtimeout=1)
self.db.test.insert({"x": 1}, w=1, wtimeout=1)
@ -667,7 +681,8 @@ class TestCollection(unittest.TestCase):
# 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)):
self.assertRaises(TimeoutError, self.db.command, "getlasterror", w=2, wtimeout=1)
self.assertRaises(TimeoutError, self.db.command,
"getlasterror", w=2, wtimeout=1)
self.db.command("getlasterror", w=1, wtimeout=1)
def test_count(self):
@ -687,9 +702,10 @@ class TestCollection(unittest.TestCase):
eval = db.test.group(*args)
self.assertEqual(eval, expected)
self.assertEqual([], db.test.group([], {}, {"count": 0},
"function (obj, prev) { prev.count++; }"))
self.assertEqual([],
db.test.group([], {}, {"count": 0},
"function (obj, prev) { prev.count++; }"
))
db.test.save({"a": 2})
db.test.save({"b": 5})
@ -697,11 +713,13 @@ class TestCollection(unittest.TestCase):
self.assertEqual([{"count": 3}],
db.test.group([], {}, {"count": 0},
"function (obj, prev) { prev.count++; }"))
"function (obj, prev) { prev.count++; }"
))
self.assertEqual([{"count": 1}],
db.test.group([], {"a": {"$gt": 1}}, {"count": 0},
"function (obj, prev) { prev.count++; }"))
"function (obj, prev) { prev.count++; }"
))
db.test.save({"a": 2, "b": 3})
@ -709,33 +727,39 @@ class TestCollection(unittest.TestCase):
{"a": None, "count": 1},
{"a": 1, "count": 1}],
db.test.group(["a"], {}, {"count": 0},
"function (obj, prev) { prev.count++; }"))
"function (obj, prev) { prev.count++; }"
))
# modifying finalize
self.assertEqual([{"a": 2, "count": 3},
{"a": None, "count": 2},
{"a": 1, "count": 2}],
db.test.group(["a"], {}, {"count": 0},
"function (obj, prev) { prev.count++; }",
"function (obj, prev) "
"{ prev.count++; }",
"function (obj) { obj.count++; }"))
# returning finalize
self.assertEqual([2, 1, 1],
db.test.group(["a"], {}, {"count": 0},
"function (obj, prev) { prev.count++; }",
"function (obj, prev) "
"{ prev.count++; }",
"function (obj) { return obj.count; }"))
# keyf
self.assertEqual([2, 2],
db.test.group("function (obj) { if (obj.a == 2) { return {a: true} }; "
db.test.group("function (obj) { if (obj.a == 2) "
"{ return {a: true} }; "
"return {b: true}; }", {}, {"count": 0},
"function (obj, prev) { prev.count++; }",
"function (obj, prev) "
"{ prev.count++; }",
"function (obj) { return obj.count; }"))
# no key
self.assertEqual([{"count": 4}],
db.test.group(None, {}, {"count": 0},
"function (obj, prev) { prev.count++; }"))
"function (obj, prev) { prev.count++; }"
))
warnings.simplefilter("error")
self.assertRaises(DeprecationWarning,
@ -744,7 +768,8 @@ class TestCollection(unittest.TestCase):
command=False)
warnings.simplefilter("default")
self.assertRaises(OperationFailure, db.test.group, [], {}, {}, "5 ++ 5")
self.assertRaises(OperationFailure, db.test.group,
[], {}, {}, "5 ++ 5")
def test_group_with_scope(self):
db = self.db
@ -761,9 +786,10 @@ class TestCollection(unittest.TestCase):
Code(reduce_function,
{"inc_value": 2}))[0]['count'])
self.assertEqual(1, db.test.group([], {}, {"count": 0},
Code(reduce_function,
{"inc_value": 0.5}))[0]['count'])
self.assertEqual(1,
db.test.group([], {}, {"count": 0},
Code(reduce_function,
{"inc_value": 0.5}))[0]['count'])
if version.at_least(db.connection, (1, 1)):
self.assertEqual(2, db.test.group([], {}, {"count": 0},
@ -855,7 +881,8 @@ class TestCollection(unittest.TestCase):
self.assertRaises(TypeError, db.test.find, snapshot=5)
list(db.test.find(snapshot=True))
self.assertRaises(OperationFailure, list, db.test.find(snapshot=True).sort("foo", 1))
self.assertRaises(OperationFailure, list,
db.test.find(snapshot=True).sort("foo", 1))
def test_find_one(self):
db = self.db
@ -867,7 +894,8 @@ class TestCollection(unittest.TestCase):
self.assertEqual(db.test.find_one(id), db.test.find_one())
self.assertEqual(db.test.find_one(None), db.test.find_one())
self.assertEqual(db.test.find_one({}), db.test.find_one())
self.assertEqual(db.test.find_one({"hello": "world"}), db.test.find_one())
self.assertEqual(db.test.find_one({"hello": "world"}),
db.test.find_one())
self.assert_("hello" in db.test.find_one(fields=["hello"]))
self.assert_("hello" not in db.test.find_one(fields=["foo"]))
@ -921,9 +949,9 @@ class TestCollection(unittest.TestCase):
def to_list(foo):
return [bar["x"] for bar in foo]
self.assertEqual([2,1,3], to_list(db.test.find()))
self.assertEqual([1,2,3], to_list(db.test.find(sort=[("x", 1)])))
self.assertEqual([3,2,1], to_list(db.test.find(sort=[("x", -1)])))
self.assertEqual([2, 1, 3], to_list(db.test.find()))
self.assertEqual([1, 2, 3], to_list(db.test.find(sort=[("x", 1)])))
self.assertEqual([3, 2, 1], to_list(db.test.find(sort=[("x", -1)])))
self.assertRaises(TypeError, db.test.find, sort=5)
self.assertRaises(TypeError, db.test.find, sort="hello")
@ -983,8 +1011,11 @@ class TestCollection(unittest.TestCase):
self.db.test.save({"query": "foo"})
self.db.test.save({"bar": "foo"})
self.assertEqual(1, self.db.test.find({"query": {"$ne": None}}).count())
self.assertEqual(1, len(list(self.db.test.find({"query": {"$ne": None}}))))
self.assertEqual(1,
self.db.test.find({"query": {"$ne": None}}).count())
self.assertEqual(1,
len(list(self.db.test.find({"query": {"$ne": None}})))
)
def test_min_query(self):
self.db.drop_collection("test")
@ -1131,24 +1162,25 @@ class TestCollection(unittest.TestCase):
def test_find_and_modify(self):
c = self.db.test
c.drop()
c.insert({'_id': 1, 'i':1})
c.insert({'_id': 1, 'i': 1})
self.assertEqual({'_id': 1, 'i':1},
c.find_and_modify({'_id':1}, {'$inc':{'i':1}}))
self.assertEqual({'_id': 1, 'i':3},
c.find_and_modify({'_id':1}, {'$inc':{'i':1}},
self.assertEqual({'_id': 1, 'i': 1},
c.find_and_modify({'_id': 1}, {'$inc': {'i': 1}}))
self.assertEqual({'_id': 1, 'i': 3},
c.find_and_modify({'_id': 1}, {'$inc': {'i': 1}},
new=True))
self.assertEqual({'_id': 1, 'i':3},
c.find_and_modify({'_id':1}, remove=True))
self.assertEqual({'_id': 1, 'i': 3},
c.find_and_modify({'_id': 1}, remove=True))
self.assertEqual(None, c.find_one({'_id':1}))
self.assertEqual(None, c.find_one({'_id': 1}))
self.assertEqual(None, c.find_and_modify({'_id':1}, {'$inc':{'i':1}}))
self.assertEqual({}, c.find_and_modify({'_id':1}, {'$inc':{'i':1}},
self.assertEqual(None,
c.find_and_modify({'_id': 1}, {'$inc': {'i': 1}}))
self.assertEqual({}, c.find_and_modify({'_id': 1}, {'$inc': {'i': 1}},
upsert=True))
self.assertEqual({'_id': 1, 'i':2},
c.find_and_modify({'_id':1}, {'$inc':{'i':1}},
self.assertEqual({'_id': 1, 'i': 2},
c.find_and_modify({'_id': 1}, {'$inc': {'i': 1}},
upsert=True, new=True))

View File

@ -92,7 +92,8 @@ class TestConnection(unittest.TestCase):
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(set([(self.host, self.port)]), Connection(self.host, self.port).nodes)
self.assertEqual(set([(self.host, self.port)]),
Connection(self.host, self.port).nodes)
def test_get_db(self):
connection = Connection(self.host, self.port)
@ -247,21 +248,25 @@ class TestConnection(unittest.TestCase):
self.assertEqual(([("localhost", 27017)], None, None, None, None, {}),
_parse_uri("mongodb://localhost", 27017))
self.assertEqual(([("localhost", 27017)], None, "fred", "foobar", None, {}),
self.assertEqual(([("localhost", 27017)], None,
"fred", "foobar", None, {}),
_parse_uri("mongodb://fred:foobar@localhost",
27017))
self.assertEqual(([("localhost", 27017)], "baz", "fred", "foobar", None, {}),
self.assertEqual(([("localhost", 27017)], "baz",
"fred", "foobar", None, {}),
_parse_uri("mongodb://fred:foobar@localhost/baz",
27017))
self.assertEqual(([("example1.com", 27017), ("example2.com", 27017)],
None, None, None, None, {}),
_parse_uri("mongodb://example1.com:27017,example2.com:27017",
27018))
_parse_uri("mongodb://"
"example1.com:27017,example2.com:27017",
27018))
self.assertEqual(([("localhost", 27017),
("localhost", 27018),
("localhost", 27019)], None, None, None, None, {}),
_parse_uri("mongodb://localhost,localhost:27018,localhost:27019",
27017))
_parse_uri("mongodb://localhost,"
"localhost:27018,localhost:27019",
27017))
self.assertEqual(([("localhost", 27018)], None, None, None, None, {}),
_parse_uri("localhost:27018", 27017))
@ -270,18 +275,26 @@ class TestConnection(unittest.TestCase):
self.assertEqual(([("localhost", 27017)], None, None, None, None, {}),
_parse_uri("localhost/", 27017))
self.assertEqual(([("localhost", 27017)], "test", None, None, "yield_historical.in", {}),
_parse_uri("mongodb://localhost/test.yield_historical.in", 27017))
self.assertEqual(([("localhost", 27017)], "test", "fred", "foobar", "yield_historical.in", {}),
_parse_uri("mongodb://fred:foobar@localhost/test.yield_historical.in",
27017))
self.assertEqual(([("localhost", 27017)], "test",
None, None, "yield_historical.in", {}),
_parse_uri("mongodb://"
"localhost/test.yield_historical.in",
27017))
self.assertEqual(([("localhost", 27017)], "test", "fred",
"foobar", "yield_historical.in", {}),
_parse_uri("mongodb://fred:foobar@localhost/"
"test.yield_historical.in",
27017))
self.assertEqual(([("example1.com", 27017), ("example2.com", 27017)],
"test", None, None, "yield_historical.in", {}),
_parse_uri("mongodb://example1.com:27017,example2.com:27017/test.yield_historical.in",
27017))
self.assertEqual(([("localhost", 27017)], "test", "fred", "foobar", "yield_historical.in", {'slaveok': 'true'}),
_parse_uri("mongodb://fred:foobar@localhost/test.yield_historical.in?slaveok=true",
27017))
_parse_uri("mongodb://example1.com:27017,example2.com"
":27017/test.yield_historical.in",
27017))
self.assertEqual(([("localhost", 27017)], "test", "fred", "foobar",
"yield_historical.in", {'slaveok': 'true'}),
_parse_uri("mongodb://fred:foobar@localhost/"
"test.yield_historical.in?slaveok=true",
27017))
def test_from_uri(self):
c = Connection(self.host, self.port)
@ -430,7 +443,8 @@ class TestConnection(unittest.TestCase):
self.assertRaises(ConnectionFailure, get_x, timeout.pymongo_test)
def get_x_timeout(db, t):
return db.test.find(network_timeout=t).where(where_func).next()["x"]
return db.test.find(
network_timeout=t).where(where_func).next()["x"]
self.assertEqual(1, get_x_timeout(timeout.pymongo_test, None))
self.assertRaises(ConnectionFailure, get_x_timeout,
no_timeout.pymongo_test, 0.1)
@ -445,8 +459,9 @@ class TestConnection(unittest.TestCase):
self.assertEqual(None, naive.pymongo_test.test.find_one()["x"].tzinfo)
self.assertEqual(utc, aware.pymongo_test.test.find_one()["x"].tzinfo)
self.assertEqual(aware.pymongo_test.test.find_one()["x"].replace(tzinfo=None),
naive.pymongo_test.test.find_one()["x"])
self.assertEqual(
aware.pymongo_test.test.find_one()["x"].replace(tzinfo=None),
naive.pymongo_test.test.find_one()["x"])
if __name__ == "__main__":

View File

@ -87,7 +87,6 @@ class TestCursor(unittest.TestCase):
self.assertRaises(TypeError, db.test.find().hint, index)
def test_limit(self):
db = self.db
@ -135,7 +134,6 @@ class TestCursor(unittest.TestCase):
break
self.assertRaises(InvalidOperation, a.limit, 5)
def test_batch_size(self):
db = self.db
db.test.drop()
@ -178,7 +176,6 @@ class TestCursor(unittest.TestCase):
cursor_count(db.test.find().batch_size(100).limit(10), 10)
cursor_count(db.test.find().batch_size(500).limit(10), 10)
def test_skip(self):
db = self.db
@ -512,12 +509,19 @@ class TestCursor(unittest.TestCase):
for a, b in izip(count(20), self.db.test.find()[40:45][20:]):
self.assertEqual(a, b['i'])
self.assertEqual(80, len(list(self.db.test.find()[40:45].limit(0).skip(20))))
for a, b in izip(count(20), self.db.test.find()[40:45].limit(0).skip(20)):
self.assertEqual(80,
len(list(self.db.test.find()[40:45].limit(0).skip(20))
)
)
for a, b in izip(count(20),
self.db.test.find()[40:45].limit(0).skip(20)):
self.assertEqual(a, b['i'])
self.assertEqual(80, len(list(self.db.test.find().limit(10).skip(40)[20:])))
for a, b in izip(count(20), self.db.test.find().limit(10).skip(40)[20:]):
self.assertEqual(80,
len(list(self.db.test.find().limit(10).skip(40)[20:]))
)
for a, b in izip(count(20),
self.db.test.find().limit(10).skip(40)[20:]):
self.assertEqual(a, b['i'])
self.assertEqual(1, len(list(self.db.test.find()[:1])))
@ -527,7 +531,10 @@ class TestCursor(unittest.TestCase):
self.assertEqual(1, len(list(self.db.test.find()[99:1000])))
self.assertEqual(0, len(list(self.db.test.find()[10:10])))
self.assertEqual(0, len(list(self.db.test.find()[:0])))
self.assertEqual(80, len(list(self.db.test.find()[10:10].limit(0).skip(20))))
self.assertEqual(80,
len(list(self.db.test.find()[10:10].limit(0).skip(20))
)
)
self.assertRaises(IndexError, lambda: self.db.test.find()[10:8])
@ -545,7 +552,8 @@ class TestCursor(unittest.TestCase):
self.assertRaises(IndexError, lambda x: self.db.test.find()[x], -1)
self.assertRaises(IndexError, lambda x: self.db.test.find()[x], 100)
self.assertRaises(IndexError, lambda x: self.db.test.find().skip(50)[x], 50)
self.assertRaises(IndexError,
lambda x: self.db.test.find().skip(50)[x], 50)
def test_count_with_limit_and_skip(self):
if not version.at_least(self.db.connection, (1, 1, 4, -1)):

View File

@ -151,7 +151,7 @@ class TestDatabase(unittest.TestCase):
def test_profiling_levels(self):
db = self.connection.pymongo_test
self.assertEqual(db.profiling_level(), OFF) #default
self.assertEqual(db.profiling_level(), OFF) # default
self.assertRaises(ValueError, db.set_profiling_level, 5.5)
self.assertRaises(ValueError, db.set_profiling_level, None)
@ -305,8 +305,10 @@ class TestDatabase(unittest.TestCase):
obj = {"x": True}
key = db.test.save(obj)
self.assertEqual(obj, db.dereference(DBRef("test", key)))
self.assertEqual(obj, db.dereference(DBRef("test", key, "pymongo_test")))
self.assertRaises(ValueError, db.dereference, DBRef("test", key, "foo"))
self.assertEqual(obj,
db.dereference(DBRef("test", key, "pymongo_test")))
self.assertRaises(ValueError,
db.dereference, DBRef("test", key, "foo"))
self.assertEqual(None, db.dereference(DBRef("test", 4)))
obj = {"_id": 4}

View File

@ -60,38 +60,55 @@ class TestDBRef(unittest.TestCase):
self.assertRaises(AttributeError, bar)
def test_repr(self):
self.assertEqual(repr(DBRef("coll", ObjectId("1234567890abcdef12345678"))),
self.assertEqual(repr(DBRef("coll",
ObjectId("1234567890abcdef12345678"))),
"DBRef('coll', ObjectId('1234567890abcdef12345678'))")
self.assertEqual(repr(DBRef(u"coll", ObjectId("1234567890abcdef12345678"))),
"DBRef(u'coll', ObjectId('1234567890abcdef12345678'))")
self.assertEqual(repr(DBRef(u"coll",
ObjectId("1234567890abcdef12345678"))),
"DBRef(u'coll', ObjectId('1234567890abcdef12345678'))"
)
self.assertEqual(repr(DBRef("coll", 5, foo="bar")),
"DBRef('coll', 5, foo='bar')")
self.assertEqual(repr(DBRef("coll", ObjectId("1234567890abcdef12345678"), "foo")),
"DBRef('coll', ObjectId('1234567890abcdef12345678'), 'foo')")
self.assertEqual(repr(DBRef("coll",
ObjectId("1234567890abcdef12345678"), "foo")),
"DBRef('coll', ObjectId('1234567890abcdef12345678'), "
"'foo')")
self.assertEqual(repr(DBRef("coll", 5, "baz", foo="bar", baz=4)),
"DBRef('coll', 5, 'baz', foo='bar', baz=4)")
def test_cmp(self):
self.assertEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
DBRef(u"coll", ObjectId("1234567890abcdef12345678")))
self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
DBRef(u"coll", ObjectId("1234567890abcdef12345678"), "foo"))
self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
self.assertNotEqual(DBRef("coll",
ObjectId("1234567890abcdef12345678")),
DBRef(u"coll",
ObjectId("1234567890abcdef12345678"), "foo"))
self.assertNotEqual(DBRef("coll",
ObjectId("1234567890abcdef12345678")),
DBRef("col", ObjectId("1234567890abcdef12345678")))
self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
self.assertNotEqual(DBRef("coll",
ObjectId("1234567890abcdef12345678")),
DBRef("coll", ObjectId("123456789011")))
self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")), 4)
self.assertEqual(DBRef("coll", ObjectId("1234567890abcdef12345678"), "foo"),
DBRef(u"coll", ObjectId("1234567890abcdef12345678"), "foo"))
self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678"), "foo"),
DBRef(u"coll", ObjectId("1234567890abcdef12345678"), "bar"))
self.assertNotEqual(DBRef("coll",
ObjectId("1234567890abcdef12345678")), 4)
self.assertEqual(DBRef("coll",
ObjectId("1234567890abcdef12345678"), "foo"),
DBRef(u"coll",
ObjectId("1234567890abcdef12345678"), "foo"))
self.assertNotEqual(DBRef("coll",
ObjectId("1234567890abcdef12345678"), "foo"),
DBRef(u"coll",
ObjectId("1234567890abcdef12345678"), "bar"))
def test_kwargs(self):
self.assertEqual(DBRef("coll", 5, foo="bar"), DBRef("coll", 5, foo="bar"))
self.assertEqual(DBRef("coll", 5, foo="bar"),
DBRef("coll", 5, foo="bar"))
self.assertNotEqual(DBRef("coll", 5, foo="bar"), DBRef("coll", 5))
self.assertNotEqual(DBRef("coll", 5, foo="bar"), DBRef("coll", 5, foo="baz"))
self.assertNotEqual(DBRef("coll", 5, foo="bar"),
DBRef("coll", 5, foo="baz"))
self.assertEqual("bar", DBRef("coll", 5, foo="bar").foo)
self.assertRaises(AttributeError, getattr, DBRef("coll", 5, foo="bar"), "bar")
self.assertRaises(AttributeError, getattr,
DBRef("coll", 5, foo="bar"), "bar")
def test_deepcopy(self):
a = DBRef('coll', 'asdf', 'db', x=[1])

View File

@ -60,7 +60,6 @@ class TestGridFile(unittest.TestCase):
g = GridOut(self.db.fs, f._id)
self.assertEqual("hello world", g.read())
f = GridIn(self.db.fs, filename="test")
f.close()
self.assertEqual(2, self.db.fs.files.find().count())
@ -173,7 +172,8 @@ class TestGridFile(unittest.TestCase):
self.assertEqual("hello", a.baz)
self.assertRaises(AttributeError, getattr, a, "mike")
b = GridIn(self.db.fs, content_type="text/html", chunk_size=1000, baz=100)
b = GridIn(self.db.fs,
content_type="text/html", chunk_size=1000, baz=100)
self.assertEqual("text/html", b.content_type)
self.assertEqual(1000, b.chunk_size)
self.assertEqual(100, b.baz)
@ -405,7 +405,8 @@ Bye""")
self.assertEqual(["he", "ll", "o ", "wo", "rl", "d"], list(g))
def test_read_chunks_unaligned_buffer_size(self):
in_data = "This is a text that doesn't quite fit in a single 16-byte chunk."
in_data = ("This is a text that doesn't "
"quite fit in a single 16-byte chunk.")
f = GridIn(self.db.fs, chunkSize=16)
f.write(in_data)
f.close()

View File

@ -90,7 +90,6 @@ class TestGridfs(unittest.TestCase):
self.assertEqual("foo", oid)
self.assertEqual("hello world", self.fs.get("foo").read())
def test_list(self):
self.assertEqual([], self.fs.list())
self.fs.put("hello world")
@ -113,7 +112,7 @@ class TestGridfs(unittest.TestCase):
self.assertEqual(0, raw["length"])
self.assertEqual(oid, raw["_id"])
self.assert_(isinstance(raw["uploadDate"], datetime.datetime))
self.assertEqual(256*1024, raw["chunkSize"])
self.assertEqual(256 * 1024, raw["chunkSize"])
self.assert_(isinstance(raw["md5"], basestring))
def test_alt_collection(self):

View File

@ -44,6 +44,7 @@ from bson.timestamp import Timestamp
from bson.tz_util import utc
from bson.json_util import default, object_hook
class TestJsonUtil(unittest.TestCase):
def setUp(self):
@ -72,9 +73,12 @@ class TestJsonUtil(unittest.TestCase):
# http://bugs.python.org/issue6105
#
# self.assertEqual("{\"ref\": {\"$ref\": \"foo\", \"$id\": 5}}",
# json.dumps({"ref": DBRef("foo", 5)}, default=default))
# self.assertEqual("{\"ref\": {\"$ref\": \"foo\", \"$id\": 5, \"$db\": \"bar\"}}",
# json.dumps({"ref": DBRef("foo", 5, "bar")}, default=default))
# json.dumps({"ref": DBRef("foo", 5)},
# default=default))
# self.assertEqual("{\"ref\": {\"$ref\": \"foo\",
# \"$id\": 5, \"$db\": \"bar\"}}",
# json.dumps({"ref": DBRef("foo", 5, "bar")},
# default=default))
def test_datetime(self):
# only millis, not micros
@ -87,21 +91,22 @@ class TestJsonUtil(unittest.TestCase):
self.assertEqual(re.IGNORECASE, res.flags)
def test_minkey(self):
self.round_trip({"m" : MinKey()})
self.round_trip({"m": MinKey()})
def test_maxkey(self):
self.round_trip({"m" : MinKey()})
self.round_trip({"m": MinKey()})
def test_timestamp(self):
res = json.dumps({"ts" : Timestamp(4, 13)}, default=default)
dct = json.loads(res);
res = json.dumps({"ts": Timestamp(4, 13)}, default=default)
dct = json.loads(res)
self.assertEqual(dct['ts']['t'], 4)
self.assertEqual(dct['ts']['i'], 13)
def test_uuid(self):
if not should_test_uuid:
raise SkipTest()
self.round_trip({'uuid' : uuid.UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479')})
self.round_trip(
{'uuid': uuid.UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479')})
if __name__ == "__main__":
unittest.main()

View File

@ -72,11 +72,13 @@ class TestMasterSlaveConnection(unittest.TestCase):
class Connection(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.connection._MasterSlaveConnection__slaves = [Connection(),
Connection()]
self.connection.disconnect()
self.assertEquals(1,
@ -89,8 +91,10 @@ class TestMasterSlaveConnection(unittest.TestCase):
def test_continue_until_slave_works(self):
class Slave(object):
calls = 0
def __init__(self, fail):
self._fail = fail
def _send_message_with_response(self, *args, **kwargs):
Slave.calls += 1
if self._fail:
@ -98,11 +102,15 @@ class TestMasterSlaveConnection(unittest.TestCase):
return 'sent'
class NotRandomList(object):
last_idx = -1;
last_idx = -1
def __init__(self):
self._items = [Slave(True), Slave(True), Slave(False), Slave(True)]
self._items = [Slave(True), Slave(True),
Slave(False), Slave(True)]
def __len__(self):
return len(self._items)
def __getitem__(self, idx):
NotRandomList.last_idx = idx
return self._items.pop(0)
@ -117,8 +125,10 @@ class TestMasterSlaveConnection(unittest.TestCase):
def test_raise_autoreconnect_if_all_slaves_fail(self):
class Slave(object):
calls = 0
def __init__(self, fail):
self._fail = fail
def _send_message_with_response(self, *args, **kwargs):
Slave.calls += 1
if self._fail:
@ -127,9 +137,12 @@ class TestMasterSlaveConnection(unittest.TestCase):
class NotRandomList(object):
def __init__(self):
self._items = [Slave(True), Slave(True), Slave(True), Slave(True)]
self._items = [Slave(True), Slave(True),
Slave(True), Slave(True)]
def __len__(self):
return len(self._items)
def __getitem__(self, idx):
return self._items.pop(0)
@ -218,7 +231,8 @@ class TestMasterSlaveConnection(unittest.TestCase):
self.db.test.create_index('username', unique=True)
self.db.test.save({'username': 'mike'}, safe=True)
self.assertRaises(OperationFailure, self.db.test.save, {'username': 'mike'}, safe=True)
self.assertRaises(OperationFailure,
self.db.test.save, {'username': 'mike'}, safe=True)
# NOTE this test is non-deterministic, but I expect
# some failures unless the db is pulling instantaneously...
@ -268,7 +282,7 @@ class TestMasterSlaveConnection(unittest.TestCase):
for i in range(10000):
db.test.insert({"i": i})
time.sleep(11) # need to sleep to be sure this gets pulled...
time.sleep(11) # need to sleep to be sure this gets pulled...
self.assertEqual(before, cursor_count())

View File

@ -29,6 +29,7 @@ from bson.objectid import ObjectId
from bson.tz_util import (FixedOffset,
utc)
def oid(x):
return ObjectId()
@ -66,10 +67,14 @@ class TestObjectId(unittest.TestCase):
def test_repr_str(self):
self.assertEqual(repr(ObjectId("1234567890abcdef12345678")),
"ObjectId('1234567890abcdef12345678')")
self.assertEqual(str(ObjectId("1234567890abcdef12345678")), "1234567890abcdef12345678")
self.assertEqual(str(ObjectId("123456789012")), "313233343536373839303132")
self.assertEqual(ObjectId("1234567890abcdef12345678").binary, '\x124Vx\x90\xab\xcd\xef\x124Vx')
self.assertEqual(str(ObjectId('\x124Vx\x90\xab\xcd\xef\x124Vx')), "1234567890abcdef12345678")
self.assertEqual(str(ObjectId("1234567890abcdef12345678")),
"1234567890abcdef12345678")
self.assertEqual(str(ObjectId("123456789012")),
"313233343536373839303132")
self.assertEqual(ObjectId("1234567890abcdef12345678").binary,
'\x124Vx\x90\xab\xcd\xef\x124Vx')
self.assertEqual(str(ObjectId('\x124Vx\x90\xab\xcd\xef\x124Vx')),
"1234567890abcdef12345678")
def test_cmp(self):
a = ObjectId()
@ -111,7 +116,7 @@ class TestObjectId(unittest.TestCase):
self.assertEqual(utc, d2.tzinfo)
d2 = d2.replace(tzinfo=None)
self.assert_(d2 - d1 < datetime.timedelta(seconds = 2))
self.assert_(d2 - d1 < datetime.timedelta(seconds=2))
def test_from_datetime(self):
d = datetime.datetime.utcnow()
@ -120,7 +125,8 @@ class TestObjectId(unittest.TestCase):
self.assertEqual(d, oid.generation_time.replace(tzinfo=None))
self.assertEqual("0" * 16, str(oid)[8:])
aware = datetime.datetime(1993, 4, 4, 2, tzinfo=FixedOffset(555, "SomeZone"))
aware = datetime.datetime(1993, 4, 4, 2,
tzinfo=FixedOffset(555, "SomeZone"))
as_utc = (aware - aware.utcoffset()).replace(tzinfo=utc)
oid = ObjectId.from_datetime(aware)
self.assertEqual(as_utc, oid.generation_time)
@ -135,7 +141,8 @@ class TestObjectId(unittest.TestCase):
# version 1.9
pickled_with_1_9 = (
"ccopy_reg\n_reconstructor\np0\n"
"(cbson.objectid\nObjectId\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n"
"(cbson.objectid\nObjectId\np1\nc__builtin__\n"
"object\np2\nNtp3\nRp4\n"
"(dp5\nS'_ObjectId__id'\np6\n"
"S'M\\x9afV\\x13v\\xc0\\x0b\\x88\\x00\\x00\\x00'\np7\nsb.")
@ -144,7 +151,8 @@ class TestObjectId(unittest.TestCase):
# the future as well.
pickled_with_1_10 = (
"ccopy_reg\n_reconstructor\np0\n"
"(cbson.objectid\nObjectId\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n"
"(cbson.objectid\nObjectId\np1\nc__builtin__\n"
"object\np2\nNtp3\nRp4\n"
"S'M\\x9afV\\x13v\\xc0\\x0b\\x88\\x00\\x00\\x00'\np5\nb."
)

View File

@ -30,6 +30,7 @@ from test_connection import get_connection
N = 50
DB = "pymongo-pooling-tests"
class MongoThread(threading.Thread):
def __init__(self, test_case):

View File

@ -72,7 +72,8 @@ class Update(threading.Thread):
error = True
try:
self.collection.update({"test": "unique"}, {"$set": {"test": "update"}}, safe=True)
self.collection.update({"test": "unique"},
{"$set": {"test": "update"}}, safe=True)
error = False
except:
if not self.expect_exception:

View File

@ -51,10 +51,10 @@ class TestTimestamp(unittest.TestCase):
self.assert_(Timestamp(0, 0))
def test_equality(self):
t = Timestamp(1,1)
self.assertNotEqual(t, Timestamp(0,1))
self.assertNotEqual(t, Timestamp(1,0))
self.assertEqual(t, Timestamp(1,1))
t = Timestamp(1, 1)
self.assertNotEqual(t, Timestamp(0, 1))
self.assertNotEqual(t, Timestamp(1, 0))
self.assertEqual(t, Timestamp(1, 1))
def test_repr(self):
t = Timestamp(0, 0)

View File

@ -14,6 +14,7 @@
"""Some tools for running tests based on MongoDB server version."""
def _padded(iter, length, padding=0):
l = list(iter)
if len(l) < length:
@ -21,6 +22,7 @@ def _padded(iter, length, padding=0):
l.append(0)
return l
def _parse_version_string(version_string):
mod = 0
if version_string.endswith("+"):
@ -43,9 +45,11 @@ def _parse_version_string(version_string):
return tuple(version)
# Note this is probably broken for very old versions of the database...
def version(connection):
return _parse_version_string(connection.server_info()["version"])
def at_least(connection, min_version):
return version(connection) >= tuple(_padded(min_version, 4))

View File

@ -25,6 +25,7 @@ from pymongo.connection import Connection
db = Connection.paired(("localhost", 27018)).test
db.test.remove({})
class Something(threading.Thread):
def run(self):
while True:

View File

@ -45,33 +45,40 @@ large = {"base_url": "http://www.example.com/test-me",
"no_of_js_attached": 10,
"no_of_images": 6
},
"harvested_words": ["10gen","web","open","source","application","paas",
"platform-as-a-service","technology","helps",
"developers","focus","building","mongodb","mongo"] * 20
"harvested_words": ["10gen", "web", "open", "source", "application",
"paas", "platform-as-a-service", "technology",
"helps", "developers", "focus", "building",
"mongodb", "mongo"] * 20
}
def setup_insert(db, collection, object):
db.drop_collection(collection)
def insert(db, collection, object):
for i in range(per_trial):
to_insert = object.copy()
to_insert["x"] = i
db[collection].insert(to_insert)
def insert_batch(db, collection, object):
for i in range(per_trial / batch_size):
db[collection].insert([object] * batch_size)
def find_one(db, collection, x):
for _ in range(per_trial):
db[collection].find_one({"x": x})
def find(db, collection, x):
for _ in range(per_trial):
for _ in db[collection].find({"x": x}):
pass
def timed(name, function, args=[], setup=None):
times = []
for _ in range(trials):
@ -84,15 +91,19 @@ def timed(name, function, args=[], setup=None):
print "%s%d" % (name + (60 - len(name)) * ".", per_trial / best_time)
return best_time
def main():
connection._TIMEOUT=60 # jack up the timeout
connection._TIMEOUT = 60 # jack up the timeout
c = connection.Connection()
c.drop_database("benchmark")
db = c.benchmark
timed("insert (small, no index)", insert, [db, 'small_none', small], setup_insert)
timed("insert (medium, no index)", insert, [db, 'medium_none', medium], setup_insert)
timed("insert (large, no index)", insert, [db, 'large_none', large], setup_insert)
timed("insert (small, no index)", insert,
[db, 'small_none', small], setup_insert)
timed("insert (medium, no index)", insert,
[db, 'medium_none', medium], setup_insert)
timed("insert (large, no index)", insert,
[db, 'large_none', large], setup_insert)
db.small_index.create_index("x", ASCENDING)
timed("insert (small, indexed)", insert, [db, 'small_index', small])
@ -101,17 +112,26 @@ def main():
db.large_index.create_index("x", ASCENDING)
timed("insert (large, indexed)", insert, [db, 'large_index', large])
timed("batch insert (small, no index)", insert_batch, [db, 'small_bulk', small], setup_insert)
timed("batch insert (medium, no index)", insert_batch, [db, 'medium_bulk', medium], setup_insert)
timed("batch insert (large, no index)", insert_batch, [db, 'large_bulk', large], setup_insert)
timed("batch insert (small, no index)", insert_batch,
[db, 'small_bulk', small], setup_insert)
timed("batch insert (medium, no index)", insert_batch,
[db, 'medium_bulk', medium], setup_insert)
timed("batch insert (large, no index)", insert_batch,
[db, 'large_bulk', large], setup_insert)
timed("find_one (small, no index)", find_one, [db, 'small_none', per_trial / 2])
timed("find_one (medium, no index)", find_one, [db, 'medium_none', per_trial / 2])
timed("find_one (large, no index)", find_one, [db, 'large_none', per_trial / 2])
timed("find_one (small, no index)", find_one,
[db, 'small_none', per_trial / 2])
timed("find_one (medium, no index)", find_one,
[db, 'medium_none', per_trial / 2])
timed("find_one (large, no index)", find_one,
[db, 'large_none', per_trial / 2])
timed("find_one (small, indexed)", find_one, [db, 'small_index', per_trial / 2])
timed("find_one (medium, indexed)", find_one, [db, 'medium_index', per_trial / 2])
timed("find_one (large, indexed)", find_one, [db, 'large_index', per_trial / 2])
timed("find_one (small, indexed)", find_one,
[db, 'small_index', per_trial / 2])
timed("find_one (medium, indexed)", find_one,
[db, 'medium_index', per_trial / 2])
timed("find_one (large, indexed)", find_one,
[db, 'large_index', per_trial / 2])
timed("find (small, no index)", find, [db, 'small_none', per_trial / 2])
timed("find (medium, no index)", find, [db, 'medium_none', per_trial / 2])
@ -122,18 +142,24 @@ def main():
timed("find (large, indexed)", find, [db, 'large_index', per_trial / 2])
# timed("find range (small, no index)", find,
# [db, 'small_none', {"$gt": per_trial / 4, "$lt": 3 * per_trial / 4}])
# [db, 'small_none',
# {"$gt": per_trial / 4, "$lt": 3 * per_trial / 4}])
# timed("find range (medium, no index)", find,
# [db, 'medium_none', {"$gt": per_trial / 4, "$lt": 3 * per_trial / 4}])
# [db, 'medium_none',
# {"$gt": per_trial / 4, "$lt": 3 * per_trial / 4}])
# timed("find range (large, no index)", find,
# [db, 'large_none', {"$gt": per_trial / 4, "$lt": 3 * per_trial / 4}])
# [db, 'large_none',
# {"$gt": per_trial / 4, "$lt": 3 * per_trial / 4}])
timed("find range (small, indexed)", find,
[db, 'small_index', {"$gt": per_trial / 2, "$lt": per_trial / 2 + batch_size}])
[db, 'small_index',
{"$gt": per_trial / 2, "$lt": per_trial / 2 + batch_size}])
timed("find range (medium, indexed)", find,
[db, 'medium_index', {"$gt": per_trial / 2, "$lt": per_trial / 2 + batch_size}])
[db, 'medium_index',
{"$gt": per_trial / 2, "$lt": per_trial / 2 + batch_size}])
timed("find range (large, indexed)", find,
[db, 'large_index', {"$gt": per_trial / 2, "$lt": per_trial / 2 + batch_size}])
[db, 'large_index',
{"$gt": per_trial / 2, "$lt": per_trial / 2 + batch_size}])
if __name__ == "__main__":
# cProfile.run("main()")