<s> from __future__ import unicode_literals <EOL> from django . core . exceptions import FieldError <EOL> from django . test import SimpleTestCase , TestCase <EOL> from . models import ( <EOL> Bookmark , Domain , Family , Genus , HybridSpecies , Kingdom , Klass , Order , <EOL> Phylum , Pizza , Species , TaggedItem , <EOL> ) <EOL> class SelectRelatedTests ( TestCase ) : <EOL> @ classmethod <EOL> def create_tree ( cls , stringtree ) : <EOL> """<STR_LIT>""" <EOL> names = stringtree . split ( ) <EOL> models = [ Domain , Kingdom , Phylum , Klass , Order , Family , Genus , Species ] <EOL> assert len ( names ) == len ( models ) , ( names , models ) <EOL> parent = None <EOL> for name , model in zip ( names , models ) : <EOL> try : <EOL> obj = model . objects . get ( name = name ) <EOL> except model . DoesNotExist : <EOL> obj = model ( name = name ) <EOL> if parent : <EOL> setattr ( obj , parent . __class__ . __name__ . lower ( ) , parent ) <EOL> obj . save ( ) <EOL> parent = obj <EOL> @ classmethod <EOL> def setUpTestData ( cls ) : <EOL> cls . create_tree ( "<STR_LIT>" ) <EOL> cls . create_tree ( "<STR_LIT>" ) <EOL> cls . create_tree ( "<STR_LIT>" ) <EOL> cls . create_tree ( "<STR_LIT>" ) <EOL> def test_access_fks_without_select_related ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertNumQueries ( <NUM_LIT:8> ) : <EOL> fly = Species . objects . get ( name = "<STR_LIT>" ) <EOL> domain = fly . genus . family . order . klass . phylum . kingdom . domain <EOL> self . assertEqual ( domain . name , '<STR_LIT>' ) <EOL> def test_access_fks_with_select_related ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> person = ( <EOL> Species . objects <EOL> . select_related ( '<STR_LIT>' ) <EOL> . get ( name = "<STR_LIT>" ) <EOL> ) <EOL> domain = person . genus . family . order . klass . phylum . kingdom . domain <EOL> self . assertEqual ( domain . name , '<STR_LIT>' ) <EOL> def test_list_without_select_related ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertNumQueries ( <NUM_LIT:9> ) : <EOL> world = Species . objects . all ( ) <EOL> families = [ o . genus . family . name for o in world ] <EOL> self . assertEqual ( sorted ( families ) , [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def test_list_with_select_related ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> world = Species . objects . all ( ) . select_related ( ) <EOL> families = [ o . genus . family . name for o in world ] <EOL> self . assertEqual ( sorted ( families ) , [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def test_list_with_depth ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertNumQueries ( <NUM_LIT:5> ) : <EOL> world = Species . objects . all ( ) . select_related ( '<STR_LIT>' ) <EOL> orders = [ o . genus . family . order . name for o in world ] <EOL> self . assertEqual ( sorted ( orders ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_select_related_with_extra ( self ) : <EOL> s = ( Species . objects . all ( ) <EOL> . select_related ( ) <EOL> . extra ( select = { '<STR_LIT:a>' : '<STR_LIT>' } ) [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( s . id + <NUM_LIT:10> , s . a ) <EOL> def test_certain_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> world = Species . objects . select_related ( '<STR_LIT>' ) <EOL> families = [ o . genus . family . name for o in world ] <EOL> self . assertEqual ( sorted ( families ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_more_certain_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> world = Species . objects . filter ( genus__name = '<STR_LIT>' ) . select_related ( '<STR_LIT>' ) <EOL> orders = [ o . genus . family . order . name for o in world ] <EOL> self . assertEqual ( orders , [ '<STR_LIT>' ] ) <EOL> def test_field_traversal ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> s = ( Species . objects . all ( ) <EOL> . select_related ( '<STR_LIT>' ) <EOL> . order_by ( '<STR_LIT:id>' ) [ <NUM_LIT:0> : <NUM_LIT:1> ] . get ( ) . genus . family . order . name ) <EOL> self . assertEqual ( s , '<STR_LIT>' ) <EOL> def test_depth_fields_fails ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> Species . objects . select_related ( '<STR_LIT>' , depth = <NUM_LIT:4> ) <EOL> def test_none_clears_list ( self ) : <EOL> queryset = Species . objects . select_related ( '<STR_LIT>' ) . select_related ( None ) <EOL> self . assertEqual ( queryset . query . select_related , False ) <EOL> def test_chaining ( self ) : <EOL> parent_1 , parent_2 = Species . objects . all ( ) [ : <NUM_LIT:2> ] <EOL> HybridSpecies . objects . create ( name = '<STR_LIT>' , parent_1 = parent_1 , parent_2 = parent_2 ) <EOL> queryset = HybridSpecies . objects . select_related ( '<STR_LIT>' ) . select_related ( '<STR_LIT>' ) <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> obj = queryset [ <NUM_LIT:0> ] <EOL> self . assertEqual ( obj . parent_1 , parent_1 ) <EOL> self . assertEqual ( obj . parent_2 , parent_2 ) <EOL> def test_select_related_after_values ( self ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> with self . assertRaisesMessage ( TypeError , message ) : <EOL> list ( Species . objects . values ( '<STR_LIT:name>' ) . select_related ( '<STR_LIT>' ) ) <EOL> def test_select_related_after_values_list ( self ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> with self . assertRaisesMessage ( TypeError , message ) : <EOL> list ( Species . objects . values_list ( '<STR_LIT:name>' ) . select_related ( '<STR_LIT>' ) ) <EOL> class SelectRelatedValidationTests ( SimpleTestCase ) : <EOL> """<STR_LIT>""" <EOL> non_relational_error = "<STR_LIT>" <EOL> invalid_error = "<STR_LIT>" <EOL> def test_non_relational_field ( self ) : <EOL> with self . assertRaisesMessage ( FieldError , self . non_relational_error % ( '<STR_LIT:name>' , '<STR_LIT>' ) ) : <EOL> list ( Species . objects . select_related ( '<STR_LIT>' ) ) <EOL> with self . assertRaisesMessage ( FieldError , self . non_relational_error % ( '<STR_LIT:name>' , '<STR_LIT>' ) ) : <EOL> list ( Species . objects . select_related ( '<STR_LIT:name>' ) ) <EOL> with self . assertRaisesMessage ( FieldError , self . non_relational_error % ( '<STR_LIT:name>' , '<STR_LIT>' ) ) : <EOL> list ( Domain . objects . select_related ( '<STR_LIT:name>' ) ) <EOL> def test_non_relational_field_nested ( self ) : <EOL> with self . assertRaisesMessage ( FieldError , self . non_relational_error % ( '<STR_LIT:name>' , '<STR_LIT>' ) ) : <EOL> list ( Species . objects . select_related ( '<STR_LIT>' ) ) <EOL> def test_many_to_many_field ( self ) : <EOL> with self . assertRaisesMessage ( FieldError , self . invalid_error % ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> list ( Pizza . objects . select_related ( '<STR_LIT>' ) ) <EOL> def test_reverse_relational_field ( self ) : <EOL> with self . assertRaisesMessage ( FieldError , self . invalid_error % ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> list ( Species . objects . select_related ( '<STR_LIT>' ) ) <EOL> def test_invalid_field ( self ) : <EOL> with self . assertRaisesMessage ( FieldError , self . invalid_error % ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> list ( Species . objects . select_related ( '<STR_LIT>' ) ) <EOL> with self . assertRaisesMessage ( FieldError , self . invalid_error % ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> list ( Species . objects . select_related ( '<STR_LIT>' ) ) <EOL> with self . assertRaisesMessage ( FieldError , self . invalid_error % ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> list ( Domain . objects . select_related ( '<STR_LIT>' ) ) <EOL> def test_generic_relations ( self ) : <EOL> with self . assertRaisesMessage ( FieldError , self . invalid_error % ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> list ( Bookmark . objects . select_related ( '<STR_LIT>' ) ) <EOL> with self . assertRaisesMessage ( FieldError , self . invalid_error % ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> list ( TaggedItem . objects . select_related ( '<STR_LIT>' ) ) </s>
