Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Decouple es query generation and execution with testability in mind

Jakub Draganek3da878264e9c
diff --git a/saleor/search/backends/newelastic.py b/saleor/search/backends/newelastic.py
index 6fa2c228c8..aa0da6f401 100644
--- a/saleor/search/backends/newelastic.py
+++ b/saleor/search/backends/newelastic.py
@@ -5,13 +5,16 @@ from elasticsearch_dsl.query import MultiMatch
from ..documents import ProductDocument
-def search_products(phrase):
+def get_search_query(phrase):
''' Execute external search for product matching phrase '''
query = MultiMatch(fields=['name', 'description'], query=phrase)
- search = (ProductDocument.search()
- .query(query)
- .source(False)
- .filter('term', is_published=True))
+ return (ProductDocument.search()
+ .query(query)
+ .source(False)
+ .filter('term', is_published=True))
+
+
+def _execute_es_search(query):
return [hit.meta.id for hit in search.execute()]
@@ -20,7 +23,5 @@ def search(query, model_or_queryset):
# TODO: remove this ugly type incoherence of old search api
if not isinstance(model_or_queryset, QuerySet):
qs = model_or_queryset.objects.all()
- return qs.filter(pk__in=search_products(query))
-
-
-
+ found_objs = _execute_es_search(get_search_query(query))
+ return qs.filter(pk__in=found_objs)
diff --git a/saleor/search/backends/test_newelastic.py b/saleor/search/backends/test_newelastic.py
index db55db3596..392254432b 100644
--- a/saleor/search/backends/test_newelastic.py
+++ b/saleor/search/backends/test_newelastic.py
@@ -1,7 +1,6 @@
from __future__ import unicode_literals
from . import newelastic
-import pytest
PHRASE = 'How fortunate man with none'
@@ -28,16 +27,6 @@ QUERY = {
}
-def elasticsearch_client(mocker):
- SETTINGS = {'URLS': ['http://localhost']}
- nes = newelastic.SearchBackend(SETTINGS)
- mocker.patch.object(
- newelastic.SearchBackend.client, 'search', returnvalue=[])
- return nes
-
-
-def test_storefront_product_search_query_syntax(elasticsearch_client):
- products = newelastic.search_products(PHRASE)
- newelastic.SearchBackend.client.search.assert_called_once_with(
- body=QUERY, doc_type=[DOC_TYPE], index=[INDEX])
+def test_storefront_product_search_query_syntax():
+ products = newelastic.get_search_query(PHRASE)
+ assert products.to_dict() == QUERY