-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
test_backends.py
1178 lines (991 loc) · 39.7 KB
/
test_backends.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import unittest
from collections import OrderedDict
from datetime import date
from io import StringIO
from unittest import mock
from django.conf import settings
from django.core import management
from django.db import connection
from django.test import TestCase
from django.test.utils import override_settings
from taggit.models import Tag
from wagtail.search.backends import (
InvalidSearchBackendError,
get_search_backend,
get_search_backends,
)
from wagtail.search.backends.base import BaseSearchBackend, FieldError, FilterFieldError
from wagtail.search.backends.database.fallback import DatabaseSearchBackend
from wagtail.search.backends.database.sqlite.utils import fts5_available
from wagtail.search.models import IndexEntry
from wagtail.search.query import (
MATCH_ALL,
MATCH_NONE,
And,
Boost,
Not,
Or,
Phrase,
PlainText,
)
from wagtail.test.search import models
from wagtail.test.utils import WagtailTestUtils
class BackendTests(WagtailTestUtils):
# To test a specific backend, subclass BackendTests and define self.backend_path.
fixtures = ["search"]
def setUp(self):
# Search WAGTAILSEARCH_BACKENDS for an entry that uses the given backend path
for backend_name, backend_conf in settings.WAGTAILSEARCH_BACKENDS.items():
if backend_conf["BACKEND"] == self.backend_path:
self.backend = get_search_backend(backend_name)
self.backend_name = backend_name
break
else:
# no conf entry found - skip tests for this backend
raise unittest.SkipTest(
"No WAGTAILSEARCH_BACKENDS entry for the backend %s" % self.backend_path
)
# HACK: This is a hack to delete all the index entries that may be present in the test database before each test is run.
IndexEntry.objects.all().delete()
management.call_command(
"update_index",
backend_name=self.backend_name,
stdout=StringIO(),
chunk_size=50,
)
def assertUnsortedListEqual(self, a, b):
"""
Checks two results lists are equal while not taking into account the ordering.
Note: This is different to assertSetEqual in that duplicate results are taken
into account.
"""
self.assertListEqual(sorted(a), sorted(b))
# SEARCH TESTS
def test_search_simple(self):
results = self.backend.search("JavaScript", models.Book)
self.assertUnsortedListEqual(
[r.title for r in results],
["JavaScript: The good parts", "JavaScript: The Definitive Guide"],
)
def test_search_count(self):
results = self.backend.search("JavaScript", models.Book)
self.assertEqual(results.count(), 2)
def test_search_blank(self):
# Blank searches should never return anything
results = self.backend.search("", models.Book)
self.assertSetEqual(set(results), set())
def test_search_all(self):
results = self.backend.search(MATCH_ALL, models.Book)
self.assertSetEqual(set(results), set(models.Book.objects.all()))
def test_search_none(self):
results = self.backend.search(MATCH_NONE, models.Book)
self.assertFalse(list(results))
def test_search_does_not_return_results_from_wrong_model(self):
# https://github.com/wagtail/wagtail/issues/10188 - if a term matches some other
# model to the one being searched, this match should not leak into the results
# (e.g. returning the object with the same ID)
results = self.backend.search("thrones", models.Author)
self.assertSetEqual(set(results), set())
def test_ranking(self):
# Note: also tests the "or" operator
results = list(
self.backend.search("JavaScript Definitive", models.Book, operator="or")
)
self.assertUnsortedListEqual(
[r.title for r in results],
["JavaScript: The good parts", "JavaScript: The Definitive Guide"],
)
# "JavaScript: The Definitive Guide" should be first
self.assertEqual(results[0].title, "JavaScript: The Definitive Guide")
def test_annotate_score(self):
results = self.backend.search("JavaScript", models.Book).annotate_score(
"_score"
)
for result in results:
self.assertIsInstance(result._score, float)
def test_annotate_score_with_slice(self):
# #3431 - Annotate score wasn't being passed to new queryset when slicing
results = self.backend.search("JavaScript", models.Book).annotate_score(
"_score"
)[:10]
for result in results:
self.assertIsInstance(result._score, float)
def test_search_and_operator(self):
# Should not return "JavaScript: The good parts" as it does not have "Definitive"
results = self.backend.search(
"JavaScript Definitive", models.Book, operator="and"
)
self.assertUnsortedListEqual(
[r.title for r in results], ["JavaScript: The Definitive Guide"]
)
def test_search_on_child_class(self):
# Searches on a child class should only return results that have the child class as well
# and all results should be instances of the child class
results = self.backend.search(MATCH_ALL, models.Novel)
self.assertSetEqual(set(results), set(models.Novel.objects.all()))
def test_search_child_class_field_from_parent(self):
# Searches the Book model for content that exists in the Novel model
# Note: "Westeros" only occurs in the Novel.setting field
# All results should be instances of the parent class
results = self.backend.search("Westeros", models.Book)
self.assertUnsortedListEqual(
[r.title for r in results],
["A Game of Thrones", "A Clash of Kings", "A Storm of Swords"],
)
self.assertIsInstance(results[0], models.Book)
def test_search_on_individual_field(self):
# The following query shouldn't search the Novel.setting field so none
# of the Novels set in "Westeros" should be returned
results = self.backend.search(
"Westeros Hobbit", models.Book, fields=["title"], operator="or"
)
self.assertUnsortedListEqual([r.title for r in results], ["The Hobbit"])
def test_search_on_unknown_field(self):
with self.assertRaises(FieldError):
list(
self.backend.search(
"Westeros Hobbit", models.Book, fields=["unknown"], operator="or"
)
)
def test_search_on_non_searchable_field(self):
with self.assertRaises(FieldError):
list(
self.backend.search(
"Westeros Hobbit",
models.Book,
fields=["number_of_pages"],
operator="or",
)
)
def test_search_on_related_fields(self):
results = self.backend.search("Bilbo Baggins", models.Novel)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"The Hobbit",
"The Fellowship of the Ring",
"The Two Towers",
"The Return of the King",
],
)
def test_search_boosting_on_related_fields(self):
# Bilbo Baggins is the protagonist of "The Hobbit" but not any of the "Lord of the Rings" novels.
# As the protagonist has more boost than other characters, "The Hobbit" should always be returned
# first
results = list(self.backend.search("Bilbo Baggins", models.Novel))
self.assertEqual(results[0].title, "The Hobbit")
# The remaining results should be scored equally so their rank is undefined
self.assertUnsortedListEqual(
[r.title for r in results[1:]],
["The Fellowship of the Ring", "The Two Towers", "The Return of the King"],
)
def test_search_callable_field(self):
# "Django Two scoops" only mentions "Python" in its "get_programming_language_display"
# callable field
results = self.backend.search("Python", models.Book)
self.assertUnsortedListEqual(
[r.title for r in results], ["Learning Python", "Two Scoops of Django 1.11"]
)
def test_search_all_unindexed(self):
# There should be no index entries for UnindexedBook
results = self.backend.search(MATCH_ALL, models.UnindexedBook)
self.assertEqual(len(results), 0)
# AUTOCOMPLETE TESTS
def test_autocomplete(self):
# This one shouldn't match "Django Two scoops" as "get_programming_language_display"
# isn't an autocomplete field
results = self.backend.autocomplete("Py", models.Book)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"Learning Python",
],
)
def test_autocomplete_uses_autocompletefield(self):
# Autocomplete should only require an AutocompleteField, not a SearchField
# TODO: given that partial_match=True has no effect as of Wagtail 5, also test that
# AutocompleteField is actually being respected, and it's not just relying on the
# presence of a SearchField (with or without partial_match)
results = self.backend.autocomplete("Georg", models.Author)
self.assertUnsortedListEqual(
[r.name for r in results],
[
"George R.R. Martin",
],
)
def test_autocomplete_with_fields_arg(self):
results = self.backend.autocomplete("Georg", models.Author, fields=["name"])
self.assertUnsortedListEqual(
[r.name for r in results],
[
"George R.R. Martin",
],
)
def test_autocomplete_not_affected_by_stemming(self):
# If SEARCH_CONFIG is set, stemming will be enabled.
# But we want to disable this for autocomplete as stemmed words don't always match on prefixes
# See: https://www.postgresql.org/docs/9.1/datatype-textsearch.html#DATATYPE-TSQUERY
results = self.backend.autocomplete("Learni", models.Book)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"Learning Python",
],
)
# FILTERING TESTS
def test_filter_exact_value(self):
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(number_of_pages=440)
)
self.assertUnsortedListEqual(
[r.title for r in results],
["The Return of the King", "The Rust Programming Language"],
)
def test_filter_exact_value_on_parent_model_field(self):
results = self.backend.search(
MATCH_ALL, models.Novel.objects.filter(number_of_pages=440)
)
self.assertUnsortedListEqual(
[r.title for r in results], ["The Return of the King"]
)
def test_filter_lt(self):
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(number_of_pages__lt=440)
)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"The Hobbit",
"JavaScript: The good parts",
"The Fellowship of the Ring",
"Foundation",
"The Two Towers",
],
)
def test_filter_lte(self):
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(number_of_pages__lte=440)
)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"The Return of the King",
"The Rust Programming Language",
"The Hobbit",
"JavaScript: The good parts",
"The Fellowship of the Ring",
"Foundation",
"The Two Towers",
],
)
def test_filter_gt(self):
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(number_of_pages__gt=440)
)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"JavaScript: The Definitive Guide",
"Learning Python",
"A Clash of Kings",
"A Game of Thrones",
"Two Scoops of Django 1.11",
"A Storm of Swords",
"Programming Rust",
],
)
def test_filter_gte(self):
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(number_of_pages__gte=440)
)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"The Return of the King",
"The Rust Programming Language",
"JavaScript: The Definitive Guide",
"Learning Python",
"A Clash of Kings",
"A Game of Thrones",
"Two Scoops of Django 1.11",
"A Storm of Swords",
"Programming Rust",
],
)
def test_filter_in_list(self):
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(number_of_pages__in=[440, 1160])
)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"The Return of the King",
"The Rust Programming Language",
"Learning Python",
],
)
def test_filter_in_iterable(self):
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(number_of_pages__in=iter([440, 1160]))
)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"The Return of the King",
"The Rust Programming Language",
"Learning Python",
],
)
def test_filter_in_values_list_subquery(self):
values = models.Book.objects.filter(number_of_pages__lt=440).values_list(
"number_of_pages", flat=True
)
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(number_of_pages__in=values)
)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"The Hobbit",
"JavaScript: The good parts",
"The Fellowship of the Ring",
"Foundation",
"The Two Towers",
],
)
def test_filter_isnull_true(self):
# Note: We don't know the birth dates of any of the programming guide authors
results = self.backend.search(
MATCH_ALL, models.Author.objects.filter(date_of_birth__isnull=True)
)
self.assertUnsortedListEqual(
[r.name for r in results],
[
"David Ascher",
"Mark Lutz",
"David Flanagan",
"Douglas Crockford",
"Daniel Roy Greenfeld",
"Audrey Roy Greenfeld",
"Carol Nichols",
"Steve Klabnik",
"Jim Blandy",
"Jason Orendorff",
],
)
def test_filter_isnull_false(self):
# Note: We know the birth dates of all of the novel authors
results = self.backend.search(
MATCH_ALL, models.Author.objects.filter(date_of_birth__isnull=False)
)
self.assertUnsortedListEqual(
[r.name for r in results],
["Isaac Asimov", "George R.R. Martin", "J. R. R. Tolkien"],
)
def test_filter_prefix(self):
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(title__startswith="Th")
)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"The Hobbit",
"The Fellowship of the Ring",
"The Two Towers",
"The Return of the King",
"The Rust Programming Language",
],
)
def test_filter_and_operator(self):
results = self.backend.search(
MATCH_ALL,
models.Book.objects.filter(number_of_pages=440)
& models.Book.objects.filter(publication_date=date(1955, 10, 20)),
)
self.assertUnsortedListEqual(
[r.title for r in results], ["The Return of the King"]
)
def test_filter_or_operator(self):
results = self.backend.search(
MATCH_ALL,
models.Book.objects.filter(number_of_pages=440)
| models.Book.objects.filter(number_of_pages=1160),
)
self.assertUnsortedListEqual(
[r.title for r in results],
[
"Learning Python",
"The Return of the King",
"The Rust Programming Language",
],
)
def test_filter_on_non_filterable_field(self):
with self.assertRaises(FieldError):
list(
self.backend.search(
MATCH_ALL, models.Author.objects.filter(name__startswith="Issac")
)
)
def test_search_with_date_filter(self):
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(publication_date__gt=date(2000, 6, 1))
)
self.assertEqual(len(results), 4)
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(publication_date__year__gte=2000)
)
self.assertEqual(len(results), 5)
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(publication_date__year__gt=2000)
)
self.assertEqual(len(results), 4)
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(publication_date__year__lte=1954)
)
self.assertEqual(len(results), 4)
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(publication_date__year__lt=1954)
)
self.assertEqual(len(results), 2)
results = self.backend.search(
MATCH_ALL, models.Book.objects.filter(publication_date__year=1954)
)
self.assertEqual(len(results), 2)
# ORDER BY RELEVANCE
def test_order_by_relevance(self):
results = self.backend.search(
MATCH_ALL,
models.Novel.objects.order_by("number_of_pages"),
order_by_relevance=False,
)
# Ordering should be set to "number_of_pages"
self.assertEqual(
[r.title for r in results],
[
"Foundation",
"The Hobbit",
"The Two Towers",
"The Fellowship of the Ring",
"The Return of the King",
"A Game of Thrones",
"A Clash of Kings",
"A Storm of Swords",
],
)
def test_order_by_non_filterable_field(self):
with self.assertRaises(FieldError):
list(
self.backend.search(
MATCH_ALL,
models.Author.objects.order_by("name"),
order_by_relevance=False,
)
)
# SLICING TESTS
def test_single_result(self):
results = self.backend.search(
MATCH_ALL,
models.Novel.objects.order_by("number_of_pages"),
order_by_relevance=False,
)
self.assertEqual(results[0].title, "Foundation")
self.assertEqual(results[1].title, "The Hobbit")
def test_limit(self):
# Note: we need consistent ordering for this test
results = self.backend.search(
MATCH_ALL,
models.Novel.objects.order_by("number_of_pages"),
order_by_relevance=False,
)
# Limit the results
results = results[:3]
self.assertListEqual(
[r.title for r in results], ["Foundation", "The Hobbit", "The Two Towers"]
)
def test_offset(self):
# Note: we need consistent ordering for this test
results = self.backend.search(
MATCH_ALL,
models.Novel.objects.order_by("number_of_pages"),
order_by_relevance=False,
)
# Offset the results
results = results[3:]
self.assertListEqual(
[r.title for r in results],
[
"The Fellowship of the Ring",
"The Return of the King",
"A Game of Thrones",
"A Clash of Kings",
"A Storm of Swords",
],
)
def test_offset_and_limit(self):
# Note: we need consistent ordering for this test
results = self.backend.search(
MATCH_ALL,
models.Novel.objects.order_by("number_of_pages"),
order_by_relevance=False,
)
# Offset the results
results = results[3:6]
self.assertListEqual(
[r.title for r in results],
[
"The Fellowship of the Ring",
"The Return of the King",
"A Game of Thrones",
],
)
# FACET TESTS
def test_facet(self):
results = self.backend.search(MATCH_ALL, models.ProgrammingGuide).facet(
"programming_language"
)
# Not testing ordering here as two of the items have the same count, so the ordering is undefined.
# See test_facet_tags for a test of the ordering
self.assertDictEqual(dict(results), {"js": 2, "py": 2, "rs": 1})
def test_facet_tags(self):
# The test data doesn't contain any tags, add some
FANTASY_BOOKS = [1, 2, 3, 4, 5, 6, 7]
SCIFI_BOOKS = [10]
for book in models.Book.objects.filter(id__in=FANTASY_BOOKS + SCIFI_BOOKS):
book = book.get_indexed_instance()
if book.id in FANTASY_BOOKS:
book.tags.add("Fantasy")
if book.id in SCIFI_BOOKS:
book.tags.add("Science Fiction")
self.backend.add(book)
index = self.backend.get_index_for_model(models.Book)
if index:
index.refresh()
fantasy_tag = Tag.objects.get(name="Fantasy")
scifi_tag = Tag.objects.get(name="Science Fiction")
results = self.backend.search(MATCH_ALL, models.Book).facet("tags")
self.assertEqual(
results,
OrderedDict(
[
(fantasy_tag.id, 7),
(None, 6),
(scifi_tag.id, 1),
]
),
)
def test_facet_with_nonexistent_field(self):
with self.assertRaises(FilterFieldError):
self.backend.search(MATCH_ALL, models.ProgrammingGuide).facet("foo")
# MISC TESTS
def test_same_rank_pages(self):
# Checks that results with a same ranking cannot be found multiple times
# across pages (see issue #3729).
same_rank_objects = set()
index = self.backend.get_index_for_model(models.Book)
for i in range(10):
obj = models.Book.objects.create(
title="Rank %s" % i,
publication_date=date(2017, 10, 18),
number_of_pages=100,
)
index.add_item(obj)
same_rank_objects.add(obj)
index.refresh()
results = self.backend.search("Rank", models.Book)
results_across_pages = set()
for i, obj in enumerate(same_rank_objects):
results_across_pages.add(results[i : i + 1][0])
self.assertSetEqual(results_across_pages, same_rank_objects)
def test_delete(self):
foundation = models.Novel.objects.filter(title="Foundation").first()
# Delete from the search index
index = self.backend.get_index_for_model(models.Novel)
if index:
index.delete_item(foundation)
index.refresh()
# Delete from the database
foundation.delete()
# To test that the book was deleted from the index as well, we will perform the slicing check from an earlier
# test where "Foundation" was the first result. We need to test it this way so we can pick up the case where
# the object still exists in the index but not in the database (in that case, just two objects would be returned
# instead of three).
# Note: we need consistent ordering for this test
results = self.backend.search(
MATCH_ALL,
models.Novel.objects.order_by("number_of_pages"),
order_by_relevance=False,
)
# Limit the results
results = results[:3]
self.assertEqual(
[r.title for r in results],
[
# "Foundation"
"The Hobbit",
"The Two Towers",
"The Fellowship of the Ring", # If this item doesn't appear, "Foundation" is still in the index
],
)
def test_plain_text_single_word(self):
results = self.backend.search(
PlainText("JavaScript"), models.Book.objects.all()
)
self.assertSetEqual(
{r.title for r in results},
{"JavaScript: The Definitive Guide", "JavaScript: The good parts"},
)
def test_incomplete_plain_text(self):
results = self.backend.search(PlainText("pro"), models.Book.objects.all())
self.assertSetEqual({r.title for r in results}, set())
def test_plain_text_multiple_words_or(self):
results = self.backend.search(
PlainText("JavaScript Definitive", operator="or"), models.Book.objects.all()
)
self.assertSetEqual(
{r.title for r in results},
{"JavaScript: The Definitive Guide", "JavaScript: The good parts"},
)
def test_plain_text_multiple_words_and(self):
results = self.backend.search(
PlainText("JavaScript Definitive Guide", operator="and"),
models.Book.objects.all(),
)
self.assertSetEqual(
{r.title for r in results}, {"JavaScript: The Definitive Guide"}
)
def test_plain_text_operator_case(self):
results = self.backend.search(
PlainText("Guide", operator="AND"), models.Book.objects.all()
)
self.assertSetEqual(
{r.title for r in results}, {"JavaScript: The Definitive Guide"}
)
results = self.backend.search(
PlainText("Guide", operator="aNd"), models.Book.objects.all()
)
self.assertSetEqual(
{r.title for r in results}, {"JavaScript: The Definitive Guide"}
)
results = self.backend.search(
"Guide", models.Book.objects.all(), operator="AND"
)
self.assertSetEqual(
{r.title for r in results}, {"JavaScript: The Definitive Guide"}
)
results = self.backend.search(
"Guide", models.Book.objects.all(), operator="aNd"
)
self.assertSetEqual(
{r.title for r in results}, {"JavaScript: The Definitive Guide"}
)
def test_plain_text_invalid_operator(self):
with self.assertRaises(ValueError):
self.backend.search(
PlainText("Guide", operator="xor"), models.Book.objects.all()
)
with self.assertRaises(ValueError):
self.backend.search("Guide", models.Book.objects.all(), operator="xor")
def test_boost(self):
results = self.backend.search(
PlainText("JavaScript Definitive")
| Boost(PlainText("Learning Python"), 2.0),
models.Book.objects.all(),
)
# Both python and JavaScript should be returned with Python at the top
self.assertEqual(
[r.title for r in results],
[
"Learning Python",
"JavaScript: The Definitive Guide",
],
)
results = self.backend.search(
PlainText("JavaScript Definitive")
| Boost(PlainText("Learning Python"), 0.5),
models.Book.objects.all(),
)
# Now they should be swapped
self.assertEqual(
[r.title for r in results],
[
"JavaScript: The Definitive Guide",
"Learning Python",
],
)
def test_match_all(self):
results = self.backend.search(MATCH_ALL, models.Book.objects.all())
self.assertEqual(len(results), 14)
def test_and(self):
results = self.backend.search(
And([PlainText("javascript"), PlainText("definitive")]),
models.Book.objects.all(),
)
self.assertSetEqual(
{r.title for r in results}, {"JavaScript: The Definitive Guide"}
)
results = self.backend.search(
PlainText("javascript") & PlainText("definitive"), models.Book.objects.all()
)
self.assertSetEqual(
{r.title for r in results}, {"JavaScript: The Definitive Guide"}
)
def test_or(self):
results = self.backend.search(
Or([PlainText("hobbit"), PlainText("towers")]), models.Book.objects.all()
)
self.assertSetEqual(
{r.title for r in results}, {"The Hobbit", "The Two Towers"}
)
results = self.backend.search(
PlainText("hobbit") | PlainText("towers"), models.Book.objects.all()
)
self.assertSetEqual(
{r.title for r in results}, {"The Hobbit", "The Two Towers"}
)
def test_not(self):
all_other_titles = {
"A Clash of Kings",
"A Game of Thrones",
"A Storm of Swords",
"Foundation",
"Learning Python",
"The Hobbit",
"The Two Towers",
"The Fellowship of the Ring",
"The Return of the King",
"The Rust Programming Language",
"Two Scoops of Django 1.11",
"Programming Rust",
}
results = self.backend.search(
Not(PlainText("javascript")), models.Book.objects.all()
)
self.assertSetEqual({r.title for r in results}, all_other_titles)
results = self.backend.search(
~PlainText("javascript"), models.Book.objects.all()
)
self.assertSetEqual({r.title for r in results}, all_other_titles)
# Tests multiple words
results = self.backend.search(
~PlainText("javascript the"), models.Book.objects.all()
)
self.assertSetEqual({r.title for r in results}, all_other_titles)
def test_operators_combination(self):
results = self.backend.search(
(
(PlainText("javascript") & ~PlainText("definitive"))
| PlainText("python")
| PlainText("rust")
)
| PlainText("two"),
models.Book.objects.all(),
)
self.assertSetEqual(
{r.title for r in results},
{
"JavaScript: The good parts",
"Learning Python",
"The Two Towers",
"The Rust Programming Language",
"Two Scoops of Django 1.11",
"Programming Rust",
},
)
def test_phrase(self):
results = self.backend.search(
Phrase("rust programming"), models.Book.objects.all()
)
self.assertSetEqual(
{r.title for r in results}, {"The Rust Programming Language"}
)
results = self.backend.search(
Phrase("programming rust"), models.Book.objects.all()
)
self.assertSetEqual({r.title for r in results}, {"Programming Rust"})
def test_update_index_no_verbosity(self):
stdout = StringIO()
management.call_command(
"update_index", verbosity=0, backend_name=self.backend_name, stdout=stdout
)
self.assertFalse(stdout.getvalue())
@override_settings(
WAGTAILSEARCH_BACKENDS={"default": {"BACKEND": "wagtail.search.backends.database"}}
)
class TestBackendLoader(TestCase):
@mock.patch("wagtail.search.backends.database.connection")
def test_import_by_name_unknown_db_vendor(self, connection):
connection.vendor = "unknown"
db = get_search_backend(backend="default")
self.assertIsInstance(db, DatabaseSearchBackend)
@mock.patch("wagtail.search.backends.database.connection")
def test_import_by_path_unknown_db_vendor(self, connection):
connection.vendor = "unknown"
db = get_search_backend(backend="wagtail.search.backends.database")
self.assertIsInstance(db, DatabaseSearchBackend)
@mock.patch("wagtail.search.backends.database.connection")
def test_import_by_full_path_unknown_db_vendor(self, connection):
connection.vendor = "unknown"
db = get_search_backend(
backend="wagtail.search.backends.database.SearchBackend"
)
self.assertIsInstance(db, DatabaseSearchBackend)
@unittest.skipIf(
connection.vendor != "postgresql",
"Only applicable to PostgreSQL database systems",
)
def test_import_by_name_postgres_db_vendor(self):
from wagtail.search.backends.database.postgres.postgres import (
PostgresSearchBackend,
)
db = get_search_backend(backend="default")
self.assertIsInstance(db, PostgresSearchBackend)
@unittest.skipIf(
connection.vendor != "postgresql",
"Only applicable to PostgreSQL database systems",
)