PYTHON-1088 - Fix various MongoClient __repr__ issues

This commit is contained in:
Bernie Hackett 2016-05-18 16:21:06 -07:00
parent 478726267b
commit 753ef14b7f
2 changed files with 16 additions and 4 deletions

View File

@ -1474,10 +1474,15 @@ class MongoClient(common.BaseObject):
return not self == other
def __repr__(self):
if len(self.__nodes) == 1:
return "MongoClient(%r, %r)" % self.address
nodes = self.__nodes.copy()
if not nodes:
# We haven't connected yet.
nodes = self.__seeds.copy()
if len(nodes) == 1:
address = iter(nodes).next()
return "MongoClient(%r, %r)" % address
else:
return "MongoClient(%r)" % ["%s:%d" % n for n in self.__nodes]
return "MongoClient(%r)" % ["%s:%d" % n for n in nodes]
def __getattr__(self, name):
"""Get a database by name.

View File

@ -214,7 +214,14 @@ class TestClient(unittest.TestCase, TestRequestMixin):
def test_repr(self):
# Making host a str avoids the 'u' prefix in Python 2, so the repr is
# the same in Python 2 and 3.
self.assertEqual(repr(MongoClient(str(host), port)),
c = MongoClient(str(host), port)
self.assertEqual(repr(c),
"MongoClient('%s', %d)" % (host, port))
c.close()
self.assertEqual(repr(c),
"MongoClient('%s', %d)" % (host, port))
c = MongoClient(str(host), port, connect=False)
self.assertEqual(repr(c),
"MongoClient('%s', %d)" % (host, port))
def test_getters(self):