Skip to content

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

Refactor dashboard search tests

Jakub Draganekb4c62f228b7d
diff --git a/saleor/search/backends/elasticsearch_dashboard.py b/saleor/search/backends/elasticsearch_dashboard.py
index ca65c3e498..93d4908272 100644
--- a/saleor/search/backends/elasticsearch_dashboard.py
+++ b/saleor/search/backends/elasticsearch_dashboard.py
@@ -4,20 +4,30 @@ from ..documents import ProductDocument, OrderDocument, UserDocument
from elasticsearch_dsl.query import MultiMatch
-def get_search_query(phrase):
- ''' Execute external search for all objects matching phrase '''
+def _search_products(phrase):
prod_query = MultiMatch(fields=['name', 'description'], query=phrase)
- products = ProductDocument.search().query(prod_query).source(False)
+ return ProductDocument.search().query(prod_query).source(False)
+
+
+def _search_users(phrase):
+ return UserDocument.search().query('match', email=phrase).source(False)
- users = UserDocument.search().query('match', email=phrase).source(False)
+def _search_orders(phrase):
order_query = MultiMatch(
fields=['user_email', 'status', 'discount_name'], query=phrase)
- orders = OrderDocument.search().query(order_query).source(False)
+ return OrderDocument.search().query(order_query).source(False)
- return {'products': products, 'users': users, 'orders': orders}
+
+def get_search_queries(phrase):
+ ''' Execute external search for all objects matching phrase '''
+ return {
+ 'products': _search_products(phrase),
+ 'users': _search_users(phrase),
+ 'orders': _search_orders(phrase)
+ }
def search(phrase):
''' Provide queryset for every search result '''
- return {k: s.to_queryset() for k, s in get_search_query(phrase).items()}
+ return {k: s.to_queryset() for k, s in get_search_queries(phrase).items()}
diff --git a/saleor/search/backends/test_dashboard.py b/saleor/search/backends/test_dashboard.py
index a9aaa74e82..3ba0621813 100644
--- a/saleor/search/backends/test_dashboard.py
+++ b/saleor/search/backends/test_dashboard.py
@@ -33,7 +33,7 @@ USERS_QUERY = {
def test_dashboard_search_query_syntax():
''' Check if generated ES queries have desired syntax '''
- searches = elasticsearch_dashboard.get_search_query(PHRASE)
+ searches = elasticsearch_dashboard.get_search_queries(PHRASE)
assert PRODUCT_QUERY == searches['products'].to_dict()
assert ORDERS_QUERY == searches['orders'].to_dict()
assert USERS_QUERY == searches['users'].to_dict()