mirror of https://github.com/django/django.git
Fixed #29644 -- Made SearchQuery.__str__() reflect negation and grouping.
This commit is contained in:
parent
7cc52250f0
commit
a3df7574f9
|
@ -158,12 +158,19 @@ class SearchQuery(SearchQueryCombinable, Value):
|
||||||
def __invert__(self):
|
def __invert__(self):
|
||||||
return type(self)(self.value, config=self.config, invert=not self.invert)
|
return type(self)(self.value, config=self.config, invert=not self.invert)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
result = super().__str__()
|
||||||
|
return ('~%s' % result) if self.invert else result
|
||||||
|
|
||||||
|
|
||||||
class CombinedSearchQuery(SearchQueryCombinable, CombinedExpression):
|
class CombinedSearchQuery(SearchQueryCombinable, CombinedExpression):
|
||||||
def __init__(self, lhs, connector, rhs, config, output_field=None):
|
def __init__(self, lhs, connector, rhs, config, output_field=None):
|
||||||
self.config = config
|
self.config = config
|
||||||
super().__init__(lhs, connector, rhs, output_field)
|
super().__init__(lhs, connector, rhs, output_field)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return '(%s)' % super().__str__()
|
||||||
|
|
||||||
|
|
||||||
class SearchRank(Func):
|
class SearchRank(Func):
|
||||||
function = 'ts_rank'
|
function = 'ts_rank'
|
||||||
|
|
|
@ -9,7 +9,7 @@ from django.contrib.postgres.search import (
|
||||||
SearchQuery, SearchRank, SearchVector,
|
SearchQuery, SearchRank, SearchVector,
|
||||||
)
|
)
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
from django.test import modify_settings
|
from django.test import SimpleTestCase, modify_settings
|
||||||
|
|
||||||
from . import PostgreSQLTestCase
|
from . import PostgreSQLTestCase
|
||||||
from .models import Character, Line, Scene
|
from .models import Character, Line, Scene
|
||||||
|
@ -292,3 +292,29 @@ class TestRankingAndWeights(GrailTestData, PostgreSQLTestCase):
|
||||||
rank=SearchRank(SearchVector('dialogue'), SearchQuery('brave sir robin')),
|
rank=SearchRank(SearchVector('dialogue'), SearchQuery('brave sir robin')),
|
||||||
).filter(rank__gt=0.3)
|
).filter(rank__gt=0.3)
|
||||||
self.assertSequenceEqual(searched, [self.verse0])
|
self.assertSequenceEqual(searched, [self.verse0])
|
||||||
|
|
||||||
|
|
||||||
|
class SearchQueryTests(SimpleTestCase):
|
||||||
|
def test_str(self):
|
||||||
|
tests = (
|
||||||
|
(~SearchQuery('a'), '~SearchQuery(a)'),
|
||||||
|
(
|
||||||
|
(SearchQuery('a') | SearchQuery('b')) & (SearchQuery('c') | SearchQuery('d')),
|
||||||
|
'((SearchQuery(a) || SearchQuery(b)) && (SearchQuery(c) || SearchQuery(d)))',
|
||||||
|
),
|
||||||
|
(
|
||||||
|
SearchQuery('a') & (SearchQuery('b') | SearchQuery('c')),
|
||||||
|
'(SearchQuery(a) && (SearchQuery(b) || SearchQuery(c)))',
|
||||||
|
),
|
||||||
|
(
|
||||||
|
(SearchQuery('a') | SearchQuery('b')) & SearchQuery('c'),
|
||||||
|
'((SearchQuery(a) || SearchQuery(b)) && SearchQuery(c))'
|
||||||
|
),
|
||||||
|
(
|
||||||
|
SearchQuery('a') & (SearchQuery('b') & (SearchQuery('c') | SearchQuery('d'))),
|
||||||
|
'(SearchQuery(a) && (SearchQuery(b) && (SearchQuery(c) || SearchQuery(d))))',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
for query, expected_str in tests:
|
||||||
|
with self.subTest(query=query):
|
||||||
|
self.assertEqual(str(query), expected_str)
|
||||||
|
|
Loading…
Reference in New Issue