minor: more PEP 8
This commit is contained in:
parent
b8b4a04442
commit
90beef7b20
@ -253,13 +253,15 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.insert(dict)
|
||||
return db.test.find_one() == dict
|
||||
|
||||
qcheck.check_unittest(self, remove_insert_find_one, qcheck.gen_mongo_dict(3))
|
||||
qcheck.check_unittest(self, remove_insert_find_one,
|
||||
qcheck.gen_mongo_dict(3))
|
||||
|
||||
def test_find_w_fields(self):
|
||||
db = self.db
|
||||
db.test.remove({})
|
||||
|
||||
db.test.insert({"x": 1, "mike": "awesome", "extra thing": "abcdefghijklmnopqrstuvwxyz"})
|
||||
db.test.insert({"x": 1, "mike": "awesome",
|
||||
"extra thing": "abcdefghijklmnopqrstuvwxyz"})
|
||||
self.assertEqual(1, db.test.count())
|
||||
self.assert_("x" in db.test.find({}).next())
|
||||
self.assert_("mike" in db.test.find({}).next())
|
||||
@ -281,10 +283,14 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.insert({"x": "hello_test"})
|
||||
|
||||
self.assertEqual(db.test.find().count(), 4)
|
||||
self.assertEqual(db.test.find({"x": re.compile("^hello.*")}).count(), 4)
|
||||
self.assertEqual(db.test.find({"x": re.compile("ello")}).count(), 4)
|
||||
self.assertEqual(db.test.find({"x": re.compile("^hello$")}).count(), 0)
|
||||
self.assertEqual(db.test.find({"x": re.compile("^hello_mi.*$")}).count(), 2)
|
||||
self.assertEqual(db.test.find({"x":
|
||||
re.compile("^hello.*")}).count(), 4)
|
||||
self.assertEqual(db.test.find({"x":
|
||||
re.compile("ello")}).count(), 4)
|
||||
self.assertEqual(db.test.find({"x":
|
||||
re.compile("^hello$")}).count(), 0)
|
||||
self.assertEqual(db.test.find({"x":
|
||||
re.compile("^hello_mi.*$")}).count(), 2)
|
||||
|
||||
def test_id_can_be_anything(self):
|
||||
db = self.db
|
||||
@ -322,17 +328,24 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.insert({"hello": {"hello": "world"}})
|
||||
|
||||
self.assertRaises(InvalidName, db.test.insert, {"$hello": "world"})
|
||||
self.assertRaises(InvalidName, db.test.insert, {"hello": {"$hello": "world"}})
|
||||
self.assertRaises(InvalidName, db.test.insert,
|
||||
{"hello": {"$hello": "world"}})
|
||||
|
||||
db.test.insert({"he$llo": "world"})
|
||||
db.test.insert({"hello": {"hello$": "world"}})
|
||||
|
||||
self.assertRaises(InvalidName, db.test.insert, {".hello": "world"})
|
||||
self.assertRaises(InvalidName, db.test.insert, {"hello": {".hello": "world"}})
|
||||
self.assertRaises(InvalidName, db.test.insert, {"hello.": "world"})
|
||||
self.assertRaises(InvalidName, db.test.insert, {"hello": {"hello.": "world"}})
|
||||
self.assertRaises(InvalidName, db.test.insert, {"hel.lo": "world"})
|
||||
self.assertRaises(InvalidName, db.test.insert, {"hello": {"hel.lo": "world"}})
|
||||
self.assertRaises(InvalidName, db.test.insert,
|
||||
{".hello": "world"})
|
||||
self.assertRaises(InvalidName, db.test.insert,
|
||||
{"hello": {".hello": "world"}})
|
||||
self.assertRaises(InvalidName, db.test.insert,
|
||||
{"hello.": "world"})
|
||||
self.assertRaises(InvalidName, db.test.insert,
|
||||
{"hello": {"hello.": "world"}})
|
||||
self.assertRaises(InvalidName, db.test.insert,
|
||||
{"hel.lo": "world"})
|
||||
self.assertRaises(InvalidName, db.test.insert,
|
||||
{"hello": {"hel.lo": "world"}})
|
||||
|
||||
db.test.update({"hello": "world"}, {"$inc": "hello"})
|
||||
|
||||
@ -429,10 +442,10 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.update({}, {"$inc": {"x": 1}})
|
||||
self.assertEqual(db.error()["err"], "can't $inc/$set an indexed field")
|
||||
|
||||
self.assertRaises(OperationFailure, db.test.update, {}, {"$inc": {"x": 1}}, safe=True)
|
||||
self.assertRaises(OperationFailure, db.test.update,
|
||||
{}, {"$inc": {"x": 1}}, safe=True)
|
||||
|
||||
# TODO test safe save?
|
||||
|
||||
def test_count(self):
|
||||
db = self.db
|
||||
db.drop_collection("test")
|
||||
@ -446,14 +459,24 @@ class TestCollection(unittest.TestCase):
|
||||
db = self.db
|
||||
db.drop_collection("test")
|
||||
|
||||
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})
|
||||
db.test.save({"a": 1})
|
||||
|
||||
self.assertEqual(3, db.test.group([], {}, {"count": 0}, "function (obj, prev) { prev.count++; }")[0]["count"])
|
||||
self.assertEqual(1, db.test.group([], {"a": {"$gt": 1}}, {"count": 0}, "function (obj, prev) { prev.count++; }")[0]["count"])
|
||||
self.assertEqual(3, db.test.group([], {},
|
||||
{"count": 0},
|
||||
"function (obj, prev) { "
|
||||
"prev.count++; }")[0]["count"])
|
||||
self.assertEqual(1, db.test.group([],
|
||||
{"a": {"$gt": 1}},
|
||||
{"count": 0},
|
||||
"function (obj, prev) { "
|
||||
"prev.count++; }")[0]["count"])
|
||||
|
||||
def test_large_limit(self):
|
||||
db = self.db
|
||||
|
||||
@ -23,12 +23,15 @@ from pymongo.errors import ConnectionFailure, InvalidName
|
||||
from pymongo.database import Database
|
||||
from pymongo.connection import Connection
|
||||
|
||||
|
||||
def get_connection():
|
||||
host = os.environ.get("DB_IP", "localhost")
|
||||
port = int(os.environ.get("DB_PORT", 27017))
|
||||
return Connection(host, port)
|
||||
|
||||
|
||||
class TestConnection(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.host = os.environ.get("DB_IP", "localhost")
|
||||
self.port = int(os.environ.get("DB_PORT", 27017))
|
||||
@ -55,7 +58,8 @@ class TestConnection(unittest.TestCase):
|
||||
self.assert_(Connection())
|
||||
|
||||
def test_connect(self):
|
||||
self.assertRaises(ConnectionFailure, Connection, "somedomainthatdoesntexist.org")
|
||||
self.assertRaises(ConnectionFailure, Connection,
|
||||
"somedomainthatdoesntexist.org")
|
||||
self.assertRaises(ConnectionFailure, Connection, self.host, 123456789)
|
||||
|
||||
self.assert_(Connection(self.host, self.port))
|
||||
@ -63,6 +67,7 @@ class TestConnection(unittest.TestCase):
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(Connection(self.host, self.port)),
|
||||
"Connection('%s', %s)" % (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)
|
||||
|
||||
@ -26,7 +26,9 @@ from pymongo.code import Code
|
||||
from pymongo import ASCENDING, DESCENDING
|
||||
from test_connection import get_connection
|
||||
|
||||
|
||||
class TestCursor(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.db = Database(get_connection(), "pymongo_test")
|
||||
|
||||
@ -51,27 +53,26 @@ class TestCursor(unittest.TestCase):
|
||||
db.test.insert({"num": i, "foo": i})
|
||||
|
||||
self.assertRaises(OperationFailure,
|
||||
db.test.find({"num": 17, "foo": 17}
|
||||
).hint([("num", ASCENDING)]).explain)
|
||||
db.test.find({"num": 17, "foo": 17})
|
||||
.hint([("num", ASCENDING)]).explain)
|
||||
self.assertRaises(OperationFailure,
|
||||
db.test.find({"num": 17, "foo": 17}
|
||||
).hint([("foo", ASCENDING)]).explain)
|
||||
db.test.find({"num": 17, "foo": 17})
|
||||
.hint([("foo", ASCENDING)]).explain)
|
||||
|
||||
index = db.test.create_index("num", ASCENDING)
|
||||
|
||||
self.assertEqual(db.test.find({}).explain()["cursor"], "BasicCursor")
|
||||
self.assertEqual(db.test.find({}).hint(index).explain()["cursor"],
|
||||
"BtreeCursor %s" % index)
|
||||
self.assertEqual(db.test.find({}).hint(index
|
||||
).hint(None
|
||||
).explain()["cursor"],
|
||||
self.assertEqual(db.test.find({}).hint(index).hint(None)
|
||||
.explain()["cursor"],
|
||||
"BasicCursor")
|
||||
self.assertEqual(db.test.find({}).hint([("num", ASCENDING)]
|
||||
).explain()["cursor"],
|
||||
self.assertEqual(db.test.find({}).hint([("num", ASCENDING)])
|
||||
.explain()["cursor"],
|
||||
"BtreeCursor %s" % index)
|
||||
self.assertRaises(OperationFailure,
|
||||
db.test.find({"num": 17, "foo": 17}
|
||||
).hint([("foo", ASCENDING)]).explain)
|
||||
db.test.find({"num": 17, "foo": 17})
|
||||
.hint([("foo", ASCENDING)]).explain)
|
||||
|
||||
a = db.test.find({"num": 17})
|
||||
a.hint(index)
|
||||
@ -178,7 +179,8 @@ class TestCursor(unittest.TestCase):
|
||||
self.assertRaises(TypeError, db.test.find().sort, "hello")
|
||||
self.assertRaises(ValueError, db.test.find().sort, [])
|
||||
self.assertRaises(TypeError, db.test.find().sort, [], ASCENDING)
|
||||
self.assertRaises(TypeError, db.test.find().sort, [("hello", DESCENDING)], DESCENDING)
|
||||
self.assertRaises(TypeError, db.test.find().sort,
|
||||
[("hello", DESCENDING)], DESCENDING)
|
||||
self.assertRaises(TypeError, db.test.find().sort, "hello", "world")
|
||||
|
||||
db.test.remove({})
|
||||
@ -200,7 +202,8 @@ class TestCursor(unittest.TestCase):
|
||||
self.assertEqual(desc, expect)
|
||||
desc = [i["x"] for i in db.test.find().sort([("x", DESCENDING)])]
|
||||
self.assertEqual(desc, expect)
|
||||
desc = [i["x"] for i in db.test.find().sort("x", ASCENDING).sort("x", DESCENDING)]
|
||||
desc = [i["x"] for i in
|
||||
db.test.find().sort("x", ASCENDING).sort("x", DESCENDING)]
|
||||
self.assertEqual(desc, expect)
|
||||
|
||||
expected = [(1, 5), (2, 5), (0, 3), (7, 3), (9, 2), (2, 1), (3, 1)]
|
||||
@ -211,8 +214,9 @@ class TestCursor(unittest.TestCase):
|
||||
for (a, b) in shuffled:
|
||||
db.test.save({"a": a, "b": b})
|
||||
|
||||
result = [(i["a"], i["b"]) for i in db.test.find().sort([("b", DESCENDING),
|
||||
("a", ASCENDING)])]
|
||||
result = [(i["a"], i["b"]) for i in
|
||||
db.test.find().sort([("b", DESCENDING),
|
||||
("a", ASCENDING)])]
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
a = db.test.find()
|
||||
@ -259,19 +263,24 @@ class TestCursor(unittest.TestCase):
|
||||
db.test.save({"x": i})
|
||||
|
||||
self.assertEqual(3, len(list(db.test.find().where('this.x < 3'))))
|
||||
self.assertEqual(3, len(list(db.test.find().where(Code('this.x < 3')))))
|
||||
self.assertEqual(3, len(list(db.test.find().where(Code('this.x < i', {"i": 3})))))
|
||||
self.assertEqual(3,
|
||||
len(list(db.test.find().where(Code('this.x < 3')))))
|
||||
self.assertEqual(3, len(list(db.test.find().where(Code('this.x < i',
|
||||
{"i": 3})))))
|
||||
self.assertEqual(10, len(list(db.test.find())))
|
||||
|
||||
self.assertEqual(3, db.test.find().where('this.x < 3').count())
|
||||
self.assertEqual(10, db.test.find().count())
|
||||
self.assertEqual(3, db.test.find().where(u'this.x < 3').count())
|
||||
self.assertEqual([0, 1, 2],
|
||||
[a["x"] for a in db.test.find().where('this.x < 3')])
|
||||
[a["x"] for a in
|
||||
db.test.find().where('this.x < 3')])
|
||||
self.assertEqual([],
|
||||
[a["x"] for a in db.test.find({"x": 5}).where('this.x < 3')])
|
||||
[a["x"] for a in
|
||||
db.test.find({"x": 5}).where('this.x < 3')])
|
||||
self.assertEqual([5],
|
||||
[a["x"] for a in db.test.find({"x": 5}).where('this.x > 3')])
|
||||
[a["x"] for a in
|
||||
db.test.find({"x": 5}).where('this.x > 3')])
|
||||
|
||||
cursor = db.test.find().where('this.x < 3').where('this.x > 7')
|
||||
self.assertEqual([8, 9], [a["x"] for a in cursor])
|
||||
@ -292,40 +301,53 @@ class TestCursor(unittest.TestCase):
|
||||
for i in range(10000):
|
||||
db.test.insert({"i": i})
|
||||
|
||||
self.assertEqual(client_cursors, db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location, db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
self.assertEqual(client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
for _ in range(10):
|
||||
db.test.find_one()
|
||||
|
||||
self.assertEqual(client_cursors, db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location, db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
self.assertEqual(client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
for _ in range(10):
|
||||
for x in db.test.find():
|
||||
break
|
||||
|
||||
self.assertEqual(client_cursors, db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location, db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
self.assertEqual(client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
a = db.test.find()
|
||||
for x in a:
|
||||
break
|
||||
|
||||
self.assertNotEqual(client_cursors, db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertNotEqual(by_location, db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
self.assertNotEqual(
|
||||
client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertNotEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
del a
|
||||
|
||||
self.assertEqual(client_cursors, db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location, db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
self.assertEqual(client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
a = db.test.find().limit(10)
|
||||
for x in a:
|
||||
break
|
||||
|
||||
self.assertEqual(client_cursors, db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location, db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
self.assertEqual(client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -21,7 +21,8 @@ import datetime
|
||||
import sys
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from pymongo.errors import InvalidName, InvalidOperation, CollectionInvalid, OperationFailure
|
||||
from pymongo.errors import InvalidName, InvalidOperation
|
||||
from pymongo.errors import CollectionInvalid, OperationFailure
|
||||
from pymongo.son import SON
|
||||
from pymongo.objectid import ObjectId
|
||||
from pymongo.database import Database
|
||||
@ -33,7 +34,9 @@ from pymongo.code import Code
|
||||
from pymongo.son_manipulator import AutoReference, NamespaceInjector
|
||||
from test_connection import get_connection
|
||||
|
||||
|
||||
class TestDatabase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.connection = get_connection()
|
||||
|
||||
@ -43,8 +46,10 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual("name", Database(self.connection, "name").name())
|
||||
|
||||
def test_cmp(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.connection, "test"),
|
||||
Database(self.connection, "mike"))
|
||||
self.assertEqual(Database(self.connection, "test"),
|
||||
Database(self.connection, "test"))
|
||||
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(Database(self.connection, "pymongo_test")),
|
||||
@ -123,8 +128,10 @@ class TestDatabase(unittest.TestCase):
|
||||
|
||||
db.test.save({"dummy": u"object"})
|
||||
|
||||
self.assertRaises(OperationFailure, db.validate_collection, "test.doesnotexist")
|
||||
self.assertRaises(OperationFailure, db.validate_collection, db.test.doesnotexist)
|
||||
self.assertRaises(OperationFailure, db.validate_collection,
|
||||
"test.doesnotexist")
|
||||
self.assertRaises(OperationFailure, db.validate_collection,
|
||||
db.test.doesnotexist)
|
||||
|
||||
self.assert_(db.validate_collection("test"))
|
||||
self.assert_(db.validate_collection(db.test))
|
||||
@ -214,14 +221,19 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertRaises(TypeError, db._password_digest, True)
|
||||
self.assertRaises(TypeError, db._password_digest, None)
|
||||
|
||||
self.assert_(isinstance(db._password_digest("mike", "password"), types.UnicodeType))
|
||||
self.assertEqual(db._password_digest("mike", "password"), u"cd7e45b3b2767dc2fa9b6b548457ed00")
|
||||
self.assertEqual(db._password_digest("mike", "password"), db._password_digest(u"mike", u"password"))
|
||||
self.assert_(isinstance(db._password_digest("mike", "password"),
|
||||
types.UnicodeType))
|
||||
self.assertEqual(db._password_digest("mike", "password"),
|
||||
u"cd7e45b3b2767dc2fa9b6b548457ed00")
|
||||
self.assertEqual(db._password_digest("mike", "password"),
|
||||
db._password_digest(u"mike", u"password"))
|
||||
|
||||
def test_authenticate(self):
|
||||
db = self.connection.pymongo_test
|
||||
db.system.users.remove({})
|
||||
db.system.users.insert({"user": u"mike", "pwd": db._password_digest("mike", "password")})
|
||||
db.system.users.insert({"user": u"mike",
|
||||
"pwd": db._password_digest("mike",
|
||||
"password")})
|
||||
|
||||
self.assertRaises(TypeError, db.authenticate, 5, "password")
|
||||
self.assertRaises(TypeError, db.authenticate, "mike", 5)
|
||||
@ -276,7 +288,8 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual(3, db.eval("function (x) {return x;}", 3))
|
||||
self.assertEqual(3, db.eval(u"function (x) {return x;}", 3))
|
||||
|
||||
self.assertEqual(None, db.eval("function (x) {db.test.save({y:x});}", 5))
|
||||
self.assertEqual(None,
|
||||
db.eval("function (x) {db.test.save({y:x});}", 5))
|
||||
self.assertEqual(db.test.find_one()["y"], 5)
|
||||
|
||||
self.assertEqual(5, db.eval("function (x, y) {return x + y;}", 2, 3))
|
||||
@ -284,7 +297,8 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual(5, db.eval("2 + 3;"))
|
||||
|
||||
self.assertEqual(5, db.eval(Code("2 + 3;")))
|
||||
#self.assertRaises( OperationFailure , db.eval , (Code("return i;")) ) TODO: turn this back on when SM is the default
|
||||
# TODO turn this back on when SM is the default
|
||||
#self.assertRaises( OperationFailure , db.eval , (Code("return i;")) )
|
||||
self.assertEqual(2, db.eval(Code("return i;", {"i": 2})))
|
||||
self.assertEqual(5, db.eval(Code("i + 3;", {"i": 2})))
|
||||
|
||||
|
||||
@ -22,7 +22,9 @@ sys.path[0:0] = [""]
|
||||
from pymongo.objectid import ObjectId
|
||||
from pymongo.dbref import DBRef
|
||||
|
||||
|
||||
class TestDBRef(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
@ -40,8 +42,10 @@ class TestDBRef(unittest.TestCase):
|
||||
|
||||
def test_read_only(self):
|
||||
a = DBRef("coll", ObjectId())
|
||||
|
||||
def foo():
|
||||
a.collection = "blah"
|
||||
|
||||
def bar():
|
||||
a.id = "aoeu"
|
||||
|
||||
@ -51,13 +55,18 @@ class TestDBRef(unittest.TestCase):
|
||||
self.assertRaises(AttributeError, bar)
|
||||
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(DBRef("coll", ObjectId("123456789012"))), "DBRef(u'coll', ObjectId('123456789012'))")
|
||||
self.assertEqual(repr(DBRef(u"coll", ObjectId("123456789012"))), "DBRef(u'coll', ObjectId('123456789012'))")
|
||||
self.assertEqual(repr(DBRef("coll", ObjectId("123456789012"))),
|
||||
"DBRef(u'coll', ObjectId('123456789012'))")
|
||||
self.assertEqual(repr(DBRef(u"coll", ObjectId("123456789012"))),
|
||||
"DBRef(u'coll', ObjectId('123456789012'))")
|
||||
|
||||
def test_cmp(self):
|
||||
self.assertEqual(DBRef("coll", ObjectId("123456789012")), DBRef(u"coll", ObjectId("123456789012")))
|
||||
self.assertNotEqual(DBRef("coll", ObjectId("123456789012")), DBRef("col", ObjectId("123456789012")))
|
||||
self.assertNotEqual(DBRef("coll", ObjectId("123456789012")), DBRef("coll", ObjectId("123456789011")))
|
||||
self.assertEqual(DBRef("coll", ObjectId("123456789012")),
|
||||
DBRef(u"coll", ObjectId("123456789012")))
|
||||
self.assertNotEqual(DBRef("coll", ObjectId("123456789012")),
|
||||
DBRef("col", ObjectId("123456789012")))
|
||||
self.assertNotEqual(DBRef("coll", ObjectId("123456789012")),
|
||||
DBRef("coll", ObjectId("123456789011")))
|
||||
self.assertNotEqual(DBRef("coll", ObjectId("123456789012")), 4)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -24,7 +24,9 @@ import qcheck
|
||||
from test_connection import get_connection
|
||||
from gridfs.grid_file import GridFile
|
||||
|
||||
|
||||
class TestGridFile(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.db = get_connection().pymongo_test
|
||||
|
||||
@ -75,14 +77,16 @@ class TestGridFile(unittest.TestCase):
|
||||
|
||||
self.assertEqual(self.db.pymongo_test.files.find().count(), 0)
|
||||
self.assertEqual(self.db.pymongo_test.chunks.find().count(), 0)
|
||||
file = GridFile({"filename": "test"}, self.db, "w", collection="pymongo_test")
|
||||
file = GridFile({"filename": "test"}, self.db, "w",
|
||||
collection="pymongo_test")
|
||||
file.write("hello world")
|
||||
file.close()
|
||||
|
||||
self.assertEqual(self.db.pymongo_test.files.find().count(), 1)
|
||||
self.assertEqual(self.db.pymongo_test.chunks.find().count(), 1)
|
||||
|
||||
file = GridFile({"filename": "test"}, self.db, collection="pymongo_test")
|
||||
file = GridFile({"filename": "test"}, self.db,
|
||||
collection="pymongo_test")
|
||||
self.assertEqual(file.read(), "hello world")
|
||||
file.close()
|
||||
|
||||
@ -90,17 +94,20 @@ class TestGridFile(unittest.TestCase):
|
||||
self.assertEqual(file.md5, "5eb63bbbe01eeed093cb22bb8f5acdc3")
|
||||
|
||||
# make sure it's still there...
|
||||
file = GridFile({"filename": "test"}, self.db, collection="pymongo_test")
|
||||
file = GridFile({"filename": "test"}, self.db,
|
||||
collection="pymongo_test")
|
||||
self.assertEqual(file.read(), "hello world")
|
||||
file.close()
|
||||
|
||||
file = GridFile({"filename": "test"}, self.db, "w", collection="pymongo_test")
|
||||
file = GridFile({"filename": "test"}, self.db, "w",
|
||||
collection="pymongo_test")
|
||||
file.close()
|
||||
|
||||
self.assertEqual(self.db.pymongo_test.files.find().count(), 1)
|
||||
self.assertEqual(self.db.pymongo_test.chunks.find().count(), 0)
|
||||
|
||||
file = GridFile({"filename": "test"}, self.db, collection="pymongo_test")
|
||||
file = GridFile({"filename": "test"}, self.db,
|
||||
collection="pymongo_test")
|
||||
self.assertEqual(file.read(), "")
|
||||
file.close()
|
||||
|
||||
@ -281,13 +288,15 @@ class TestGridFile(unittest.TestCase):
|
||||
self.assertEqual(self.db.fs.files.find().count(), self.files)
|
||||
self.assertEqual(self.db.fs.chunks.find().count(), self.chunks)
|
||||
|
||||
self.assertEqual(GridFile({"filename": filename}, self.db).read(), data)
|
||||
self.assertEqual(GridFile({"filename": filename}, self.db).read(),
|
||||
data)
|
||||
|
||||
f = GridFile({"filename": filename}, self.db)
|
||||
self.assertEqual(f.read(10) + f.read(10), data)
|
||||
return True
|
||||
|
||||
qcheck.check_unittest(self, helper, qcheck.gen_string(qcheck.gen_range(0, 20)))
|
||||
qcheck.check_unittest(self, helper,
|
||||
qcheck.gen_string(qcheck.gen_range(0, 20)))
|
||||
|
||||
def test_modes(self):
|
||||
self.db.fs.files.remove({})
|
||||
|
||||
@ -22,7 +22,9 @@ sys.path[0:0] = [""]
|
||||
import gridfs
|
||||
from test_connection import get_connection
|
||||
|
||||
|
||||
class TestGridfs(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.db = get_connection().pymongo_test
|
||||
self.db.drop_collection("fs.files")
|
||||
@ -112,7 +114,8 @@ class TestGridfs(unittest.TestCase):
|
||||
f.close()
|
||||
|
||||
self.assertEqual([], self.fs.list())
|
||||
self.assertEqual(["mike", "test", "hello world"], self.fs.list("pymongo_test"))
|
||||
self.assertEqual(["mike", "test", "hello world"],
|
||||
self.fs.list("pymongo_test"))
|
||||
|
||||
def test_remove_alt_coll(self):
|
||||
f = self.fs.open("mike", "w", "pymongo_test")
|
||||
@ -126,12 +129,17 @@ class TestGridfs(unittest.TestCase):
|
||||
f.close()
|
||||
|
||||
self.fs.remove("test")
|
||||
self.assertEqual(["mike", "test", "hello world"], self.fs.list("pymongo_test"))
|
||||
self.assertEqual(["mike", "test", "hello world"],
|
||||
self.fs.list("pymongo_test"))
|
||||
self.fs.remove("test", "pymongo_test")
|
||||
self.assertEqual(["mike", "hello world"], self.fs.list("pymongo_test"))
|
||||
|
||||
self.assertEqual(self.fs.open("mike", collection="pymongo_test").read(), "hi")
|
||||
self.assertEqual(self.fs.open("hello world", collection="pymongo_test").read(), "fly")
|
||||
self.assertEqual(self.fs.open("mike",
|
||||
collection="pymongo_test").read(),
|
||||
"hi")
|
||||
self.assertEqual(self.fs.open("hello world",
|
||||
collection="pymongo_test").read(),
|
||||
"fly")
|
||||
|
||||
self.fs.remove({}, "pymongo_test")
|
||||
|
||||
|
||||
@ -27,7 +27,9 @@ from pymongo.database import Database
|
||||
from pymongo.connection import Connection
|
||||
from pymongo.master_slave_connection import MasterSlaveConnection
|
||||
|
||||
|
||||
class TestMasterSlaveConnection(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
host = os.environ.get("DB_IP", "localhost")
|
||||
self.master = Connection(host, int(os.environ.get("DB_PORT", 27017)))
|
||||
@ -35,13 +37,15 @@ class TestMasterSlaveConnection(unittest.TestCase):
|
||||
self.slaves = []
|
||||
try:
|
||||
self.slaves.append(Connection(os.environ.get("DB_IP2", host),
|
||||
int(os.environ.get("DB_PORT2", 27018))))
|
||||
int(os.environ.get("DB_PORT2",
|
||||
27018))))
|
||||
except ConnectionFailure:
|
||||
pass
|
||||
|
||||
try:
|
||||
self.slaves.append(Connection(os.environ.get("DB_IP3", host),
|
||||
int(os.environ.get("DB_PORT3", 27019))))
|
||||
int(os.environ.get("DB_PORT3",
|
||||
27019))))
|
||||
except ConnectionFailure:
|
||||
pass
|
||||
|
||||
@ -55,9 +59,11 @@ class TestMasterSlaveConnection(unittest.TestCase):
|
||||
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(self.connection),
|
||||
"MasterSlaveConnection(%r, %r)" % (self.master, self.slaves))
|
||||
"MasterSlaveConnection(%r, %r)" %
|
||||
(self.master, self.slaves))
|
||||
|
||||
def test_get_db(self):
|
||||
|
||||
def make_db(base, name):
|
||||
return base[name]
|
||||
|
||||
@ -70,7 +76,8 @@ class TestMasterSlaveConnection(unittest.TestCase):
|
||||
|
||||
self.assert_(isinstance(self.connection.test, Database))
|
||||
self.assertEqual(self.connection.test, self.connection["test"])
|
||||
self.assertEqual(self.connection.test, Database(self.connection, "test"))
|
||||
self.assertEqual(self.connection.test, Database(self.connection,
|
||||
"test"))
|
||||
|
||||
def test_database_names(self):
|
||||
self.connection.pymongo_test.test.save({"dummy": u"object"})
|
||||
@ -99,6 +106,7 @@ class TestMasterSlaveConnection(unittest.TestCase):
|
||||
self.assert_("pymongo_test" not in dbs)
|
||||
|
||||
def test_iteration(self):
|
||||
|
||||
def iterate():
|
||||
[a for a in self.connection]
|
||||
|
||||
@ -167,12 +175,14 @@ class TestMasterSlaveConnection(unittest.TestCase):
|
||||
self.failIf(count)
|
||||
|
||||
def test_kill_cursors(self):
|
||||
|
||||
def cursor_count():
|
||||
count = 0
|
||||
res = self.connection.master.test_pymongo._command({"cursorInfo":1})
|
||||
res = self.connection.master.test_pymongo._command({
|
||||
"cursorInfo": 1})
|
||||
count += res["clientCursors_size"]
|
||||
for slave in self.connection.slaves:
|
||||
res = slave.test_pymongo._command({"cursorInfo":1})
|
||||
res = slave.test_pymongo._command({"cursorInfo": 1})
|
||||
count += res["clientCursors_size"]
|
||||
return count
|
||||
|
||||
|
||||
@ -21,7 +21,9 @@ sys.path[0:0] = [""]
|
||||
from pymongo.objectid import ObjectId
|
||||
from pymongo.errors import InvalidId
|
||||
|
||||
|
||||
class TestObjectId(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
@ -40,7 +42,8 @@ class TestObjectId(unittest.TestCase):
|
||||
self.assert_(ObjectId(a))
|
||||
|
||||
def test_repr_str(self):
|
||||
self.assertEqual(repr(ObjectId("123456789012")), "ObjectId('123456789012')")
|
||||
self.assertEqual(repr(ObjectId("123456789012")),
|
||||
"ObjectId('123456789012')")
|
||||
self.assertEqual(str(ObjectId("123456789012")), "123456789012")
|
||||
|
||||
def test_cmp(self):
|
||||
|
||||
@ -32,9 +32,10 @@ from pymongo.connection import Connection
|
||||
|
||||
skip_tests = True
|
||||
|
||||
|
||||
class TestPaired(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
# logging.getLogger("pymongo.connection").setLevel(logging.DEBUG)
|
||||
left_host = os.environ.get("DB_IP", "localhost")
|
||||
left_port = int(os.environ.get("DB_PORT", 27017))
|
||||
self.left = (left_host, left_port)
|
||||
@ -45,7 +46,6 @@ class TestPaired(unittest.TestCase):
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
# logging.getLogger("pymongo.connection").setLevel(logging.NOTSET)
|
||||
|
||||
def skip(self):
|
||||
if skip_tests:
|
||||
@ -58,7 +58,8 @@ class TestPaired(unittest.TestCase):
|
||||
self.assertRaises(TypeError, Connection.paired, "localhost")
|
||||
self.assertRaises(TypeError, Connection.paired, None)
|
||||
self.assertRaises(TypeError, Connection.paired, 5, self.right)
|
||||
self.assertRaises(TypeError, Connection.paired, "localhost", self.right)
|
||||
self.assertRaises(TypeError, Connection.paired,
|
||||
"localhost", self.right)
|
||||
self.assertRaises(TypeError, Connection.paired, None, self.right)
|
||||
self.assertRaises(TypeError, Connection.paired, self.left, 5)
|
||||
self.assertRaises(TypeError, Connection.paired, self.left, "localhost")
|
||||
@ -66,7 +67,8 @@ class TestPaired(unittest.TestCase):
|
||||
|
||||
def test_connect(self):
|
||||
self.skip()
|
||||
self.assertRaises(ConnectionFailure, Connection.paired, self.bad, self.bad)
|
||||
self.assertRaises(ConnectionFailure, Connection.paired,
|
||||
self.bad, self.bad)
|
||||
|
||||
connection = Connection.paired(self.left, self.right)
|
||||
self.assert_(connection)
|
||||
@ -80,8 +82,10 @@ class TestPaired(unittest.TestCase):
|
||||
self.assertEqual(port, connection.port())
|
||||
|
||||
slave = self.left == (host, port) and self.right or self.left
|
||||
self.assertRaises(ConfigurationError, Connection.paired, slave, self.bad)
|
||||
self.assertRaises(ConfigurationError, Connection.paired, self.bad, slave)
|
||||
self.assertRaises(ConfigurationError, Connection.paired,
|
||||
slave, self.bad)
|
||||
self.assertRaises(ConfigurationError, Connection.paired,
|
||||
self.bad, slave)
|
||||
|
||||
def test_repr(self):
|
||||
self.skip()
|
||||
|
||||
@ -23,7 +23,9 @@ sys.path[0:0] = [""]
|
||||
|
||||
from pymongo.connection import Connection
|
||||
|
||||
|
||||
class TestPooling(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.host = os.environ.get("DB_IP", "localhost")
|
||||
self.port = int(os.environ.get("DB_PORT", 27017))
|
||||
@ -139,7 +141,9 @@ class TestPooling(unittest.TestCase):
|
||||
t = SaveAndFind(self.pooled_db)
|
||||
t.start()
|
||||
|
||||
|
||||
class SaveAndFind(threading.Thread):
|
||||
|
||||
def __init__(self, database):
|
||||
threading.Thread.__init__(self)
|
||||
self.database = database
|
||||
|
||||
@ -21,7 +21,9 @@ sys.path[0:0] = [""]
|
||||
|
||||
import pymongo
|
||||
|
||||
|
||||
class TestPyMongo(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.host = os.environ.get("DB_IP", "localhost")
|
||||
self.port = int(os.environ.get("DB_PORT", 27017))
|
||||
|
||||
@ -24,7 +24,9 @@ from pymongo.objectid import ObjectId
|
||||
from pymongo.dbref import DBRef
|
||||
from pymongo.son import SON
|
||||
|
||||
|
||||
class TestSON(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
@ -33,7 +35,9 @@ class TestSON(unittest.TestCase):
|
||||
a["hello"] = "world"
|
||||
a["mike"] = "awesome"
|
||||
a["hello_"] = "mike"
|
||||
self.assertEqual(a.items(), [("hello", "world"), ("mike", "awesome"), ("hello_", "mike")])
|
||||
self.assertEqual(a.items(), [("hello", "world"),
|
||||
("mike", "awesome"),
|
||||
("hello_", "mike")])
|
||||
|
||||
b = SON({"hello": "world"})
|
||||
self.assertEqual(b["hello"], "world")
|
||||
@ -86,16 +90,20 @@ class TestSON(unittest.TestCase):
|
||||
</twonk>
|
||||
"""
|
||||
self.assertEqual(SON.from_xml(smorgasbord),
|
||||
SON([(u"_id", ObjectId("\x28\x5A\x66\x49\x23\xB5\xFC\xD8\xEC\x00\x00\x00")),
|
||||
SON([(u"_id", ObjectId("\x28\x5A\x66\x49\x23\xB5\xFC"
|
||||
"\xD8\xEC\x00\x00\x00")),
|
||||
(u"the_answer", 42),
|
||||
(u"b", u"foo"),
|
||||
(u"c", True),
|
||||
(u"pi", 3.14159265358979),
|
||||
(u"an_array", [u"x", u"y", u"z", SON([(u"subobject", u"yup")])]),
|
||||
(u"now", datetime.datetime(1973, 11, 26, 6, 47, 32, 57000)),
|
||||
(u"an_array", [u"x", u"y", u"z",
|
||||
SON([(u"subobject", u"yup")])]),
|
||||
(u"now", datetime.datetime(1973, 11, 26, 6,
|
||||
47, 32, 57000)),
|
||||
(u"dbref",
|
||||
DBRef("namespace",
|
||||
ObjectId("\xCA\x5C\x67\x49\x6C\x01\xD8\x96\xF7\x01\x00\x00"))),
|
||||
ObjectId("\xCA\x5C\x67\x49\x6C\x01"
|
||||
"\xD8\x96\xF7\x01\x00\x00"))),
|
||||
(u"regex", re.compile(u"foobar", re.IGNORECASE)),
|
||||
(u"$where", "this is code"),
|
||||
(u"mynull", None),
|
||||
|
||||
@ -28,7 +28,9 @@ from pymongo.son_manipulator import NamespaceInjector, ObjectIdShuffler
|
||||
from pymongo.database import Database
|
||||
from test_connection import get_connection
|
||||
|
||||
|
||||
class TestSONManipulator(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.db = Database(get_connection(), "pymongo_test")
|
||||
|
||||
|
||||
@ -20,7 +20,9 @@ import time
|
||||
|
||||
from pymongo.thread_util import TimeoutableLock
|
||||
|
||||
|
||||
class AcquireWithTimeout(threading.Thread):
|
||||
|
||||
def __init__(self, lock, timeout):
|
||||
threading.Thread.__init__(self)
|
||||
self.__lock = lock
|
||||
@ -29,7 +31,9 @@ class AcquireWithTimeout(threading.Thread):
|
||||
def run(self):
|
||||
assert self.__lock.acquire(timeout=self.__timeout)
|
||||
|
||||
|
||||
class AcquireHoldRelease(threading.Thread):
|
||||
|
||||
def __init__(self, lock, hold_time, timeout, counter):
|
||||
threading.Thread.__init__(self)
|
||||
self.__lock = lock
|
||||
@ -44,7 +48,9 @@ class AcquireHoldRelease(threading.Thread):
|
||||
time.sleep(self.__hold_time)
|
||||
self.__lock.release()
|
||||
|
||||
|
||||
class TestTimeoutableLock(unittest.TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
lock = TimeoutableLock()
|
||||
|
||||
@ -94,7 +100,8 @@ class TestTimeoutableLock(unittest.TestCase):
|
||||
counter.assert_count = 0
|
||||
|
||||
def start_and_join(thread_count):
|
||||
threads = [AcquireHoldRelease(lock, 0.1, 0.3, counter) for _ in range(thread_count)]
|
||||
threads = [AcquireHoldRelease(lock, 0.1, 0.3, counter)
|
||||
for _ in range(thread_count)]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
for thread in threads:
|
||||
|
||||
@ -19,7 +19,9 @@ import threading
|
||||
|
||||
from test_connection import get_connection
|
||||
|
||||
|
||||
class SaveAndFind(threading.Thread):
|
||||
|
||||
def __init__(self, collection):
|
||||
threading.Thread.__init__(self)
|
||||
self.collection = collection
|
||||
@ -30,7 +32,9 @@ class SaveAndFind(threading.Thread):
|
||||
sum += document["x"]
|
||||
assert sum == 499500
|
||||
|
||||
|
||||
class TestThreads(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.db = get_connection().pymongo_test
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user