<s> import re <EOL> import sys <EOL> import unittest <EOL> from cStringIO import StringIO <EOL> from iktomi . cli . sqla import Sqla , drop_everything <EOL> from sqlalchemy import ( <EOL> create_engine , orm , MetaData , Column , Integer , ForeignKey , <EOL> ) <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> from sqlalchemy . dialects . mysql import MEDIUMTEXT <EOL> try : <EOL> from unittest import mock <EOL> except ImportError : <EOL> import mock <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class SqlaTests ( unittest . TestCase ) : <EOL> def test_drop_everything ( self ) : <EOL> Base = declarative_base ( ) <EOL> class A ( Base ) : <EOL> __tablename__ = '<STR_LIT:A>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> b_id = Column ( ForeignKey ( '<STR_LIT>' , use_alter = True ) ) <EOL> class B ( Base ) : <EOL> __tablename__ = '<STR_LIT:B>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> a_id = Column ( ForeignKey ( A . id ) ) <EOL> engine = create_engine ( '<STR_LIT>' ) <EOL> Base . metadata . create_all ( bind = engine ) <EOL> self . assertTrue ( engine . has_table ( '<STR_LIT:A>' ) ) <EOL> self . assertTrue ( engine . has_table ( '<STR_LIT:B>' ) ) <EOL> drop_everything ( engine ) <EOL> self . assertFalse ( engine . has_table ( '<STR_LIT:A>' ) ) <EOL> self . assertFalse ( engine . has_table ( '<STR_LIT:B>' ) ) <EOL> def test_specific_dialect ( self ) : <EOL> Base = declarative_base ( ) <EOL> class Obj ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> text = Column ( MEDIUMTEXT ) <EOL> engine = create_engine ( '<STR_LIT>' ) <EOL> cli = Sqla ( orm . sessionmaker ( bind = engine ) , metadata = Base . metadata ) <EOL> schema = cli . _schema ( Obj . __table__ ) <EOL> self . assertIn ( '<STR_LIT>' , schema ) <EOL> def test_create_drop_tables_single_meta ( self ) : <EOL> Base = declarative_base ( ) <EOL> class A ( Base ) : <EOL> __tablename__ = '<STR_LIT:A>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> engine = create_engine ( '<STR_LIT>' ) <EOL> cli = Sqla ( orm . sessionmaker ( bind = engine ) , metadata = Base . metadata ) <EOL> for verbose in [ False , True ] : <EOL> cli . command_create_tables ( verbose = verbose ) <EOL> self . assertTrue ( engine . has_table ( '<STR_LIT:A>' ) ) <EOL> with mock . patch . object ( sys . stdin , '<STR_LIT>' , return_value = '<STR_LIT:n>' ) : <EOL> try : <EOL> cli . command_drop_tables ( ) <EOL> except SystemExit : <EOL> pass <EOL> self . assertTrue ( engine . has_table ( '<STR_LIT:A>' ) ) <EOL> with mock . patch . object ( sys . stdin , '<STR_LIT>' , return_value = '<STR_LIT:y>' ) : <EOL> cli . command_drop_tables ( ) <EOL> self . assertFalse ( engine . has_table ( '<STR_LIT:A>' ) ) <EOL> def test_create_drop_tables_several_meta ( self ) : <EOL> Base1 = declarative_base ( ) <EOL> class A1 ( Base1 ) : <EOL> __tablename__ = '<STR_LIT:A>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> Base2 = declarative_base ( ) <EOL> class A2 ( Base2 ) : <EOL> __tablename__ = '<STR_LIT:A>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> engine1 = create_engine ( '<STR_LIT>' ) <EOL> engine2 = create_engine ( '<STR_LIT>' ) <EOL> binds = { <EOL> A1 . __table__ : engine1 , <EOL> A2 . __table__ : engine2 , <EOL> } <EOL> meta = { <EOL> '<STR_LIT>' : Base1 . metadata , <EOL> '<STR_LIT>' : Base2 . metadata , <EOL> '<STR_LIT>' : MetaData ( ) , <EOL> } <EOL> cli = Sqla ( orm . sessionmaker ( binds = binds ) , metadata = meta ) <EOL> for verbose in [ False , True ] : <EOL> cli . command_create_tables ( verbose = verbose ) <EOL> self . assertTrue ( engine1 . has_table ( '<STR_LIT:A>' ) ) <EOL> self . assertTrue ( engine2 . has_table ( '<STR_LIT:A>' ) ) <EOL> with mock . patch . object ( sys . stdin , '<STR_LIT>' , return_value = '<STR_LIT:y>' ) : <EOL> cli . command_drop_tables ( '<STR_LIT>' ) <EOL> self . assertFalse ( engine1 . has_table ( '<STR_LIT:A>' ) ) <EOL> self . assertTrue ( engine2 . has_table ( '<STR_LIT:A>' ) ) <EOL> with mock . patch . object ( sys . stdin , '<STR_LIT>' , return_value = '<STR_LIT:y>' ) : <EOL> cli . command_drop_tables ( ) <EOL> self . assertFalse ( engine1 . has_table ( '<STR_LIT:A>' ) ) <EOL> self . assertFalse ( engine2 . has_table ( '<STR_LIT:A>' ) ) <EOL> cli . command_create_tables ( '<STR_LIT>' , verbose = verbose ) <EOL> self . assertTrue ( engine1 . has_table ( '<STR_LIT:A>' ) ) <EOL> self . assertFalse ( engine2 . has_table ( '<STR_LIT:A>' ) ) <EOL> with mock . patch . object ( sys . stdin , '<STR_LIT>' , return_value = '<STR_LIT:y>' ) : <EOL> cli . command_drop_tables ( ) <EOL> self . assertFalse ( engine1 . has_table ( '<STR_LIT:A>' ) ) <EOL> self . assertFalse ( engine2 . has_table ( '<STR_LIT:A>' ) ) <EOL> cli . command_create_tables ( '<STR_LIT>' , verbose = verbose ) <EOL> self . assertFalse ( engine1 . has_table ( '<STR_LIT:A>' ) ) <EOL> self . assertFalse ( engine2 . has_table ( '<STR_LIT:A>' ) ) <EOL> def test_reset ( self ) : <EOL> Base = declarative_base ( ) <EOL> class A ( Base ) : <EOL> __tablename__ = '<STR_LIT:A>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> id_values = [ id_value1 , id_value2 ] = [ <NUM_LIT:12> , <NUM_LIT> ] <EOL> def initial ( db ) : <EOL> db . add ( A ( id = id_values . pop ( <NUM_LIT:0> ) ) ) <EOL> engine = create_engine ( '<STR_LIT>' ) <EOL> cli = Sqla ( orm . sessionmaker ( bind = engine ) , metadata = Base . metadata , <EOL> initial = initial ) <EOL> with mock . patch . object ( sys . stdin , '<STR_LIT>' , return_value = '<STR_LIT:y>' ) : <EOL> cli . command_reset ( ) <EOL> query = cli . session . query ( A ) <EOL> self . assertEqual ( query . count ( ) , <NUM_LIT:1> ) <EOL> a = query . one ( ) <EOL> self . assertEqual ( a . id , id_value1 ) <EOL> with mock . patch . object ( sys . stdin , '<STR_LIT>' , return_value = '<STR_LIT:y>' ) : <EOL> cli . command_reset ( ) <EOL> query = cli . session . query ( A ) <EOL> self . assertEqual ( query . count ( ) , <NUM_LIT:1> ) <EOL> a = query . one ( ) <EOL> self . assertEqual ( a . id , id_value2 ) <EOL> _created_tables = re . compile ( r'<STR_LIT>' , re . I ) . findall <EOL> def test_schema_single_meta ( self ) : <EOL> Base = declarative_base ( ) <EOL> class A ( Base ) : <EOL> __tablename__ = '<STR_LIT:A>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> class B ( Base ) : <EOL> __tablename__ = '<STR_LIT:B>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> engine = create_engine ( '<STR_LIT>' ) <EOL> cli = Sqla ( orm . sessionmaker ( bind = engine ) , metadata = Base . metadata ) <EOL> output = StringIO ( ) <EOL> with mock . patch . object ( sys , '<STR_LIT>' , output ) : <EOL> cli . command_schema ( ) <EOL> created = self . _created_tables ( output . getvalue ( ) ) <EOL> self . assertEqual ( len ( created ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( created . count ( '<STR_LIT:A>' ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( created . count ( '<STR_LIT:B>' ) , <NUM_LIT:1> ) <EOL> output = StringIO ( ) <EOL> with mock . patch . object ( sys , '<STR_LIT>' , output ) : <EOL> cli . command_schema ( '<STR_LIT:A>' ) <EOL> created = self . _created_tables ( output . getvalue ( ) ) <EOL> self . assertEqual ( created , [ '<STR_LIT:A>' ] ) <EOL> output = StringIO ( ) <EOL> with mock . patch . object ( sys , '<STR_LIT>' , output ) : <EOL> try : <EOL> cli . command_schema ( '<STR_LIT:C>' ) <EOL> except SystemExit : <EOL> pass <EOL> created = self . _created_tables ( output . getvalue ( ) ) <EOL> self . assertEqual ( created , [ ] ) <EOL> def test_schema_several_meta ( self ) : <EOL> Base1 = declarative_base ( ) <EOL> class A1 ( Base1 ) : <EOL> __tablename__ = '<STR_LIT:A>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> class B1 ( Base1 ) : <EOL> __tablename__ = '<STR_LIT:B>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> Base2 = declarative_base ( ) <EOL> class A2 ( Base2 ) : <EOL> __tablename__ = '<STR_LIT:A>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> engine1 = create_engine ( '<STR_LIT>' ) <EOL> engine2 = create_engine ( '<STR_LIT>' ) <EOL> binds = { <EOL> A1 . __table__ : engine1 , <EOL> B1 . __table__ : engine1 , <EOL> A2 . __table__ : engine2 , <EOL> } <EOL> meta = { <EOL> '<STR_LIT>' : Base1 . metadata , <EOL> '<STR_LIT>' : Base2 . metadata , <EOL> '<STR_LIT>' : MetaData ( ) , <EOL> } <EOL> cli = Sqla ( orm . sessionmaker ( binds = binds ) , metadata = meta ) <EOL> output = StringIO ( ) <EOL> with mock . patch . object ( sys , '<STR_LIT>' , output ) : <EOL> cli . command_schema ( ) <EOL> created = self . _created_tables ( output . getvalue ( ) ) <EOL> self . assertEqual ( len ( created ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( created . count ( '<STR_LIT:A>' ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( created . count ( '<STR_LIT:B>' ) , <NUM_LIT:1> ) <EOL> output = StringIO ( ) <EOL> with mock . patch . object ( sys , '<STR_LIT>' , output ) : <EOL> cli . command_schema ( '<STR_LIT>' ) <EOL> created = self . _created_tables ( output . getvalue ( ) ) <EOL> self . assertEqual ( len ( created ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( created . count ( '<STR_LIT:A>' ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( created . count ( '<STR_LIT:B>' ) , <NUM_LIT:1> ) <EOL> output = StringIO ( ) <EOL> with mock . patch . object ( sys , '<STR_LIT>' , output ) : <EOL> cli . command_schema ( '<STR_LIT>' ) <EOL> created = self . _created_tables ( output . getvalue ( ) ) <EOL> self . assertEqual ( created , [ '<STR_LIT:B>' ] ) <EOL> output = StringIO ( ) <EOL> with mock . patch . object ( sys , '<STR_LIT>' , output ) : <EOL> try : <EOL> cli . command_schema ( '<STR_LIT>' ) <EOL> except SystemExit : <EOL> pass <EOL> created = self . _created_tables ( output . getvalue ( ) ) <EOL> self . assertEqual ( created , [ ] ) <EOL> output = StringIO ( ) <EOL> with mock . patch . object ( sys , '<STR_LIT>' , output ) : <EOL> try : <EOL> cli . command_schema ( '<STR_LIT>' ) <EOL> except SystemExit : <EOL> pass <EOL> created = self . _created_tables ( output . getvalue ( ) ) <EOL> self . assertEqual ( created , [ ] ) <EOL> def test_gen ( self ) : <EOL> gen_a = mock . MagicMock ( ) <EOL> cli = Sqla ( orm . sessionmaker ( ) , MetaData ( ) , generators = { '<STR_LIT:a>' : gen_a } ) <EOL> try : <EOL> cli . command_gen ( ) <EOL> except SystemExit : <EOL> pass <EOL> gen_a . assert_not_called ( ) <EOL> gen_a . reset_mock ( ) <EOL> cli . command_gen ( '<STR_LIT:a>' ) <EOL> gen_a . assert_called_once_with ( cli . session , <NUM_LIT:0> ) <EOL> gen_a . reset_mock ( ) <EOL> cli . command_gen ( '<STR_LIT>' ) <EOL> gen_a . assert_called_once_with ( cli . session , <NUM_LIT> ) </s>
