From 126761db5165fe9a5d7241ba14af91bbadc9d6e6 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Mon, 9 Nov 2015 12:04:50 +0000 Subject: [PATCH 1/5] Added --postgres argument to runtests.py Makes running the tests against PostgreSQL a bit easier --- docs/contributing/developing.rst | 20 +++++++++++++------- runtests.py | 12 +++++++++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/docs/contributing/developing.rst b/docs/contributing/developing.rst index a2e41d4d1a..3ac0a68349 100644 --- a/docs/contributing/developing.rst +++ b/docs/contributing/developing.rst @@ -71,17 +71,23 @@ an argument to ``runtests.py``:: **Testing against PostgreSQL** -By default, Wagtail tests against SQLite. If you need to test against a -different database, set the ``DATABASE_ENGINE`` environment variable to the -name of the Django database backend to test against:: +By default, Wagtail tests against SQLite. You can switch to using PostgreSQL by +using the ``--postgres`` argument:: - DATABASE_ENGINE=django.db.backends.postgresql_psycopg2 python runtests.py - -This will create a new database called ``test_wagtail`` in PostgreSQL and run -the tests against it. + python runtests.py --postgres If you need to use a different user, password or host. Use the ``PGUSER``, ``PGPASSWORD`` and ``PGHOST`` environment variables. +**Testing against a different database** + +If you need to test against a different database, set the ``DATABASE_ENGINE`` +environment variable to the name of the Django database backend to test against:: + + DATABASE_ENGINE=django.db.backends.mysql python runtests.py + +This will create a new database called ``test_wagtail`` in MySQL and run +the tests against it. + **Testing Elasticsearch** To test Elasticsearch, you need to have the ``elasticsearch`` package installed. diff --git a/runtests.py b/runtests.py index a1925f907d..1a674c29a1 100755 --- a/runtests.py +++ b/runtests.py @@ -1,4 +1,5 @@ #!/usr/bin/env python + import sys import os import shutil @@ -6,8 +7,6 @@ import warnings from django.core.management import execute_from_command_line -from wagtail.tests.settings import STATIC_ROOT, MEDIA_ROOT - os.environ['DJANGO_SETTINGS_MODULE'] = 'wagtail.tests.settings' @@ -17,10 +16,17 @@ def runtests(): warnings.simplefilter('default', DeprecationWarning) warnings.simplefilter('default', PendingDeprecationWarning) - argv = sys.argv[:1] + ['test'] + sys.argv[1:] + args = sys.argv[1:] + + if '--postgres' in args: + os.environ['DATABASE_ENGINE'] = 'django.db.backends.postgresql_psycopg2' + args.remove('--postgres') + + argv = sys.argv[:1] + ['test'] + args try: execute_from_command_line(argv) finally: + from wagtail.tests.settings import STATIC_ROOT, MEDIA_ROOT shutil.rmtree(STATIC_ROOT, ignore_errors=True) shutil.rmtree(MEDIA_ROOT, ignore_errors=True) From 7459329d93026e02aa6888ffaf4fea916ba9deef Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Mon, 9 Nov 2015 12:13:07 +0000 Subject: [PATCH 2/5] Added --elasticsearch argument to runtests.py --- docs/contributing/developing.rst | 15 +++++++++------ runtests.py | 4 ++++ wagtail/tests/settings.py | 13 ++----------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/docs/contributing/developing.rst b/docs/contributing/developing.rst index 3ac0a68349..01e5447961 100644 --- a/docs/contributing/developing.rst +++ b/docs/contributing/developing.rst @@ -92,16 +92,19 @@ the tests against it. To test Elasticsearch, you need to have the ``elasticsearch`` package installed. -Once installed, Wagtail will attempt to connect to a local instance of -Elasticsearch (``http://localhost:9200``) and use the index ``test_wagtail``. +Once installed, you can test Wagtail with Elasticsearch by passing the +``--elasticsearch`` argument to ``runtests.py``:: + + python runtests.py --elasticsearch + + +Wagtail will attempt to connect to a local instance of Elasticsearch +(``http://localhost:9200``) and use the index ``test_wagtail``. If your Elasticsearch instance is located somewhere else, you can set the ``ELASTICSEARCH_URL`` environment variable to point to its location:: - ELASTICSEARCH_URL=http://my-elasticsearch-instance:9200 python runtests.py - -If you no longer want Wagtail to test against Elasticsearch, uninstall the -``elasticsearch`` package. + ELASTICSEARCH_URL=http://my-elasticsearch-instance:9200 python runtests.py --elasticsearch Compiling static assets ~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/runtests.py b/runtests.py index 1a674c29a1..2e9a9de231 100755 --- a/runtests.py +++ b/runtests.py @@ -22,6 +22,10 @@ def runtests(): os.environ['DATABASE_ENGINE'] = 'django.db.backends.postgresql_psycopg2' args.remove('--postgres') + if '--elasticsearch' in args: + os.environ.setdefault('ELASTICSEARCH_URL', 'http://localhost:9200') + args.remove('--elasticsearch') + argv = sys.argv[:1] + ['test'] + args try: execute_from_command_line(argv) diff --git a/wagtail/tests/settings.py b/wagtail/tests/settings.py index 9af3be45eb..cf4f36055d 100644 --- a/wagtail/tests/settings.py +++ b/wagtail/tests/settings.py @@ -157,23 +157,14 @@ WAGTAILSEARCH_BACKENDS = { AUTH_USER_MODEL = 'customuser.CustomUser' -try: - # Only add Elasticsearch backend if the elasticsearch-py library is installed - import elasticsearch # noqa - - # Import succeeded, add an Elasticsearch backend +if 'ELASTICSEARCH_URL' in os.environ: WAGTAILSEARCH_BACKENDS['elasticsearch'] = { 'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch', + 'URLS': [os.environ['ELASTICSEARCH_URL']], 'TIMEOUT': 10, 'max_retries': 1, 'AUTO_UPDATE': False, } - if 'ELASTICSEARCH_URL' in os.environ: - WAGTAILSEARCH_BACKENDS['elasticsearch']['URLS'] = [os.environ['ELASTICSEARCH_URL']] - -except ImportError: - pass - WAGTAIL_SITE_NAME = "Test Site" From 83d3aaeb2f4e71ba3e570c0594069e12a37cb1b1 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Mon, 9 Nov 2015 15:57:01 +0000 Subject: [PATCH 3/5] Added --elasticsearch flag to travis/tox configuration --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 5983171644..d890cba8f3 100644 --- a/tox.ini +++ b/tox.ini @@ -9,7 +9,7 @@ ignore = E501,E128,E261,E302,E303,E124,E126 exclude = wagtail/project_template/* [testenv] -commands=coverage run runtests.py +commands=coverage run runtests.py --elasticsearch basepython = py27: python2.7 From 3b2e54bc7c7df3aee805ab1939c24a9f8898da2e Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 19 Nov 2015 16:00:54 +0000 Subject: [PATCH 4/5] Make elasticsearch-py a dev requirement The Elasticsearch tests now depend on an environment variable being set so we can safely install elasticsearch-py without Wagtail automatically assuming Elasticsearch is installed and running the tests. This makes developer setup slightly easier, but also opens up the possibility for running some of the ES tests that don't depend on a running ES instance. --- docs/contributing/developing.rst | 6 ++---- requirements-dev.txt | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/contributing/developing.rst b/docs/contributing/developing.rst index 01e5447961..818bb9e8d0 100644 --- a/docs/contributing/developing.rst +++ b/docs/contributing/developing.rst @@ -90,10 +90,8 @@ the tests against it. **Testing Elasticsearch** -To test Elasticsearch, you need to have the ``elasticsearch`` package installed. - -Once installed, you can test Wagtail with Elasticsearch by passing the -``--elasticsearch`` argument to ``runtests.py``:: +You can test Wagtail against Elasticsearch by passing the ``--elasticsearch`` +argument to ``runtests.py``:: python runtests.py --elasticsearch diff --git a/requirements-dev.txt b/requirements-dev.txt index 8b55456462..64468d0cea 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,6 +3,7 @@ mock>=1.0.0 python-dateutil>=2.2 pytz>=2014.7 Pillow>=2.7.0 +elasticsearch>=1.0.0 # For coverage and PEP8 linting coverage>=3.7.0 From 3f18768a36d55d47276924dc61168f8ffd2e2f8e Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 19 Nov 2015 16:03:55 +0000 Subject: [PATCH 5/5] Always run Elasticsearch tests that don't depend on a running instance We now only skip tests that actually require a running Elasticsearch instance. This also greatly cleans up the elasticsearch test suite. --- .../tests/test_elasticsearch_backend.py | 162 +++++------------- 1 file changed, 46 insertions(+), 116 deletions(-) diff --git a/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py b/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py index 1b1e015f99..72e870cc79 100644 --- a/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py +++ b/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py @@ -1,16 +1,25 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +import os import unittest import datetime import json import mock +from elasticsearch.serializer import JSONSerializer from django.test import TestCase from django.db.models import Q from wagtail.wagtailsearch.backends import get_search_backend +from wagtail.wagtailsearch.backends.elasticsearch import ( + ElasticSearch, + ElasticSearchMapping, + ElasticSearchResults, + ElasticSearchQuery, + ElasticSearchAtomicIndexRebuilder, +) from wagtail.tests.search import models from .test_backends import BackendTests @@ -223,24 +232,12 @@ class TestElasticSearchBackend(BackendTests, TestCase): class TestElasticSearchQuery(TestCase): def assertDictEqual(self, a, b): - default = self.JSONSerializer().default + default = JSONSerializer().default self.assertEqual(json.dumps(a, sort_keys=True, default=default), json.dumps(b, sort_keys=True, default=default)) - def setUp(self): - # Import using a try-catch block to prevent crashes if the elasticsearch-py - # module is not installed - try: - from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearchQuery - from elasticsearch.serializer import JSONSerializer - except ImportError: - raise unittest.SkipTest("elasticsearch-py not installed") - - self.ElasticSearchQuery = ElasticSearchQuery - self.JSONSerializer = JSONSerializer - def test_simple(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.all(), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.all(), "Hello") # Check it expected_result = {'filtered': {'filter': {'prefix': {'content_type': 'searchtests_searchtest'}}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -248,7 +245,7 @@ class TestElasticSearchQuery(TestCase): def test_none_query_string(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.all(), None) + query = ElasticSearchQuery(models.SearchTest.objects.all(), None) # Check it expected_result = {'filtered': {'filter': {'prefix': {'content_type': 'searchtests_searchtest'}}, 'query': {'match_all': {}}}} @@ -256,7 +253,7 @@ class TestElasticSearchQuery(TestCase): def test_and_operator(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.all(), "Hello", operator='and') + query = ElasticSearchQuery(models.SearchTest.objects.all(), "Hello", operator='and') # Check it expected_result = {'filtered': {'filter': {'prefix': {'content_type': 'searchtests_searchtest'}}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials'], 'operator': 'and'}}}} @@ -264,7 +261,7 @@ class TestElasticSearchQuery(TestCase): def test_filter(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title="Test"), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(title="Test"), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'term': {'title_filter': 'Test'}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -272,7 +269,7 @@ class TestElasticSearchQuery(TestCase): def test_and_filter(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title="Test", live=True), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(title="Test", live=True), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'and': [{'term': {'live_filter': True}}, {'term': {'title_filter': 'Test'}}]}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -286,7 +283,7 @@ class TestElasticSearchQuery(TestCase): def test_or_filter(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(Q(title="Test") | Q(live=True)), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(Q(title="Test") | Q(live=True)), "Hello") # Make sure field filters are sorted (as they can be in any order which may cause false positives) query = query.get_query() @@ -299,7 +296,7 @@ class TestElasticSearchQuery(TestCase): def test_negated_filter(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.exclude(live=True), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.exclude(live=True), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'not': {'term': {'live_filter': True}}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -307,7 +304,7 @@ class TestElasticSearchQuery(TestCase): def test_fields(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.all(), "Hello", fields=['title']) + query = ElasticSearchQuery(models.SearchTest.objects.all(), "Hello", fields=['title']) # Check it expected_result = {'filtered': {'filter': {'prefix': {'content_type': 'searchtests_searchtest'}}, 'query': {'match': {'title': 'Hello'}}}} @@ -315,7 +312,7 @@ class TestElasticSearchQuery(TestCase): def test_fields_with_and_operator(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.all(), "Hello", fields=['title'], operator='and') + query = ElasticSearchQuery(models.SearchTest.objects.all(), "Hello", fields=['title'], operator='and') # Check it expected_result = {'filtered': {'filter': {'prefix': {'content_type': 'searchtests_searchtest'}}, 'query': {'match': {'title': {'query': 'Hello', 'operator': 'and'}}}}} @@ -323,7 +320,7 @@ class TestElasticSearchQuery(TestCase): def test_multiple_fields(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.all(), "Hello", fields=['title', 'content']) + query = ElasticSearchQuery(models.SearchTest.objects.all(), "Hello", fields=['title', 'content']) # Check it expected_result = {'filtered': {'filter': {'prefix': {'content_type': 'searchtests_searchtest'}}, 'query': {'multi_match': {'fields': ['title', 'content'], 'query': 'Hello'}}}} @@ -331,7 +328,7 @@ class TestElasticSearchQuery(TestCase): def test_multiple_fields_with_and_operator(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.all(), "Hello", fields=['title', 'content'], operator='and') + query = ElasticSearchQuery(models.SearchTest.objects.all(), "Hello", fields=['title', 'content'], operator='and') # Check it expected_result = {'filtered': {'filter': {'prefix': {'content_type': 'searchtests_searchtest'}}, 'query': {'multi_match': {'fields': ['title', 'content'], 'query': 'Hello', 'operator': 'and'}}}} @@ -339,7 +336,7 @@ class TestElasticSearchQuery(TestCase): def test_exact_lookup(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title__exact="Test"), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(title__exact="Test"), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'term': {'title_filter': 'Test'}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -347,7 +344,7 @@ class TestElasticSearchQuery(TestCase): def test_none_lookup(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title=None), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(title=None), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'missing': {'field': 'title_filter'}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -355,7 +352,7 @@ class TestElasticSearchQuery(TestCase): def test_isnull_true_lookup(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title__isnull=True), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(title__isnull=True), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'missing': {'field': 'title_filter'}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -363,7 +360,7 @@ class TestElasticSearchQuery(TestCase): def test_isnull_false_lookup(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title__isnull=False), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(title__isnull=False), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'not': {'missing': {'field': 'title_filter'}}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -371,7 +368,7 @@ class TestElasticSearchQuery(TestCase): def test_startswith_lookup(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title__startswith="Test"), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(title__startswith="Test"), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'prefix': {'title_filter': 'Test'}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -381,7 +378,7 @@ class TestElasticSearchQuery(TestCase): # This also tests conversion of python dates to strings # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(published_date__gt=datetime.datetime(2014, 4, 29)), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(published_date__gt=datetime.datetime(2014, 4, 29)), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'range': {'published_date_filter': {'gt': '2014-04-29'}}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -389,7 +386,7 @@ class TestElasticSearchQuery(TestCase): def test_lt_lookup(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(published_date__lt=datetime.datetime(2014, 4, 29)), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(published_date__lt=datetime.datetime(2014, 4, 29)), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'range': {'published_date_filter': {'lt': '2014-04-29'}}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -397,7 +394,7 @@ class TestElasticSearchQuery(TestCase): def test_gte_lookup(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(published_date__gte=datetime.datetime(2014, 4, 29)), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(published_date__gte=datetime.datetime(2014, 4, 29)), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'range': {'published_date_filter': {'gte': '2014-04-29'}}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -405,7 +402,7 @@ class TestElasticSearchQuery(TestCase): def test_lte_lookup(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(published_date__lte=datetime.datetime(2014, 4, 29)), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(published_date__lte=datetime.datetime(2014, 4, 29)), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'range': {'published_date_filter': {'lte': '2014-04-29'}}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -416,7 +413,7 @@ class TestElasticSearchQuery(TestCase): end_date = datetime.datetime(2014, 8, 19) # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.filter(published_date__range=(start_date, end_date)), "Hello") + query = ElasticSearchQuery(models.SearchTest.objects.filter(published_date__range=(start_date, end_date)), "Hello") # Check it expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'searchtests_searchtest'}}, {'range': {'published_date_filter': {'gte': '2014-04-29', 'lte': '2014-08-19'}}}]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} @@ -424,7 +421,7 @@ class TestElasticSearchQuery(TestCase): def test_custom_ordering(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.order_by('published_date'), "Hello", order_by_relevance=False) + query = ElasticSearchQuery(models.SearchTest.objects.order_by('published_date'), "Hello", order_by_relevance=False) # Check it expected_result = [{'published_date_filter': 'asc'}] @@ -432,7 +429,7 @@ class TestElasticSearchQuery(TestCase): def test_custom_ordering_reversed(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.order_by('-published_date'), "Hello", order_by_relevance=False) + query = ElasticSearchQuery(models.SearchTest.objects.order_by('-published_date'), "Hello", order_by_relevance=False) # Check it expected_result = [{'published_date_filter': 'desc'}] @@ -440,7 +437,7 @@ class TestElasticSearchQuery(TestCase): def test_custom_ordering_multiple(self): # Create a query - query = self.ElasticSearchQuery(models.SearchTest.objects.order_by('published_date', 'live'), "Hello", order_by_relevance=False) + query = ElasticSearchQuery(models.SearchTest.objects.order_by('published_date', 'live'), "Hello", order_by_relevance=False) # Check it expected_result = [{'published_date_filter': 'asc'}, {'live_filter': 'asc'}] @@ -449,35 +446,22 @@ class TestElasticSearchQuery(TestCase): class TestElasticSearchResults(TestCase): def assertDictEqual(self, a, b): - default = self.JSONSerializer().default + default = JSONSerializer().default self.assertEqual(json.dumps(a, sort_keys=True, default=default), json.dumps(b, sort_keys=True, default=default)) def setUp(self): - # Import using a try-catch block to prevent crashes if the elasticsearch-py - # module is not installed - try: - from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearch - from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearchResults - from elasticsearch.serializer import JSONSerializer - except ImportError: - raise unittest.SkipTest("elasticsearch-py not installed") - - self.ElasticSearch = ElasticSearch - self.ElasticSearchResults = ElasticSearchResults - self.JSONSerializer = JSONSerializer - self.objects = [] for i in range(3): self.objects.append(models.SearchTest.objects.create(title=str(i))) def get_results(self): - backend = self.ElasticSearch({}) + backend = ElasticSearch({}) query = mock.MagicMock() query.queryset = models.SearchTest.objects.all() query.get_query.return_value = 'QUERY' query.get_sort.return_value = None - return self.ElasticSearchResults(backend, query) + return ElasticSearchResults(backend, query) def construct_search_response(self, results): return { @@ -634,20 +618,10 @@ class TestElasticSearchResults(TestCase): class TestElasticSearchMapping(TestCase): def assertDictEqual(self, a, b): - default = self.JSONSerializer().default + default = JSONSerializer().default self.assertEqual(json.dumps(a, sort_keys=True, default=default), json.dumps(b, sort_keys=True, default=default)) def setUp(self): - # Import using a try-catch block to prevent crashes if the elasticsearch-py - # module is not installed - try: - from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearchMapping - from elasticsearch.serializer import JSONSerializer - except ImportError: - raise unittest.SkipTest("elasticsearch-py not installed") - - self.JSONSerializer = JSONSerializer - # Create ES mapping self.es_mapping = ElasticSearchMapping(models.SearchTest) @@ -706,20 +680,10 @@ class TestElasticSearchMapping(TestCase): class TestElasticSearchMappingInheritance(TestCase): def assertDictEqual(self, a, b): - default = self.JSONSerializer().default + default = JSONSerializer().default self.assertEqual(json.dumps(a, sort_keys=True, default=default), json.dumps(b, sort_keys=True, default=default)) def setUp(self): - # Import using a try-catch block to prevent crashes if the elasticsearch-py - # module is not installed - try: - from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearchMapping - from elasticsearch.serializer import JSONSerializer - except ImportError: - raise unittest.SkipTest("elasticsearch-py not installed") - - self.JSONSerializer = JSONSerializer - # Create ES mapping self.es_mapping = ElasticSearchMapping(models.SearchTestChild) @@ -796,18 +760,8 @@ class TestElasticSearchMappingInheritance(TestCase): class TestBackendConfiguration(TestCase): - def setUp(self): - # Import using a try-catch block to prevent crashes if the elasticsearch-py - # module is not installed - try: - from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearch - except ImportError: - raise unittest.SkipTest("elasticsearch-py not installed") - - self.ElasticSearch = ElasticSearch - def test_default_settings(self): - backend = self.ElasticSearch(params={}) + backend = ElasticSearch(params={}) self.assertEqual(len(backend.es_hosts), 1) self.assertEqual(backend.es_hosts[0]['host'], 'localhost') @@ -816,7 +770,7 @@ class TestBackendConfiguration(TestCase): def test_hosts(self): # This tests that HOSTS goes to es_hosts - backend = self.ElasticSearch(params={ + backend = ElasticSearch(params={ 'HOSTS': [ { 'host': '127.0.0.1', @@ -833,7 +787,7 @@ class TestBackendConfiguration(TestCase): def test_urls(self): # This test backwards compatibility with old URLS setting - backend = self.ElasticSearch(params={ + backend = ElasticSearch(params={ 'URLS': [ 'http://localhost:12345', 'https://127.0.0.1:54321', @@ -862,26 +816,13 @@ class TestBackendConfiguration(TestCase): self.assertEqual(backend.es_hosts[3]['url_prefix'], '/hello') +@unittest.skipUnless(os.environ.get('ELASTICSEARCH_URL', False), "ELASTICSEARCH_URL not set") class TestRebuilder(TestCase): def assertDictEqual(self, a, b): - default = self.JSONSerializer().default + default = JSONSerializer().default self.assertEqual(json.dumps(a, sort_keys=True, default=default), json.dumps(b, sort_keys=True, default=default)) def setUp(self): - # Import using a try-catch block to prevent crashes if the elasticsearch-py - # module is not installed - try: - from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearch - from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearchMapping - from elasticsearch import NotFoundError, JSONSerializer - except ImportError: - raise unittest.SkipTest("elasticsearch-py not installed") - - self.ElasticSearch = ElasticSearch - self.ElasticSearchMapping = ElasticSearchMapping - self.NotFoundError = NotFoundError - self.JSONSerializer = JSONSerializer - self.backend = get_search_backend('elasticsearch') self.es = self.backend.es self.rebuilder = self.backend.get_rebuilder() @@ -921,7 +862,7 @@ class TestRebuilder(TestCase): self.rebuilder.add_model(models.SearchTest) # Check the mapping went into Elasticsearch correctly - mapping = self.ElasticSearchMapping(models.SearchTest) + mapping = ElasticSearchMapping(models.SearchTest) response = self.es.indices.get_mapping(self.backend.es_index, mapping.get_document_type()) # Make some minor tweaks to the mapping so it matches what is in ES @@ -937,20 +878,9 @@ class TestRebuilder(TestCase): self.assertDictEqual(expected_mapping, response[self.backend.es_index]['mappings']) +@unittest.skipUnless(os.environ.get('ELASTICSEARCH_URL', False), "ELASTICSEARCH_URL not set") class TestAtomicRebuilder(TestCase): def setUp(self): - # Import using a try-catch block to prevent crashes if the elasticsearch-py - # module is not installed - try: - from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearch - from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearchAtomicIndexRebuilder - from elasticsearch import NotFoundError - except ImportError: - raise unittest.SkipTest("elasticsearch-py not installed") - - self.ElasticSearch = ElasticSearch - self.NotFoundError = NotFoundError - self.backend = get_search_backend('elasticsearch') self.backend.rebuilder_class = ElasticSearchAtomicIndexRebuilder self.es = self.backend.es