2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2014-12-04 20:47:48 +00:00
|
|
|
from django.core.exceptions import FieldError
|
2015-04-17 21:38:20 +00:00
|
|
|
from django.test import SimpleTestCase, TestCase
|
2010-09-13 05:28:47 +00:00
|
|
|
|
2014-12-04 20:47:48 +00:00
|
|
|
from .models import (
|
2015-01-28 12:35:27 +00:00
|
|
|
Bookmark, Domain, Family, Genus, HybridSpecies, Kingdom, Klass, Order,
|
|
|
|
Phylum, Pizza, Species, TaggedItem,
|
2014-12-04 20:47:48 +00:00
|
|
|
)
|
2010-09-13 05:28:47 +00:00
|
|
|
|
2011-10-14 17:03:08 +00:00
|
|
|
|
2010-09-13 05:28:47 +00:00
|
|
|
class SelectRelatedTests(TestCase):
|
|
|
|
|
2014-10-18 21:30:58 +00:00
|
|
|
@classmethod
|
|
|
|
def create_tree(cls, stringtree):
|
2010-09-13 05:28:47 +00:00
|
|
|
"""
|
|
|
|
Helper to create a complete tree.
|
|
|
|
"""
|
|
|
|
names = stringtree.split()
|
|
|
|
models = [Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species]
|
|
|
|
assert len(names) == len(models), (names, models)
|
|
|
|
|
|
|
|
parent = None
|
|
|
|
for name, model in zip(names, models):
|
|
|
|
try:
|
|
|
|
obj = model.objects.get(name=name)
|
|
|
|
except model.DoesNotExist:
|
|
|
|
obj = model(name=name)
|
|
|
|
if parent:
|
|
|
|
setattr(obj, parent.__class__.__name__.lower(), parent)
|
|
|
|
obj.save()
|
|
|
|
parent = obj
|
|
|
|
|
2014-10-18 21:30:58 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.create_tree("Eukaryota Animalia Anthropoda Insecta Diptera Drosophilidae Drosophila melanogaster")
|
|
|
|
cls.create_tree("Eukaryota Animalia Chordata Mammalia Primates Hominidae Homo sapiens")
|
|
|
|
cls.create_tree("Eukaryota Plantae Magnoliophyta Magnoliopsida Fabales Fabaceae Pisum sativum")
|
|
|
|
cls.create_tree("Eukaryota Fungi Basidiomycota Homobasidiomycatae Agaricales Amanitacae Amanita muscaria")
|
2010-09-13 05:28:47 +00:00
|
|
|
|
|
|
|
def test_access_fks_without_select_related(self):
|
|
|
|
"""
|
|
|
|
Normally, accessing FKs doesn't fill in related objects
|
|
|
|
"""
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(8):
|
2010-10-12 03:33:19 +00:00
|
|
|
fly = Species.objects.get(name="melanogaster")
|
|
|
|
domain = fly.genus.family.order.klass.phylum.kingdom.domain
|
|
|
|
self.assertEqual(domain.name, 'Eukaryota')
|
2010-09-13 05:28:47 +00:00
|
|
|
|
|
|
|
def test_access_fks_with_select_related(self):
|
|
|
|
"""
|
|
|
|
A select_related() call will fill in those related objects without any
|
|
|
|
extra queries
|
|
|
|
"""
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2015-09-11 23:33:12 +00:00
|
|
|
person = (
|
|
|
|
Species.objects
|
|
|
|
.select_related('genus__family__order__klass__phylum__kingdom__domain')
|
|
|
|
.get(name="sapiens")
|
|
|
|
)
|
2010-10-12 03:33:19 +00:00
|
|
|
domain = person.genus.family.order.klass.phylum.kingdom.domain
|
|
|
|
self.assertEqual(domain.name, 'Eukaryota')
|
2010-09-13 05:28:47 +00:00
|
|
|
|
|
|
|
def test_list_without_select_related(self):
|
|
|
|
"""
|
|
|
|
select_related() also of course applies to entire lists, not just
|
|
|
|
items. This test verifies the expected behavior without select_related.
|
|
|
|
"""
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(9):
|
2010-10-12 03:33:19 +00:00
|
|
|
world = Species.objects.all()
|
|
|
|
families = [o.genus.family.name for o in world]
|
2010-12-01 21:35:54 +00:00
|
|
|
self.assertEqual(sorted(families), [
|
|
|
|
'Amanitacae',
|
2010-10-12 03:33:19 +00:00
|
|
|
'Drosophilidae',
|
|
|
|
'Fabaceae',
|
2010-12-01 21:35:54 +00:00
|
|
|
'Hominidae',
|
2010-10-12 03:33:19 +00:00
|
|
|
])
|
2010-09-13 05:28:47 +00:00
|
|
|
|
|
|
|
def test_list_with_select_related(self):
|
|
|
|
"""
|
|
|
|
select_related() also of course applies to entire lists, not just
|
|
|
|
items. This test verifies the expected behavior with select_related.
|
|
|
|
"""
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 03:33:19 +00:00
|
|
|
world = Species.objects.all().select_related()
|
|
|
|
families = [o.genus.family.name for o in world]
|
2010-12-01 21:35:54 +00:00
|
|
|
self.assertEqual(sorted(families), [
|
|
|
|
'Amanitacae',
|
2010-10-12 03:33:19 +00:00
|
|
|
'Drosophilidae',
|
|
|
|
'Fabaceae',
|
2010-12-01 21:35:54 +00:00
|
|
|
'Hominidae',
|
2010-10-12 03:33:19 +00:00
|
|
|
])
|
2010-09-13 05:28:47 +00:00
|
|
|
|
|
|
|
def test_list_with_depth(self):
|
|
|
|
"""
|
2013-06-28 23:00:50 +00:00
|
|
|
Passing a relationship field lookup specifier to select_related() will
|
|
|
|
stop the descent at a particular level. This can be used on lists as
|
|
|
|
well.
|
2010-09-13 05:28:47 +00:00
|
|
|
"""
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(5):
|
2013-06-28 23:00:50 +00:00
|
|
|
world = Species.objects.all().select_related('genus__family')
|
|
|
|
orders = [o.genus.family.order.name for o in world]
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(sorted(orders), ['Agaricales', 'Diptera', 'Fabales', 'Primates'])
|
2010-09-13 05:28:47 +00:00
|
|
|
|
|
|
|
def test_select_related_with_extra(self):
|
2013-12-08 17:20:06 +00:00
|
|
|
s = (Species.objects.all()
|
2013-12-12 20:23:24 +00:00
|
|
|
.select_related()
|
|
|
|
.extra(select={'a': 'select_related_species.id + 10'})[0])
|
2010-09-13 05:28:47 +00:00
|
|
|
self.assertEqual(s.id + 10, s.a)
|
|
|
|
|
|
|
|
def test_certain_fields(self):
|
|
|
|
"""
|
|
|
|
The optional fields passed to select_related() control which related
|
2013-06-28 23:00:50 +00:00
|
|
|
models we pull in. This allows for smaller queries.
|
2010-09-13 05:28:47 +00:00
|
|
|
|
|
|
|
In this case, we explicitly say to select the 'genus' and
|
|
|
|
'genus.family' models, leading to the same number of queries as before.
|
|
|
|
"""
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 03:33:19 +00:00
|
|
|
world = Species.objects.select_related('genus__family')
|
|
|
|
families = [o.genus.family.name for o in world]
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(sorted(families), ['Amanitacae', 'Drosophilidae', 'Fabaceae', 'Hominidae'])
|
2010-09-13 05:28:47 +00:00
|
|
|
|
|
|
|
def test_more_certain_fields(self):
|
|
|
|
"""
|
|
|
|
In this case, we explicitly say to select the 'genus' and
|
|
|
|
'genus.family' models, leading to the same number of queries as before.
|
|
|
|
"""
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(2):
|
2010-10-12 03:33:19 +00:00
|
|
|
world = Species.objects.filter(genus__name='Amanita')\
|
|
|
|
.select_related('genus__family')
|
|
|
|
orders = [o.genus.family.order.name for o in world]
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(orders, ['Agaricales'])
|
2010-09-13 05:28:47 +00:00
|
|
|
|
|
|
|
def test_field_traversal(self):
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2013-12-08 17:20:06 +00:00
|
|
|
s = (Species.objects.all()
|
2013-12-12 20:23:24 +00:00
|
|
|
.select_related('genus__family__order')
|
|
|
|
.order_by('id')[0:1].get().genus.family.order.name)
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(s, 'Diptera')
|
2010-09-13 05:28:47 +00:00
|
|
|
|
|
|
|
def test_depth_fields_fails(self):
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
Species.objects.select_related('genus__family__order', depth=4)
|
2013-05-30 15:05:42 +00:00
|
|
|
|
|
|
|
def test_none_clears_list(self):
|
|
|
|
queryset = Species.objects.select_related('genus').select_related(None)
|
|
|
|
self.assertEqual(queryset.query.select_related, False)
|
2013-10-15 12:15:02 +00:00
|
|
|
|
|
|
|
def test_chaining(self):
|
|
|
|
parent_1, parent_2 = Species.objects.all()[:2]
|
|
|
|
HybridSpecies.objects.create(name='hybrid', parent_1=parent_1, parent_2=parent_2)
|
|
|
|
queryset = HybridSpecies.objects.select_related('parent_1').select_related('parent_2')
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
obj = queryset[0]
|
2013-10-15 19:28:04 +00:00
|
|
|
self.assertEqual(obj.parent_1, parent_1)
|
|
|
|
self.assertEqual(obj.parent_2, parent_2)
|
2014-12-04 20:47:48 +00:00
|
|
|
|
2015-08-31 04:18:03 +00:00
|
|
|
def test_select_related_after_values(self):
|
|
|
|
"""
|
|
|
|
Running select_related() after calling values() raises a TypeError
|
|
|
|
"""
|
|
|
|
message = "Cannot call select_related() after .values() or .values_list()"
|
|
|
|
with self.assertRaisesMessage(TypeError, message):
|
|
|
|
list(Species.objects.values('name').select_related('genus'))
|
|
|
|
|
|
|
|
def test_select_related_after_values_list(self):
|
|
|
|
"""
|
|
|
|
Running select_related() after calling values_list() raises a TypeError
|
|
|
|
"""
|
|
|
|
message = "Cannot call select_related() after .values() or .values_list()"
|
|
|
|
with self.assertRaisesMessage(TypeError, message):
|
|
|
|
list(Species.objects.values_list('name').select_related('genus'))
|
|
|
|
|
2014-12-04 20:47:48 +00:00
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class SelectRelatedValidationTests(SimpleTestCase):
|
2014-12-04 20:47:48 +00:00
|
|
|
"""
|
|
|
|
select_related() should thrown an error on fields that do not exist and
|
|
|
|
non-relational fields.
|
|
|
|
"""
|
|
|
|
non_relational_error = "Non-relational field given in select_related: '%s'. Choices are: %s"
|
|
|
|
invalid_error = "Invalid field name(s) given in select_related: '%s'. Choices are: %s"
|
|
|
|
|
|
|
|
def test_non_relational_field(self):
|
|
|
|
with self.assertRaisesMessage(FieldError, self.non_relational_error % ('name', 'genus')):
|
|
|
|
list(Species.objects.select_related('name__some_field'))
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(FieldError, self.non_relational_error % ('name', 'genus')):
|
|
|
|
list(Species.objects.select_related('name'))
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(FieldError, self.non_relational_error % ('name', '(none)')):
|
|
|
|
list(Domain.objects.select_related('name'))
|
|
|
|
|
2015-10-10 01:03:04 +00:00
|
|
|
def test_non_relational_field_nested(self):
|
|
|
|
with self.assertRaisesMessage(FieldError, self.non_relational_error % ('name', 'family')):
|
|
|
|
list(Species.objects.select_related('genus__name'))
|
|
|
|
|
2014-12-04 20:47:48 +00:00
|
|
|
def test_many_to_many_field(self):
|
|
|
|
with self.assertRaisesMessage(FieldError, self.invalid_error % ('toppings', '(none)')):
|
|
|
|
list(Pizza.objects.select_related('toppings'))
|
|
|
|
|
|
|
|
def test_reverse_relational_field(self):
|
|
|
|
with self.assertRaisesMessage(FieldError, self.invalid_error % ('child_1', 'genus')):
|
|
|
|
list(Species.objects.select_related('child_1'))
|
|
|
|
|
|
|
|
def test_invalid_field(self):
|
|
|
|
with self.assertRaisesMessage(FieldError, self.invalid_error % ('invalid_field', 'genus')):
|
|
|
|
list(Species.objects.select_related('invalid_field'))
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(FieldError, self.invalid_error % ('related_invalid_field', 'family')):
|
|
|
|
list(Species.objects.select_related('genus__related_invalid_field'))
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(FieldError, self.invalid_error % ('invalid_field', '(none)')):
|
|
|
|
list(Domain.objects.select_related('invalid_field'))
|
|
|
|
|
|
|
|
def test_generic_relations(self):
|
|
|
|
with self.assertRaisesMessage(FieldError, self.invalid_error % ('tags', '')):
|
|
|
|
list(Bookmark.objects.select_related('tags'))
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(FieldError, self.invalid_error % ('content_object', 'content_type')):
|
|
|
|
list(TaggedItem.objects.select_related('content_object'))
|