adds dynamic class-based repr (#713)

This commit is contained in:
Slam
2018-01-24 15:21:29 +01:00
committed by Honza Král
parent eaa67a100d
commit 64202273e0
2 changed files with 11 additions and 5 deletions
+2 -2
View File
@@ -183,10 +183,10 @@ class Elasticsearch(object):
try: try:
# get a list of all connections # get a list of all connections
cons = self.transport.hosts cons = self.transport.hosts
# truncate to 10 if there are too many # truncate to 5 if there are too many
if len(cons) > 5: if len(cons) > 5:
cons = cons[:5] + ['...'] cons = cons[:5] + ['...']
return '<Elasticsearch(%r)>' % cons return '<{cls}({cons})>'.format(cls=self.__class__.__name__, cons=cons)
except: except:
# probably operating on custom transport and connection_pool, ignore # probably operating on custom transport and connection_pool, ignore
return super(Elasticsearch, self).__repr__() return super(Elasticsearch, self).__repr__()
+9 -3
View File
@@ -75,12 +75,18 @@ class TestClient(ElasticsearchTestCase):
def test_repr_contains_hosts(self): def test_repr_contains_hosts(self):
self.assertEquals('<Elasticsearch([{}])>', repr(self.client)) self.assertEquals('<Elasticsearch([{}])>', repr(self.client))
def test_repr_subclass(self):
class OtherElasticsearch(Elasticsearch): pass
self.assertEqual('<OtherElasticsearch([{}])>', repr(OtherElasticsearch()))
def test_repr_contains_hosts_passed_in(self): def test_repr_contains_hosts_passed_in(self):
self.assertIn("es.org", repr(Elasticsearch(['es.org:123']))) self.assertIn("es.org", repr(Elasticsearch(['es.org:123'])))
def test_repr_truncates_host_to_10(self): def test_repr_truncates_host_to_5(self):
hosts = [{"host": "es" + str(i)} for i in range(20)] hosts = [{"host": "es" + str(i)} for i in range(10)]
self.assertNotIn("es5", repr(Elasticsearch(hosts))) es = Elasticsearch(hosts)
self.assertNotIn("es5", repr(es))
self.assertIn('...', repr(es))
def test_index_uses_post_if_id_is_empty(self): def test_index_uses_post_if_id_is_empty(self):
self.client.index(index='my-index', doc_type='test-doc', id='', body={}) self.client.index(index='my-index', doc_type='test-doc', id='', body={})