<s> from __future__ import absolute_import , unicode_literals <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db import connection <EOL> from django . db . models . query import get_prefetcher <EOL> from django . test import TestCase <EOL> from django . test . utils import override_settings <EOL> from django . utils import six <EOL> from django . utils . encoding import force_text <EOL> from . models import ( Author , Book , Reader , Qualification , Teacher , Department , <EOL> TaggedItem , Bookmark , AuthorAddress , FavoriteAuthors , AuthorWithAge , <EOL> BookWithYear , BookReview , Person , House , Room , Employee , Comment , <EOL> LessonEntry , WordEntry , Author2 ) <EOL> class PrefetchRelatedTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . book1 = Book . objects . create ( title = "<STR_LIT>" ) <EOL> self . book2 = Book . objects . create ( title = "<STR_LIT>" ) <EOL> self . book3 = Book . objects . create ( title = "<STR_LIT>" ) <EOL> self . book4 = Book . objects . create ( title = "<STR_LIT>" ) <EOL> self . author1 = Author . objects . create ( name = "<STR_LIT>" , <EOL> first_book = self . book1 ) <EOL> self . author2 = Author . objects . create ( name = "<STR_LIT>" , <EOL> first_book = self . book1 ) <EOL> self . author3 = Author . objects . create ( name = "<STR_LIT>" , <EOL> first_book = self . book1 ) <EOL> self . author4 = Author . objects . create ( name = "<STR_LIT>" , <EOL> first_book = self . book4 ) <EOL> self . book1 . authors . add ( self . author1 , self . author2 , self . author3 ) <EOL> self . book2 . authors . add ( self . author1 ) <EOL> self . book3 . authors . add ( self . author3 ) <EOL> self . book4 . authors . add ( self . author4 ) <EOL> self . reader1 = Reader . objects . create ( name = "<STR_LIT>" ) <EOL> self . reader2 = Reader . objects . create ( name = "<STR_LIT>" ) <EOL> self . reader1 . books_read . add ( self . book1 , self . book4 ) <EOL> self . reader2 . books_read . add ( self . book2 , self . book4 ) <EOL> def test_m2m_forward ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> lists = [ list ( b . authors . all ( ) ) for b in Book . objects . prefetch_related ( '<STR_LIT>' ) ] <EOL> normal_lists = [ list ( b . authors . all ( ) ) for b in Book . objects . all ( ) ] <EOL> self . assertEqual ( lists , normal_lists ) <EOL> def test_m2m_reverse ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> lists = [ list ( a . books . all ( ) ) for a in Author . objects . prefetch_related ( '<STR_LIT>' ) ] <EOL> normal_lists = [ list ( a . books . all ( ) ) for a in Author . objects . all ( ) ] <EOL> self . assertEqual ( lists , normal_lists ) <EOL> def test_foreignkey_forward ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> books = [ a . first_book for a in Author . objects . prefetch_related ( '<STR_LIT>' ) ] <EOL> normal_books = [ a . first_book for a in Author . objects . all ( ) ] <EOL> self . assertEqual ( books , normal_books ) <EOL> def test_foreignkey_reverse ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> lists = [ list ( b . first_time_authors . all ( ) ) <EOL> for b in Book . objects . prefetch_related ( '<STR_LIT>' ) ] <EOL> self . assertQuerysetEqual ( self . book2 . authors . all ( ) , [ "<STR_LIT>" ] ) <EOL> def test_onetoone_reverse_no_match ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> book = Book . objects . prefetch_related ( '<STR_LIT>' ) . all ( ) [ <NUM_LIT:0> ] <EOL> with self . assertNumQueries ( <NUM_LIT:0> ) : <EOL> with self . assertRaises ( BookWithYear . DoesNotExist ) : <EOL> book . bookwithyear <EOL> def test_survives_clone ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> lists = [ list ( b . first_time_authors . all ( ) ) <EOL> for b in Book . objects . prefetch_related ( '<STR_LIT>' ) . exclude ( id = <NUM_LIT:1000> ) ] <EOL> def test_len ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> qs = Book . objects . prefetch_related ( '<STR_LIT>' ) <EOL> length = len ( qs ) <EOL> lists = [ list ( b . first_time_authors . all ( ) ) <EOL> for b in qs ] <EOL> def test_bool ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> qs = Book . objects . prefetch_related ( '<STR_LIT>' ) <EOL> x = bool ( qs ) <EOL> lists = [ list ( b . first_time_authors . all ( ) ) <EOL> for b in qs ] <EOL> def test_count ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> qs = Book . objects . prefetch_related ( '<STR_LIT>' ) <EOL> [ b . first_time_authors . count ( ) for b in qs ] <EOL> def test_exists ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> qs = Book . objects . prefetch_related ( '<STR_LIT>' ) <EOL> [ b . first_time_authors . exists ( ) for b in qs ] <EOL> def test_in_and_prefetch_related ( self ) : <EOL> """<STR_LIT>""" <EOL> qs = Book . objects . prefetch_related ( '<STR_LIT>' ) <EOL> self . assertTrue ( qs [ <NUM_LIT:0> ] in qs ) <EOL> def test_clear ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertNumQueries ( <NUM_LIT:5> ) : <EOL> with_prefetch = Author . objects . prefetch_related ( '<STR_LIT>' ) <EOL> without_prefetch = with_prefetch . prefetch_related ( None ) <EOL> lists = [ list ( a . books . all ( ) ) for a in without_prefetch ] <EOL> def test_m2m_then_m2m ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> qs = Author . objects . prefetch_related ( '<STR_LIT>' ) <EOL> lists = [ [ [ six . text_type ( r ) for r in b . read_by . all ( ) ] <EOL> for b in a . books . all ( ) ] <EOL> for a in qs ] <EOL> self . assertEqual ( lists , <EOL> [ <EOL> [ [ "<STR_LIT>" ] , [ "<STR_LIT>" ] ] , <EOL> [ [ "<STR_LIT>" ] ] , <EOL> [ [ "<STR_LIT>" ] , [ ] ] , <EOL> [ [ "<STR_LIT>" , "<STR_LIT>" ] ] , <EOL> ] ) <EOL> def test_overriding_prefetch ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> qs = Author . objects . prefetch_related ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> lists = [ [ [ six . text_type ( r ) for r in b . read_by . all ( ) ] <EOL> for b in a . books . all ( ) ] <EOL> for a in qs ] <EOL> self . assertEqual ( lists , <EOL> [ <EOL> [ [ "<STR_LIT>" ] , [ "<STR_LIT>" ] ] , <EOL> [ [ "<STR_LIT>" ] ] , <EOL> [ [ "<STR_LIT>" ] , [ ] ] , <EOL> [ [ "<STR_LIT>" , "<STR_LIT>" ] ] , <EOL> ] ) <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> qs = Author . objects . prefetch_related ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> lists = [ [ [ six . text_type ( r ) for r in b . read_by . all ( ) ] <EOL> for b in a . books . all ( ) ] <EOL> for a in qs ] <EOL> self . assertEqual ( lists , <EOL> [ <EOL> [ [ "<STR_LIT>" ] , [ "<STR_LIT>" ] ] , <EOL> [ [ "<STR_LIT>" ] ] , <EOL> [ [ "<STR_LIT>" ] , [ ] ] , <EOL> [ [ "<STR_LIT>" , "<STR_LIT>" ] ] , <EOL> ] ) <EOL> def test_get ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> author = Author . objects . prefetch_related ( '<STR_LIT>' ) . get ( name = "<STR_LIT>" ) <EOL> lists = [ [ six . text_type ( r ) for r in b . read_by . all ( ) ] <EOL> for b in author . books . all ( ) ] <EOL> self . assertEqual ( lists , [ [ "<STR_LIT>" ] , [ "<STR_LIT>" ] ] ) <EOL> def test_foreign_key_then_m2m ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> qs = Author . objects . select_related ( '<STR_LIT>' ) . prefetch_related ( '<STR_LIT>' ) <EOL> lists = [ [ six . text_type ( r ) for r in a . first_book . read_by . all ( ) ] <EOL> for a in qs ] <EOL> self . assertEqual ( lists , [ [ "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] ] ) <EOL> def test_attribute_error ( self ) : <EOL> qs = Reader . objects . all ( ) . prefetch_related ( '<STR_LIT>' ) <EOL> with self . assertRaises ( AttributeError ) as cm : <EOL> list ( qs ) <EOL> self . assertTrue ( '<STR_LIT>' in str ( cm . exception ) ) <EOL> def test_invalid_final_lookup ( self ) : <EOL> qs = Book . objects . prefetch_related ( '<STR_LIT>' ) <EOL> with self . assertRaises ( ValueError ) as cm : <EOL> list ( qs ) <EOL> self . assertTrue ( '<STR_LIT>' in str ( cm . exception ) ) <EOL> self . assertTrue ( "<STR_LIT:name>" in str ( cm . exception ) ) <EOL> class DefaultManagerTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . qual1 = Qualification . objects . create ( name = "<STR_LIT>" ) <EOL> self . qual2 = Qualification . objects . create ( name = "<STR_LIT>" ) <EOL> self . qual3 = Qualification . objects . create ( name = "<STR_LIT>" ) <EOL> self . qual4 = Qualification . objects . create ( name = "<STR_LIT>" ) <EOL> self . teacher1 = Teacher . objects . create ( name = "<STR_LIT>" ) <EOL> self . teacher2 = Teacher . objects . create ( name = "<STR_LIT>" ) <EOL> self . teacher3 = Teacher . objects . create ( name = "<STR_LIT>" ) <EOL> self . teacher1 . qualifications . add ( self . qual1 , self . qual2 , self . qual3 , self . qual4 ) <EOL> self . teacher2 . qualifications . add ( self . qual1 ) <EOL> self . teacher3 . qualifications . add ( self . qual2 ) <EOL> self . dept1 = Department . objects . create ( name = "<STR_LIT>" ) <EOL> self . dept2 = Department . objects . create ( name = "<STR_LIT>" ) <EOL> self . dept1 . teachers . add ( self . teacher1 , self . teacher2 ) <EOL> self . dept2 . teachers . add ( self . teacher1 , self . teacher3 ) <EOL> def test_m2m_then_m2m ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> qs = Department . objects . prefetch_related ( '<STR_LIT>' ) <EOL> depts = "<STR_LIT>" . join ( [ "<STR_LIT>" % <EOL> ( dept . name , "<STR_LIT:U+002CU+0020>" . join ( six . text_type ( t ) for t in dept . teachers . all ( ) ) ) <EOL> for dept in qs ] ) <EOL> self . assertEqual ( depts , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class GenericRelationTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> book1 = Book . objects . create ( title = "<STR_LIT>" ) <EOL> book2 = Book . objects . create ( title = "<STR_LIT>" ) <EOL> book3 = Book . objects . create ( title = "<STR_LIT>" ) <EOL> reader1 = Reader . objects . create ( name = "<STR_LIT>" ) <EOL> reader2 = Reader . objects . create ( name = "<STR_LIT>" ) <EOL> reader3 = Reader . objects . create ( name = "<STR_LIT>" ) <EOL> book1 . read_by . add ( reader1 , reader2 ) <EOL> book2 . read_by . add ( reader2 ) <EOL> book3 . read_by . add ( reader3 ) <EOL> self . book1 , self . book2 , self . book3 = book1 , book2 , book3 <EOL> self . reader1 , self . reader2 , self . reader3 = reader1 , reader2 , reader3 <EOL> def test_prefetch_GFK ( self ) : <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . book1 ) <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . reader1 ) <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . book2 ) <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . reader3 ) <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> qs = TaggedItem . objects . prefetch_related ( '<STR_LIT>' ) <EOL> list ( qs ) <EOL> def test_prefetch_GFK_nonint_pk ( self ) : <EOL> Comment . objects . create ( comment = "<STR_LIT>" , content_object = self . book1 ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> qs = Comment . objects . prefetch_related ( '<STR_LIT>' ) <EOL> [ c . content_object for c in qs ] <EOL> def test_traverse_GFK ( self ) : <EOL> """<STR_LIT>""" <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . book1 ) <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . book2 ) <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . book3 ) <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . reader1 ) <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . reader2 ) <EOL> ct = ContentType . objects . get_for_model ( Book ) <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> qs = TaggedItem . objects . filter ( content_type = ct , tag = '<STR_LIT>' ) . prefetch_related ( '<STR_LIT>' ) <EOL> readers_of_awesome_books = set ( [ r . name for tag in qs <EOL> for r in tag . content_object . read_by . all ( ) ] ) <EOL> self . assertEqual ( readers_of_awesome_books , set ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) ) <EOL> def test_nullable_GFK ( self ) : <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . book1 , <EOL> created_by = self . reader1 ) <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . book2 ) <EOL> TaggedItem . objects . create ( tag = "<STR_LIT>" , content_object = self . book3 ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> result = [ t . created_by for t in TaggedItem . objects . prefetch_related ( '<STR_LIT>' ) ] <EOL> self . assertEqual ( result , <EOL> [ t . created_by for t in TaggedItem . objects . all ( ) ] ) <EOL> def test_generic_relation ( self ) : <EOL> b = Bookmark . objects . create ( url = '<STR_LIT>' ) <EOL> t1 = TaggedItem . objects . create ( content_object = b , tag = '<STR_LIT>' ) <EOL> t2 = TaggedItem . objects . create ( content_object = b , tag = '<STR_LIT>' ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> tags = [ t . tag for b in Bookmark . objects . prefetch_related ( '<STR_LIT>' ) <EOL> for t in b . tags . all ( ) ] <EOL> self . assertEqual ( sorted ( tags ) , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def test_charfield_GFK ( self ) : <EOL> b = Bookmark . objects . create ( url = '<STR_LIT>' ) <EOL> t1 = TaggedItem . objects . create ( content_object = b , tag = '<STR_LIT>' ) <EOL> t2 = TaggedItem . objects . create ( content_object = b , favorite = b , tag = '<STR_LIT>' ) <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> bookmark = Bookmark . objects . filter ( pk = b . pk ) . prefetch_related ( '<STR_LIT>' , '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( sorted ( [ i . tag for i in bookmark . tags . all ( ) ] ) , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . assertEqual ( [ i . tag for i in bookmark . favorite_tags . all ( ) ] , [ "<STR_LIT>" ] ) <EOL> class MultiTableInheritanceTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . book1 = BookWithYear . objects . create ( <EOL> title = "<STR_LIT>" , published_year = <NUM_LIT> ) <EOL> self . book2 = BookWithYear . objects . create ( <EOL> title = "<STR_LIT>" , published_year = <NUM_LIT> ) <EOL> self . author1 = AuthorWithAge . objects . create ( <EOL> name = '<STR_LIT>' , first_book = self . book1 , age = <NUM_LIT:50> ) <EOL> self . author2 = AuthorWithAge . objects . create ( <EOL> name = '<STR_LIT>' , first_book = self . book1 , age = <NUM_LIT> ) <EOL> self . author3 = AuthorWithAge . objects . create ( <EOL> name = '<STR_LIT>' , first_book = self . book2 , age = <NUM_LIT> ) <EOL> self . authorAddress = AuthorAddress . objects . create ( <EOL> author = self . author1 , address = '<STR_LIT>' ) <EOL> self . book2 . aged_authors . add ( self . author2 , self . author3 ) <EOL> self . br1 = BookReview . objects . create ( <EOL> book = self . book1 , notes = "<STR_LIT>" ) <EOL> self . br2 = BookReview . objects . create ( <EOL> book = self . book2 , notes = "<STR_LIT>" ) <EOL> def test_foreignkey ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> qs = AuthorWithAge . objects . prefetch_related ( '<STR_LIT>' ) <EOL> addresses = [ [ six . text_type ( address ) for address in obj . addresses . all ( ) ] <EOL> for obj in qs ] <EOL> self . assertEqual ( addresses , [ [ six . text_type ( self . authorAddress ) ] , [ ] , [ ] ] ) <EOL> def test_foreignkey_to_inherited ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> qs = BookReview . objects . prefetch_related ( '<STR_LIT>' ) <EOL> titles = [ obj . book . title for obj in qs ] <EOL> self . assertEqual ( titles , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def test_m2m_to_inheriting_model ( self ) : <EOL> qs = AuthorWithAge . objects . prefetch_related ( '<STR_LIT>' ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> lst = [ [ six . text_type ( book ) for book in author . books_with_year . all ( ) ] <EOL> for author in qs ] <EOL> qs = AuthorWithAge . objects . all ( ) <EOL> lst2 = [ [ six . text_type ( book ) for book in author . books_with_year . all ( ) ] <EOL> for author in qs ] <EOL> self . assertEqual ( lst , lst2 ) <EOL> qs = BookWithYear . objects . prefetch_related ( '<STR_LIT>' ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> lst = [ [ six . text_type ( author ) for author in book . aged_authors . all ( ) ] <EOL> for book in qs ] <EOL> qs = BookWithYear . objects . all ( ) <EOL> lst2 = [ [ six . text_type ( author ) for author in book . aged_authors . all ( ) ] <EOL> for book in qs ] <EOL> self . assertEqual ( lst , lst2 ) <EOL> def test_parent_link_prefetch ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> [ a . author for a in AuthorWithAge . objects . prefetch_related ( '<STR_LIT>' ) ] <EOL> @ override_settings ( DEBUG = True ) <EOL> def test_child_link_prefetch ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> l = [ a . authorwithage for a in Author . objects . prefetch_related ( '<STR_LIT>' ) ] <EOL> self . assertIn ( '<STR_LIT>' , connection . queries [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] . lower ( ) ) <EOL> self . assertIn ( '<STR_LIT>' , connection . queries [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( l , [ a . authorwithage for a in Author . objects . all ( ) ] ) <EOL> class ForeignKeyToFieldTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . book = Book . objects . create ( title = "<STR_LIT>" ) <EOL> self . author1 = Author . objects . create ( name = '<STR_LIT>' , first_book = self . book ) <EOL> self . author2 = Author . objects . create ( name = '<STR_LIT>' , first_book = self . book ) <EOL> self . author3 = Author . objects . create ( name = '<STR_LIT>' , first_book = self . book ) <EOL> self . authorAddress = AuthorAddress . objects . create ( <EOL> author = self . author1 , address = '<STR_LIT>' <EOL> ) <EOL> FavoriteAuthors . objects . create ( author = self . author1 , <EOL> likes_author = self . author2 ) <EOL> FavoriteAuthors . objects . create ( author = self . author2 , <EOL> likes_author = self . author3 ) <EOL> FavoriteAuthors . objects . create ( author = self . author3 , <EOL> likes_author = self . author1 ) <EOL> def test_foreignkey ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> qs = Author . objects . prefetch_related ( '<STR_LIT>' ) <EOL> addresses = [ [ six . text_type ( address ) for address in obj . addresses . all ( ) ] <EOL> for obj in qs ] <EOL> self . assertEqual ( addresses , [ [ six . text_type ( self . authorAddress ) ] , [ ] , [ ] ] ) <EOL> def test_m2m ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> qs = Author . objects . all ( ) . prefetch_related ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> favorites = [ ( <EOL> [ six . text_type ( i_like ) for i_like in author . favorite_authors . all ( ) ] , <EOL> [ six . text_type ( likes_me ) for likes_me in author . favors_me . all ( ) ] <EOL> ) for author in qs ] <EOL> self . assertEqual ( <EOL> favorites , <EOL> [ <EOL> ( [ six . text_type ( self . author2 ) ] , [ six . text_type ( self . author3 ) ] ) , <EOL> ( [ six . text_type ( self . author3 ) ] , [ six . text_type ( self . author1 ) ] ) , <EOL> ( [ six . text_type ( self . author1 ) ] , [ six . text_type ( self . author2 ) ] ) <EOL> ] <EOL> ) <EOL> class LookupOrderingTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . person1 = Person . objects . create ( name = "<STR_LIT>" ) <EOL> self . person2 = Person . objects . create ( name = "<STR_LIT>" ) <EOL> self . house1 = House . objects . create ( address = "<STR_LIT>" ) <EOL> self . house2 = House . objects . create ( address = "<STR_LIT>" ) <EOL> self . house3 = House . objects . create ( address = "<STR_LIT>" ) <EOL> self . house4 = House . objects . create ( address = "<STR_LIT>" ) <EOL> self . room1_1 = Room . objects . create ( name = "<STR_LIT>" , house = self . house1 ) <EOL> self . room1_2 = Room . objects . create ( name = "<STR_LIT>" , house = self . house1 ) <EOL> self . room1_3 = Room . objects . create ( name = "<STR_LIT>" , house = self . house1 ) <EOL> self . room2_1 = Room . objects . create ( name = "<STR_LIT>" , house = self . house2 ) <EOL> self . room2_2 = Room . objects . create ( name = "<STR_LIT>" , house = self . house2 ) <EOL> self . room3_1 = Room . objects . create ( name = "<STR_LIT>" , house = self . house3 ) <EOL> self . room3_2 = Room . objects . create ( name = "<STR_LIT>" , house = self . house3 ) <EOL> self . room3_3 = Room . objects . create ( name = "<STR_LIT>" , house = self . house3 ) <EOL> self . room4_1 = Room . objects . create ( name = "<STR_LIT>" , house = self . house4 ) <EOL> self . room4_2 = Room . objects . create ( name = "<STR_LIT>" , house = self . house4 ) <EOL> self . person1 . houses . add ( self . house1 , self . house2 ) <EOL> self . person2 . houses . add ( self . house3 , self . house4 ) <EOL> def test_order ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:4> ) : <EOL> qs = Person . objects . prefetch_related ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> [ list ( p . primary_house . occupants . all ( ) ) for p in qs ] <EOL> class NullableTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> boss = Employee . objects . create ( name = "<STR_LIT>" ) <EOL> worker1 = Employee . objects . create ( name = "<STR_LIT>" , boss = boss ) <EOL> worker2 = Employee . objects . create ( name = "<STR_LIT>" , boss = boss ) <EOL> def test_traverse_nullable ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> qs = Employee . objects . select_related ( '<STR_LIT>' ) . prefetch_related ( '<STR_LIT>' ) <EOL> co_serfs = [ list ( e . boss . serfs . all ( ) ) if e . boss is not None else [ ] <EOL> for e in qs ] <EOL> qs2 = Employee . objects . select_related ( '<STR_LIT>' ) <EOL> co_serfs2 = [ list ( e . boss . serfs . all ( ) ) if e . boss is not None else [ ] <EOL> for e in qs2 ] <EOL> self . assertEqual ( co_serfs , co_serfs2 ) <EOL> def test_prefetch_nullable ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> qs = Employee . objects . prefetch_related ( '<STR_LIT>' ) <EOL> co_serfs = [ list ( e . boss . serfs . all ( ) ) if e . boss is not None else [ ] <EOL> for e in qs ] <EOL> qs2 = Employee . objects . all ( ) <EOL> co_serfs2 = [ list ( e . boss . serfs . all ( ) ) if e . boss is not None else [ ] <EOL> for e in qs2 ] <EOL> self . assertEqual ( co_serfs , co_serfs2 ) <EOL> def test_in_bulk ( self ) : <EOL> """<STR_LIT>""" <EOL> boss1 = Employee . objects . create ( name = "<STR_LIT>" ) <EOL> boss2 = Employee . objects . create ( name = "<STR_LIT>" ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> bulk = Employee . objects . prefetch_related ( '<STR_LIT>' ) . in_bulk ( [ boss1 . pk , boss2 . pk ] ) <EOL> for b in bulk . values ( ) : <EOL> list ( b . serfs . all ( ) ) <EOL> class MultiDbTests ( TestCase ) : <EOL> multi_db = True <EOL> def test_using_is_honored_m2m ( self ) : <EOL> B = Book . objects . using ( '<STR_LIT>' ) <EOL> A = Author . objects . using ( '<STR_LIT>' ) <EOL> book1 = B . create ( title = "<STR_LIT>" ) <EOL> book2 = B . create ( title = "<STR_LIT>" ) <EOL> book3 = B . create ( title = "<STR_LIT>" ) <EOL> book4 = B . create ( title = "<STR_LIT>" ) <EOL> author1 = A . create ( name = "<STR_LIT>" , first_book = book1 ) <EOL> author2 = A . create ( name = "<STR_LIT>" , first_book = book1 ) <EOL> author3 = A . create ( name = "<STR_LIT>" , first_book = book1 ) <EOL> author4 = A . create ( name = "<STR_LIT>" , first_book = book4 ) <EOL> book1 . authors . add ( author1 , author2 , author3 ) <EOL> book2 . authors . add ( author1 ) <EOL> book3 . authors . add ( author3 ) <EOL> book4 . authors . add ( author4 ) <EOL> qs1 = B . prefetch_related ( '<STR_LIT>' ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> , using = '<STR_LIT>' ) : <EOL> books = "<STR_LIT>" . join ( [ "<STR_LIT>" % <EOL> ( book . title , "<STR_LIT:U+002CU+0020>" . join ( a . name for a in book . authors . all ( ) ) ) <EOL> for book in qs1 ] ) <EOL> self . assertEqual ( books , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> qs2 = A . prefetch_related ( '<STR_LIT>' ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> , using = '<STR_LIT>' ) : <EOL> authors = "<STR_LIT>" . join ( [ "<STR_LIT>" % <EOL> ( author . name , "<STR_LIT:U+002CU+0020>" . join ( b . title for b in author . books . all ( ) ) ) <EOL> for author in qs2 ] ) <EOL> self . assertEqual ( authors , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_using_is_honored_fkey ( self ) : <EOL> B = Book . objects . using ( '<STR_LIT>' ) <EOL> A = Author . objects . using ( '<STR_LIT>' ) <EOL> book1 = B . create ( title = "<STR_LIT>" ) <EOL> book2 = B . create ( title = "<STR_LIT>" ) <EOL> author1 = A . create ( name = "<STR_LIT>" , first_book = book1 ) <EOL> author2 = A . create ( name = "<STR_LIT>" , first_book = book2 ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> , using = '<STR_LIT>' ) : <EOL> books = "<STR_LIT:U+002CU+0020>" . join ( a . first_book . title for a in A . prefetch_related ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( "<STR_LIT>" , books ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> , using = '<STR_LIT>' ) : <EOL> books = "<STR_LIT>" . join ( "<STR_LIT>" % <EOL> ( b . title , "<STR_LIT:U+002CU+0020>" . join ( a . name for a in b . first_time_authors . all ( ) ) ) <EOL> for b in B . prefetch_related ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( books , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_using_is_honored_inheritance ( self ) : <EOL> B = BookWithYear . objects . using ( '<STR_LIT>' ) <EOL> A = AuthorWithAge . objects . using ( '<STR_LIT>' ) <EOL> book1 = B . create ( title = "<STR_LIT>" , published_year = <NUM_LIT> ) <EOL> book2 = B . create ( title = "<STR_LIT>" , published_year = <NUM_LIT> ) <EOL> author1 = A . create ( name = '<STR_LIT>' , first_book = book1 , age = <NUM_LIT:50> ) <EOL> author2 = A . create ( name = '<STR_LIT>' , first_book = book1 , age = <NUM_LIT> ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> , using = '<STR_LIT>' ) : <EOL> authors = "<STR_LIT:U+002CU+0020>" . join ( a . author . name for a in A . prefetch_related ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( authors , "<STR_LIT>" ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> , using = '<STR_LIT>' ) : <EOL> ages = "<STR_LIT:U+002CU+0020>" . join ( str ( a . authorwithage . age ) for a in A . prefetch_related ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( ages , "<STR_LIT>" ) <EOL> class Ticket19607Tests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> for id , name1 , name2 in [ <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] : <EOL> LessonEntry . objects . create ( id = id , name1 = name1 , name2 = name2 ) <EOL> for id , lesson_entry_id , name in [ <EOL> ( <NUM_LIT:1> , <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:3> , <NUM_LIT:2> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:4> , <NUM_LIT:2> , '<STR_LIT>' ) , <EOL> ] : <EOL> WordEntry . objects . create ( id = id , lesson_entry_id = lesson_entry_id , name = name ) <EOL> def test_bug ( self ) : <EOL> list ( WordEntry . objects . prefetch_related ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> class Ticket21410Tests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . book1 = Book . objects . create ( title = "<STR_LIT>" ) <EOL> self . book2 = Book . objects . create ( title = "<STR_LIT>" ) <EOL> self . book3 = Book . objects . create ( title = "<STR_LIT>" ) <EOL> self . book4 = Book . objects . create ( title = "<STR_LIT>" ) <EOL> self . author1 = Author2 . objects . create ( name = "<STR_LIT>" , <EOL> first_book = self . book1 ) <EOL> self . author2 = Author2 . objects . create ( name = "<STR_LIT>" , <EOL> first_book = self . book1 ) <EOL> self . author3 = Author2 . objects . create ( name = "<STR_LIT>" , <EOL> first_book = self . book1 ) <EOL> self . author4 = Author2 . objects . create ( name = "<STR_LIT>" , <EOL> first_book = self . book4 ) <EOL> self . author1 . favorite_books . add ( self . book1 , self . book2 , self . book3 ) <EOL> self . author2 . favorite_books . add ( self . book1 ) <EOL> self . author3 . favorite_books . add ( self . book2 ) <EOL> self . author4 . favorite_books . add ( self . book3 ) <EOL> def test_bug ( self ) : <EOL> list ( Author2 . objects . prefetch_related ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> class Ticket21760Tests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . rooms = [ ] <EOL> for _ in range ( <NUM_LIT:3> ) : <EOL> house = House . objects . create ( ) <EOL> for _ in range ( <NUM_LIT:3> ) : <EOL> self . rooms . append ( Room . objects . create ( house = house ) ) <EOL> def test_bug ( self ) : <EOL> prefetcher = get_prefetcher ( self . rooms [ <NUM_LIT:0> ] , '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> queryset = prefetcher . get_prefetch_queryset ( list ( Room . objects . all ( ) ) ) [ <NUM_LIT:0> ] <EOL> self . assertNotIn ( '<STR_LIT>' , force_text ( queryset . query ) ) </s>
