PageRenderTime 62ms CodeModel.GetById 30ms app.highlight 14ms RepoModel.GetById 16ms app.codeStats 0ms

/tests/regressiontests/select_related_onetoone/tests.py

https://code.google.com/p/mango-py/
Python | 90 lines | 76 code | 14 blank | 0 comment | 0 complexity | 46bf9c55392355bb871763ab04452209 MD5 | raw file
 1from django import db
 2from django.conf import settings
 3from django.test import TestCase
 4
 5from models import (User, UserProfile, UserStat, UserStatResult, StatDetails,
 6    AdvancedUserStat, Image, Product)
 7
 8class ReverseSelectRelatedTestCase(TestCase):
 9    def setUp(self):
10        user = User.objects.create(username="test")
11        userprofile = UserProfile.objects.create(user=user, state="KS",
12                                                 city="Lawrence")
13        results = UserStatResult.objects.create(results='first results')
14        userstat = UserStat.objects.create(user=user, posts=150,
15                                           results=results)
16        details = StatDetails.objects.create(base_stats=userstat, comments=259)
17
18        user2 = User.objects.create(username="bob")
19        results2 = UserStatResult.objects.create(results='moar results')
20        advstat = AdvancedUserStat.objects.create(user=user2, posts=200, karma=5,
21                                                  results=results2)
22        StatDetails.objects.create(base_stats=advstat, comments=250)
23
24    def test_basic(self):
25        def test():
26            u = User.objects.select_related("userprofile").get(username="test")
27            self.assertEqual(u.userprofile.state, "KS")
28        self.assertNumQueries(1, test)
29
30    def test_follow_next_level(self):
31        def test():
32            u = User.objects.select_related("userstat__results").get(username="test")
33            self.assertEqual(u.userstat.posts, 150)
34            self.assertEqual(u.userstat.results.results, 'first results')
35        self.assertNumQueries(1, test)
36
37    def test_follow_two(self):
38        def test():
39            u = User.objects.select_related("userprofile", "userstat").get(username="test")
40            self.assertEqual(u.userprofile.state, "KS")
41            self.assertEqual(u.userstat.posts, 150)
42        self.assertNumQueries(1, test)
43
44    def test_follow_two_next_level(self):
45        def test():
46            u = User.objects.select_related("userstat__results", "userstat__statdetails").get(username="test")
47            self.assertEqual(u.userstat.results.results, 'first results')
48            self.assertEqual(u.userstat.statdetails.comments, 259)
49        self.assertNumQueries(1, test)
50
51    def test_forward_and_back(self):
52        def test():
53            stat = UserStat.objects.select_related("user__userprofile").get(user__username="test")
54            self.assertEqual(stat.user.userprofile.state, 'KS')
55            self.assertEqual(stat.user.userstat.posts, 150)
56        self.assertNumQueries(1, test)
57
58    def test_back_and_forward(self):
59        def test():
60            u = User.objects.select_related("userstat").get(username="test")
61            self.assertEqual(u.userstat.user.username, 'test')
62        self.assertNumQueries(1, test)
63
64    def test_not_followed_by_default(self):
65        def test():
66            u = User.objects.select_related().get(username="test")
67            self.assertEqual(u.userstat.posts, 150)
68        self.assertNumQueries(2, test)
69
70    def test_follow_from_child_class(self):
71        def test():
72            stat = AdvancedUserStat.objects.select_related('user', 'statdetails').get(posts=200)
73            self.assertEqual(stat.statdetails.comments, 250)
74            self.assertEqual(stat.user.username, 'bob')
75        self.assertNumQueries(1, test)
76
77    def test_follow_inheritance(self):
78        def test():
79            stat = UserStat.objects.select_related('user', 'advanceduserstat').get(posts=200)
80            self.assertEqual(stat.advanceduserstat.posts, 200)
81            self.assertEqual(stat.user.username, 'bob')
82            self.assertEqual(stat.advanceduserstat.user.username, 'bob')
83        self.assertNumQueries(1, test)
84
85    def test_nullable_relation(self):
86        im = Image.objects.create(name="imag1")
87        p1 = Product.objects.create(name="Django Plushie", image=im)
88        p2 = Product.objects.create(name="Talking Django Plushie")
89
90        self.assertEqual(len(Product.objects.select_related("image")), 2)