<s> from . import config <EOL> from . import assertions , schema <EOL> from . util import adict <EOL> from . . import util <EOL> from . engines import drop_all_tables <EOL> from . entities import BasicEntity , ComparableEntity <EOL> import sys <EOL> import sqlalchemy as sa <EOL> from sqlalchemy . ext . declarative import declarative_base , DeclarativeMeta <EOL> class TestBase ( object ) : <EOL> __whitelist__ = ( ) <EOL> __requires__ = ( ) <EOL> __unsupported_on__ = ( ) <EOL> __only_on__ = None <EOL> __skip_if__ = None <EOL> def assert_ ( self , val , msg = None ) : <EOL> assert val , msg <EOL> def setup ( self ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> self . setUp ( ) <EOL> def teardown ( self ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> self . tearDown ( ) <EOL> class TablesTest ( TestBase ) : <EOL> run_setup_bind = '<STR_LIT>' <EOL> run_define_tables = '<STR_LIT>' <EOL> run_create_tables = '<STR_LIT>' <EOL> run_inserts = '<STR_LIT>' <EOL> run_deletes = '<STR_LIT>' <EOL> run_dispose_bind = None <EOL> bind = None <EOL> metadata = None <EOL> tables = None <EOL> other = None <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> cls . _init_class ( ) <EOL> cls . _setup_once_tables ( ) <EOL> cls . _setup_once_inserts ( ) <EOL> @ classmethod <EOL> def _init_class ( cls ) : <EOL> if cls . run_define_tables == '<STR_LIT>' : <EOL> if cls . run_create_tables == '<STR_LIT>' : <EOL> cls . run_create_tables = '<STR_LIT>' <EOL> assert cls . run_inserts in ( '<STR_LIT>' , None ) <EOL> if cls . other is None : <EOL> cls . other = adict ( ) <EOL> if cls . tables is None : <EOL> cls . tables = adict ( ) <EOL> if cls . bind is None : <EOL> setattr ( cls , '<STR_LIT>' , cls . setup_bind ( ) ) <EOL> if cls . metadata is None : <EOL> setattr ( cls , '<STR_LIT>' , sa . MetaData ( ) ) <EOL> if cls . metadata . bind is None : <EOL> cls . metadata . bind = cls . bind <EOL> @ classmethod <EOL> def _setup_once_inserts ( cls ) : <EOL> if cls . run_inserts == '<STR_LIT>' : <EOL> cls . _load_fixtures ( ) <EOL> cls . insert_data ( ) <EOL> @ classmethod <EOL> def _setup_once_tables ( cls ) : <EOL> if cls . run_define_tables == '<STR_LIT>' : <EOL> cls . define_tables ( cls . metadata ) <EOL> if cls . run_create_tables == '<STR_LIT>' : <EOL> cls . metadata . create_all ( cls . bind ) <EOL> cls . tables . update ( cls . metadata . tables ) <EOL> def _setup_each_tables ( self ) : <EOL> if self . run_define_tables == '<STR_LIT>' : <EOL> self . tables . clear ( ) <EOL> if self . run_create_tables == '<STR_LIT>' : <EOL> drop_all_tables ( self . metadata , self . bind ) <EOL> self . metadata . clear ( ) <EOL> self . define_tables ( self . metadata ) <EOL> if self . run_create_tables == '<STR_LIT>' : <EOL> self . metadata . create_all ( self . bind ) <EOL> self . tables . update ( self . metadata . tables ) <EOL> elif self . run_create_tables == '<STR_LIT>' : <EOL> drop_all_tables ( self . metadata , self . bind ) <EOL> self . metadata . create_all ( self . bind ) <EOL> def _setup_each_inserts ( self ) : <EOL> if self . run_inserts == '<STR_LIT>' : <EOL> self . _load_fixtures ( ) <EOL> self . insert_data ( ) <EOL> def _teardown_each_tables ( self ) : <EOL> if self . run_define_tables != '<STR_LIT>' and self . run_deletes == '<STR_LIT>' : <EOL> for table in reversed ( self . metadata . sorted_tables ) : <EOL> try : <EOL> table . delete ( ) . execute ( ) . close ( ) <EOL> except sa . exc . DBAPIError as ex : <EOL> util . print_ ( <EOL> ( "<STR_LIT>" % ( table , ex ) ) , <EOL> file = sys . stderr ) <EOL> def setup ( self ) : <EOL> self . _setup_each_tables ( ) <EOL> self . _setup_each_inserts ( ) <EOL> def teardown ( self ) : <EOL> self . _teardown_each_tables ( ) <EOL> @ classmethod <EOL> def _teardown_once_metadata_bind ( cls ) : <EOL> if cls . run_create_tables : <EOL> drop_all_tables ( cls . metadata , cls . bind ) <EOL> if cls . run_dispose_bind == '<STR_LIT>' : <EOL> cls . dispose_bind ( cls . bind ) <EOL> cls . metadata . bind = None <EOL> if cls . run_setup_bind is not None : <EOL> cls . bind = None <EOL> @ classmethod <EOL> def teardown_class ( cls ) : <EOL> cls . _teardown_once_metadata_bind ( ) <EOL> @ classmethod <EOL> def setup_bind ( cls ) : <EOL> return config . db <EOL> @ classmethod <EOL> def dispose_bind ( cls , bind ) : <EOL> if hasattr ( bind , '<STR_LIT>' ) : <EOL> bind . dispose ( ) <EOL> elif hasattr ( bind , '<STR_LIT>' ) : <EOL> bind . close ( ) <EOL> @ classmethod <EOL> def define_tables ( cls , metadata ) : <EOL> pass <EOL> @ classmethod <EOL> def fixtures ( cls ) : <EOL> return { } <EOL> @ classmethod <EOL> def insert_data ( cls ) : <EOL> pass <EOL> def sql_count_ ( self , count , fn ) : <EOL> self . assert_sql_count ( self . bind , fn , count ) <EOL> def sql_eq_ ( self , callable_ , statements , with_sequences = None ) : <EOL> self . assert_sql ( self . bind , <EOL> callable_ , statements , with_sequences ) <EOL> @ classmethod <EOL> def _load_fixtures ( cls ) : <EOL> """<STR_LIT>""" <EOL> headers , rows = { } , { } <EOL> for table , data in cls . fixtures ( ) . items ( ) : <EOL> if len ( data ) < <NUM_LIT:2> : <EOL> continue <EOL> if isinstance ( table , util . string_types ) : <EOL> table = cls . tables [ table ] <EOL> headers [ table ] = data [ <NUM_LIT:0> ] <EOL> rows [ table ] = data [ <NUM_LIT:1> : ] <EOL> for table in cls . metadata . sorted_tables : <EOL> if table not in headers : <EOL> continue <EOL> cls . bind . execute ( <EOL> table . insert ( ) , <EOL> [ dict ( zip ( headers [ table ] , column_values ) ) <EOL> for column_values in rows [ table ] ] ) <EOL> from sqlalchemy import event <EOL> class RemovesEvents ( object ) : <EOL> @ util . memoized_property <EOL> def _event_fns ( self ) : <EOL> return set ( ) <EOL> def event_listen ( self , target , name , fn ) : <EOL> self . _event_fns . add ( ( target , name , fn ) ) <EOL> event . listen ( target , name , fn ) <EOL> def teardown ( self ) : <EOL> for key in self . _event_fns : <EOL> event . remove ( * key ) <EOL> super_ = super ( RemovesEvents , self ) <EOL> if hasattr ( super_ , "<STR_LIT>" ) : <EOL> super_ . teardown ( ) <EOL> class _ORMTest ( object ) : <EOL> @ classmethod <EOL> def teardown_class ( cls ) : <EOL> sa . orm . session . Session . close_all ( ) <EOL> sa . orm . clear_mappers ( ) <EOL> class ORMTest ( _ORMTest , TestBase ) : <EOL> pass <EOL> class MappedTest ( _ORMTest , TablesTest , assertions . AssertsExecutionResults ) : <EOL> run_setup_classes = '<STR_LIT>' <EOL> run_setup_mappers = '<STR_LIT>' <EOL> classes = None <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> cls . _init_class ( ) <EOL> if cls . classes is None : <EOL> cls . classes = adict ( ) <EOL> cls . _setup_once_tables ( ) <EOL> cls . _setup_once_classes ( ) <EOL> cls . _setup_once_mappers ( ) <EOL> cls . _setup_once_inserts ( ) <EOL> @ classmethod <EOL> def teardown_class ( cls ) : <EOL> cls . _teardown_once_class ( ) <EOL> cls . _teardown_once_metadata_bind ( ) <EOL> def setup ( self ) : <EOL> self . _setup_each_tables ( ) <EOL> self . _setup_each_mappers ( ) <EOL> self . _setup_each_inserts ( ) <EOL> def teardown ( self ) : <EOL> sa . orm . session . Session . close_all ( ) <EOL> self . _teardown_each_mappers ( ) <EOL> self . _teardown_each_tables ( ) <EOL> @ classmethod <EOL> def _teardown_once_class ( cls ) : <EOL> cls . classes . clear ( ) <EOL> _ORMTest . teardown_class ( ) <EOL> @ classmethod <EOL> def _setup_once_classes ( cls ) : <EOL> if cls . run_setup_classes == '<STR_LIT>' : <EOL> cls . _with_register_classes ( cls . setup_classes ) <EOL> @ classmethod <EOL> def _setup_once_mappers ( cls ) : <EOL> if cls . run_setup_mappers == '<STR_LIT>' : <EOL> cls . _with_register_classes ( cls . setup_mappers ) <EOL> def _setup_each_mappers ( self ) : <EOL> if self . run_setup_mappers == '<STR_LIT>' : <EOL> self . _with_register_classes ( self . setup_mappers ) <EOL> @ classmethod <EOL> def _with_register_classes ( cls , fn ) : <EOL> """<STR_LIT>""" <EOL> cls_registry = cls . classes <EOL> class FindFixture ( type ) : <EOL> def __init__ ( cls , classname , bases , dict_ ) : <EOL> cls_registry [ classname ] = cls <EOL> return type . __init__ ( cls , classname , bases , dict_ ) <EOL> class _Base ( util . with_metaclass ( FindFixture , object ) ) : <EOL> pass <EOL> class Basic ( BasicEntity , _Base ) : <EOL> pass <EOL> class Comparable ( ComparableEntity , _Base ) : <EOL> pass <EOL> cls . Basic = Basic <EOL> cls . Comparable = Comparable <EOL> fn ( ) <EOL> def _teardown_each_mappers ( self ) : <EOL> if self . run_setup_mappers != '<STR_LIT>' : <EOL> sa . orm . clear_mappers ( ) <EOL> @ classmethod <EOL> def setup_classes ( cls ) : <EOL> pass <EOL> @ classmethod <EOL> def setup_mappers ( cls ) : <EOL> pass <EOL> class DeclarativeMappedTest ( MappedTest ) : <EOL> run_setup_classes = '<STR_LIT>' <EOL> run_setup_mappers = '<STR_LIT>' <EOL> @ classmethod <EOL> def _setup_once_tables ( cls ) : <EOL> pass <EOL> @ classmethod <EOL> def _with_register_classes ( cls , fn ) : <EOL> cls_registry = cls . classes <EOL> class FindFixtureDeclarative ( DeclarativeMeta ) : <EOL> def __init__ ( cls , classname , bases , dict_ ) : <EOL> cls_registry [ classname ] = cls <EOL> return DeclarativeMeta . __init__ ( <EOL> cls , classname , bases , dict_ ) <EOL> class DeclarativeBasic ( object ) : <EOL> __table_cls__ = schema . Table <EOL> _DeclBase = declarative_base ( metadata = cls . metadata , <EOL> metaclass = FindFixtureDeclarative , <EOL> cls = DeclarativeBasic ) <EOL> cls . DeclarativeBasic = _DeclBase <EOL> fn ( ) <EOL> if cls . metadata . tables and cls . run_create_tables : <EOL> cls . metadata . create_all ( config . db ) </s>
