2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2012-05-24 19:25:01 +08:00
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import Organiser, Pool, PoolStyle, Tournament
|
2012-05-24 19:25:01 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-05-24 19:25:01 +08:00
|
|
|
class ExistingRelatedInstancesTests(TestCase):
|
|
|
|
fixtures = ['tournament.json']
|
|
|
|
|
|
|
|
def test_foreign_key(self):
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
tournament = Tournament.objects.get(pk=1)
|
|
|
|
pool = tournament.pool_set.all()[0]
|
|
|
|
self.assertIs(tournament, pool.tournament)
|
|
|
|
|
|
|
|
def test_foreign_key_prefetch_related(self):
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
tournament = (Tournament.objects.prefetch_related('pool_set').get(pk=1))
|
|
|
|
pool = tournament.pool_set.all()[0]
|
|
|
|
self.assertIs(tournament, pool.tournament)
|
|
|
|
|
|
|
|
def test_foreign_key_multiple_prefetch(self):
|
|
|
|
with self.assertNumQueries(2):
|
2013-01-03 05:11:32 +08:00
|
|
|
tournaments = list(Tournament.objects.prefetch_related('pool_set').order_by('pk'))
|
2012-05-24 19:25:01 +08:00
|
|
|
pool1 = tournaments[0].pool_set.all()[0]
|
|
|
|
self.assertIs(tournaments[0], pool1.tournament)
|
|
|
|
pool2 = tournaments[1].pool_set.all()[0]
|
|
|
|
self.assertIs(tournaments[1], pool2.tournament)
|
|
|
|
|
2013-01-03 04:42:52 +08:00
|
|
|
def test_queryset_or(self):
|
|
|
|
tournament_1 = Tournament.objects.get(pk=1)
|
|
|
|
tournament_2 = Tournament.objects.get(pk=2)
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
pools = tournament_1.pool_set.all() | tournament_2.pool_set.all()
|
|
|
|
related_objects = set(pool.tournament for pool in pools)
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(related_objects, {tournament_1, tournament_2})
|
2013-01-03 04:42:52 +08:00
|
|
|
|
|
|
|
def test_queryset_or_different_cached_items(self):
|
|
|
|
tournament = Tournament.objects.get(pk=1)
|
|
|
|
organiser = Organiser.objects.get(pk=1)
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
pools = tournament.pool_set.all() | organiser.pool_set.all()
|
|
|
|
first = pools.filter(pk=1)[0]
|
|
|
|
self.assertIs(first.tournament, tournament)
|
|
|
|
self.assertIs(first.organiser, organiser)
|
|
|
|
|
|
|
|
def test_queryset_or_only_one_with_precache(self):
|
|
|
|
tournament_1 = Tournament.objects.get(pk=1)
|
|
|
|
tournament_2 = Tournament.objects.get(pk=2)
|
|
|
|
# 2 queries here as pool id 3 has tournament 2, which is not cached
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
pools = tournament_1.pool_set.all() | Pool.objects.filter(pk=3)
|
|
|
|
related_objects = set(pool.tournament for pool in pools)
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(related_objects, {tournament_1, tournament_2})
|
2013-01-03 04:42:52 +08:00
|
|
|
# and the other direction
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
pools = Pool.objects.filter(pk=3) | tournament_1.pool_set.all()
|
|
|
|
related_objects = set(pool.tournament for pool in pools)
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(related_objects, {tournament_1, tournament_2})
|
2013-01-03 04:42:52 +08:00
|
|
|
|
|
|
|
def test_queryset_and(self):
|
|
|
|
tournament = Tournament.objects.get(pk=1)
|
|
|
|
organiser = Organiser.objects.get(pk=1)
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
pools = tournament.pool_set.all() & organiser.pool_set.all()
|
|
|
|
first = pools.filter(pk=1)[0]
|
|
|
|
self.assertIs(first.tournament, tournament)
|
|
|
|
self.assertIs(first.organiser, organiser)
|
|
|
|
|
2012-05-24 19:25:01 +08:00
|
|
|
def test_one_to_one(self):
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
style = PoolStyle.objects.get(pk=1)
|
|
|
|
pool = style.pool
|
|
|
|
self.assertIs(style, pool.poolstyle)
|
|
|
|
|
|
|
|
def test_one_to_one_select_related(self):
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
style = PoolStyle.objects.select_related('pool').get(pk=1)
|
|
|
|
pool = style.pool
|
|
|
|
self.assertIs(style, pool.poolstyle)
|
|
|
|
|
|
|
|
def test_one_to_one_multi_select_related(self):
|
|
|
|
with self.assertNumQueries(1):
|
2013-01-03 05:11:32 +08:00
|
|
|
poolstyles = list(PoolStyle.objects.select_related('pool').order_by('pk'))
|
2012-05-24 19:25:01 +08:00
|
|
|
self.assertIs(poolstyles[0], poolstyles[0].pool.poolstyle)
|
|
|
|
self.assertIs(poolstyles[1], poolstyles[1].pool.poolstyle)
|
|
|
|
|
|
|
|
def test_one_to_one_prefetch_related(self):
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
style = PoolStyle.objects.prefetch_related('pool').get(pk=1)
|
|
|
|
pool = style.pool
|
|
|
|
self.assertIs(style, pool.poolstyle)
|
|
|
|
|
|
|
|
def test_one_to_one_multi_prefetch_related(self):
|
|
|
|
with self.assertNumQueries(2):
|
2013-01-03 05:11:32 +08:00
|
|
|
poolstyles = list(PoolStyle.objects.prefetch_related('pool').order_by('pk'))
|
2012-05-24 19:25:01 +08:00
|
|
|
self.assertIs(poolstyles[0], poolstyles[0].pool.poolstyle)
|
|
|
|
self.assertIs(poolstyles[1], poolstyles[1].pool.poolstyle)
|
|
|
|
|
|
|
|
def test_reverse_one_to_one(self):
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
pool = Pool.objects.get(pk=2)
|
|
|
|
style = pool.poolstyle
|
|
|
|
self.assertIs(pool, style.pool)
|
|
|
|
|
|
|
|
def test_reverse_one_to_one_select_related(self):
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
pool = Pool.objects.select_related('poolstyle').get(pk=2)
|
|
|
|
style = pool.poolstyle
|
|
|
|
self.assertIs(pool, style.pool)
|
|
|
|
|
|
|
|
def test_reverse_one_to_one_prefetch_related(self):
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
pool = Pool.objects.prefetch_related('poolstyle').get(pk=2)
|
|
|
|
style = pool.poolstyle
|
|
|
|
self.assertIs(pool, style.pool)
|
|
|
|
|
|
|
|
def test_reverse_one_to_one_multi_select_related(self):
|
|
|
|
with self.assertNumQueries(1):
|
2013-01-03 05:11:32 +08:00
|
|
|
pools = list(Pool.objects.select_related('poolstyle').order_by('pk'))
|
2012-05-24 19:25:01 +08:00
|
|
|
self.assertIs(pools[1], pools[1].poolstyle.pool)
|
|
|
|
self.assertIs(pools[2], pools[2].poolstyle.pool)
|
|
|
|
|
|
|
|
def test_reverse_one_to_one_multi_prefetch_related(self):
|
|
|
|
with self.assertNumQueries(2):
|
2013-01-03 05:11:32 +08:00
|
|
|
pools = list(Pool.objects.prefetch_related('poolstyle').order_by('pk'))
|
2012-05-24 19:25:01 +08:00
|
|
|
self.assertIs(pools[1], pools[1].poolstyle.pool)
|
|
|
|
self.assertIs(pools[2], pools[2].poolstyle.pool)
|