From c598534271dcccbf16b5a811ff0a15faaaa29560 Mon Sep 17 00:00:00 2001 From: Bertrand Bordage Date: Fri, 24 Nov 2017 03:40:51 +0100 Subject: [PATCH] Tests invalid search operators and removes a useless check. --- wagtail/wagtailsearch/backends/base.py | 6 ---- wagtail/wagtailsearch/query.py | 4 +-- wagtail/wagtailsearch/tests/test_backends.py | 30 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/wagtail/wagtailsearch/backends/base.py b/wagtail/wagtailsearch/backends/base.py index 9cf04d5c9b..40c8fb411d 100644 --- a/wagtail/wagtailsearch/backends/base.py +++ b/wagtail/wagtailsearch/backends/base.py @@ -279,12 +279,6 @@ class BaseSearchBackend(object): for prefetch in prefetch_related: queryset = queryset.prefetch_related(prefetch) - # Check operator - if operator is not None: - operator = operator.lower() - if operator not in ['or', 'and']: - raise ValueError("operator must be either 'or' or 'and'") - # Search search_query = self.query_compiler_class( queryset, query, fields=fields, operator=operator, order_by_relevance=order_by_relevance diff --git a/wagtail/wagtailsearch/query.py b/wagtail/wagtailsearch/query.py index 276cffe90f..8fc346c526 100644 --- a/wagtail/wagtailsearch/query.py +++ b/wagtail/wagtailsearch/query.py @@ -138,9 +138,9 @@ class PlainText(SearchQueryShortcut): def __init__(self, query_string: str, operator: str = DEFAULT_OPERATOR, boost: float = 1): self.query_string = query_string - if operator.lower() not in self.OPERATORS: + self.operator = operator.lower() + if self.operator not in self.OPERATORS: raise ValueError("`operator` must be either 'or' or 'and'.") - self.operator = operator self.boost = boost def apply(self, func): diff --git a/wagtail/wagtailsearch/tests/test_backends.py b/wagtail/wagtailsearch/tests/test_backends.py index 82cddef955..7c6fa71cee 100644 --- a/wagtail/wagtailsearch/tests/test_backends.py +++ b/wagtail/wagtailsearch/tests/test_backends.py @@ -538,6 +538,36 @@ class BackendTests(WagtailTestUtils): self.assertSetEqual({r.title for r in results}, {'JavaScript: The Definitive Guide'}) + def test_plain_text_operator_case(self): + results = self.backend.search(PlainText('Guide', operator='AND'), + models.Book.objects.all()) + self.assertSetEqual({r.title for r in results}, + {'JavaScript: The Definitive Guide'}) + + results = self.backend.search(PlainText('Guide', operator='aNd'), + models.Book.objects.all()) + self.assertSetEqual({r.title for r in results}, + {'JavaScript: The Definitive Guide'}) + + results = self.backend.search('Guide', models.Book.objects.all(), + operator='AND') + self.assertSetEqual({r.title for r in results}, + {'JavaScript: The Definitive Guide'}) + + results = self.backend.search('Guide', models.Book.objects.all(), + operator='aNd') + self.assertSetEqual({r.title for r in results}, + {'JavaScript: The Definitive Guide'}) + + def test_plain_text_invalid_operator(self): + with self.assertRaises(ValueError): + self.backend.search(PlainText('Guide', operator='xor'), + models.Book.objects.all()) + + with self.assertRaises(ValueError): + self.backend.search('Guide', models.Book.objects.all(), + operator='xor') + def test_filter_equivalent(self): filter = Filter(Term('Javascript')) term = filter.child