Skip to content

Commit 6758328

Browse files
authored
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.
1 parent 1357ae8 commit 6758328

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

changelog.d/14310.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow use of postgres and sqllite full-text search operators in search queries.

synapse/storage/databases/main/search.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,8 @@ def _tokenize_query(query: str) -> TokenList:
824824
in_phrase = False
825825
parts = deque(query.split('"'))
826826
for i, part in enumerate(parts):
827-
# The contents inside double quotes is treated as a phrase, a trailing
828-
# double quote is not implied.
829-
in_phrase = bool(i % 2) and i != (len(parts) - 1)
827+
# The contents inside double quotes is treated as a phrase.
828+
in_phrase = bool(i % 2)
830829

831830
# Pull out the individual words, discarding any non-word characters.
832831
words = deque(re.findall(r"([\w\-]+)", part, re.UNICODE))

tests/storage/test_room_search.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ class MessageSearchTest(HomeserverTestCase):
239239
("fox -nope", (True, False)),
240240
("fox -brown", (False, True)),
241241
('"fox" quick', True),
242-
('"fox quick', True),
243242
('"quick brown', True),
244243
('" quick "', True),
245244
('" nope"', False),
@@ -269,6 +268,15 @@ def prepare(
269268
response = self.helper.send(self.room_id, self.PHRASE, tok=self.access_token)
270269
self.assertIn("event_id", response)
271270

271+
# The behaviour of a missing trailing double quote changed in PostgreSQL 14
272+
# from ignoring the initial double quote to treating it as a phrase.
273+
main_store = homeserver.get_datastores().main
274+
found = False
275+
if isinstance(main_store.database_engine, PostgresEngine):
276+
assert main_store.database_engine._version is not None
277+
found = main_store.database_engine._version < 140000
278+
self.COMMON_CASES.append(('"fox quick', (found, True)))
279+
272280
def test_tokenize_query(self) -> None:
273281
"""Test the custom logic to tokenize a user's query."""
274282
cases = (
@@ -280,9 +288,9 @@ def test_tokenize_query(self) -> None:
280288
("fox -brown", ["fox", SearchToken.Not, "brown"]),
281289
("- fox", [SearchToken.Not, "fox"]),
282290
('"fox" quick', [Phrase(["fox"]), SearchToken.And, "quick"]),
283-
# No trailing double quoe.
284-
('"fox quick', ["fox", SearchToken.And, "quick"]),
285-
('"-fox quick', [SearchToken.Not, "fox", SearchToken.And, "quick"]),
291+
# No trailing double quote.
292+
('"fox quick', [Phrase(["fox", "quick"])]),
293+
('"-fox quick', [Phrase(["-fox", "quick"])]),
286294
('" quick "', [Phrase(["quick"])]),
287295
(
288296
'q"uick brow"n',

0 commit comments

Comments
 (0)