0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Fixed backend configration tests

The latest version of the Elasticsearch client attempts to establish a
connection upon initialisation. This commit mocks out the backend.
This commit is contained in:
Karl Hobley 2017-12-15 11:03:11 +00:00 committed by Matt Westcott
parent 6308e6b816
commit ed83585b86
3 changed files with 204 additions and 99 deletions

View File

@ -705,18 +705,27 @@ class TestElasticsearch2MappingInheritance(TestCase):
self.assertDictEqual(document, expected_result)
@mock.patch('wagtail.search.backends.elasticsearch2.Elasticsearch')
class TestBackendConfiguration(TestCase):
def test_default_settings(self):
backend = Elasticsearch2SearchBackend(params={})
def test_default_settings(self, Elasticsearch):
Elasticsearch2SearchBackend(params={})
self.assertEqual(len(backend.hosts), 1)
self.assertEqual(backend.hosts[0]['host'], 'localhost')
self.assertEqual(backend.hosts[0]['port'], 9200)
self.assertEqual(backend.hosts[0]['use_ssl'], False)
Elasticsearch.assert_called_with(
hosts=[
{
'host': 'localhost',
'port': 9200,
'url_prefix': '',
'use_ssl': False,
'verify_certs': False,
'http_auth': None
}
],
timeout=10
)
def test_hosts(self):
# This tests that HOSTS goes to es_hosts
backend = Elasticsearch2SearchBackend(params={
def test_hosts(self, Elasticsearch):
Elasticsearch2SearchBackend(params={
'HOSTS': [
{
'host': '127.0.0.1',
@ -727,14 +736,21 @@ class TestBackendConfiguration(TestCase):
]
})
self.assertEqual(len(backend.hosts), 1)
self.assertEqual(backend.hosts[0]['host'], '127.0.0.1')
self.assertEqual(backend.hosts[0]['port'], 9300)
self.assertEqual(backend.hosts[0]['use_ssl'], True)
Elasticsearch.assert_called_with(
hosts=[
{
'host': '127.0.0.1',
'port': 9300,
'use_ssl': True,
'verify_certs': True,
}
],
timeout=10
)
def test_urls(self):
def test_urls(self, Elasticsearch):
# This test backwards compatibility with old URLS setting
backend = Elasticsearch2SearchBackend(params={
Elasticsearch2SearchBackend(params={
'URLS': [
'http://localhost:12345',
'https://127.0.0.1:54321',
@ -743,24 +759,43 @@ class TestBackendConfiguration(TestCase):
],
})
self.assertEqual(len(backend.hosts), 4)
self.assertEqual(backend.hosts[0]['host'], 'localhost')
self.assertEqual(backend.hosts[0]['port'], 12345)
self.assertEqual(backend.hosts[0]['use_ssl'], False)
self.assertEqual(backend.hosts[1]['host'], '127.0.0.1')
self.assertEqual(backend.hosts[1]['port'], 54321)
self.assertEqual(backend.hosts[1]['use_ssl'], True)
self.assertEqual(backend.hosts[2]['host'], 'elasticsearch.mysite.com')
self.assertEqual(backend.hosts[2]['port'], 80)
self.assertEqual(backend.hosts[2]['use_ssl'], False)
self.assertEqual(backend.hosts[2]['http_auth'], ('username', 'password'))
self.assertEqual(backend.hosts[3]['host'], 'elasticsearch.mysite.com')
self.assertEqual(backend.hosts[3]['port'], 443)
self.assertEqual(backend.hosts[3]['use_ssl'], True)
self.assertEqual(backend.hosts[3]['url_prefix'], '/hello')
Elasticsearch.assert_called_with(
hosts=[
{
'host': 'localhost',
'port': 12345,
'url_prefix': '',
'use_ssl': False,
'verify_certs': False,
'http_auth': None,
},
{
'host': '127.0.0.1',
'port': 54321,
'url_prefix': '',
'use_ssl': True,
'verify_certs': True,
'http_auth': None,
},
{
'host': 'elasticsearch.mysite.com',
'port': 80,
'url_prefix': '',
'use_ssl': False,
'verify_certs': False,
'http_auth': ('username', 'password')
},
{
'host': 'elasticsearch.mysite.com',
'port': 443,
'url_prefix': '/hello',
'use_ssl': True,
'verify_certs': True,
'http_auth': None,
},
],
timeout=10
)
class TestGetModelRoot(TestCase):

View File

@ -706,18 +706,27 @@ class TestElasticsearch5MappingInheritance(TestCase):
self.assertDictEqual(document, expected_result)
@mock.patch('wagtail.search.backends.elasticsearch2.Elasticsearch')
class TestBackendConfiguration(TestCase):
def test_default_settings(self):
backend = Elasticsearch5SearchBackend(params={})
def test_default_settings(self, Elasticsearch):
Elasticsearch5SearchBackend(params={})
self.assertEqual(len(backend.hosts), 1)
self.assertEqual(backend.hosts[0]['host'], 'localhost')
self.assertEqual(backend.hosts[0]['port'], 9200)
self.assertEqual(backend.hosts[0]['use_ssl'], False)
Elasticsearch.assert_called_with(
hosts=[
{
'host': 'localhost',
'port': 9200,
'url_prefix': '',
'use_ssl': False,
'verify_certs': False,
'http_auth': None
}
],
timeout=10
)
def test_hosts(self):
# This tests that HOSTS goes to es_hosts
backend = Elasticsearch5SearchBackend(params={
def test_hosts(self, Elasticsearch):
Elasticsearch5SearchBackend(params={
'HOSTS': [
{
'host': '127.0.0.1',
@ -728,14 +737,21 @@ class TestBackendConfiguration(TestCase):
]
})
self.assertEqual(len(backend.hosts), 1)
self.assertEqual(backend.hosts[0]['host'], '127.0.0.1')
self.assertEqual(backend.hosts[0]['port'], 9300)
self.assertEqual(backend.hosts[0]['use_ssl'], True)
Elasticsearch.assert_called_with(
hosts=[
{
'host': '127.0.0.1',
'port': 9300,
'use_ssl': True,
'verify_certs': True,
}
],
timeout=10
)
def test_urls(self):
def test_urls(self, Elasticsearch):
# This test backwards compatibility with old URLS setting
backend = Elasticsearch5SearchBackend(params={
Elasticsearch5SearchBackend(params={
'URLS': [
'http://localhost:12345',
'https://127.0.0.1:54321',
@ -744,21 +760,40 @@ class TestBackendConfiguration(TestCase):
],
})
self.assertEqual(len(backend.hosts), 4)
self.assertEqual(backend.hosts[0]['host'], 'localhost')
self.assertEqual(backend.hosts[0]['port'], 12345)
self.assertEqual(backend.hosts[0]['use_ssl'], False)
self.assertEqual(backend.hosts[1]['host'], '127.0.0.1')
self.assertEqual(backend.hosts[1]['port'], 54321)
self.assertEqual(backend.hosts[1]['use_ssl'], True)
self.assertEqual(backend.hosts[2]['host'], 'elasticsearch.mysite.com')
self.assertEqual(backend.hosts[2]['port'], 80)
self.assertEqual(backend.hosts[2]['use_ssl'], False)
self.assertEqual(backend.hosts[2]['http_auth'], ('username', 'password'))
self.assertEqual(backend.hosts[3]['host'], 'elasticsearch.mysite.com')
self.assertEqual(backend.hosts[3]['port'], 443)
self.assertEqual(backend.hosts[3]['use_ssl'], True)
self.assertEqual(backend.hosts[3]['url_prefix'], '/hello')
Elasticsearch.assert_called_with(
hosts=[
{
'host': 'localhost',
'port': 12345,
'url_prefix': '',
'use_ssl': False,
'verify_certs': False,
'http_auth': None,
},
{
'host': '127.0.0.1',
'port': 54321,
'url_prefix': '',
'use_ssl': True,
'verify_certs': True,
'http_auth': None,
},
{
'host': 'elasticsearch.mysite.com',
'port': 80,
'url_prefix': '',
'use_ssl': False,
'verify_certs': False,
'http_auth': ('username', 'password')
},
{
'host': 'elasticsearch.mysite.com',
'port': 443,
'url_prefix': '/hello',
'use_ssl': True,
'verify_certs': True,
'http_auth': None,
},
],
timeout=10
)

View File

@ -707,18 +707,27 @@ class TestElasticsearch6MappingInheritance(TestCase):
self.assertDictEqual(document, expected_result)
@mock.patch('wagtail.search.backends.elasticsearch2.Elasticsearch')
class TestBackendConfiguration(TestCase):
def test_default_settings(self):
backend = Elasticsearch6SearchBackend(params={})
def test_default_settings(self, Elasticsearch):
Elasticsearch6SearchBackend(params={})
self.assertEqual(len(backend.hosts), 1)
self.assertEqual(backend.hosts[0]['host'], 'localhost')
self.assertEqual(backend.hosts[0]['port'], 9200)
self.assertEqual(backend.hosts[0]['use_ssl'], False)
Elasticsearch.assert_called_with(
hosts=[
{
'host': 'localhost',
'port': 9200,
'url_prefix': '',
'use_ssl': False,
'verify_certs': False,
'http_auth': None
}
],
timeout=10
)
def test_hosts(self):
# This tests that HOSTS goes to es_hosts
backend = Elasticsearch6SearchBackend(params={
def test_hosts(self, Elasticsearch):
Elasticsearch6SearchBackend(params={
'HOSTS': [
{
'host': '127.0.0.1',
@ -729,14 +738,21 @@ class TestBackendConfiguration(TestCase):
]
})
self.assertEqual(len(backend.hosts), 1)
self.assertEqual(backend.hosts[0]['host'], '127.0.0.1')
self.assertEqual(backend.hosts[0]['port'], 9300)
self.assertEqual(backend.hosts[0]['use_ssl'], True)
Elasticsearch.assert_called_with(
hosts=[
{
'host': '127.0.0.1',
'port': 9300,
'use_ssl': True,
'verify_certs': True,
}
],
timeout=10
)
def test_urls(self):
def test_urls(self, Elasticsearch):
# This test backwards compatibility with old URLS setting
backend = Elasticsearch6SearchBackend(params={
Elasticsearch6SearchBackend(params={
'URLS': [
'http://localhost:12345',
'https://127.0.0.1:54321',
@ -745,21 +761,40 @@ class TestBackendConfiguration(TestCase):
],
})
self.assertEqual(len(backend.hosts), 4)
self.assertEqual(backend.hosts[0]['host'], 'localhost')
self.assertEqual(backend.hosts[0]['port'], 12345)
self.assertEqual(backend.hosts[0]['use_ssl'], False)
self.assertEqual(backend.hosts[1]['host'], '127.0.0.1')
self.assertEqual(backend.hosts[1]['port'], 54321)
self.assertEqual(backend.hosts[1]['use_ssl'], True)
self.assertEqual(backend.hosts[2]['host'], 'elasticsearch.mysite.com')
self.assertEqual(backend.hosts[2]['port'], 80)
self.assertEqual(backend.hosts[2]['use_ssl'], False)
self.assertEqual(backend.hosts[2]['http_auth'], ('username', 'password'))
self.assertEqual(backend.hosts[3]['host'], 'elasticsearch.mysite.com')
self.assertEqual(backend.hosts[3]['port'], 443)
self.assertEqual(backend.hosts[3]['use_ssl'], True)
self.assertEqual(backend.hosts[3]['url_prefix'], '/hello')
Elasticsearch.assert_called_with(
hosts=[
{
'host': 'localhost',
'port': 12345,
'url_prefix': '',
'use_ssl': False,
'verify_certs': False,
'http_auth': None,
},
{
'host': '127.0.0.1',
'port': 54321,
'url_prefix': '',
'use_ssl': True,
'verify_certs': True,
'http_auth': None,
},
{
'host': 'elasticsearch.mysite.com',
'port': 80,
'url_prefix': '',
'use_ssl': False,
'verify_certs': False,
'http_auth': ('username', 'password')
},
{
'host': 'elasticsearch.mysite.com',
'port': 443,
'url_prefix': '/hello',
'use_ssl': True,
'verify_certs': True,
'http_auth': None,
},
],
timeout=10
)