12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from typing import List , Tuple , Union
15
+ from typing import List , Tuple
16
16
from unittest .case import SkipTest
17
- from unittest .mock import PropertyMock , patch
18
17
19
18
from twisted .test .proto_helpers import MemoryReactor
20
19
@@ -220,24 +219,22 @@ class MessageSearchTest(HomeserverTestCase):
220
219
221
220
PHRASE = "the quick brown fox jumps over the lazy dog"
222
221
223
- # Each entry is a search query, followed by either a boolean of whether it is
224
- # in the phrase OR a tuple of booleans: whether it matches using websearch
225
- # and using plain search.
226
- COMMON_CASES : List [Tuple [str , Union [bool , Tuple [bool , bool ]]]] = [
222
+ # Each entry is a search query, followed by a boolean of whether it is in the phrase.
223
+ COMMON_CASES = [
227
224
("nope" , False ),
228
225
("brown" , True ),
229
226
("quick brown" , True ),
230
227
("brown quick" , True ),
231
228
("quick \t brown" , True ),
232
229
("jump" , True ),
233
230
("brown nope" , False ),
234
- ('"brown quick"' , ( False , True ) ),
231
+ ('"brown quick"' , False ),
235
232
('"jumps over"' , True ),
236
- ('"quick fox"' , ( False , True ) ),
233
+ ('"quick fox"' , False ),
237
234
("nope OR doublenope" , False ),
238
- ("furphy OR fox" , ( True , False ) ),
239
- ("fox -nope" , ( True , False ) ),
240
- ("fox -brown" , ( False , True ) ),
235
+ ("furphy OR fox" , True ),
236
+ ("fox -nope" , True ),
237
+ ("fox -brown" , False ),
241
238
('"fox" quick' , True ),
242
239
('"quick brown' , True ),
243
240
('" quick "' , True ),
@@ -246,11 +243,11 @@ class MessageSearchTest(HomeserverTestCase):
246
243
# TODO Test non-ASCII cases.
247
244
248
245
# Case that fail on SQLite.
249
- POSTGRES_CASES : List [ Tuple [ str , Union [ bool , Tuple [ bool , bool ]]]] = [
246
+ POSTGRES_CASES = [
250
247
# SQLite treats NOT as a binary operator.
251
- ("- fox" , ( False , True ) ),
252
- ("- nope" , ( True , False ) ),
253
- ('"-fox quick' , ( False , True ) ),
248
+ ("- fox" , False ),
249
+ ("- nope" , True ),
250
+ ('"-fox quick' , False ),
254
251
# PostgreSQL skips stop words.
255
252
('"the quick brown"' , True ),
256
253
('"over lazy"' , True ),
@@ -275,7 +272,7 @@ def prepare(
275
272
if isinstance (main_store .database_engine , PostgresEngine ):
276
273
assert main_store .database_engine ._version is not None
277
274
found = main_store .database_engine ._version < 140000
278
- self .COMMON_CASES .append (('"fox quick' , ( found , True ) ))
275
+ self .COMMON_CASES .append (('"fox quick' , found ))
279
276
280
277
def test_tokenize_query (self ) -> None :
281
278
"""Test the custom logic to tokenize a user's query."""
@@ -315,16 +312,10 @@ def test_tokenize_query(self) -> None:
315
312
)
316
313
317
314
def _check_test_cases (
318
- self ,
319
- store : DataStore ,
320
- cases : List [Tuple [str , Union [bool , Tuple [bool , bool ]]]],
321
- index = 0 ,
315
+ self , store : DataStore , cases : List [Tuple [str , bool ]]
322
316
) -> None :
323
317
# Run all the test cases versus search_msgs
324
318
for query , expect_to_contain in cases :
325
- if isinstance (expect_to_contain , tuple ):
326
- expect_to_contain = expect_to_contain [index ]
327
-
328
319
result = self .get_success (
329
320
store .search_msgs ([self .room_id ], query , ["content.body" ])
330
321
)
@@ -343,9 +334,6 @@ def _check_test_cases(
343
334
344
335
# Run them again versus search_rooms
345
336
for query , expect_to_contain in cases :
346
- if isinstance (expect_to_contain , tuple ):
347
- expect_to_contain = expect_to_contain [index ]
348
-
349
337
result = self .get_success (
350
338
store .search_rooms ([self .room_id ], query , ["content.body" ], 10 )
351
339
)
@@ -366,38 +354,15 @@ def test_postgres_web_search_for_phrase(self):
366
354
"""
367
355
Test searching for phrases using typical web search syntax, as per postgres' websearch_to_tsquery.
368
356
This test is skipped unless the postgres instance supports websearch_to_tsquery.
369
- """
370
-
371
- store = self .hs .get_datastores ().main
372
- if not isinstance (store .database_engine , PostgresEngine ):
373
- raise SkipTest ("Test only applies when postgres is used as the database" )
374
-
375
- if store .database_engine .tsquery_func != "websearch_to_tsquery" :
376
- raise SkipTest (
377
- "Test only applies when postgres supporting websearch_to_tsquery is used as the database"
378
- )
379
357
380
- self ._check_test_cases (store , self .COMMON_CASES + self .POSTGRES_CASES , index = 0 )
381
-
382
- def test_postgres_non_web_search_for_phrase (self ):
383
- """
384
- Test postgres searching for phrases without using web search, which is used when websearch_to_tsquery isn't
385
- supported by the current postgres version.
358
+ See https://www.postgresql.org/docs/current/textsearch-controls.html
386
359
"""
387
360
388
361
store = self .hs .get_datastores ().main
389
362
if not isinstance (store .database_engine , PostgresEngine ):
390
363
raise SkipTest ("Test only applies when postgres is used as the database" )
391
364
392
- # Patch supports_websearch_to_tsquery to always return False to ensure we're testing the plainto_tsquery path.
393
- with patch (
394
- "synapse.storage.engines.postgres.PostgresEngine.tsquery_func" ,
395
- new_callable = PropertyMock ,
396
- ) as supports_websearch_to_tsquery :
397
- supports_websearch_to_tsquery .return_value = "plainto_tsquery"
398
- self ._check_test_cases (
399
- store , self .COMMON_CASES + self .POSTGRES_CASES , index = 1
400
- )
365
+ self ._check_test_cases (store , self .COMMON_CASES + self .POSTGRES_CASES )
401
366
402
367
def test_sqlite_search (self ):
403
368
"""
@@ -407,4 +372,4 @@ def test_sqlite_search(self):
407
372
if not isinstance (store .database_engine , Sqlite3Engine ):
408
373
raise SkipTest ("Test only applies when sqlite is used as the database" )
409
374
410
- self ._check_test_cases (store , self .COMMON_CASES , index = 0 )
375
+ self ._check_test_cases (store , self .COMMON_CASES )
0 commit comments