make sure host name is a string. store host and port. __repr__
This commit is contained in:
parent
91ba6510f1
commit
5ff12097e6
18
mongo.py
18
mongo.py
@ -22,7 +22,7 @@ class Mongo(object):
|
||||
def __init__(self, host="localhost", port=27017):
|
||||
"""Open a new connection to the database at host:port.
|
||||
|
||||
Raises TypeError if host is not an instance of (str, unicode) or port is
|
||||
Raises TypeError if host is not an instance of string or port is
|
||||
not an instance of int. Raises ConnectionException if the connection
|
||||
cannot be made.
|
||||
|
||||
@ -31,11 +31,14 @@ class Mongo(object):
|
||||
connect to
|
||||
- `port` (optional): the port number on which to connect
|
||||
"""
|
||||
if not isinstance(host, types.StringTypes):
|
||||
raise TypeError("host must be an instance of (str, unicode)")
|
||||
if not isinstance(host, types.StringType):
|
||||
raise TypeError("host must be an instance of str")
|
||||
if not isinstance(port, types.IntType):
|
||||
raise TypeError("port must be an instance of int")
|
||||
|
||||
self.__host = host
|
||||
self.__port = port
|
||||
|
||||
try:
|
||||
self.__connection = socket.socket()
|
||||
self.__connection.connect((host, port))
|
||||
@ -43,12 +46,15 @@ class Mongo(object):
|
||||
raise ConnectionException("could not connect to %s:%s, got: %s" %
|
||||
(host, port, traceback.format_exc()))
|
||||
|
||||
def __repr__(self):
|
||||
return "Mongo(" + repr(self.__host) + ", " + repr(self.__port) + ")"
|
||||
|
||||
class TestMongo(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.host = os.environ.get("db_ip", "localhost")
|
||||
self.port = int(os.environ.get("db_port", 27017))
|
||||
|
||||
def testConnection(self):
|
||||
def test_connection(self):
|
||||
self.assertRaises(TypeError, Mongo, 1)
|
||||
self.assertRaises(TypeError, Mongo, 1.14)
|
||||
self.assertRaises(TypeError, Mongo, None)
|
||||
@ -63,5 +69,9 @@ class TestMongo(unittest.TestCase):
|
||||
|
||||
self.assertTrue(Mongo(self.host, self.port))
|
||||
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(Mongo(self.host, self.port)),
|
||||
"Mongo('%s', %s)" % (self.host, self.port))
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user