From 8edda0cca6db83866969808b6d4f3b34885794cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Wed, 12 Nov 2014 17:53:26 +0100 Subject: [PATCH] @leplatrem review. --- daybed/{indexer.py => indexer/__init__.py} | 44 ++----------------- daybed/indexer/utils.py | 51 ++++++++++++++++++++++ daybed/tests/test_indexer.py | 31 ++++++++----- 3 files changed, 73 insertions(+), 53 deletions(-) rename daybed/{indexer.py => indexer/__init__.py} (86%) create mode 100644 daybed/indexer/utils.py diff --git a/daybed/indexer.py b/daybed/indexer/__init__.py similarity index 86% rename from daybed/indexer.py rename to daybed/indexer/__init__.py index 0fb516e..84d00b3 100644 --- a/daybed/indexer.py +++ b/daybed/indexer/__init__.py @@ -4,7 +4,7 @@ from elasticsearch.exceptions import RequestError, ElasticsearchException from daybed import logger -from six.moves.urllib.parse import urlparse +from .utils import build_elasticsearch_hosts class SearchError(Exception): @@ -17,46 +17,8 @@ def __init__(self, *args): class ElasticSearchIndexer(object): def __init__(self, hosts, prefix): - self.built_hosts = [] - - for host in hosts: - arguments = urlparse(host) - - # If the argument is not an URL, let it go. - if not arguments.netloc: - self.built_hosts.append(host) - continue - - http_auth = None - use_ssl = False - port = 80 - - netloc = arguments.netloc.split('@') - - if len(netloc) == 2: - http_auth = netloc[0] - netloc = netloc[1] - else: - netloc = arguments.netloc - - if ':' in netloc: - hostname, port = netloc.split(':') - if arguments.scheme == 'https': - use_ssl = True - else: - hostname = netloc - if arguments.scheme == 'https': - use_ssl = True - port = 443 - - self.built_hosts.append({ - 'host': hostname, - 'port': int(port), - 'use_ssl': use_ssl, - 'http_auth': http_auth - }) - - self.client = elasticsearch.Elasticsearch(self.built_hosts) + built_hosts = build_elasticsearch_hosts(hosts) + self.client = elasticsearch.Elasticsearch(built_hosts) self.prefix = lambda x: u'%s_%s' % (prefix, x) def search(self, model_id, query, params): diff --git a/daybed/indexer/utils.py b/daybed/indexer/utils.py new file mode 100644 index 0000000..165dba8 --- /dev/null +++ b/daybed/indexer/utils.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +from six.moves.urllib.parse import urlparse + + +def build_elasticsearch_hosts(hosts): + """Take a list of hosts and build an Elasticsearch parameter list. + + >>> build_elasticsearch_hosts(['https://admin:password@localhost']) + [{'use_ssl': True, 'host': 'localhost', 'http_auth': 'admin:password', 'port': 443}] + + """ + built_hosts = [] + + for host in hosts: + arguments = urlparse(host) + + # If the argument is not an URL, let it go. + if not arguments.netloc: + built_hosts.append(host) + continue + + http_auth = None + use_ssl = False + port = 80 + + netloc = arguments.netloc.split('@') + + if len(netloc) == 2: + http_auth = netloc[0] + netloc = netloc[1] + else: + netloc = arguments.netloc + + if ':' in netloc: + hostname, port = netloc.split(':') + if arguments.scheme == 'https': + use_ssl = True + else: + hostname = netloc + if arguments.scheme == 'https': + use_ssl = True + port = 443 + + built_hosts.append({ + 'host': hostname, + 'port': int(port), + 'use_ssl': use_ssl, + 'http_auth': http_auth + }) + + return built_hosts diff --git a/daybed/tests/test_indexer.py b/daybed/tests/test_indexer.py index 45f0342..2929942 100644 --- a/daybed/tests/test_indexer.py +++ b/daybed/tests/test_indexer.py @@ -5,6 +5,7 @@ from daybed.schemas import registry from daybed import indexer +from daybed.indexer.utils import build_elasticsearch_hosts from .support import BaseWebTest, unittest from .test_views import MODEL_DEFINITION, MODEL_RECORD @@ -12,44 +13,50 @@ class ConfigurationTest(unittest.TestCase): - def test_default_config(self): + @mock.patch('elasticsearch.Elasticsearch') + def test_default_config(self, elasticsearch_mock): new_indexer = indexer.ElasticSearchIndexer(['localhost:9200'], 'daybed_') - self.assertEqual(new_indexer.built_hosts, ['localhost:9200']) + elasticsearch_mock.assert_called_with(['localhost:9200']) - def test_default_port(self): + @mock.patch('elasticsearch.Elasticsearch') + def test_default_port(self, elasticsearch_mock): new_indexer = indexer.ElasticSearchIndexer(['localhost'], 'daybed_') - self.assertEqual(new_indexer.built_hosts, ['localhost']) + elasticsearch_mock.assert_called_with(['localhost']) - def test_http_url(self): + @mock.patch('elasticsearch.Elasticsearch') + def test_http_url(self, elasticsearch_mock): new_indexer = indexer.ElasticSearchIndexer(['http://localhost:9200'], 'daybed_') - self.assertEqual(new_indexer.built_hosts, [{ + elasticsearch_mock.assert_called_with([{ 'host': 'localhost', 'port': 9200, 'use_ssl': False, 'http_auth': None }]) - def test_https_url(self): + @mock.patch('elasticsearch.Elasticsearch') + def test_https_url(self, elasticsearch_mock): new_indexer = indexer.ElasticSearchIndexer(['https://localhost'], 'daybed_') - self.assertEqual(new_indexer.built_hosts, [{ + elasticsearch_mock.assert_called_with([{ 'host': 'localhost', 'port': 443, 'use_ssl': True, 'http_auth': None }]) - def test_http_url_with_basic_auth(self): + @mock.patch('elasticsearch.Elasticsearch') + def test_http_url_with_basic_auth(self, elasticsearch_mock): new_indexer = indexer.ElasticSearchIndexer(['http://admin:password@localhost'], 'daybed_') - self.assertEqual(new_indexer.built_hosts, [{ + elasticsearch_mock.assert_called_with([{ 'host': 'localhost', 'port': 80, 'use_ssl': False, 'http_auth': 'admin:password' }]) - def test_https_url_with_basic_auth(self): + @mock.patch('elasticsearch.Elasticsearch') + def test_https_url_with_basic_auth(self, elasticsearch_mock): new_indexer = indexer.ElasticSearchIndexer(['https://admin:password@localhost'], 'daybed_') - self.assertEqual(new_indexer.built_hosts, [{ + elasticsearch_mock.assert_called_with([{ 'host': 'localhost', 'port': 443, 'use_ssl': True,