Fix tests for change in PostgreSQL 14 behavior change. (#14310)
PostgreSQL 14 changed the behavior of `websearch_to_tsquery` to improve some behaviour. The tests were hitting those edge-cases about handling of hanging double quotes. This fixes the tests to take into account the PostgreSQL version.
This commit is contained in:
parent
1357ae869f
commit
67583281e3
|
@ -0,0 +1 @@
|
||||||
|
Allow use of postgres and sqllite full-text search operators in search queries.
|
|
@ -824,9 +824,8 @@ def _tokenize_query(query: str) -> TokenList:
|
||||||
in_phrase = False
|
in_phrase = False
|
||||||
parts = deque(query.split('"'))
|
parts = deque(query.split('"'))
|
||||||
for i, part in enumerate(parts):
|
for i, part in enumerate(parts):
|
||||||
# The contents inside double quotes is treated as a phrase, a trailing
|
# The contents inside double quotes is treated as a phrase.
|
||||||
# double quote is not implied.
|
in_phrase = bool(i % 2)
|
||||||
in_phrase = bool(i % 2) and i != (len(parts) - 1)
|
|
||||||
|
|
||||||
# Pull out the individual words, discarding any non-word characters.
|
# Pull out the individual words, discarding any non-word characters.
|
||||||
words = deque(re.findall(r"([\w\-]+)", part, re.UNICODE))
|
words = deque(re.findall(r"([\w\-]+)", part, re.UNICODE))
|
||||||
|
|
|
@ -239,7 +239,6 @@ class MessageSearchTest(HomeserverTestCase):
|
||||||
("fox -nope", (True, False)),
|
("fox -nope", (True, False)),
|
||||||
("fox -brown", (False, True)),
|
("fox -brown", (False, True)),
|
||||||
('"fox" quick', True),
|
('"fox" quick', True),
|
||||||
('"fox quick', True),
|
|
||||||
('"quick brown', True),
|
('"quick brown', True),
|
||||||
('" quick "', True),
|
('" quick "', True),
|
||||||
('" nope"', False),
|
('" nope"', False),
|
||||||
|
@ -269,6 +268,15 @@ class MessageSearchTest(HomeserverTestCase):
|
||||||
response = self.helper.send(self.room_id, self.PHRASE, tok=self.access_token)
|
response = self.helper.send(self.room_id, self.PHRASE, tok=self.access_token)
|
||||||
self.assertIn("event_id", response)
|
self.assertIn("event_id", response)
|
||||||
|
|
||||||
|
# The behaviour of a missing trailing double quote changed in PostgreSQL 14
|
||||||
|
# from ignoring the initial double quote to treating it as a phrase.
|
||||||
|
main_store = homeserver.get_datastores().main
|
||||||
|
found = False
|
||||||
|
if isinstance(main_store.database_engine, PostgresEngine):
|
||||||
|
assert main_store.database_engine._version is not None
|
||||||
|
found = main_store.database_engine._version < 140000
|
||||||
|
self.COMMON_CASES.append(('"fox quick', (found, True)))
|
||||||
|
|
||||||
def test_tokenize_query(self) -> None:
|
def test_tokenize_query(self) -> None:
|
||||||
"""Test the custom logic to tokenize a user's query."""
|
"""Test the custom logic to tokenize a user's query."""
|
||||||
cases = (
|
cases = (
|
||||||
|
@ -280,9 +288,9 @@ class MessageSearchTest(HomeserverTestCase):
|
||||||
("fox -brown", ["fox", SearchToken.Not, "brown"]),
|
("fox -brown", ["fox", SearchToken.Not, "brown"]),
|
||||||
("- fox", [SearchToken.Not, "fox"]),
|
("- fox", [SearchToken.Not, "fox"]),
|
||||||
('"fox" quick', [Phrase(["fox"]), SearchToken.And, "quick"]),
|
('"fox" quick', [Phrase(["fox"]), SearchToken.And, "quick"]),
|
||||||
# No trailing double quoe.
|
# No trailing double quote.
|
||||||
('"fox quick', ["fox", SearchToken.And, "quick"]),
|
('"fox quick', [Phrase(["fox", "quick"])]),
|
||||||
('"-fox quick', [SearchToken.Not, "fox", SearchToken.And, "quick"]),
|
('"-fox quick', [Phrase(["-fox", "quick"])]),
|
||||||
('" quick "', [Phrase(["quick"])]),
|
('" quick "', [Phrase(["quick"])]),
|
||||||
(
|
(
|
||||||
'q"uick brow"n',
|
'q"uick brow"n',
|
||||||
|
|
Loading…
Reference in New Issue