89 lines
3.6 KiB
Python
89 lines
3.6 KiB
Python
from __future__ import absolute_import
|
|
|
|
from django.test import TestCase
|
|
|
|
from .models import Tournament, Pool, PoolStyle
|
|
|
|
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):
|
|
tournaments = list(Tournament.objects.prefetch_related('pool_set'))
|
|
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)
|
|
|
|
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):
|
|
poolstyles = list(PoolStyle.objects.select_related('pool'))
|
|
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):
|
|
poolstyles = list(PoolStyle.objects.prefetch_related('pool'))
|
|
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):
|
|
pools = list(Pool.objects.select_related('poolstyle'))
|
|
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):
|
|
pools = list(Pool.objects.prefetch_related('poolstyle'))
|
|
self.assertIs(pools[1], pools[1].poolstyle.pool)
|
|
self.assertIs(pools[2], pools[2].poolstyle.pool)
|