<s> from __future__ import absolute_import , unicode_literals <EOL> from operator import attrgetter <EOL> from django . core . exceptions import FieldError <EOL> from django . db import connection <EOL> from django . test import TestCase <EOL> from django . test . utils import CaptureQueriesContext <EOL> from django . utils import six <EOL> from . models import ( Chef , CommonInfo , ItalianRestaurant , ParkingLot , Place , <EOL> Post , Restaurant , Student , StudentWorker , Supplier , Worker , MixinModel ) <EOL> class ModelInheritanceTests ( TestCase ) : <EOL> def test_abstract ( self ) : <EOL> w1 = Worker . objects . create ( name = "<STR_LIT>" , age = <NUM_LIT> , job = "<STR_LIT>" ) <EOL> w2 = Worker . objects . create ( name = "<STR_LIT>" , age = <NUM_LIT> , job = "<STR_LIT>" ) <EOL> s = Student . objects . create ( name = "<STR_LIT>" , age = <NUM_LIT:5> , school_class = "<STR_LIT>" ) <EOL> self . assertEqual ( six . text_type ( w1 ) , "<STR_LIT>" ) <EOL> self . assertEqual ( six . text_type ( s ) , "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> Worker . objects . values ( "<STR_LIT:name>" ) , [ <EOL> { "<STR_LIT:name>" : "<STR_LIT>" } , <EOL> { "<STR_LIT:name>" : "<STR_LIT>" } , <EOL> ] , <EOL> lambda o : o <EOL> ) <EOL> self . assertEqual ( Student . _meta . ordering , [ ] ) <EOL> self . assertRaises ( AttributeError , lambda : CommonInfo . objects . all ( ) ) <EOL> self . assertRaises ( Student . DoesNotExist , <EOL> StudentWorker . objects . get , pk = <NUM_LIT> <EOL> ) <EOL> self . assertRaises ( Worker . DoesNotExist , <EOL> StudentWorker . objects . get , pk = <NUM_LIT> <EOL> ) <EOL> sw1 = StudentWorker ( ) <EOL> sw1 . name = "<STR_LIT>" <EOL> sw1 . age = <NUM_LIT> <EOL> sw1 . save ( ) <EOL> sw2 = StudentWorker ( ) <EOL> sw2 . name = "<STR_LIT>" <EOL> sw2 . age = <NUM_LIT> <EOL> sw2 . save ( ) <EOL> self . assertRaises ( Student . MultipleObjectsReturned , <EOL> StudentWorker . objects . get , pk__lt = sw2 . pk + <NUM_LIT:100> <EOL> ) <EOL> self . assertRaises ( Worker . MultipleObjectsReturned , <EOL> StudentWorker . objects . get , pk__lt = sw2 . pk + <NUM_LIT:100> <EOL> ) <EOL> def test_multiple_table ( self ) : <EOL> post = Post . objects . create ( title = "<STR_LIT>" ) <EOL> post . attached_comment_set . create ( content = "<STR_LIT>" , is_spam = True ) <EOL> post . attached_link_set . create ( <EOL> content = "<STR_LIT>" , <EOL> url = "<STR_LIT>" <EOL> ) <EOL> self . assertRaises ( AttributeError , <EOL> getattr , post , "<STR_LIT>" <EOL> ) <EOL> p1 = Place . objects . create ( name = "<STR_LIT>" , address = "<STR_LIT>" ) <EOL> p2 = Place . objects . create ( name = "<STR_LIT>" , address = "<STR_LIT>" ) <EOL> r = Restaurant . objects . create ( <EOL> name = "<STR_LIT>" , <EOL> address = "<STR_LIT>" , <EOL> serves_hot_dogs = True , <EOL> serves_pizza = False , <EOL> rating = <NUM_LIT:2> <EOL> ) <EOL> c = Chef . objects . create ( name = "<STR_LIT>" ) <EOL> ir = ItalianRestaurant . objects . create ( <EOL> name = "<STR_LIT>" , <EOL> address = "<STR_LIT>" , <EOL> serves_hot_dogs = False , <EOL> serves_pizza = False , <EOL> serves_gnocchi = True , <EOL> rating = <NUM_LIT:4> , <EOL> chef = c <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> ItalianRestaurant . objects . filter ( address = "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) <EOL> ) <EOL> ir . address = "<STR_LIT>" <EOL> ir . save ( ) <EOL> self . assertQuerysetEqual ( <EOL> ItalianRestaurant . objects . filter ( address = "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) <EOL> ) <EOL> self . assertEqual ( <EOL> [ f . name for f in Restaurant . _meta . fields ] , <EOL> [ "<STR_LIT:id>" , "<STR_LIT:name>" , "<STR_LIT:address>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> ) <EOL> self . assertEqual ( <EOL> [ f . name for f in ItalianRestaurant . _meta . fields ] , <EOL> [ "<STR_LIT:id>" , "<STR_LIT:name>" , "<STR_LIT:address>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> ) <EOL> self . assertEqual ( Restaurant . _meta . ordering , [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( Place . objects . filter ( supplier__name = "<STR_LIT:foo>" ) , [ ] ) <EOL> self . assertRaises ( FieldError , <EOL> Restaurant . objects . filter , supplier__name = "<STR_LIT:foo>" <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> Restaurant . objects . filter ( name = "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> ItalianRestaurant . objects . filter ( address = "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) <EOL> ) <EOL> p = Place . objects . get ( name = "<STR_LIT>" ) <EOL> self . assertIs ( type ( p ) , Place ) <EOL> self . assertEqual ( <EOL> p . restaurant , Restaurant . objects . get ( name = "<STR_LIT>" ) <EOL> ) <EOL> self . assertEqual ( <EOL> Place . objects . get ( name = "<STR_LIT>" ) . restaurant . italianrestaurant , <EOL> ItalianRestaurant . objects . get ( name = "<STR_LIT>" ) <EOL> ) <EOL> self . assertEqual ( <EOL> Restaurant . objects . get ( name = "<STR_LIT>" ) . italianrestaurant , <EOL> ItalianRestaurant . objects . get ( name = "<STR_LIT>" ) <EOL> ) <EOL> self . assertRaises ( ItalianRestaurant . DoesNotExist , <EOL> lambda : p . restaurant . italianrestaurant <EOL> ) <EOL> self . assertRaises ( Place . DoesNotExist , <EOL> ItalianRestaurant . objects . get , name = "<STR_LIT>" <EOL> ) <EOL> self . assertRaises ( Place . MultipleObjectsReturned , <EOL> Restaurant . objects . get , id__lt = <NUM_LIT> <EOL> ) <EOL> s1 = Supplier . objects . create ( name = "<STR_LIT>" , address = "<STR_LIT>" ) <EOL> s1 . customers = [ r , ir ] <EOL> s2 = Supplier . objects . create ( name = "<STR_LIT>" , address = "<STR_LIT>" ) <EOL> s2 . customers = [ ir ] <EOL> p = Place . objects . get ( name = "<STR_LIT>" ) <EOL> self . assertRaises ( Restaurant . DoesNotExist , <EOL> lambda : p . restaurant <EOL> ) <EOL> self . assertEqual ( p . supplier , s1 ) <EOL> self . assertQuerysetEqual ( <EOL> ir . provider . order_by ( "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> Restaurant . objects . filter ( provider__name__contains = "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> ItalianRestaurant . objects . filter ( provider__name__contains = "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) , <EOL> ) <EOL> park1 = ParkingLot . objects . create ( <EOL> name = "<STR_LIT>" , address = "<STR_LIT>" , main_site = s1 <EOL> ) <EOL> park2 = ParkingLot . objects . create ( <EOL> name = "<STR_LIT>" , address = "<STR_LIT>" , main_site = ir <EOL> ) <EOL> self . assertEqual ( <EOL> Restaurant . objects . get ( lot__name = "<STR_LIT>" ) . name , <EOL> "<STR_LIT>" <EOL> ) <EOL> rows = Restaurant . objects . filter ( <EOL> serves_hot_dogs = True , name__contains = "<STR_LIT:D>" <EOL> ) . update ( <EOL> name = "<STR_LIT>" , serves_hot_dogs = False <EOL> ) <EOL> self . assertEqual ( rows , <NUM_LIT:1> ) <EOL> r1 = Restaurant . objects . get ( pk = r . pk ) <EOL> self . assertFalse ( r1 . serves_hot_dogs ) <EOL> self . assertEqual ( r1 . name , "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> ItalianRestaurant . objects . values ( "<STR_LIT:name>" , "<STR_LIT>" ) , [ <EOL> { "<STR_LIT>" : <NUM_LIT:4> , "<STR_LIT:name>" : "<STR_LIT>" } <EOL> ] , <EOL> lambda o : o <EOL> ) <EOL> self . assertNumQueries ( <NUM_LIT:2> , <EOL> lambda : ItalianRestaurant . objects . all ( ) [ <NUM_LIT:0> ] . chef <EOL> ) <EOL> self . assertNumQueries ( <NUM_LIT:1> , <EOL> lambda : ItalianRestaurant . objects . select_related ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . chef <EOL> ) <EOL> def test_select_related_defer ( self ) : <EOL> """<STR_LIT>""" <EOL> Restaurant . objects . create ( <EOL> name = "<STR_LIT>" , <EOL> address = "<STR_LIT>" , <EOL> serves_hot_dogs = True , <EOL> serves_pizza = False , <EOL> rating = <NUM_LIT:2> , <EOL> ) <EOL> ItalianRestaurant . objects . create ( <EOL> name = "<STR_LIT>" , <EOL> address = "<STR_LIT>" , <EOL> serves_hot_dogs = False , <EOL> serves_pizza = False , <EOL> serves_gnocchi = True , <EOL> rating = <NUM_LIT:4> , <EOL> ) <EOL> qs = ( Restaurant . objects <EOL> . select_related ( "<STR_LIT>" ) <EOL> . defer ( "<STR_LIT>" ) <EOL> . order_by ( "<STR_LIT>" ) ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> objs = list ( qs . all ( ) ) <EOL> self . assertTrue ( objs [ <NUM_LIT:1> ] . italianrestaurant . serves_gnocchi ) <EOL> self . assertEqual ( qs [ <NUM_LIT:0> ] . name , '<STR_LIT>' ) <EOL> self . assertEqual ( qs [ <NUM_LIT:0> ] . rating , <NUM_LIT:2> ) <EOL> self . assertEqual ( qs [ <NUM_LIT:1> ] . italianrestaurant . name , '<STR_LIT>' ) <EOL> self . assertEqual ( qs [ <NUM_LIT:1> ] . italianrestaurant . rating , <NUM_LIT:4> ) <EOL> def test_mixin_init ( self ) : <EOL> m = MixinModel ( ) <EOL> self . assertEqual ( m . other_attr , <NUM_LIT:1> ) <EOL> def test_update_query_counts ( self ) : <EOL> """<STR_LIT>""" <EOL> c = Chef . objects . create ( name = "<STR_LIT>" ) <EOL> ir = ItalianRestaurant . objects . create ( <EOL> name = "<STR_LIT>" , <EOL> address = "<STR_LIT>" , <EOL> serves_hot_dogs = False , <EOL> serves_pizza = False , <EOL> serves_gnocchi = True , <EOL> rating = <NUM_LIT:4> , <EOL> chef = c <EOL> ) <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> ir . save ( ) <EOL> def test_update_parent_filtering ( self ) : <EOL> """<STR_LIT>""" <EOL> supplier = Supplier . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> address = '<STR_LIT>' <EOL> ) <EOL> with CaptureQueriesContext ( connection ) as captured_queries : <EOL> Place . objects . filter ( pk = supplier . pk ) . update ( name = supplier . name ) <EOL> expected_sql = captured_queries [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> with CaptureQueriesContext ( connection ) as captured_queries : <EOL> supplier . save ( update_fields = ( '<STR_LIT:name>' , ) ) <EOL> for query in captured_queries : <EOL> sql = query [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in sql : <EOL> self . assertEqual ( expected_sql , sql ) </s>
