mongo-python-driver/test/test_connection.py
2009-12-16 11:37:07 -05:00

241 lines
8.0 KiB
Python

# Copyright 2009 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 the connection module."""
import unittest
import os
import warnings
import sys
sys.path[0:0] = [""]
from nose.plugins.skip import SkipTest
from pymongo.errors import ConnectionFailure, InvalidName, AutoReconnect
from pymongo.database import Database
from pymongo.connection import Connection
def get_connection(*args, **kwargs):
host = os.environ.get("DB_IP", "localhost")
port = int(os.environ.get("DB_PORT", 27017))
return Connection(host, port, *args, **kwargs)
class TestConnection(unittest.TestCase):
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, [])
self.assertRaises(TypeError, Connection, "localhost", "27017")
self.assertRaises(TypeError, Connection, "localhost", 1.14)
self.assertRaises(TypeError, Connection, "localhost", [])
def test_constants(self):
Connection.HOST = self.host
Connection.PORT = self.port
self.assert_(Connection())
Connection.HOST = "somedomainthatdoesntexist.org"
Connection.PORT = 123456789
self.assertRaises(ConnectionFailure, Connection)
self.assert_(Connection(self.host, self.port))
Connection.HOST = self.host
Connection.PORT = self.port
self.assert_(Connection())
def test_connect(self):
self.assertRaises(ConnectionFailure, Connection,
"somedomainthatdoesntexist.org")
self.assertRaises(ConnectionFailure, Connection, self.host, 123456789)
self.assert_(Connection(self.host, self.port))
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)
def test_get_db(self):
connection = Connection(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.assert_(isinstance(connection.test, Database))
self.assertEqual(connection.test, connection["test"])
self.assertEqual(connection.test, Database(connection, "test"))
def test_database_names(self):
connection = Connection(self.host, self.port)
connection.pymongo_test.test.save({"dummy": u"object"})
connection.pymongo_test_mike.test.save({"dummy": u"object"})
dbs = connection.database_names()
self.assert_("pymongo_test" in dbs)
self.assert_("pymongo_test_mike" in dbs)
def test_drop_database(self):
connection = Connection(self.host, self.port)
self.assertRaises(TypeError, connection.drop_database, 5)
self.assertRaises(TypeError, connection.drop_database, None)
connection.pymongo_test.test.save({"dummy": u"object"})
dbs = connection.database_names()
self.assert_("pymongo_test" in dbs)
connection.drop_database("pymongo_test")
dbs = connection.database_names()
self.assert_("pymongo_test" not in dbs)
connection.pymongo_test.test.save({"dummy": u"object"})
dbs = connection.database_names()
self.assert_("pymongo_test" in dbs)
connection.drop_database(connection.pymongo_test)
dbs = connection.database_names()
self.assert_("pymongo_test" not in dbs)
def test_iteration(self):
connection = Connection(self.host, self.port)
def iterate():
[a for a in connection]
self.assertRaises(TypeError, iterate)
# TODO this test is probably very dependent on the machine its running on
# due to timing issues, but I want to get something in here.
def test_low_network_timeout(self):
c = None
i = 0
n = 10
while c is None and i < n:
try:
c = Connection(self.host, self.port, network_timeout=0.0001)
except AutoReconnect:
i += 1
if i == n:
raise SkipTest()
coll = c.pymongo_test.test
for _ in range(1000):
try:
coll.find_one()
except AutoReconnect:
pass
except AssertionError:
self.fail()
# NOTE this probably doesn't all belong in this file, but it's easier in
# one place
def test_deprecated_method_for_attr(self):
c = Connection(self.host, self.port)
db = c.foo
coll = db.bar
warnings.simplefilter("error")
self.assertRaises(DeprecationWarning, c.host)
self.assertRaises(DeprecationWarning, c.port)
self.assertRaises(DeprecationWarning, db.connection)
self.assertRaises(DeprecationWarning, db.name)
self.assertRaises(DeprecationWarning, coll.full_name)
self.assertRaises(DeprecationWarning, coll.name)
self.assertRaises(DeprecationWarning, coll.database)
warnings.resetwarnings()
warnings.simplefilter("ignore")
self.assertEqual(c.host, c.host())
self.assertEqual(c.port, c.port())
self.assertEqual(db.connection, db.connection())
self.assertEqual(db.name, db.name())
self.assertEqual(coll.full_name, coll.full_name())
self.assertEqual(coll.name, coll.name())
self.assertEqual(coll.database, coll.database())
warnings.resetwarnings()
warnings.simplefilter("default")
self.assertEqual(self.host, c.host)
self.assertEqual(self.port, c.port)
self.assertEqual(c, db.connection)
self.assertEqual("foo", db.name)
self.assertEqual("foo.bar", coll.full_name)
self.assertEqual("bar", coll.name)
self.assertEqual(db, coll.database)
def test_disconnect(self):
c = Connection(self.host, self.port)
coll = c.foo.bar
c.disconnect()
c.disconnect()
coll.count()
c.disconnect()
c.disconnect()
coll.count()
# TODO come up with a different way to test `network_timeout`. This is just
# too sketchy.
#
# def test_socket_timeout(self):
# no_timeout = Connection(self.host, self.port)
# timeout = Connection(self.host, self.port, network_timeout=0.1)
# no_timeout.pymongo_test.drop_collection("test")
# no_timeout.pymongo_test.test.save({"x": 1})
# where_func = """function (doc) {
# var d = new Date().getTime() + 1000;
# var x = new Date().getTime();
# while (x < d) {
# x = new Date().getTime();
# }
# return true;
# }"""
# def get_x(db):
# return db.test.find().where(where_func).next()["x"]
# self.assertEqual(1, get_x(no_timeout.pymongo_test))
# self.assertRaises(ConnectionFailure, get_x, timeout.pymongo_test)
# self.assertEqual(1, no_timeout.pymongo_test.test.find().next()["x"])
if __name__ == "__main__":
unittest.main()