mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #31147 -- Added SearchHeadline to django.contrib.postgres.
This commit is contained in:
parent
aee0bebc2f
commit
65ab4f9f03
5 changed files with 239 additions and 6 deletions
|
@ -5,16 +5,20 @@ These tests use dialogue from the 1975 film Monty Python and the Holy Grail.
|
|||
All text copyright Python (Monty) Pictures. Thanks to sacred-texts.com for the
|
||||
transcript.
|
||||
"""
|
||||
from django.contrib.postgres.search import (
|
||||
SearchConfig, SearchQuery, SearchRank, SearchVector,
|
||||
)
|
||||
from django.db import connection
|
||||
from django.db.models import F
|
||||
from django.test import SimpleTestCase, modify_settings, skipUnlessDBFeature
|
||||
from django.test import modify_settings, skipUnlessDBFeature
|
||||
|
||||
from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase
|
||||
from .models import Character, Line, Scene
|
||||
|
||||
try:
|
||||
from django.contrib.postgres.search import (
|
||||
SearchConfig, SearchHeadline, SearchQuery, SearchRank, SearchVector,
|
||||
)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
class GrailTestData:
|
||||
|
||||
|
@ -436,7 +440,7 @@ class SearchVectorIndexTests(PostgreSQLTestCase):
|
|||
)
|
||||
|
||||
|
||||
class SearchQueryTests(SimpleTestCase):
|
||||
class SearchQueryTests(PostgreSQLSimpleTestCase):
|
||||
def test_str(self):
|
||||
tests = (
|
||||
(~SearchQuery('a'), '~SearchQuery(a)'),
|
||||
|
@ -460,3 +464,118 @@ class SearchQueryTests(SimpleTestCase):
|
|||
for query, expected_str in tests:
|
||||
with self.subTest(query=query):
|
||||
self.assertEqual(str(query), expected_str)
|
||||
|
||||
|
||||
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.postgres'})
|
||||
class SearchHeadlineTests(GrailTestData, PostgreSQLTestCase):
|
||||
def test_headline(self):
|
||||
searched = Line.objects.annotate(
|
||||
headline=SearchHeadline(
|
||||
F('dialogue'),
|
||||
SearchQuery('brave sir robin'),
|
||||
config=SearchConfig('english'),
|
||||
),
|
||||
).get(pk=self.verse0.pk)
|
||||
self.assertEqual(
|
||||
searched.headline,
|
||||
'<b>Robin</b>. He was not at all afraid to be killed in nasty '
|
||||
'ways. <b>Brave</b>, <b>brave</b>, <b>brave</b>, <b>brave</b> '
|
||||
'<b>Sir</b> <b>Robin</b>',
|
||||
)
|
||||
|
||||
def test_headline_untyped_args(self):
|
||||
searched = Line.objects.annotate(
|
||||
headline=SearchHeadline('dialogue', 'killed', config='english'),
|
||||
).get(pk=self.verse0.pk)
|
||||
self.assertEqual(
|
||||
searched.headline,
|
||||
'Robin. He was not at all afraid to be <b>killed</b> in nasty '
|
||||
'ways. Brave, brave, brave, brave Sir Robin!',
|
||||
)
|
||||
|
||||
def test_headline_with_config(self):
|
||||
searched = Line.objects.annotate(
|
||||
headline=SearchHeadline(
|
||||
'dialogue',
|
||||
SearchQuery('cadeaux', config='french'),
|
||||
config='french',
|
||||
),
|
||||
).get(pk=self.french.pk)
|
||||
self.assertEqual(
|
||||
searched.headline,
|
||||
'Oh. Un beau <b>cadeau</b>. Oui oui.',
|
||||
)
|
||||
|
||||
def test_headline_with_config_from_field(self):
|
||||
searched = Line.objects.annotate(
|
||||
headline=SearchHeadline(
|
||||
'dialogue',
|
||||
SearchQuery('cadeaux', config=F('dialogue_config')),
|
||||
config=F('dialogue_config'),
|
||||
),
|
||||
).get(pk=self.french.pk)
|
||||
self.assertEqual(
|
||||
searched.headline,
|
||||
'Oh. Un beau <b>cadeau</b>. Oui oui.',
|
||||
)
|
||||
|
||||
def test_headline_separator_options(self):
|
||||
searched = Line.objects.annotate(
|
||||
headline=SearchHeadline(
|
||||
'dialogue',
|
||||
'brave sir robin',
|
||||
start_sel='<span>',
|
||||
stop_sel='</span>',
|
||||
),
|
||||
).get(pk=self.verse0.pk)
|
||||
self.assertEqual(
|
||||
searched.headline,
|
||||
'<span>Robin</span>. He was not at all afraid to be killed in '
|
||||
'nasty ways. <span>Brave</span>, <span>brave</span>, <span>brave'
|
||||
'</span>, <span>brave</span> <span>Sir</span> <span>Robin</span>',
|
||||
)
|
||||
|
||||
def test_headline_highlight_all_option(self):
|
||||
searched = Line.objects.annotate(
|
||||
headline=SearchHeadline(
|
||||
'dialogue',
|
||||
SearchQuery('brave sir robin', config='english'),
|
||||
highlight_all=True,
|
||||
),
|
||||
).get(pk=self.verse0.pk)
|
||||
self.assertIn(
|
||||
'<b>Bravely</b> bold <b>Sir</b> <b>Robin</b>, rode forth from '
|
||||
'Camelot. He was not afraid to die, o ',
|
||||
searched.headline,
|
||||
)
|
||||
|
||||
def test_headline_short_word_option(self):
|
||||
searched = Line.objects.annotate(
|
||||
headline=SearchHeadline(
|
||||
'dialogue',
|
||||
SearchQuery('brave sir robin', config='english'),
|
||||
short_word=6,
|
||||
),
|
||||
).get(pk=self.verse0.pk)
|
||||
self.assertIs(searched.headline.endswith(
|
||||
'<b>Brave</b>, <b>brave</b>, <b>brave</b>, <b>brave</b> <b>Sir</b>'
|
||||
), True)
|
||||
|
||||
def test_headline_fragments_words_options(self):
|
||||
searched = Line.objects.annotate(
|
||||
headline=SearchHeadline(
|
||||
'dialogue',
|
||||
SearchQuery('brave sir robin', config='english'),
|
||||
fragment_delimiter='...<br>',
|
||||
max_fragments=4,
|
||||
max_words=3,
|
||||
min_words=1,
|
||||
),
|
||||
).get(pk=self.verse0.pk)
|
||||
self.assertEqual(
|
||||
searched.headline,
|
||||
'<b>Sir</b> <b>Robin</b>, rode...<br>'
|
||||
'<b>Brave</b> <b>Sir</b> <b>Robin</b>...<br>'
|
||||
'<b>Brave</b>, <b>brave</b>, <b>brave</b>...<br>'
|
||||
'<b>brave</b> <b>Sir</b> <b>Robin</b>',
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue