Add in __repr__ to client to simplify debugging

This commit is contained in:
Honza Král
2013-10-22 01:22:08 +02:00
parent 78d5a0b5a8
commit 2bd421f29d
3 changed files with 30 additions and 1 deletions
+12
View File
@@ -79,6 +79,18 @@ class Elasticsearch(object):
self.indices = IndicesClient(weakref.proxy(self))
self.cluster = ClusterClient(weakref.proxy(self))
def __repr__(self):
try:
# get a lost of all connections
cons = self.transport.hosts
# truncate to 10 if there are too many
if len(cons) > 5:
cons = cons[:5] + ['...']
return '<Elasticsearch(%r)>' % cons
except:
# probably operating on custom transport and connection_pool, ignore
return super(Elasticsearch, self).__repr__()
def _bulk_body(self, body):
# if not passed in a string, serialize items and join by newline
if not isinstance(body, (type(''), type(u''))):
+1
View File
@@ -9,6 +9,7 @@ from elasticsearch import Elasticsearch
class DummyTransport(object):
def __init__(self, hosts, responses=None, **kwargs):
self.hosts = hosts
self.responses = responses
self.call_count = 0
self.calls = defaultdict(list)
+17 -1
View File
@@ -1,6 +1,6 @@
from mock import patch
from elasticsearch.client import _normalize_hosts
from elasticsearch.client import _normalize_hosts, Elasticsearch
from ..test_cases import TestCase, ElasticsearchTestCase
@@ -41,3 +41,19 @@ class TestClient(ElasticsearchTestCase):
self.client.search(index='i', doc_type='t', from_=10)
calls = self.assert_url_called('GET', '/i/t/_search')
self.assertEquals([({'from': '10'}, None)], calls)
def test_repr_contains_hosts(self):
self.assertEquals('<Elasticsearch([{}])>', repr(self.client))
def test_repr_contains_hosts_passed_in(self):
self.assertEquals(
'<Elasticsearch([%r])>' % {"host": "es.org", "port": 123},
repr(Elasticsearch(['es.org:123']))
)
def test_repr_truncates_host_to_10(self):
hosts = [{"host": "es" + str(i)} for i in range(20)]
self.assertEquals(
'<Elasticsearch(%r)>' % [{'host': 'es0'}, {'host': 'es1'}, {'host': 'es2'}, {'host': 'es3'}, {'host': 'es4'}, '...'],
repr(Elasticsearch(hosts))
)