2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2012-12-30 05:19:47 +08:00
|
|
|
import warnings
|
|
|
|
|
2010-09-13 13:28:47 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
from .models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species
|
2010-09-13 13:28:47 +08:00
|
|
|
|
2011-10-15 01:03:08 +08:00
|
|
|
|
2010-09-13 13:28:47 +08:00
|
|
|
class SelectRelatedTests(TestCase):
|
|
|
|
|
|
|
|
def create_tree(self, stringtree):
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
def create_base_data(self):
|
|
|
|
self.create_tree("Eukaryota Animalia Anthropoda Insecta Diptera Drosophilidae Drosophila melanogaster")
|
|
|
|
self.create_tree("Eukaryota Animalia Chordata Mammalia Primates Hominidae Homo sapiens")
|
|
|
|
self.create_tree("Eukaryota Plantae Magnoliophyta Magnoliopsida Fabales Fabaceae Pisum sativum")
|
|
|
|
self.create_tree("Eukaryota Fungi Basidiomycota Homobasidiomycatae Agaricales Amanitacae Amanita muscaria")
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# The test runner sets settings.DEBUG to False, but we want to gather
|
|
|
|
# queries so we'll set it to True here and reset it at the end of the
|
|
|
|
# test case.
|
|
|
|
self.create_base_data()
|
|
|
|
|
|
|
|
def test_access_fks_without_select_related(self):
|
|
|
|
"""
|
|
|
|
Normally, accessing FKs doesn't fill in related objects
|
|
|
|
"""
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(8):
|
2010-10-12 11:33:19 +08:00
|
|
|
fly = Species.objects.get(name="melanogaster")
|
|
|
|
domain = fly.genus.family.order.klass.phylum.kingdom.domain
|
|
|
|
self.assertEqual(domain.name, 'Eukaryota')
|
2010-09-13 13:28:47 +08: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-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2012-12-30 05:19:47 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore", DeprecationWarning)
|
|
|
|
person = Species.objects.select_related(depth=10).get(name="sapiens")
|
2010-10-12 11:33:19 +08:00
|
|
|
domain = person.genus.family.order.klass.phylum.kingdom.domain
|
|
|
|
self.assertEqual(domain.name, 'Eukaryota')
|
2010-09-13 13:28:47 +08: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-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(9):
|
2010-10-12 11:33:19 +08:00
|
|
|
world = Species.objects.all()
|
|
|
|
families = [o.genus.family.name for o in world]
|
2010-12-02 05:35:54 +08:00
|
|
|
self.assertEqual(sorted(families), [
|
|
|
|
'Amanitacae',
|
2010-10-12 11:33:19 +08:00
|
|
|
'Drosophilidae',
|
|
|
|
'Fabaceae',
|
2010-12-02 05:35:54 +08:00
|
|
|
'Hominidae',
|
2010-10-12 11:33:19 +08:00
|
|
|
])
|
2010-09-13 13:28:47 +08: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-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
world = Species.objects.all().select_related()
|
|
|
|
families = [o.genus.family.name for o in world]
|
2010-12-02 05:35:54 +08:00
|
|
|
self.assertEqual(sorted(families), [
|
|
|
|
'Amanitacae',
|
2010-10-12 11:33:19 +08:00
|
|
|
'Drosophilidae',
|
|
|
|
'Fabaceae',
|
2010-12-02 05:35:54 +08:00
|
|
|
'Hominidae',
|
2010-10-12 11:33:19 +08:00
|
|
|
])
|
2010-09-13 13:28:47 +08:00
|
|
|
|
|
|
|
def test_depth(self, depth=1, expected=7):
|
|
|
|
"""
|
|
|
|
The "depth" argument to select_related() will stop the descent at a
|
|
|
|
particular level.
|
|
|
|
"""
|
2011-10-15 01:03:08 +08:00
|
|
|
# Notice: one fewer queries than above because of depth=1
|
|
|
|
with self.assertNumQueries(expected):
|
2012-12-30 05:19:47 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore", DeprecationWarning)
|
|
|
|
pea = Species.objects.select_related(depth=depth).get(name="sativum")
|
2010-10-12 11:33:19 +08:00
|
|
|
self.assertEqual(
|
|
|
|
pea.genus.family.order.klass.phylum.kingdom.domain.name,
|
|
|
|
'Eukaryota'
|
|
|
|
)
|
2010-09-13 13:28:47 +08:00
|
|
|
|
|
|
|
def test_larger_depth(self):
|
|
|
|
"""
|
|
|
|
The "depth" argument to select_related() will stop the descent at a
|
|
|
|
particular level. This tests a larger depth value.
|
|
|
|
"""
|
|
|
|
self.test_depth(depth=5, expected=3)
|
|
|
|
|
|
|
|
def test_list_with_depth(self):
|
|
|
|
"""
|
|
|
|
The "depth" argument to select_related() will stop the descent at a
|
|
|
|
particular level. This can be used on lists as well.
|
|
|
|
"""
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(5):
|
2012-12-30 05:19:47 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore", DeprecationWarning)
|
|
|
|
world = Species.objects.all().select_related(depth=2)
|
|
|
|
orders = [o.genus.family.order.name for o in world]
|
2010-12-02 05:35:54 +08:00
|
|
|
self.assertEqual(sorted(orders),
|
|
|
|
['Agaricales', 'Diptera', 'Fabales', 'Primates'])
|
2010-09-13 13:28:47 +08:00
|
|
|
|
|
|
|
def test_select_related_with_extra(self):
|
2012-12-30 05:19:47 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore", DeprecationWarning)
|
|
|
|
s = Species.objects.all().select_related(depth=1)\
|
|
|
|
.extra(select={'a': 'select_related_species.id + 10'})[0]
|
2010-09-13 13:28:47 +08:00
|
|
|
self.assertEqual(s.id + 10, s.a)
|
|
|
|
|
|
|
|
def test_certain_fields(self):
|
|
|
|
"""
|
|
|
|
The optional fields passed to select_related() control which related
|
|
|
|
models we pull in. This allows for smaller queries and can act as an
|
|
|
|
alternative (or, in addition to) the depth parameter.
|
|
|
|
|
|
|
|
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-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
world = Species.objects.select_related('genus__family')
|
|
|
|
families = [o.genus.family.name for o in world]
|
2010-12-02 05:35:54 +08:00
|
|
|
self.assertEqual(sorted(families),
|
|
|
|
['Amanitacae', 'Drosophilidae', 'Fabaceae', 'Hominidae'])
|
2010-09-13 13:28:47 +08: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-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(2):
|
2010-10-12 11:33:19 +08:00
|
|
|
world = Species.objects.filter(genus__name='Amanita')\
|
|
|
|
.select_related('genus__family')
|
|
|
|
orders = [o.genus.family.order.name for o in world]
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(orders, ['Agaricales'])
|
2010-09-13 13:28:47 +08:00
|
|
|
|
|
|
|
def test_field_traversal(self):
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
s = Species.objects.all().select_related('genus__family__order'
|
|
|
|
).order_by('id')[0:1].get().genus.family.order.name
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(s, 'Diptera')
|
2010-09-13 13:28:47 +08:00
|
|
|
|
|
|
|
def test_depth_fields_fails(self):
|
2012-12-30 05:19:47 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore", DeprecationWarning)
|
|
|
|
self.assertRaises(TypeError,
|
|
|
|
Species.objects.select_related,
|
|
|
|
'genus__family__order', depth=4
|
|
|
|
)
|
2013-05-30 23:05:42 +08:00
|
|
|
|
|
|
|
def test_none_clears_list(self):
|
|
|
|
queryset = Species.objects.select_related('genus').select_related(None)
|
|
|
|
self.assertEqual(queryset.query.select_related, False)
|