<s> from django import db <EOL> from django . conf import settings <EOL> from django . test import TestCase <EOL> from models import ( User , UserProfile , UserStat , UserStatResult , StatDetails , <EOL> AdvancedUserStat , Image , Product ) <EOL> class ReverseSelectRelatedTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . old_debug = settings . DEBUG <EOL> settings . DEBUG = True <EOL> user = User . objects . create ( username = "<STR_LIT:test>" ) <EOL> userprofile = UserProfile . objects . create ( user = user , state = "<STR_LIT>" , <EOL> city = "<STR_LIT>" ) <EOL> results = UserStatResult . objects . create ( results = '<STR_LIT>' ) <EOL> userstat = UserStat . objects . create ( user = user , posts = <NUM_LIT> , <EOL> results = results ) <EOL> details = StatDetails . objects . create ( base_stats = userstat , comments = <NUM_LIT> ) <EOL> user2 = User . objects . create ( username = "<STR_LIT>" ) <EOL> results2 = UserStatResult . objects . create ( results = '<STR_LIT>' ) <EOL> advstat = AdvancedUserStat . objects . create ( user = user2 , posts = <NUM_LIT:200> , karma = <NUM_LIT:5> , <EOL> results = results2 ) <EOL> StatDetails . objects . create ( base_stats = advstat , comments = <NUM_LIT> ) <EOL> db . reset_queries ( ) <EOL> def assertQueries ( self , queries ) : <EOL> self . assertEqual ( len ( db . connection . queries ) , queries ) <EOL> def tearDown ( self ) : <EOL> settings . DEBUG = self . old_debug <EOL> def test_basic ( self ) : <EOL> u = User . objects . select_related ( "<STR_LIT>" ) . get ( username = "<STR_LIT:test>" ) <EOL> self . assertEqual ( u . userprofile . state , "<STR_LIT>" ) <EOL> self . assertQueries ( <NUM_LIT:1> ) <EOL> def test_follow_next_level ( self ) : <EOL> u = User . objects . select_related ( "<STR_LIT>" ) . get ( username = "<STR_LIT:test>" ) <EOL> self . assertEqual ( u . userstat . posts , <NUM_LIT> ) <EOL> self . assertEqual ( u . userstat . results . results , '<STR_LIT>' ) <EOL> self . assertQueries ( <NUM_LIT:1> ) <EOL> def test_follow_two ( self ) : <EOL> u = User . objects . select_related ( "<STR_LIT>" , "<STR_LIT>" ) . get ( username = "<STR_LIT:test>" ) <EOL> self . assertEqual ( u . userprofile . state , "<STR_LIT>" ) <EOL> self . assertEqual ( u . userstat . posts , <NUM_LIT> ) <EOL> self . assertQueries ( <NUM_LIT:1> ) <EOL> def test_follow_two_next_level ( self ) : <EOL> u = User . objects . select_related ( "<STR_LIT>" , "<STR_LIT>" ) . get ( username = "<STR_LIT:test>" ) <EOL> self . assertEqual ( u . userstat . results . results , '<STR_LIT>' ) <EOL> self . assertEqual ( u . userstat . statdetails . comments , <NUM_LIT> ) <EOL> self . assertQueries ( <NUM_LIT:1> ) <EOL> def test_forward_and_back ( self ) : <EOL> stat = UserStat . objects . select_related ( "<STR_LIT>" ) . get ( user__username = "<STR_LIT:test>" ) <EOL> self . assertEqual ( stat . user . userprofile . state , '<STR_LIT>' ) <EOL> self . assertEqual ( stat . user . userstat . posts , <NUM_LIT> ) <EOL> self . assertQueries ( <NUM_LIT:1> ) <EOL> def test_back_and_forward ( self ) : <EOL> u = User . objects . select_related ( "<STR_LIT>" ) . get ( username = "<STR_LIT:test>" ) <EOL> self . assertEqual ( u . userstat . user . username , '<STR_LIT:test>' ) <EOL> self . assertQueries ( <NUM_LIT:1> ) <EOL> def test_not_followed_by_default ( self ) : <EOL> u = User . objects . select_related ( ) . get ( username = "<STR_LIT:test>" ) <EOL> self . assertEqual ( u . userstat . posts , <NUM_LIT> ) <EOL> self . assertQueries ( <NUM_LIT:2> ) <EOL> def test_follow_from_child_class ( self ) : <EOL> stat = AdvancedUserStat . objects . select_related ( '<STR_LIT:user>' , '<STR_LIT>' ) . get ( posts = <NUM_LIT:200> ) <EOL> self . assertEqual ( stat . statdetails . comments , <NUM_LIT> ) <EOL> self . assertEqual ( stat . user . username , '<STR_LIT>' ) <EOL> self . assertQueries ( <NUM_LIT:1> ) <EOL> def test_follow_inheritance ( self ) : <EOL> stat = UserStat . objects . select_related ( '<STR_LIT:user>' , '<STR_LIT>' ) . get ( posts = <NUM_LIT:200> ) <EOL> self . assertEqual ( stat . advanceduserstat . posts , <NUM_LIT:200> ) <EOL> self . assertEqual ( stat . user . username , '<STR_LIT>' ) <EOL> self . assertEqual ( stat . advanceduserstat . user . username , '<STR_LIT>' ) <EOL> self . assertQueries ( <NUM_LIT:1> ) <EOL> def test_nullable_relation ( self ) : <EOL> im = Image . objects . create ( name = "<STR_LIT>" ) <EOL> p1 = Product . objects . create ( name = "<STR_LIT>" , image = im ) <EOL> p2 = Product . objects . create ( name = "<STR_LIT>" ) <EOL> self . assertEqual ( len ( Product . objects . select_related ( "<STR_LIT:image>" ) ) , <NUM_LIT:2> ) </s>
