#NEWLINE# def test_latest_book( self ) : #NEWLINE# #INDENT# author1 = Author . objects . create ( name = 'Johnny' ) #NEWLINE# author2 = Author . objects . create ( name = 'Johnny' ) #NEWLINE# for i in range ( 3 , 6 ) : #NEWLINE# #INDENT# Book . objects . create ( name = ( 'Book %s' % i ) , author = author1 ) #NEWLINE# time . sleep ( 0.1 ) #NEWLINE# #UNINDENT# for i in Author . objects . prefetch ( 'latest_book' ) . filter ( pk = author1 . pk ) : #NEWLINE# #INDENT# self . assertTrue ( hasattr ( i , 'prefetched_latest_book' ) ) #NEWLINE# self . assertEqual ( i . latest_book . name , 'Book 5' , i . latest_book . name ) #NEWLINE# #UNINDENT# for i in Author . objects . prefetch ( 'latest_book_as_class' ) . filter ( pk = author1 . pk ) : #NEWLINE# #INDENT# self . assertTrue ( hasattr ( i , 'prefetched_latest_book' ) ) #NEWLINE# self . assertEqual ( i . latest_book . name , 'Book 5' , i ) #NEWLINE# #UNINDENT# for i in Author . objects . prefetch ( 'latest_book_as_class' ) . filter ( pk = author2 . pk ) : #NEWLINE# #INDENT# self . assertTrue ( hasattr ( author1 , 'prefetched_latest_book' ) ) #NEWLINE# self . assertEqual ( i . latest_book , None , i ) #NEWLINE# #UNINDENT# for i in Author . objects . filter ( pk = author1 . pk ) : #NEWLINE# #INDENT# self . assertFalse ( hasattr ( i , 'prefetched_latest_book' ) ) #NEWLINE# self . assertEqual ( i . latest_book . name , 'Book 5' , i ) #NEWLINE# #UNINDENT# for i in Author . objects . filter ( pk = author2 . pk ) : #NEWLINE# #INDENT# self . assertFalse ( hasattr ( i , 'prefetched_latest_book' ) ) #NEWLINE# self . assertEqual ( i . latest_book , None , i )