
from sa_gentestbase import *

class AB( Test_AB0):
    def test_AB_poly_1__inh_tableinh__Alink_A__Blink_A__BAlink_A__Alazy_True__Blazy_True(me):
        meta=me.meta
        class A( Base):
            props = ['id', 'name', 'link1']
            data = property( lambda me: me.name)
        class B( A):
            props = ['id', 'name', 'link1', 'data2', 'link2']
            data = property( lambda me: me.data2)
        
        table_A = Table( 'A', meta,
            Column( 'id', Integer, primary_key= True, ),
            Column( 'name', String, ),
            Column( 'link1_id', Integer, ForeignKey( 'A.id', name= 'whatever1', use_alter= True, ), ),
            Column( 'atype', String, ),
        )
        table_B = Table( 'B', meta,
            Column( 'data2', String, ),
            Column( 'id', Integer, ForeignKey( 'A.id', ), primary_key= True, ),
            Column( 'link2_id', Integer, ForeignKey( 'A.id', name= 'whatever3', use_alter= True, ), ),
        )
        
        meta.create_all()
        
        p_union = polymorphic_union( {
                        'A': table_A.select( table_A.c.atype == 'A', ),
                        'B': join( table_A, table_B, table_B.c.id == table_A.c.id, ),
                        }, None, ) #tableinh
        mapper_A = mapper( A, table_A, 
                    polymorphic_identity= 'A', 
                    polymorphic_on= p_union.c.atype, 
                    select_table= p_union, 
                    )
        mapper_A.add_property( 'link1', relation( A, 
                    foreignkey= table_A.c.link1_id, 
                    lazy= True, 
                    post_update= True, 
                    primaryjoin= table_A.c.link1_id == table_A.c.id, 
                    remote_side= table_A.c.id, 
                    uselist= False, 
                    ) )
        
        mapper_B = mapper( B, table_B, 
                    inherit_condition= table_A.c.id == table_B.c.id, 
                    inherits= mapper_A, 
                    polymorphic_identity= 'B', 
                    )
        mapper_B.add_property( 'link2', relation( A, 
                    foreignkey= table_B.c.link2_id, 
                    lazy= True, 
                    post_update= True, 
                    primaryjoin= table_B.c.link2_id == table_A.c.id, 
                    uselist= False, 
                    ) )
        
        
        
        #populate
        a = A()
        b = B()
        a.name = 'anna'
        a.link1 = a
        b.name = 'ben'
        b.link1 = a
        b.data2 = 'gun'
        b.link2 = a
        
        session = create_session()
        session.save(a)
        session.save(b)
        session.flush()
        
        expects = [
            dict( klas= A, table= table_A, oid= a.id, exp_single= str(a),
                    exp_multi = [ str(a), str(b) ]),
            dict( klas= B, table= table_B, oid= b.id, exp_single= str(b),
                    exp_multi = [ str(b) ]),
        
        ]
        
        me.query( session, expects, idname='id' )


if __name__ == '__main__':
    setup()
    unittest.main()
