0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Fix PageManager to pass 'operator' and 'order_by_relevance' kwargs to the queryset.

Also add tests for the 'operator' kwarg.
This commit is contained in:
Matt Westcott 2015-10-13 14:29:10 +01:00
parent 432a88d0d3
commit 4133d4d243
4 changed files with 46 additions and 2 deletions

View File

@ -235,8 +235,10 @@ class PageManager(models.Manager):
def not_public(self):
return self.get_queryset().not_public()
def search(self, query_string, fields=None, backend='default'):
return self.get_queryset().search(query_string, fields=fields, backend=backend)
def search(self, query_string, fields=None,
operator=None, order_by_relevance=True, backend='default'):
return self.get_queryset().search(query_string, fields=fields,
operator=operator, order_by_relevance=order_by_relevance, backend=backend)
def specific(self):
return self.get_queryset().specific()

View File

@ -332,6 +332,20 @@ class TestPageQuerySetSearch(TestCase):
self.assertIn(Page.objects.get(url_path='/home/events/tentative-unpublished-event/').specific, pages)
self.assertIn(Page.objects.get(url_path='/home/events/someone-elses-event/').specific, pages)
def test_operators(self):
results = EventPage.objects.search("moon ponies", operator='and')
self.assertEqual(list(results), [
Page.objects.get(url_path='/home/events/tentative-unpublished-event/').specific
])
results = EventPage.objects.search("moon ponies", operator='or')
sorted_results = sorted(results, key=lambda page: page.url_path)
self.assertEqual(sorted_results, [
Page.objects.get(url_path='/home/events/someone-elses-event/').specific,
Page.objects.get(url_path='/home/events/tentative-unpublished-event/').specific,
])
def test_custom_order(self):
pages = EventPage.objects.order_by('url_path').search('moon', fields=['location'], order_by_relevance=False)

View File

@ -34,6 +34,17 @@ class TestDocumentQuerySet(TestCase):
results = models.Document.objects.search("Test")
self.assertEqual(list(results), [document])
def test_operators(self):
aaa_document = models.Document.objects.create(title="AAA Test document")
zzz_document = models.Document.objects.create(title="ZZZ Test document")
results = models.Document.objects.search("aaa test", operator='and')
self.assertEqual(list(results), [aaa_document])
results = models.Document.objects.search("aaa test", operator='or')
sorted_results = sorted(results, key=lambda doc: doc.title)
self.assertEqual(sorted_results, [aaa_document, zzz_document])
def test_custom_ordering(self):
aaa_document = models.Document.objects.create(title="AAA Test document")
zzz_document = models.Document.objects.create(title="ZZZ Test document")

View File

@ -99,6 +99,23 @@ class TestImageQuerySet(TestCase):
results = Image.objects.search("Test")
self.assertEqual(list(results), [image])
def test_operators(self):
aaa_image = Image.objects.create(
title="AAA Test image",
file=get_test_image_file(),
)
zzz_image = Image.objects.create(
title="ZZZ Test image",
file=get_test_image_file(),
)
results = Image.objects.search("aaa test", operator='and')
self.assertEqual(list(results), [aaa_image])
results = Image.objects.search("aaa test", operator='or')
sorted_results = sorted(results, key=lambda img: img.title)
self.assertEqual(sorted_results, [aaa_image, zzz_image])
def test_custom_ordering(self):
aaa_image = Image.objects.create(
title="AAA Test image",