From 64202273e02ab5f0b99271771d4e8143b3599c3e Mon Sep 17 00:00:00 2001 From: Slam <3lnc.slam@gmail.com> Date: Wed, 24 Jan 2018 16:21:29 +0200 Subject: [PATCH] adds dynamic class-based `repr` (#713) --- elasticsearch/client/__init__.py | 4 ++-- test_elasticsearch/test_client/__init__.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/elasticsearch/client/__init__.py b/elasticsearch/client/__init__.py index ca903a9c..00b33c20 100644 --- a/elasticsearch/client/__init__.py +++ b/elasticsearch/client/__init__.py @@ -183,10 +183,10 @@ class Elasticsearch(object): try: # get a list of all connections cons = self.transport.hosts - # truncate to 10 if there are too many + # truncate to 5 if there are too many if len(cons) > 5: cons = cons[:5] + ['...'] - return '' % cons + return '<{cls}({cons})>'.format(cls=self.__class__.__name__, cons=cons) except: # probably operating on custom transport and connection_pool, ignore return super(Elasticsearch, self).__repr__() diff --git a/test_elasticsearch/test_client/__init__.py b/test_elasticsearch/test_client/__init__.py index 1bcf913b..25079663 100644 --- a/test_elasticsearch/test_client/__init__.py +++ b/test_elasticsearch/test_client/__init__.py @@ -75,12 +75,18 @@ class TestClient(ElasticsearchTestCase): def test_repr_contains_hosts(self): self.assertEquals('', repr(self.client)) + def test_repr_subclass(self): + class OtherElasticsearch(Elasticsearch): pass + self.assertEqual('', repr(OtherElasticsearch())) + def test_repr_contains_hosts_passed_in(self): self.assertIn("es.org", repr(Elasticsearch(['es.org:123']))) - def test_repr_truncates_host_to_10(self): - hosts = [{"host": "es" + str(i)} for i in range(20)] - self.assertNotIn("es5", repr(Elasticsearch(hosts))) + def test_repr_truncates_host_to_5(self): + hosts = [{"host": "es" + str(i)} for i in range(10)] + es = Elasticsearch(hosts) + self.assertNotIn("es5", repr(es)) + self.assertIn('...', repr(es)) def test_index_uses_post_if_id_is_empty(self): self.client.index(index='my-index', doc_type='test-doc', id='', body={})