2012-03-30 16:02:08 +08:00
|
|
|
from __future__ import absolute_import
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2010-01-27 21:30:29 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2011-10-14 05:34:56 +08:00
|
|
|
from .models import (User, UserProfile, UserStat, UserStatResult, StatDetails,
|
2010-02-24 03:24:27 +08:00
|
|
|
AdvancedUserStat, Image, Product)
|
2010-01-27 21:30:29 +08:00
|
|
|
|
2011-10-15 01:03:08 +08:00
|
|
|
|
2010-01-27 21:30:29 +08:00
|
|
|
class ReverseSelectRelatedTestCase(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
user = User.objects.create(username="test")
|
|
|
|
userprofile = UserProfile.objects.create(user=user, state="KS",
|
|
|
|
city="Lawrence")
|
|
|
|
results = UserStatResult.objects.create(results='first results')
|
|
|
|
userstat = UserStat.objects.create(user=user, posts=150,
|
|
|
|
results=results)
|
|
|
|
details = StatDetails.objects.create(base_stats=userstat, comments=259)
|
|
|
|
|
|
|
|
user2 = User.objects.create(username="bob")
|
|
|
|
results2 = UserStatResult.objects.create(results='moar results')
|
2010-03-20 23:02:59 +08:00
|
|
|
advstat = AdvancedUserStat.objects.create(user=user2, posts=200, karma=5,
|
2010-01-27 21:30:29 +08:00
|
|
|
results=results2)
|
|
|
|
StatDetails.objects.create(base_stats=advstat, comments=250)
|
|
|
|
|
|
|
|
def test_basic(self):
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
u = User.objects.select_related("userprofile").get(username="test")
|
|
|
|
self.assertEqual(u.userprofile.state, "KS")
|
2010-01-27 21:30:29 +08:00
|
|
|
|
|
|
|
def test_follow_next_level(self):
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
u = User.objects.select_related("userstat__results").get(username="test")
|
|
|
|
self.assertEqual(u.userstat.posts, 150)
|
|
|
|
self.assertEqual(u.userstat.results.results, 'first results')
|
2010-01-27 21:30:29 +08:00
|
|
|
|
|
|
|
def test_follow_two(self):
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
u = User.objects.select_related("userprofile", "userstat").get(username="test")
|
|
|
|
self.assertEqual(u.userprofile.state, "KS")
|
|
|
|
self.assertEqual(u.userstat.posts, 150)
|
2010-01-27 21:30:29 +08:00
|
|
|
|
|
|
|
def test_follow_two_next_level(self):
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
u = User.objects.select_related("userstat__results", "userstat__statdetails").get(username="test")
|
|
|
|
self.assertEqual(u.userstat.results.results, 'first results')
|
|
|
|
self.assertEqual(u.userstat.statdetails.comments, 259)
|
2010-01-27 21:30:29 +08:00
|
|
|
|
|
|
|
def test_forward_and_back(self):
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
stat = UserStat.objects.select_related("user__userprofile").get(user__username="test")
|
|
|
|
self.assertEqual(stat.user.userprofile.state, 'KS')
|
|
|
|
self.assertEqual(stat.user.userstat.posts, 150)
|
2010-01-27 21:30:29 +08:00
|
|
|
|
|
|
|
def test_back_and_forward(self):
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
u = User.objects.select_related("userstat").get(username="test")
|
|
|
|
self.assertEqual(u.userstat.user.username, 'test')
|
2010-01-27 21:30:29 +08:00
|
|
|
|
|
|
|
def test_not_followed_by_default(self):
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(2):
|
2010-10-12 11:33:19 +08:00
|
|
|
u = User.objects.select_related().get(username="test")
|
|
|
|
self.assertEqual(u.userstat.posts, 150)
|
2010-01-27 21:30:29 +08:00
|
|
|
|
|
|
|
def test_follow_from_child_class(self):
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
stat = AdvancedUserStat.objects.select_related('user', 'statdetails').get(posts=200)
|
|
|
|
self.assertEqual(stat.statdetails.comments, 250)
|
|
|
|
self.assertEqual(stat.user.username, 'bob')
|
2010-01-27 21:30:29 +08:00
|
|
|
|
|
|
|
def test_follow_inheritance(self):
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
stat = UserStat.objects.select_related('user', 'advanceduserstat').get(posts=200)
|
|
|
|
self.assertEqual(stat.advanceduserstat.posts, 200)
|
|
|
|
self.assertEqual(stat.user.username, 'bob')
|
|
|
|
self.assertEqual(stat.advanceduserstat.user.username, 'bob')
|
2010-03-20 23:02:59 +08:00
|
|
|
|
2010-02-24 03:24:27 +08:00
|
|
|
def test_nullable_relation(self):
|
|
|
|
im = Image.objects.create(name="imag1")
|
|
|
|
p1 = Product.objects.create(name="Django Plushie", image=im)
|
|
|
|
p2 = Product.objects.create(name="Talking Django Plushie")
|
2010-03-20 23:02:59 +08:00
|
|
|
|
2010-02-24 03:24:27 +08:00
|
|
|
self.assertEqual(len(Product.objects.select_related("image")), 2)
|