<s> """<STR_LIT>""" <EOL> from sqlalchemy . testing import assert_raises <EOL> import datetime <EOL> from sqlalchemy . orm import attributes , exc as orm_exc , configure_mappers <EOL> import sqlalchemy as sa <EOL> from sqlalchemy import testing , and_ <EOL> from sqlalchemy import Integer , String , ForeignKey , SmallInteger , Boolean <EOL> from sqlalchemy . types import TypeDecorator <EOL> from sqlalchemy . testing . schema import Table <EOL> from sqlalchemy . testing . schema import Column <EOL> from sqlalchemy import orm <EOL> from sqlalchemy . orm import mapper , relationship , create_session , Session <EOL> from sqlalchemy . testing import eq_ <EOL> from sqlalchemy . testing import fixtures <EOL> from test . orm import _fixtures <EOL> from sqlalchemy . testing . assertsql import CompiledSQL <EOL> class LazyTest ( _fixtures . FixtureTest ) : <EOL> run_inserts = '<STR_LIT>' <EOL> run_deletes = None <EOL> def test_basic ( self ) : <EOL> users , Address , addresses , User = ( <EOL> self . tables . users , <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . classes . User ) <EOL> mapper ( User , users , properties = { <EOL> '<STR_LIT>' : relationship ( <EOL> mapper ( Address , addresses ) , lazy = '<STR_LIT>' ) <EOL> } ) <EOL> sess = create_session ( ) <EOL> q = sess . query ( User ) <EOL> eq_ ( <EOL> [ User ( id = <NUM_LIT:7> , <EOL> addresses = [ Address ( id = <NUM_LIT:1> , email_address = '<STR_LIT>' ) ] ) ] , <EOL> q . filter ( users . c . id == <NUM_LIT:7> ) . all ( ) <EOL> ) <EOL> def test_needs_parent ( self ) : <EOL> """<STR_LIT>""" <EOL> users , Address , addresses , User = ( <EOL> self . tables . users , <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . classes . User ) <EOL> mapper ( User , users , properties = { <EOL> '<STR_LIT>' : relationship ( <EOL> mapper ( Address , addresses ) , lazy = '<STR_LIT>' ) <EOL> } ) <EOL> sess = create_session ( ) <EOL> q = sess . query ( User ) <EOL> u = q . filter ( users . c . id == <NUM_LIT:7> ) . first ( ) <EOL> sess . expunge ( u ) <EOL> assert_raises ( orm_exc . DetachedInstanceError , getattr , u , '<STR_LIT>' ) <EOL> def test_orderby ( self ) : <EOL> users , Address , addresses , User = ( <EOL> self . tables . users , <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . classes . User ) <EOL> mapper ( User , users , properties = { <EOL> '<STR_LIT>' : relationship ( <EOL> mapper ( Address , addresses ) , <EOL> lazy = '<STR_LIT>' , order_by = addresses . c . email_address ) , <EOL> } ) <EOL> q = create_session ( ) . query ( User ) <EOL> assert [ <EOL> User ( id = <NUM_LIT:7> , addresses = [ <EOL> Address ( id = <NUM_LIT:1> ) <EOL> ] ) , <EOL> User ( id = <NUM_LIT:8> , addresses = [ <EOL> Address ( id = <NUM_LIT:3> , email_address = '<STR_LIT>' ) , <EOL> Address ( id = <NUM_LIT:4> , email_address = '<STR_LIT>' ) , <EOL> Address ( id = <NUM_LIT:2> , email_address = '<STR_LIT>' ) <EOL> ] ) , <EOL> User ( id = <NUM_LIT:9> , addresses = [ <EOL> Address ( id = <NUM_LIT:5> ) <EOL> ] ) , <EOL> User ( id = <NUM_LIT:10> , addresses = [ ] ) <EOL> ] == q . all ( ) <EOL> def test_orderby_secondary ( self ) : <EOL> """<STR_LIT>""" <EOL> Address , addresses , users , User = ( <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . tables . users , <EOL> self . classes . User ) <EOL> mapper ( Address , addresses ) <EOL> mapper ( User , users , properties = dict ( <EOL> addresses = relationship ( Address , lazy = '<STR_LIT>' ) , <EOL> ) ) <EOL> q = create_session ( ) . query ( User ) <EOL> l = q . filter ( users . c . id == addresses . c . user_id ) . order_by ( addresses . c . email_address ) . all ( ) <EOL> assert [ <EOL> User ( id = <NUM_LIT:8> , addresses = [ <EOL> Address ( id = <NUM_LIT:2> , email_address = '<STR_LIT>' ) , <EOL> Address ( id = <NUM_LIT:3> , email_address = '<STR_LIT>' ) , <EOL> Address ( id = <NUM_LIT:4> , email_address = '<STR_LIT>' ) , <EOL> ] ) , <EOL> User ( id = <NUM_LIT:9> , addresses = [ <EOL> Address ( id = <NUM_LIT:5> ) <EOL> ] ) , <EOL> User ( id = <NUM_LIT:7> , addresses = [ <EOL> Address ( id = <NUM_LIT:1> ) <EOL> ] ) , <EOL> ] == l <EOL> def test_orderby_desc ( self ) : <EOL> Address , addresses , users , User = ( <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . tables . users , <EOL> self . classes . User ) <EOL> mapper ( Address , addresses ) <EOL> mapper ( User , users , properties = dict ( <EOL> addresses = relationship ( <EOL> Address , lazy = '<STR_LIT>' , <EOL> order_by = [ sa . desc ( addresses . c . email_address ) ] ) , <EOL> ) ) <EOL> sess = create_session ( ) <EOL> assert [ <EOL> User ( id = <NUM_LIT:7> , addresses = [ <EOL> Address ( id = <NUM_LIT:1> ) <EOL> ] ) , <EOL> User ( id = <NUM_LIT:8> , addresses = [ <EOL> Address ( id = <NUM_LIT:2> , email_address = '<STR_LIT>' ) , <EOL> Address ( id = <NUM_LIT:4> , email_address = '<STR_LIT>' ) , <EOL> Address ( id = <NUM_LIT:3> , email_address = '<STR_LIT>' ) , <EOL> ] ) , <EOL> User ( id = <NUM_LIT:9> , addresses = [ <EOL> Address ( id = <NUM_LIT:5> ) <EOL> ] ) , <EOL> User ( id = <NUM_LIT:10> , addresses = [ ] ) <EOL> ] == sess . query ( User ) . all ( ) <EOL> def test_no_orphan ( self ) : <EOL> """<STR_LIT>""" <EOL> users , Address , addresses , User = ( <EOL> self . tables . users , <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . classes . User ) <EOL> mapper ( User , users , properties = { <EOL> '<STR_LIT>' : relationship ( <EOL> Address , cascade = "<STR_LIT>" , lazy = '<STR_LIT>' ) <EOL> } ) <EOL> mapper ( Address , addresses ) <EOL> sess = create_session ( ) <EOL> user = sess . query ( User ) . get ( <NUM_LIT:7> ) <EOL> assert getattr ( User , '<STR_LIT>' ) . hasparent ( <EOL> attributes . instance_state ( user . addresses [ <NUM_LIT:0> ] ) , optimistic = True ) <EOL> assert not sa . orm . class_mapper ( Address ) . _is_orphan ( <EOL> attributes . instance_state ( user . addresses [ <NUM_LIT:0> ] ) ) <EOL> def test_limit ( self ) : <EOL> """<STR_LIT>""" <EOL> users , items , order_items , orders , Item , User , Address , Order , addresses = ( <EOL> self . tables . users , <EOL> self . tables . items , <EOL> self . tables . order_items , <EOL> self . tables . orders , <EOL> self . classes . Item , <EOL> self . classes . User , <EOL> self . classes . Address , <EOL> self . classes . Order , <EOL> self . tables . addresses ) <EOL> mapper ( Item , items ) <EOL> mapper ( Order , orders , properties = { <EOL> '<STR_LIT>' : relationship ( Item , secondary = order_items , lazy = '<STR_LIT>' ) <EOL> } ) <EOL> mapper ( User , users , properties = { <EOL> '<STR_LIT>' : relationship ( <EOL> mapper ( Address , addresses ) , lazy = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : relationship ( Order , lazy = '<STR_LIT>' ) <EOL> } ) <EOL> sess = create_session ( ) <EOL> q = sess . query ( User ) <EOL> if testing . against ( '<STR_LIT>' ) : <EOL> l = q . limit ( <NUM_LIT:2> ) . all ( ) <EOL> assert self . static . user_all_result [ : <NUM_LIT:2> ] == l <EOL> else : <EOL> l = q . limit ( <NUM_LIT:2> ) . offset ( <NUM_LIT:1> ) . all ( ) <EOL> assert self . static . user_all_result [ <NUM_LIT:1> : <NUM_LIT:3> ] == l <EOL> def test_distinct ( self ) : <EOL> users , items , order_items , orders , Item , User , Address , Order , addresses = ( <EOL> self . tables . users , <EOL> self . tables . items , <EOL> self . tables . order_items , <EOL> self . tables . orders , <EOL> self . classes . Item , <EOL> self . classes . User , <EOL> self . classes . Address , <EOL> self . classes . Order , <EOL> self . tables . addresses ) <EOL> mapper ( Item , items ) <EOL> mapper ( Order , orders , properties = { <EOL> '<STR_LIT>' : relationship ( Item , secondary = order_items , lazy = '<STR_LIT>' ) <EOL> } ) <EOL> mapper ( User , users , properties = { <EOL> '<STR_LIT>' : relationship ( <EOL> mapper ( Address , addresses ) , lazy = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : relationship ( Order , lazy = '<STR_LIT>' ) <EOL> } ) <EOL> sess = create_session ( ) <EOL> q = sess . query ( User ) <EOL> u2 = users . alias ( '<STR_LIT>' ) <EOL> s = sa . union_all ( <EOL> u2 . select ( use_labels = True ) , <EOL> u2 . select ( use_labels = True ) , u2 . select ( use_labels = True ) ) . alias ( '<STR_LIT:u>' ) <EOL> l = q . filter ( s . c . u2_id == User . id ) . order_by ( User . id ) . distinct ( ) . all ( ) <EOL> eq_ ( self . static . user_all_result , l ) <EOL> def test_uselist_false_warning ( self ) : <EOL> """<STR_LIT>""" <EOL> User , users , orders , Order = ( <EOL> self . classes . User , <EOL> self . tables . users , <EOL> self . tables . orders , <EOL> self . classes . Order ) <EOL> mapper ( User , users , properties = { <EOL> '<STR_LIT>' : relationship ( Order , uselist = False ) <EOL> } ) <EOL> mapper ( Order , orders ) <EOL> s = create_session ( ) <EOL> u1 = s . query ( User ) . filter ( User . id == <NUM_LIT:7> ) . one ( ) <EOL> assert_raises ( sa . exc . SAWarning , getattr , u1 , '<STR_LIT>' ) <EOL> def test_one_to_many_scalar ( self ) : <EOL> Address , addresses , users , User = ( <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . tables . users , <EOL> self . classes . User ) <EOL> mapper ( User , users , properties = dict ( <EOL> address = relationship ( <EOL> mapper ( Address , addresses ) , lazy = '<STR_LIT>' , uselist = False ) <EOL> ) ) <EOL> q = create_session ( ) . query ( User ) <EOL> l = q . filter ( users . c . id == <NUM_LIT:7> ) . all ( ) <EOL> assert [ User ( id = <NUM_LIT:7> , address = Address ( id = <NUM_LIT:1> ) ) ] == l <EOL> def test_many_to_one_binds ( self ) : <EOL> Address , addresses , users , User = ( <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . tables . users , <EOL> self . classes . User ) <EOL> mapper ( Address , addresses , <EOL> primary_key = [ addresses . c . user_id , addresses . c . email_address ] ) <EOL> mapper ( User , users , properties = dict ( <EOL> address = relationship ( <EOL> Address , uselist = False , <EOL> primaryjoin = sa . and_ ( <EOL> users . c . id == addresses . c . user_id , <EOL> addresses . c . email_address == '<STR_LIT>' ) ) <EOL> ) ) <EOL> q = create_session ( ) . query ( User ) <EOL> eq_ ( <EOL> [ <EOL> User ( id = <NUM_LIT:7> , address = None ) , <EOL> User ( id = <NUM_LIT:8> , address = Address ( id = <NUM_LIT:3> ) ) , <EOL> User ( id = <NUM_LIT:9> , address = None ) , <EOL> User ( id = <NUM_LIT:10> , address = None ) , <EOL> ] , <EOL> list ( q ) <EOL> ) <EOL> def test_double ( self ) : <EOL> """<STR_LIT>""" <EOL> users , orders , User , Address , Order , addresses = ( <EOL> self . tables . users , <EOL> self . tables . orders , <EOL> self . classes . User , <EOL> self . classes . Address , <EOL> self . classes . Order , <EOL> self . tables . addresses ) <EOL> openorders = sa . alias ( orders , '<STR_LIT>' ) <EOL> closedorders = sa . alias ( orders , '<STR_LIT>' ) <EOL> mapper ( Address , addresses ) <EOL> mapper ( Order , orders ) <EOL> open_mapper = mapper ( Order , openorders , non_primary = True ) <EOL> closed_mapper = mapper ( Order , closedorders , non_primary = True ) <EOL> mapper ( User , users , properties = dict ( <EOL> addresses = relationship ( Address , lazy = True ) , <EOL> open_orders = relationship ( <EOL> open_mapper , <EOL> primaryjoin = sa . and_ ( <EOL> openorders . c . isopen == <NUM_LIT:1> , <EOL> users . c . id == openorders . c . user_id ) , lazy = '<STR_LIT>' ) , <EOL> closed_orders = relationship ( <EOL> closed_mapper , <EOL> primaryjoin = sa . and_ ( <EOL> closedorders . c . isopen == <NUM_LIT:0> , <EOL> users . c . id == closedorders . c . user_id ) , lazy = '<STR_LIT>' ) <EOL> ) ) <EOL> q = create_session ( ) . query ( User ) <EOL> assert [ <EOL> User ( <EOL> id = <NUM_LIT:7> , <EOL> addresses = [ Address ( id = <NUM_LIT:1> ) ] , <EOL> open_orders = [ Order ( id = <NUM_LIT:3> ) ] , <EOL> closed_orders = [ Order ( id = <NUM_LIT:1> ) , Order ( id = <NUM_LIT:5> ) ] <EOL> ) , <EOL> User ( <EOL> id = <NUM_LIT:8> , <EOL> addresses = [ Address ( id = <NUM_LIT:2> ) , Address ( id = <NUM_LIT:3> ) , Address ( id = <NUM_LIT:4> ) ] , <EOL> open_orders = [ ] , <EOL> closed_orders = [ ] <EOL> ) , <EOL> User ( <EOL> id = <NUM_LIT:9> , <EOL> addresses = [ Address ( id = <NUM_LIT:5> ) ] , <EOL> open_orders = [ Order ( id = <NUM_LIT:4> ) ] , <EOL> closed_orders = [ Order ( id = <NUM_LIT:2> ) ] <EOL> ) , <EOL> User ( id = <NUM_LIT:10> ) <EOL> ] == q . all ( ) <EOL> sess = create_session ( ) <EOL> user = sess . query ( User ) . get ( <NUM_LIT:7> ) <EOL> eq_ ( <EOL> [ Order ( id = <NUM_LIT:1> ) , Order ( id = <NUM_LIT:5> ) ] , <EOL> create_session ( ) . query ( closed_mapper ) . with_parent ( <EOL> user , property = '<STR_LIT>' ) . all ( ) <EOL> ) <EOL> eq_ ( <EOL> [ Order ( id = <NUM_LIT:3> ) ] , <EOL> create_session ( ) . query ( open_mapper ) . <EOL> with_parent ( user , property = '<STR_LIT>' ) . all ( ) <EOL> ) <EOL> def test_many_to_many ( self ) : <EOL> keywords , items , item_keywords , Keyword , Item = ( <EOL> self . tables . keywords , <EOL> self . tables . items , <EOL> self . tables . item_keywords , <EOL> self . classes . Keyword , <EOL> self . classes . Item ) <EOL> mapper ( Keyword , keywords ) <EOL> mapper ( Item , items , properties = dict ( <EOL> keywords = relationship ( <EOL> Keyword , secondary = item_keywords , lazy = '<STR_LIT>' ) , <EOL> ) ) <EOL> q = create_session ( ) . query ( Item ) <EOL> assert self . static . item_keyword_result == q . all ( ) <EOL> eq_ ( <EOL> self . static . item_keyword_result [ <NUM_LIT:0> : <NUM_LIT:2> ] , <EOL> q . join ( '<STR_LIT>' ) . filter ( keywords . c . name == '<STR_LIT>' ) . all ( ) <EOL> ) <EOL> def test_uses_get ( self ) : <EOL> """<STR_LIT>""" <EOL> Address , addresses , users , User = ( <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . tables . users , <EOL> self . classes . User ) <EOL> for pj in ( <EOL> None , <EOL> users . c . id == addresses . c . user_id , <EOL> addresses . c . user_id == users . c . id <EOL> ) : <EOL> mapper ( Address , addresses , properties = dict ( <EOL> user = relationship ( <EOL> mapper ( User , users ) , lazy = '<STR_LIT>' , primaryjoin = pj ) <EOL> ) ) <EOL> sess = create_session ( ) <EOL> a1 = sess . query ( Address ) . filter_by ( email_address = "<STR_LIT>" ) . one ( ) <EOL> u1 = sess . query ( User ) . get ( <NUM_LIT:8> ) <EOL> def go ( ) : <EOL> assert a1 . user is u1 <EOL> self . assert_sql_count ( testing . db , go , <NUM_LIT:0> ) <EOL> sa . orm . clear_mappers ( ) <EOL> def test_uses_get_compatible_types ( self ) : <EOL> """<STR_LIT>""" <EOL> User , Address = self . classes . User , self . classes . Address <EOL> class IntDecorator ( TypeDecorator ) : <EOL> impl = Integer <EOL> class SmallintDecorator ( TypeDecorator ) : <EOL> impl = SmallInteger <EOL> class SomeDBInteger ( sa . Integer ) : <EOL> pass <EOL> for tt in [ <EOL> Integer , <EOL> SmallInteger , <EOL> IntDecorator , <EOL> SmallintDecorator , <EOL> SomeDBInteger , <EOL> ] : <EOL> m = sa . MetaData ( ) <EOL> users = Table ( <EOL> '<STR_LIT>' , m , <EOL> Column ( <EOL> '<STR_LIT:id>' , Integer , primary_key = True , <EOL> test_needs_autoincrement = True ) , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:30> ) , nullable = False ) , <EOL> ) <EOL> addresses = Table ( <EOL> '<STR_LIT>' , m , <EOL> Column ( <EOL> '<STR_LIT:id>' , Integer , primary_key = True , <EOL> test_needs_autoincrement = True ) , <EOL> Column ( '<STR_LIT>' , tt , ForeignKey ( '<STR_LIT>' ) ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT:50> ) , nullable = False ) , <EOL> ) <EOL> mapper ( Address , addresses , properties = dict ( <EOL> user = relationship ( mapper ( User , users ) ) <EOL> ) ) <EOL> sess = create_session ( bind = testing . db ) <EOL> a1 = sess . query ( Address ) . filter_by ( email_address = "<STR_LIT>" ) . one ( ) <EOL> u1 = sess . query ( User ) . get ( <NUM_LIT:8> ) <EOL> def go ( ) : <EOL> assert a1 . user is u1 <EOL> self . assert_sql_count ( testing . db , go , <NUM_LIT:0> ) <EOL> sa . orm . clear_mappers ( ) <EOL> def test_many_to_one ( self ) : <EOL> users , Address , addresses , User = ( <EOL> self . tables . users , <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . classes . User ) <EOL> mapper ( Address , addresses , properties = dict ( <EOL> user = relationship ( mapper ( User , users ) , lazy = '<STR_LIT>' ) <EOL> ) ) <EOL> sess = create_session ( ) <EOL> q = sess . query ( Address ) <EOL> a = q . filter ( addresses . c . id == <NUM_LIT:1> ) . one ( ) <EOL> assert a . user is not None <EOL> u1 = sess . query ( User ) . get ( <NUM_LIT:7> ) <EOL> assert a . user is u1 <EOL> def test_backrefs_dont_lazyload ( self ) : <EOL> users , Address , addresses , User = ( <EOL> self . tables . users , <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . classes . User ) <EOL> mapper ( User , users , properties = { <EOL> '<STR_LIT>' : relationship ( Address , backref = '<STR_LIT:user>' ) <EOL> } ) <EOL> mapper ( Address , addresses ) <EOL> sess = create_session ( ) <EOL> ad = sess . query ( Address ) . filter_by ( id = <NUM_LIT:1> ) . one ( ) <EOL> assert ad . user . id == <NUM_LIT:7> <EOL> def go ( ) : <EOL> ad . user = None <EOL> assert ad . user is None <EOL> self . assert_sql_count ( testing . db , go , <NUM_LIT:0> ) <EOL> u1 = sess . query ( User ) . filter_by ( id = <NUM_LIT:7> ) . one ( ) <EOL> def go ( ) : <EOL> assert ad not in u1 . addresses <EOL> self . assert_sql_count ( testing . db , go , <NUM_LIT:1> ) <EOL> sess . expire ( u1 , [ '<STR_LIT>' ] ) <EOL> def go ( ) : <EOL> assert ad in u1 . addresses <EOL> self . assert_sql_count ( testing . db , go , <NUM_LIT:1> ) <EOL> sess . expire ( u1 , [ '<STR_LIT>' ] ) <EOL> ad2 = Address ( ) <EOL> def go ( ) : <EOL> ad2 . user = u1 <EOL> assert ad2 . user is u1 <EOL> self . assert_sql_count ( testing . db , go , <NUM_LIT:0> ) <EOL> def go ( ) : <EOL> assert ad2 in u1 . addresses <EOL> self . assert_sql_count ( testing . db , go , <NUM_LIT:1> ) <EOL> class GetterStateTest ( _fixtures . FixtureTest ) : <EOL> """<STR_LIT>""" <EOL> run_inserts = None <EOL> def _unhashable_fixture ( self , metadata , load_on_pending = False ) : <EOL> class MyHashType ( sa . TypeDecorator ) : <EOL> impl = sa . String ( <NUM_LIT:100> ) <EOL> def process_bind_param ( self , value , dialect ) : <EOL> return "<STR_LIT:;>" . join ( <EOL> "<STR_LIT>" % ( k , v ) <EOL> for k , v in <EOL> sorted ( value . items ( ) , key = lambda key : key [ <NUM_LIT:0> ] ) ) <EOL> def process_result_value ( self , value , dialect ) : <EOL> return dict ( elem . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) for elem in value . split ( "<STR_LIT:;>" ) ) <EOL> category = Table ( <EOL> '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT:data>' , MyHashType ( ) ) <EOL> ) <EOL> article = Table ( <EOL> '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT:data>' , MyHashType ( ) ) <EOL> ) <EOL> class Category ( fixtures . ComparableEntity ) : <EOL> pass <EOL> class Article ( fixtures . ComparableEntity ) : <EOL> pass <EOL> mapper ( Category , category ) <EOL> mapper ( Article , article , properties = { <EOL> "<STR_LIT>" : relationship ( <EOL> Category , <EOL> primaryjoin = orm . foreign ( article . c . data ) == category . c . data , <EOL> load_on_pending = load_on_pending <EOL> ) <EOL> } ) <EOL> metadata . create_all ( ) <EOL> sess = Session ( autoflush = False ) <EOL> data = { "<STR_LIT>" : "<STR_LIT>" } <EOL> a1 = Article ( id = <NUM_LIT:1> , data = data ) <EOL> c1 = Category ( id = <NUM_LIT:1> , data = data ) <EOL> if load_on_pending : <EOL> sess . add ( c1 ) <EOL> else : <EOL> sess . add_all ( [ c1 , a1 ] ) <EOL> sess . flush ( ) <EOL> if load_on_pending : <EOL> sess . add ( a1 ) <EOL> return Category , Article , sess , a1 , c1 <EOL> def _u_ad_fixture ( self , populate_user , dont_use_get = False ) : <EOL> users , Address , addresses , User = ( <EOL> self . tables . users , <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . classes . User ) <EOL> mapper ( User , users , properties = { <EOL> '<STR_LIT>' : relationship ( Address , back_populates = '<STR_LIT:user>' ) <EOL> } ) <EOL> mapper ( Address , addresses , properties = { <EOL> '<STR_LIT:user>' : relationship ( <EOL> User , <EOL> primaryjoin = and_ ( <EOL> users . c . id == addresses . c . user_id , users . c . id != <NUM_LIT> ) <EOL> if dont_use_get else None , <EOL> back_populates = '<STR_LIT>' <EOL> ) <EOL> } ) <EOL> sess = create_session ( ) <EOL> a1 = Address ( email_address = '<STR_LIT>' ) <EOL> sess . add ( a1 ) <EOL> if populate_user : <EOL> a1 . user = User ( name = '<STR_LIT>' ) <EOL> sess . flush ( ) <EOL> if populate_user : <EOL> sess . expire_all ( ) <EOL> return User , Address , sess , a1 <EOL> def test_no_use_get_params_missing ( self ) : <EOL> User , Address , sess , a1 = self . _u_ad_fixture ( False , True ) <EOL> def go ( ) : <EOL> eq_ ( a1 . user , None ) <EOL> self . assert_sql_count ( <EOL> testing . db , <EOL> go , <EOL> <NUM_LIT:0> <EOL> ) <EOL> @ testing . provide_metadata <EOL> def test_no_use_get_params_not_hashable ( self ) : <EOL> Category , Article , sess , a1 , c1 = self . _unhashable_fixture ( self . metadata ) <EOL> def go ( ) : <EOL> eq_ ( a1 . category , c1 ) <EOL> self . assert_sql_count ( <EOL> testing . db , <EOL> go , <EOL> <NUM_LIT:1> <EOL> ) <EOL> @ testing . provide_metadata <EOL> def test_no_use_get_params_not_hashable_on_pending ( self ) : <EOL> Category , Article , sess , a1 , c1 = self . _unhashable_fixture ( self . metadata , load_on_pending = True ) <EOL> def go ( ) : <EOL> eq_ ( a1 . category , c1 ) <EOL> self . assert_sql_count ( <EOL> testing . db , <EOL> go , <EOL> <NUM_LIT:1> <EOL> ) <EOL> def test_get_empty_passive_return_never_set ( self ) : <EOL> User , Address , sess , a1 = self . _u_ad_fixture ( False ) <EOL> eq_ ( <EOL> Address . user . impl . get ( <EOL> attributes . instance_state ( a1 ) , <EOL> attributes . instance_dict ( a1 ) , <EOL> passive = attributes . PASSIVE_RETURN_NEVER_SET ) , <EOL> attributes . NEVER_SET <EOL> ) <EOL> assert '<STR_LIT>' not in a1 . __dict__ <EOL> assert '<STR_LIT:user>' not in a1 . __dict__ <EOL> def test_history_empty_passive_return_never_set ( self ) : <EOL> User , Address , sess , a1 = self . _u_ad_fixture ( False ) <EOL> eq_ ( <EOL> Address . user . impl . get_history ( <EOL> attributes . instance_state ( a1 ) , <EOL> attributes . instance_dict ( a1 ) , <EOL> passive = attributes . PASSIVE_RETURN_NEVER_SET ) , <EOL> ( ( ) , ( ) , ( ) ) <EOL> ) <EOL> assert '<STR_LIT>' not in a1 . __dict__ <EOL> assert '<STR_LIT:user>' not in a1 . __dict__ <EOL> def test_get_empty_passive_no_initialize ( self ) : <EOL> User , Address , sess , a1 = self . _u_ad_fixture ( False ) <EOL> eq_ ( <EOL> Address . user . impl . get ( <EOL> attributes . instance_state ( a1 ) , <EOL> attributes . instance_dict ( a1 ) , <EOL> passive = attributes . PASSIVE_NO_INITIALIZE ) , <EOL> attributes . PASSIVE_NO_RESULT <EOL> ) <EOL> assert '<STR_LIT>' not in a1 . __dict__ <EOL> assert '<STR_LIT:user>' not in a1 . __dict__ <EOL> def test_history_empty_passive_no_initialize ( self ) : <EOL> User , Address , sess , a1 = self . _u_ad_fixture ( False ) <EOL> eq_ ( <EOL> Address . user . impl . get_history ( <EOL> attributes . instance_state ( a1 ) , <EOL> attributes . instance_dict ( a1 ) , <EOL> passive = attributes . PASSIVE_NO_INITIALIZE ) , <EOL> attributes . HISTORY_BLANK <EOL> ) <EOL> assert '<STR_LIT>' not in a1 . __dict__ <EOL> assert '<STR_LIT:user>' not in a1 . __dict__ <EOL> def test_get_populated_passive_no_initialize ( self ) : <EOL> User , Address , sess , a1 = self . _u_ad_fixture ( True ) <EOL> eq_ ( <EOL> Address . user . impl . get ( <EOL> attributes . instance_state ( a1 ) , <EOL> attributes . instance_dict ( a1 ) , <EOL> passive = attributes . PASSIVE_NO_INITIALIZE ) , <EOL> attributes . PASSIVE_NO_RESULT <EOL> ) <EOL> assert '<STR_LIT>' not in a1 . __dict__ <EOL> assert '<STR_LIT:user>' not in a1 . __dict__ <EOL> def test_history_populated_passive_no_initialize ( self ) : <EOL> User , Address , sess , a1 = self . _u_ad_fixture ( True ) <EOL> eq_ ( <EOL> Address . user . impl . get_history ( <EOL> attributes . instance_state ( a1 ) , <EOL> attributes . instance_dict ( a1 ) , <EOL> passive = attributes . PASSIVE_NO_INITIALIZE ) , <EOL> attributes . HISTORY_BLANK <EOL> ) <EOL> assert '<STR_LIT>' not in a1 . __dict__ <EOL> assert '<STR_LIT:user>' not in a1 . __dict__ <EOL> def test_get_populated_passive_return_never_set ( self ) : <EOL> User , Address , sess , a1 = self . _u_ad_fixture ( True ) <EOL> eq_ ( <EOL> Address . user . impl . get ( <EOL> attributes . instance_state ( a1 ) , <EOL> attributes . instance_dict ( a1 ) , <EOL> passive = attributes . PASSIVE_RETURN_NEVER_SET ) , <EOL> User ( name = '<STR_LIT>' ) <EOL> ) <EOL> def test_history_populated_passive_return_never_set ( self ) : <EOL> User , Address , sess , a1 = self . _u_ad_fixture ( True ) <EOL> eq_ ( <EOL> Address . user . impl . get_history ( <EOL> attributes . instance_state ( a1 ) , <EOL> attributes . instance_dict ( a1 ) , <EOL> passive = attributes . PASSIVE_RETURN_NEVER_SET ) , <EOL> ( ( ) , [ User ( name = '<STR_LIT>' ) , ] , ( ) ) <EOL> ) <EOL> class M2OGetTest ( _fixtures . FixtureTest ) : <EOL> run_inserts = '<STR_LIT>' <EOL> run_deletes = None <EOL> def test_m2o_noload ( self ) : <EOL> """<STR_LIT>""" <EOL> users , Address , addresses , User = ( <EOL> self . tables . users , <EOL> self . classes . Address , <EOL> self . tables . addresses , <EOL> self . classes . User ) <EOL> mapper ( User , users ) <EOL> mapper ( Address , addresses , properties = { <EOL> '<STR_LIT:user>' : relationship ( User ) <EOL> } ) <EOL> sess = create_session ( ) <EOL> ad1 = Address ( email_address = '<STR_LIT>' , id = <NUM_LIT:12> ) <EOL> sess . add ( ad1 ) <EOL> sess . flush ( ) <EOL> sess . expunge_all ( ) <EOL> ad2 = sess . query ( Address ) . get ( <NUM_LIT:1> ) <EOL> ad3 = sess . query ( Address ) . get ( ad1 . id ) <EOL> def go ( ) : <EOL> assert ad2 . user . name == '<STR_LIT>' <EOL> assert ad3 . user is None <EOL> self . assert_sql_count ( testing . db , go , <NUM_LIT:1> ) <EOL> class CorrelatedTest ( fixtures . MappedTest ) : <EOL> @ classmethod <EOL> def define_tables ( self , meta ) : <EOL> Table ( '<STR_LIT>' , meta , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:50> ) ) ) <EOL> Table ( '<STR_LIT>' , meta , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT:date>' , sa . Date ) , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) ) <EOL> @ classmethod <EOL> def insert_data ( cls ) : <EOL> stuff , user_t = cls . tables . stuff , cls . tables . user_t <EOL> user_t . insert ( ) . execute ( <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> stuff . insert ( ) . execute ( <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:date>' : datetime . date ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:15> ) } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:date>' : datetime . date ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:15> ) } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:date>' : datetime . date ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:15> ) } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:4> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:date>' : datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:15> ) } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:5> , '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT:date>' : datetime . date ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:15> ) } ) <EOL> def test_correlated_lazyload ( self ) : <EOL> stuff , user_t = self . tables . stuff , self . tables . user_t <EOL> class User ( fixtures . ComparableEntity ) : <EOL> pass <EOL> class Stuff ( fixtures . ComparableEntity ) : <EOL> pass <EOL> mapper ( Stuff , stuff ) <EOL> stuff_view = sa . select ( [ stuff . c . id ] ) . where ( stuff . c . user_id == user_t . c . id ) . correlate ( user_t ) . order_by ( sa . desc ( stuff . c . date ) ) . limit ( <NUM_LIT:1> ) <EOL> mapper ( User , user_t , properties = { <EOL> '<STR_LIT>' : relationship ( <EOL> Stuff , <EOL> primaryjoin = sa . and_ ( <EOL> user_t . c . id == stuff . c . user_id , <EOL> stuff . c . id == ( stuff_view . as_scalar ( ) ) ) ) <EOL> } ) <EOL> sess = create_session ( ) <EOL> eq_ ( <EOL> sess . query ( User ) . all ( ) , <EOL> [ <EOL> User ( <EOL> name = '<STR_LIT>' , <EOL> stuff = [ Stuff ( date = datetime . date ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:15> ) , id = <NUM_LIT:2> ) ] ) , <EOL> User ( <EOL> name = '<STR_LIT>' , <EOL> stuff = [ Stuff ( id = <NUM_LIT:4> , date = datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:15> ) ) ] ) , <EOL> User ( <EOL> name = '<STR_LIT>' , <EOL> stuff = [ Stuff ( id = <NUM_LIT:5> , date = datetime . date ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:15> ) ) ] ) <EOL> ] <EOL> ) <EOL> class O2MWOSideFixedTest ( fixtures . MappedTest ) : <EOL> @ classmethod <EOL> def define_tables ( self , meta ) : <EOL> Table ( '<STR_LIT>' , meta , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT>' , Boolean ) , <EOL> ) <EOL> Table ( '<STR_LIT>' , meta , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT>' , ForeignKey ( '<STR_LIT>' ) ) <EOL> ) <EOL> @ classmethod <EOL> def setup_classes ( cls ) : <EOL> class Person ( cls . Basic ) : <EOL> pass <EOL> class City ( cls . Basic ) : <EOL> pass <EOL> @ classmethod <EOL> def setup_mappers ( cls ) : <EOL> Person , City = cls . classes . Person , cls . classes . City <EOL> city , person = cls . tables . city , cls . tables . person <EOL> mapper ( Person , person , properties = { <EOL> '<STR_LIT>' : relationship ( City , <EOL> primaryjoin = and_ ( <EOL> person . c . city_id == city . c . id , <EOL> city . c . deleted == False ) , <EOL> backref = '<STR_LIT>' <EOL> ) <EOL> } ) <EOL> mapper ( City , city ) <EOL> def _fixture ( self , include_other ) : <EOL> city , person = self . tables . city , self . tables . person <EOL> if include_other : <EOL> city . insert ( ) . execute ( <EOL> { "<STR_LIT:id>" : <NUM_LIT:1> , "<STR_LIT>" : False } , <EOL> ) <EOL> person . insert ( ) . execute ( <EOL> { "<STR_LIT:id>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:1> } , <EOL> { "<STR_LIT:id>" : <NUM_LIT:2> , "<STR_LIT>" : <NUM_LIT:1> } , <EOL> ) <EOL> city . insert ( ) . execute ( <EOL> { "<STR_LIT:id>" : <NUM_LIT:2> , "<STR_LIT>" : True } , <EOL> ) <EOL> person . insert ( ) . execute ( <EOL> { "<STR_LIT:id>" : <NUM_LIT:3> , "<STR_LIT>" : <NUM_LIT:2> } , <EOL> { "<STR_LIT:id>" : <NUM_LIT:4> , "<STR_LIT>" : <NUM_LIT:2> } , <EOL> ) <EOL> def test_lazyload_assert_expected_sql ( self ) : <EOL> self . _fixture ( True ) <EOL> City = self . classes . City <EOL> sess = Session ( testing . db ) <EOL> c1 , c2 = sess . query ( City ) . order_by ( City . id ) . all ( ) <EOL> def go ( ) : <EOL> eq_ ( <EOL> [ p . id for p in c2 . people ] , <EOL> [ ] <EOL> ) <EOL> self . assert_sql_execution ( <EOL> testing . db , <EOL> go , <EOL> CompiledSQL ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> { "<STR_LIT>" : <NUM_LIT:2> , "<STR_LIT>" : <NUM_LIT:1> } <EOL> ) <EOL> ) <EOL> def test_lazyload_people_other_exists ( self ) : <EOL> self . _fixture ( True ) <EOL> City = self . classes . City <EOL> sess = Session ( testing . db ) <EOL> c1 , c2 = sess . query ( City ) . order_by ( City . id ) . all ( ) <EOL> eq_ ( <EOL> [ p . id for p in c1 . people ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> ) <EOL> eq_ ( <EOL> [ p . id for p in c2 . people ] , <EOL> [ ] <EOL> ) <EOL> def test_lazyload_people_no_other_exists ( self ) : <EOL> self . _fixture ( False ) <EOL> City = self . classes . City <EOL> sess = Session ( testing . db ) <EOL> c2 , = sess . query ( City ) . order_by ( City . id ) . all ( ) <EOL> eq_ ( <EOL> [ p . id for p in c2 . people ] , <EOL> [ ] <EOL> ) <EOL> class RefersToSelfLazyLoadInterferenceTest ( fixtures . MappedTest ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def define_tables ( cls , metadata ) : <EOL> Table ( <EOL> '<STR_LIT:a>' , metadata , <EOL> Column ( '<STR_LIT>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT>' , ForeignKey ( '<STR_LIT>' ) ) , <EOL> ) <EOL> Table ( <EOL> '<STR_LIT:b>' , metadata , <EOL> Column ( '<STR_LIT>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT>' , ForeignKey ( '<STR_LIT>' ) ) , <EOL> ) <EOL> Table ( <EOL> '<STR_LIT:c>' , metadata , <EOL> Column ( '<STR_LIT>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT>' , ForeignKey ( '<STR_LIT>' ) ) , <EOL> ) <EOL> @ classmethod <EOL> def setup_classes ( cls ) : <EOL> class A ( cls . Basic ) : <EOL> pass <EOL> class B ( cls . Basic ) : <EOL> pass <EOL> class C ( cls . Basic ) : <EOL> pass <EOL> @ classmethod <EOL> def setup_mappers ( cls ) : <EOL> mapper ( cls . classes . A , cls . tables . a , properties = { <EOL> "<STR_LIT:b>" : relationship ( cls . classes . B ) <EOL> } ) <EOL> bm = mapper ( cls . classes . B , cls . tables . b , properties = { <EOL> "<STR_LIT>" : relationship ( <EOL> cls . classes . B , remote_side = cls . tables . b . c . b_id ) , <EOL> "<STR_LIT>" : relationship ( cls . classes . C ) <EOL> } ) <EOL> mapper ( cls . classes . C , cls . tables . c ) <EOL> bmp = bm . _props <EOL> configure_mappers ( ) <EOL> bmp . sort ( ) <EOL> def test_lazy_doesnt_interfere ( self ) : <EOL> A , B , C = self . classes ( "<STR_LIT:A>" , "<STR_LIT:B>" , "<STR_LIT:C>" ) <EOL> session = Session ( ) <EOL> b = B ( ) <EOL> session . add ( b ) <EOL> session . flush ( ) <EOL> b . parent_id = b . b_id <EOL> b . zc . append ( C ( ) ) <EOL> b . zc . append ( C ( ) ) <EOL> session . commit ( ) <EOL> session . query ( B ) . options ( <EOL> sa . orm . joinedload ( '<STR_LIT>' ) . joinedload ( '<STR_LIT>' ) ) . all ( ) <EOL> class TypeCoerceTest ( fixtures . MappedTest , testing . AssertsExecutionResults , ) : <EOL> """<STR_LIT>""" <EOL> __only_on__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class StringAsInt ( TypeDecorator ) : <EOL> impl = String ( <NUM_LIT:50> ) <EOL> def column_expression ( self , col ) : <EOL> return sa . cast ( col , Integer ) <EOL> def bind_expression ( self , col ) : <EOL> return sa . cast ( col , String ) <EOL> @ classmethod <EOL> def define_tables ( cls , metadata ) : <EOL> Table ( <EOL> '<STR_LIT>' , metadata , <EOL> Column ( "<STR_LIT:id>" , cls . StringAsInt , primary_key = True ) , <EOL> ) <EOL> Table ( <EOL> "<STR_LIT>" , metadata , <EOL> Column ( "<STR_LIT:id>" , Integer , primary_key = True ) , <EOL> Column ( "<STR_LIT>" , Integer ) , <EOL> ) <EOL> @ classmethod <EOL> def setup_classes ( cls ) : <EOL> class Person ( cls . Basic ) : <EOL> pass <EOL> class Pet ( cls . Basic ) : <EOL> pass <EOL> @ classmethod <EOL> def setup_mappers ( cls ) : <EOL> mapper ( cls . classes . Person , cls . tables . person , properties = dict ( <EOL> pets = relationship ( <EOL> cls . classes . Pet , primaryjoin = ( <EOL> orm . foreign ( cls . tables . pets . c . person_id ) == <EOL> sa . cast ( <EOL> sa . type_coerce ( cls . tables . person . c . id , Integer ) , <EOL> Integer <EOL> ) <EOL> ) <EOL> ) <EOL> ) ) <EOL> mapper ( cls . classes . Pet , cls . tables . pets ) <EOL> def test_lazyload_singlecast ( self ) : <EOL> Person = self . classes . Person <EOL> Pet = self . classes . Pet <EOL> s = Session ( ) <EOL> s . add_all ( [ <EOL> Person ( id = <NUM_LIT:5> ) , Pet ( id = <NUM_LIT:1> , person_id = <NUM_LIT:5> ) <EOL> ] ) <EOL> s . commit ( ) <EOL> p1 = s . query ( Person ) . first ( ) <EOL> with self . sql_execution_asserter ( ) as asserter : <EOL> p1 . pets <EOL> asserter . assert_ ( <EOL> CompiledSQL ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> [ { '<STR_LIT>' : <NUM_LIT:5> } ] <EOL> ) <EOL> ) </s>
