2012-03-30 08:02:08 +00:00
|
|
|
from __future__ import absolute_import
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2010-01-27 13:30:29 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2011-10-13 21:34:56 +00:00
|
|
|
from .models import (User, UserProfile, UserStat, UserStatResult, StatDetails,
|
2010-02-23 19:24:27 +00:00
|
|
|
AdvancedUserStat, Image, Product)
|
2010-01-27 13:30:29 +00:00
|
|
|
|
2011-10-14 17:03:08 +00:00
|
|
|
|
2010-01-27 13:30:29 +00: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 15:02:59 +00:00
|
|
|
advstat = AdvancedUserStat.objects.create(user=user2, posts=200, karma=5,
|
2010-01-27 13:30:29 +00:00
|
|
|
results=results2)
|
|
|
|
StatDetails.objects.create(base_stats=advstat, comments=250)
|
|
|
|
|
|
|
|
def test_basic(self):
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 03:33:19 +00:00
|
|
|
u = User.objects.select_related("userprofile").get(username="test")
|
|
|
|
self.assertEqual(u.userprofile.state, "KS")
|
2010-01-27 13:30:29 +00:00
|
|
|
|
|
|
|
def test_follow_next_level(self):
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 03:33:19 +00: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 13:30:29 +00:00
|
|
|
|
|
|
|
def test_follow_two(self):
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 03:33:19 +00: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 13:30:29 +00:00
|
|
|
|
|
|
|
def test_follow_two_next_level(self):
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 03:33:19 +00: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 13:30:29 +00:00
|
|
|
|
|
|
|
def test_forward_and_back(self):
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 03:33:19 +00: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 13:30:29 +00:00
|
|
|
|
|
|
|
def test_back_and_forward(self):
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 03:33:19 +00:00
|
|
|
u = User.objects.select_related("userstat").get(username="test")
|
|
|
|
self.assertEqual(u.userstat.user.username, 'test')
|
2010-01-27 13:30:29 +00:00
|
|
|
|
|
|
|
def test_not_followed_by_default(self):
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(2):
|
2010-10-12 03:33:19 +00:00
|
|
|
u = User.objects.select_related().get(username="test")
|
|
|
|
self.assertEqual(u.userstat.posts, 150)
|
2010-01-27 13:30:29 +00:00
|
|
|
|
|
|
|
def test_follow_from_child_class(self):
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 03:33:19 +00: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 13:30:29 +00:00
|
|
|
|
|
|
|
def test_follow_inheritance(self):
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 03:33:19 +00: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 15:02:59 +00:00
|
|
|
|
2010-02-23 19:24:27 +00: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 15:02:59 +00:00
|
|
|
|
2012-04-10 12:29:25 +00:00
|
|
|
with self.assertNumQueries(1):
|
|
|
|
result = sorted(Product.objects.select_related("image"), key=lambda x: x.name)
|
|
|
|
self.assertEqual([p.name for p in result], ["Django Plushie", "Talking Django Plushie"])
|
|
|
|
|
|
|
|
self.assertEqual(p1.image, im)
|
|
|
|
# Check for ticket #13839
|
|
|
|
self.assertIsNone(p2.image)
|
|
|
|
|
|
|
|
def test_missing_reverse(self):
|
|
|
|
"""
|
|
|
|
Ticket #13839: select_related() should NOT cache None
|
|
|
|
for missing objects on a reverse 1-1 relation.
|
|
|
|
"""
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
user = User.objects.select_related('userprofile').get(username='bob')
|
|
|
|
with self.assertRaises(UserProfile.DoesNotExist):
|
|
|
|
user.userprofile
|
|
|
|
|
|
|
|
def test_nullable_missing_reverse(self):
|
|
|
|
"""
|
|
|
|
Ticket #13839: select_related() should NOT cache None
|
|
|
|
for missing objects on a reverse 0-1 relation.
|
|
|
|
"""
|
|
|
|
Image.objects.create(name="imag1")
|
|
|
|
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
image = Image.objects.select_related('product').get()
|
|
|
|
with self.assertRaises(Product.DoesNotExist):
|
|
|
|
image.product
|