Skip to content

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

Add new integration test for search backend

Jakub Draganek7130ade5dd79
diff --git a/tests/cassettes/test_search_with_empty_result.yaml b/tests/cassettes/test_search_with_empty_result.yaml
index 6a53ca002d..8d89f12854 100644
--- a/tests/cassettes/test_search_with_empty_result.yaml
+++ b/tests/cassettes/test_search_with_empty_result.yaml
@@ -1,34 +1,17 @@
interactions:
-- request:
- body: '{"query": {"bool": {"must": {"multi_match": {"fields": ["_all", "_partials"],
- "query": "foo"}}, "filter": [{"match": {"content_type": "product.Product"}},
- {"bool": {"must": [{"bool": {"should": [{"range": {"available_on_filter": {"lte":
- "2017-10-16"}}}, {"bool": {"must_not": {"term": {"field": "available_on_filter"}}}}]}},
- {"term": {"is_published_filter": true}}]}}]}}}'
- headers:
- connection: [keep-alive]
- content-type: [application/json]
- method: GET
- uri: http://search:9200/storefront__product_product/_search?from=0&_source=pk
- response:
- body: {string: '{"took":4,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
- headers:
- content-length: ['122']
- content-type: [application/json; charset=UTF-8]
- status: {code: 200, message: OK}
- request:
body: '{"query": {"bool": {"filter": [{"match": {"content_type": "product.Product"}},
{"bool": {"must": [{"bool": {"should": [{"range": {"available_on_filter": {"lte":
- "2017-10-16"}}}, {"bool": {"must_not": {"term": {"field": "available_on_filter"}}}}]}},
+ "2017-10-17"}}}, {"bool": {"must_not": {"term": {"field": "available_on_filter"}}}}]}},
{"term": {"is_published_filter": true}}]}}], "must": {"multi_match": {"fields":
["_all", "_partials"], "query": "foo"}}}}}'
headers:
connection: [keep-alive]
content-type: [application/json]
method: GET
- uri: http://search:9200/storefront__product_product/_search?_source=pk&from=0
+ uri: http://search:9200/storefront__product_product/_search?from=0&_source=pk
response:
- body: {string: '{"took":113,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
+ body: {string: '{"took":114,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
headers:
content-length: ['124']
content-type: [application/json; charset=UTF-8]
diff --git a/tests/cassettes/test_search_with_result.yaml b/tests/cassettes/test_search_with_result.yaml
new file mode 100644
index 0000000000..46e29c281b
--- /dev/null
+++ b/tests/cassettes/test_search_with_result.yaml
@@ -0,0 +1,19 @@
+interactions:
+- request:
+ body: '{"query": {"bool": {"filter": [{"match": {"content_type": "product.Product"}},
+ {"bool": {"must": [{"bool": {"should": [{"range": {"available_on_filter": {"lte":
+ "2017-10-17"}}}, {"bool": {"must_not": {"term": {"field": "available_on_filter"}}}}]}},
+ {"term": {"is_published_filter": true}}]}}], "must": {"multi_match": {"fields":
+ ["_all", "_partials"], "query": "Group"}}}}}'
+ headers:
+ connection: [keep-alive]
+ content-type: [application/json]
+ method: GET
+ uri: http://search:9200/storefront__product_product/_search?from=0&_source=pk
+ response:
+ body: {string: '{"took":8,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":3.2523599,"hits":[{"_index":"storefront__product_product","_type":"product_product","_id":"product_product:29","_score":3.2523599,"_source":{"pk":"29"}},{"_index":"storefront__product_product","_type":"product_product","_id":"product_product:41","_score":1.7389073,"_source":{"pk":"41"}},{"_index":"storefront__product_product","_type":"product_product","_id":"product_product:59","_score":1.7389073,"_source":{"pk":"59"}}]}}'}
+ headers:
+ content-length: ['531']
+ content-type: [application/json; charset=UTF-8]
+ status: {code: 200, message: OK}
+version: 1
diff --git a/tests/test_search.py b/tests/test_search.py
index bf5acc7e80..113108f5da 100644
--- a/tests/test_search.py
+++ b/tests/test_search.py
@@ -1,5 +1,7 @@
+from saleor.product.models import Product
from django.core.management import call_command
from django.core.urlresolvers import reverse
+from decimal import Decimal
import pytest
@@ -10,9 +12,48 @@ def test_index_products(product_list):
call_command('update_index', **options)
+MATCH_SEARCH_REQUEST = ['method', 'host', 'port', 'path', 'body']
+
+
@pytest.mark.integration
- record_mode='once', match_on=['url', 'method', 'body'])
[email protected](record_mode='once', match_on=MATCH_SEARCH_REQUEST)
def test_search_with_empty_result(db, client):
WORD = 'foo'
- client.get(reverse('search:search'), {'q': WORD})
+ response = client.get(reverse('search:search'), {'q': WORD})
+ assert 0 == len(response.context['results'].object_list)
+ assert WORD == response.context['query']
+
+
+def indexed_products(product_class, default_category):
+ ''' Products to be found by search backend
+
+ We need existing objects with primary keys same as search service
+ returned in response. Otherwise search view won't find anything.
+ Purpose of this fixture is for integration with search service only!
+ pks must be in response in appropiate recorded cassette.
+ '''
+ first_product = Product.objects.create(
+ pk=59,
+ name='Test product',
+ price=Decimal(10.0),
+ product_class=product_class)
+ first_product.categories.add(default_category)
+ second_product = Product.objects.create(
+ pk=41,
+ name='Test product',
+ price=Decimal(10.0),
+ product_class=product_class)
+ second_product.categories.add(default_category)
+ return [first_product, second_product]
+
+
[email protected](record_mode='once', match_on=MATCH_SEARCH_REQUEST)
+def test_search_with_result(db, product_in_stock, indexed_products, client):
+ EXISTING_PHRASE = 'Group'
+ response = client.get(reverse('search:search'), {'q': EXISTING_PHRASE})
+ print(response.context['results'].object_list)
+ assert len(indexed_products) == len(
+ response.context['results'].object_list)
+ assert EXISTING_PHRASE == response.context['query']