def test_render_table_upgrade(self): m = MetaData() t = Table( 'test', m, Column('id', Integer, primary_key=True), Column('name', Unicode(255)), Column("address_id", Integer, ForeignKey("address.id")), Column("timestamp", DATETIME, server_default="NOW()"), Column("amount", Numeric(5, 2)), UniqueConstraint("name", name="uq_name"), UniqueConstraint("timestamp"), ) eq_ignore_whitespace( autogenerate._add_table(t, self.autogen_context), "op.create_table('test'," "sa.Column('id', sa.Integer(), nullable=False)," "sa.Column('name', sa.Unicode(length=255), nullable=True)," "sa.Column('address_id', sa.Integer(), nullable=True)," "sa.Column('timestamp', sa.DATETIME(), " "server_default='NOW()', " "nullable=True)," "sa.Column('amount', sa.Numeric(precision=5, scale=2), nullable=True)," "sa.ForeignKeyConstraint(['address_id'], ['address.id'], )," "sa.PrimaryKeyConstraint('id')," "sa.UniqueConstraint('name', name='uq_name')," "sa.UniqueConstraint('timestamp')" ")")
def test_render_table_upgrade(self): m = MetaData() t = Table('test', m, Column('id', Integer, primary_key=True), Column('name', Unicode(255)), Column("address_id", Integer, ForeignKey("address.id")), Column("timestamp", DATETIME, server_default="NOW()"), Column("amount", Numeric(5, 2)), UniqueConstraint("name", name="uq_name"), UniqueConstraint("timestamp"), ) eq_ignore_whitespace( autogenerate._add_table(t, self.autogen_context), "op.create_table('test'," "sa.Column('id', sa.Integer(), nullable=False)," "sa.Column('name', sa.Unicode(length=255), nullable=True)," "sa.Column('address_id', sa.Integer(), nullable=True)," "sa.Column('timestamp', sa.DATETIME(), " "server_default='NOW()', " "nullable=True)," "sa.Column('amount', sa.Numeric(precision=5, scale=2), nullable=True)," "sa.ForeignKeyConstraint(['address_id'], ['address.id'], )," "sa.PrimaryKeyConstraint('id')," "sa.UniqueConstraint('name', name='uq_name')," "sa.UniqueConstraint('timestamp')" ")" )
def test_render_custom(self): def render(type_, obj, context): if type_ == "foreign_key": return None if type_ == "column": if obj.name == "y": return None else: return "col(%s)" % obj.name return "render:%s" % type_ autogen_context = { "opts": { 'render_item': render, 'alembic_module_prefix': 'sa.' } } t = Table('t', MetaData(), Column('x', Integer), Column('y', Integer), PrimaryKeyConstraint('x'), ForeignKeyConstraint(['x'], ['y'])) result = autogenerate._add_table(t, autogen_context) eq_(result, """sa.create_table('t', col(x), render:primary_key\n)""")
def test_render_custom(self): def render(type_, obj, context): if type_ == "foreign_key": return None if type_ == "column": if obj.name == "y": return None else: return "col(%s)" % obj.name return "render:%s" % type_ autogen_context = {"opts": { 'render_item': render, 'alembic_module_prefix': 'sa.' }} t = Table('t', MetaData(), Column('x', Integer), Column('y', Integer), PrimaryKeyConstraint('x'), ForeignKeyConstraint(['x'], ['y']) ) result = autogenerate._add_table( t, autogen_context ) eq_( result, """sa.create_table('t', col(x), render:primary_key\n)""" )
def test_render_table_w_fk_schema(self): m = MetaData() t = Table('test', m, Column('id', Integer, primary_key=True), Column('q', Integer, ForeignKey('foo.address.id')), ) eq_ignore_whitespace( autogenerate._add_table(t, self.autogen_context), "op.create_table('test'," "sa.Column('id', sa.Integer(), nullable=False)," "sa.Column('q', sa.Integer(), nullable=True)," "sa.ForeignKeyConstraint(['q'], ['foo.address.id'], )," "sa.PrimaryKeyConstraint('id')" ")" )
def test_render_addtl_args(self): m = MetaData() t = Table('test', m, Column('id', Integer, primary_key=True), Column('q', Integer, ForeignKey('bar.address.id')), postgresql_arg1="some_arg", mysql_engine="InnoDB") eq_ignore_whitespace( autogenerate._add_table(t, self.autogen_context), "op.create_table('test'," "sa.Column('id', sa.Integer(), nullable=False)," "sa.Column('q', sa.Integer(), nullable=True)," "sa.ForeignKeyConstraint(['q'], ['bar.address.id'], )," "sa.PrimaryKeyConstraint('id')," "mysql_engine='InnoDB',postgresql_arg1='some_arg')")
def test_render_addtl_args(self): m = MetaData() t = Table('test', m, Column('id', Integer, primary_key=True), Column('q', Integer, ForeignKey('bar.address.id')), postgresql_arg1="some_arg", mysql_engine="InnoDB" ) eq_ignore_whitespace( autogenerate._add_table(t, self.autogen_context), "op.create_table('test'," "sa.Column('id', sa.Integer(), nullable=False)," "sa.Column('q', sa.Integer(), nullable=True)," "sa.ForeignKeyConstraint(['q'], ['bar.address.id'], )," "sa.PrimaryKeyConstraint('id')," "mysql_engine='InnoDB',postgresql_arg1='some_arg')" )