mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Tests invalid search operators and removes a useless check.
This commit is contained in:
parent
1bc7cf5934
commit
c598534271
@ -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
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user