#NEWLINE# def test_latest_n_books( self ) : #NEWLINE# #INDENT# author1 = Author . objects . create ( name = 'Johnny' ) #NEWLINE# for i in range ( 20 , 30 ) : #NEWLINE# #INDENT# Book . objects . create ( name = ( 'Book %s' % i ) , author = author1 ) #NEWLINE# time . sleep ( 0.05 ) #NEWLINE# #UNINDENT# for i in Author . objects . prefetch ( 'latest_n_books' ) . filter ( pk = author1 . pk ) : #NEWLINE# #INDENT# self . assertTrue ( hasattr ( i , 'prefetched_latest_2_books' ) ) #NEWLINE# self . assertEqual ( [ j . name for j in i . prefetched_latest_2_books ] , [ 'Book 29' , 'Book 28' ] ) #NEWLINE# #UNINDENT# for i in Author . objects . prefetch ( P ( 'latest_n_books' ) ) . filter ( pk = author1 . pk ) : #NEWLINE# #INDENT# self . assertTrue ( hasattr ( i , 'prefetched_latest_2_books' ) ) #NEWLINE# self . assertEqual ( [ j . name for j in i . prefetched_latest_2_books ] , [ 'Book 29' , 'Book 28' ] ) #NEWLINE# #UNINDENT# for i in Author . objects . prefetch ( P ( 'latest_n_books' , 5 ) ) . filter ( pk = author1 . pk ) : #NEWLINE# #INDENT# self . assertTrue ( hasattr ( i , 'prefetched_latest_5_books' ) ) #NEWLINE# self . assertEqual ( [ j . name for j in i . prefetched_latest_5_books ] , [ 'Book 29' , 'Book 28' , 'Book 27' , 'Book 26' , 'Book 25' ] ) #NEWLINE# #UNINDENT# for i in Author . objects . prefetch ( P ( 'latest_n_books' , count = 5 ) ) . filter ( pk = author1 . pk ) : #NEWLINE# #INDENT# self . assertTrue ( hasattr ( i , 'prefetched_latest_5_books' ) ) #NEWLINE# self . assertEqual ( [ j . name for j in i . prefetched_latest_5_books ] , [ 'Book 29' , 'Book 28' , 'Book 27' , 'Book 26' , 'Book 25' ] )