From 5982ed3905f34c96e37c06673f131042a8a6f34e Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 19 Nov 2015 15:07:40 +0000 Subject: [PATCH] Tests for select_on_queryset --- .../tests/test_related_fields.py | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 wagtail/wagtailsearch/tests/test_related_fields.py diff --git a/wagtail/wagtailsearch/tests/test_related_fields.py b/wagtail/wagtailsearch/tests/test_related_fields.py new file mode 100644 index 0000000000..057b01ef95 --- /dev/null +++ b/wagtail/wagtailsearch/tests/test_related_fields.py @@ -0,0 +1,92 @@ +import unittest + +import django +from django.test import TestCase + +from wagtail.wagtailsearch import index +from wagtail.tests.search.models import SearchTest, SearchTestChild +from wagtail.tests.testapp.models import ManyToManyBlogPage, Advert + + +class TestSelectOnQuerySet(TestCase): + def test_select_on_queryset_with_foreign_key(self): + fields = index.RelatedFields('page', [ + index.SearchField('title'), + ]) + + queryset = fields.select_on_queryset(SearchTestChild.objects.all()) + + # ForeignKey should be select_related + self.assertFalse(queryset._prefetch_related_lookups) + self.assertIn('page', queryset.query.select_related) + + def test_select_on_queryset_with_one_to_one(self): + fields = index.RelatedFields('searchtest_ptr', [ + index.SearchField('title'), + ]) + + queryset = fields.select_on_queryset(SearchTestChild.objects.all()) + + # OneToOneField should be select_related + self.assertFalse(queryset._prefetch_related_lookups) + self.assertIn('searchtest_ptr', queryset.query.select_related) + + def test_select_on_queryset_with_many_to_many(self): + fields = index.RelatedFields('adverts', [ + index.SearchField('title'), + ]) + + queryset = fields.select_on_queryset(ManyToManyBlogPage.objects.all()) + + # ManyToManyField should be prefetch_related + self.assertIn('adverts', queryset._prefetch_related_lookups) + self.assertFalse(queryset.query.select_related) + + @unittest.skipIf(django.VERSION < (1, 8), "Reverse relations not supported on Django 1.7") + def test_select_on_queryset_with_reverse_foreign_key(self): + fields = index.RelatedFields('categories', [ + index.RelatedFields('category', [ + index.SearchField('name') + ]) + ]) + + queryset = fields.select_on_queryset(ManyToManyBlogPage.objects.all()) + + # reverse ForeignKey should be prefetch_related + self.assertIn('categories', queryset._prefetch_related_lookups) + self.assertFalse(queryset.query.select_related) + + @unittest.skipIf(django.VERSION < (1, 8), "Reverse relations not supported on Django 1.7") + def test_select_on_queryset_with_reverse_one_to_one(self): + fields = index.RelatedFields('searchtestchild', [ + index.SearchField('subtitle'), + ]) + + queryset = fields.select_on_queryset(SearchTest.objects.all()) + + # reverse OneToOneField should be select_related + self.assertFalse(queryset._prefetch_related_lookups) + self.assertIn('searchtestchild', queryset.query.select_related) + + @unittest.skipIf(django.VERSION < (1, 8), "Reverse relations not supported on Django 1.7") + def test_select_on_queryset_with_reverse_many_to_many(self): + fields = index.RelatedFields('manytomanyblogpage', [ + index.SearchField('title'), + ]) + + queryset = fields.select_on_queryset(Advert.objects.all()) + + # reverse ManyToManyField should be prefetch_related + self.assertIn('manytomanyblogpage', queryset._prefetch_related_lookups) + self.assertFalse(queryset.query.select_related) + + def test_select_on_queryset_with_taggable_manager(self): + fields = index.RelatedFields('tags', [ + index.SearchField('name'), + ]) + + queryset = fields.select_on_queryset(SearchTestChild.objects.all()) + + # Tags should be prefetch_related + self.assertIn('tags', queryset._prefetch_related_lookups) + self.assertFalse(queryset.query.select_related)