<s> """<STR_LIT>""" <EOL> from sqlalchemy import Table , Column , ForeignKey <EOL> from elixir import * <EOL> import elixir <EOL> def setup_entity_raise ( cls ) : <EOL> try : <EOL> setup_entities ( [ cls ] ) <EOL> except Exception , e : <EOL> pass <EOL> else : <EOL> assert False , "<STR_LIT>" % cls . __name__ <EOL> def setup ( ) : <EOL> elixir . options_defaults . update ( dict ( autoload = True , shortnames = True ) ) <EOL> def teardown ( ) : <EOL> elixir . options_defaults . update ( dict ( autoload = False , shortnames = False ) ) <EOL> class TestAutoload ( object ) : <EOL> def setup ( self ) : <EOL> metadata . bind = '<STR_LIT>' <EOL> def teardown ( self ) : <EOL> cleanup_all ( True ) <EOL> def test_simple ( self ) : <EOL> conn = metadata . bind . connect ( ) <EOL> conn . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conn . close ( ) <EOL> class A ( Entity ) : <EOL> pass <EOL> setup_all ( ) <EOL> a1 = A ( name = "<STR_LIT>" ) <EOL> a2 = A ( name = "<STR_LIT>" ) <EOL> session . commit ( ) <EOL> session . expunge_all ( ) <EOL> a1 = A . get_by ( name = "<STR_LIT>" ) <EOL> a2 = A . get_by ( name = "<STR_LIT>" ) <EOL> assert a1 . name == '<STR_LIT>' <EOL> assert a2 . name == '<STR_LIT>' <EOL> def test_fk_auto_join_sa ( self ) : <EOL> person_table = Table ( '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:32> ) ) ) <EOL> animal_table = Table ( '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:30> ) ) , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) ) <EOL> metadata . create_all ( ) <EOL> metadata . clear ( ) <EOL> class Person ( Entity ) : <EOL> pets = OneToMany ( '<STR_LIT>' ) <EOL> class Animal ( Entity ) : <EOL> owner = ManyToOne ( '<STR_LIT>' ) <EOL> setup_all ( ) <EOL> snowball = Animal ( name = "<STR_LIT>" ) <EOL> snowball2 = Animal ( name = "<STR_LIT>" ) <EOL> slh = Animal ( name = "<STR_LIT>" ) <EOL> homer = Person ( name = "<STR_LIT>" , pets = [ slh , snowball ] ) <EOL> lisa = Person ( name = "<STR_LIT>" , pets = [ snowball2 ] ) <EOL> session . commit ( ) <EOL> session . expunge_all ( ) <EOL> homer = Person . get_by ( name = "<STR_LIT>" ) <EOL> lisa = Person . get_by ( name = "<STR_LIT>" ) <EOL> slh = Animal . get_by ( name = "<STR_LIT>" ) <EOL> assert len ( homer . pets ) == <NUM_LIT:2> <EOL> assert homer == slh . owner <EOL> assert lisa . pets [ <NUM_LIT:0> ] . name == "<STR_LIT>" <EOL> def test_fk_auto_join_colname ( self ) : <EOL> person_table = Table ( '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:32> ) ) ) <EOL> animal_table = Table ( '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:30> ) ) , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) ) <EOL> metadata . create_all ( ) <EOL> metadata . clear ( ) <EOL> class Person ( Entity ) : <EOL> pets = OneToMany ( '<STR_LIT>' , inverse = '<STR_LIT>' ) <EOL> animals = OneToMany ( '<STR_LIT>' , inverse = '<STR_LIT>' ) <EOL> class Animal ( Entity ) : <EOL> owner = ManyToOne ( '<STR_LIT>' , colname = '<STR_LIT>' ) <EOL> feeder = ManyToOne ( '<STR_LIT>' , colname = '<STR_LIT>' ) <EOL> setup_all ( ) <EOL> snowball = Animal ( name = "<STR_LIT>" ) <EOL> slh = Animal ( name = "<STR_LIT>" ) <EOL> homer = Person ( name = "<STR_LIT>" , animals = [ snowball , slh ] , pets = [ slh ] ) <EOL> lisa = Person ( name = "<STR_LIT>" , pets = [ snowball ] ) <EOL> session . commit ( ) <EOL> session . expunge_all ( ) <EOL> homer = Person . get_by ( name = "<STR_LIT>" ) <EOL> lisa = Person . get_by ( name = "<STR_LIT>" ) <EOL> slh = Animal . get_by ( name = "<STR_LIT>" ) <EOL> assert len ( homer . animals ) == <NUM_LIT:2> <EOL> assert homer == lisa . pets [ <NUM_LIT:0> ] . feeder <EOL> assert homer == slh . owner <EOL> def test_selfref ( self ) : <EOL> person_table = Table ( '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:32> ) ) ) <EOL> metadata . create_all ( ) <EOL> metadata . clear ( ) <EOL> class Person ( Entity ) : <EOL> father = ManyToOne ( '<STR_LIT>' ) <EOL> children = OneToMany ( '<STR_LIT>' ) <EOL> setup_all ( ) <EOL> grampa = Person ( name = "<STR_LIT>" ) <EOL> homer = Person ( name = "<STR_LIT>" ) <EOL> bart = Person ( name = "<STR_LIT>" ) <EOL> lisa = Person ( name = "<STR_LIT>" ) <EOL> grampa . children . append ( homer ) <EOL> homer . children . append ( bart ) <EOL> lisa . father = homer <EOL> session . commit ( ) <EOL> session . expunge_all ( ) <EOL> p = Person . get_by ( name = "<STR_LIT>" ) <EOL> assert p in p . father . children <EOL> assert p . father . name == "<STR_LIT>" <EOL> assert p . father is Person . get_by ( name = "<STR_LIT>" ) <EOL> assert p is Person . get_by ( name = "<STR_LIT>" ) . father <EOL> def test_m2m ( self ) : <EOL> person_table = Table ( '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:32> ) ) ) <EOL> category_table = Table ( '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:30> ) , primary_key = True ) ) <EOL> person_category_table = Table ( '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT:30> ) , ForeignKey ( '<STR_LIT>' ) ) ) <EOL> metadata . create_all ( ) <EOL> metadata . clear ( ) <EOL> class Person ( Entity ) : <EOL> categories = ManyToMany ( '<STR_LIT>' , <EOL> tablename = '<STR_LIT>' ) <EOL> class Category ( Entity ) : <EOL> persons = ManyToMany ( '<STR_LIT>' , <EOL> tablename = '<STR_LIT>' ) <EOL> setup_all ( ) <EOL> stupid = Category ( name = "<STR_LIT>" ) <EOL> simpson = Category ( name = "<STR_LIT>" ) <EOL> old = Category ( name = "<STR_LIT>" ) <EOL> grampa = Person ( name = "<STR_LIT>" , categories = [ simpson , old ] ) <EOL> homer = Person ( name = "<STR_LIT>" , categories = [ simpson , stupid ] ) <EOL> bart = Person ( name = "<STR_LIT>" ) <EOL> lisa = Person ( name = "<STR_LIT>" ) <EOL> simpson . persons . extend ( [ bart , lisa ] ) <EOL> session . commit ( ) <EOL> session . expunge_all ( ) <EOL> c = Category . get_by ( name = "<STR_LIT>" ) <EOL> grampa = Person . get_by ( name = "<STR_LIT>" ) <EOL> assert len ( c . persons ) == <NUM_LIT:4> <EOL> assert c in grampa . categories <EOL> def test_m2m_selfref ( self ) : <EOL> person_table = Table ( '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:32> ) ) ) <EOL> person_person_table = Table ( '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) ) <EOL> metadata . create_all ( ) <EOL> metadata . clear ( ) <EOL> class Person ( Entity ) : <EOL> appreciate = ManyToMany ( '<STR_LIT>' , <EOL> tablename = '<STR_LIT>' , <EOL> local_colname = '<STR_LIT>' ) <EOL> isappreciatedby = ManyToMany ( '<STR_LIT>' , <EOL> tablename = '<STR_LIT>' , <EOL> local_colname = '<STR_LIT>' ) <EOL> setup_all ( ) <EOL> barney = Person ( name = "<STR_LIT>" ) <EOL> homer = Person ( name = "<STR_LIT>" , appreciate = [ barney ] ) <EOL> session . commit ( ) <EOL> session . expunge_all ( ) <EOL> homer = Person . get_by ( name = "<STR_LIT>" ) <EOL> barney = Person . get_by ( name = "<STR_LIT>" ) <EOL> assert barney in homer . appreciate <EOL> assert homer in barney . isappreciatedby <EOL> def _create_table_a ( self ) : <EOL> a_table = Table ( '<STR_LIT:a>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:32> ) ) ) <EOL> metadata . create_all ( ) <EOL> metadata . clear ( ) <EOL> def test_override_pk_fails ( self ) : <EOL> self . _create_table_a ( ) <EOL> class A ( Entity ) : <EOL> id = Field ( Integer , primary_key = True ) <EOL> setup_entity_raise ( A ) <EOL> def test_override_non_pk_fails ( self ) : <EOL> self . _create_table_a ( ) <EOL> class A ( Entity ) : <EOL> name = Field ( String ( <NUM_LIT:30> ) ) <EOL> setup_entity_raise ( A ) <EOL> def test_override_pk ( self ) : <EOL> self . _create_table_a ( ) <EOL> class A ( Entity ) : <EOL> using_options ( allowcoloverride = True ) <EOL> id = Field ( Integer , primary_key = True ) <EOL> setup_entities ( [ A ] ) <EOL> def test_override_non_pk ( self ) : <EOL> self . _create_table_a ( ) <EOL> class A ( Entity ) : <EOL> using_options ( allowcoloverride = True ) <EOL> name = Field ( String ( <NUM_LIT:30> ) ) <EOL> setup_entities ( [ A ] ) <EOL> assert isinstance ( A . table . columns [ '<STR_LIT:name>' ] . type , String ) <EOL> def test_nopk ( self ) : <EOL> table = Table ( '<STR_LIT:a>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer ) , <EOL> Column ( '<STR_LIT:name>' , String ( <NUM_LIT:32> ) ) ) <EOL> metadata . create_all ( ) <EOL> metadata . clear ( ) <EOL> class A ( Entity ) : <EOL> using_mapper_options ( primary_key = [ '<STR_LIT:id>' ] ) <EOL> setup_all ( ) <EOL> a1 = A ( id = <NUM_LIT:1> , name = "<STR_LIT>" ) <EOL> session . commit ( ) <EOL> session . expunge_all ( ) <EOL> res = A . query . all ( ) <EOL> assert len ( res ) == <NUM_LIT:1> <EOL> assert res [ <NUM_LIT:0> ] . name == "<STR_LIT>" <EOL> def test_inheritance ( self ) : <EOL> table = Table ( '<STR_LIT>' , metadata , <EOL> Column ( '<STR_LIT:id>' , Integer , primary_key = True ) , <EOL> Column ( '<STR_LIT>' , elixir . options . POLYMORPHIC_COL_TYPE ) ) <EOL> metadata . create_all ( ) <EOL> metadata . clear ( ) <EOL> class Father ( Entity ) : <EOL> pass <EOL> class Son ( Father ) : <EOL> pass <EOL> setup_all ( ) <EOL> def test_autoload_mixed ( self ) : <EOL> conn = metadata . bind . connect ( ) <EOL> conn . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conn . close ( ) <EOL> class User ( Entity ) : <EOL> using_options ( tablename = '<STR_LIT:user>' , autoload = True ) <EOL> class Item ( Entity ) : <EOL> using_options ( autoload = False ) <EOL> owner = ManyToOne ( '<STR_LIT>' ) <EOL> setup_all ( True ) <EOL> colname = Item . table . c [ '<STR_LIT>' ] . foreign_keys [ <NUM_LIT:0> ] . column . name <EOL> assert colname == '<STR_LIT>' </s>
