Beispiel #1
0
 def test_inline_ck_constraint(self):
     t = Table('t', self.metadata, Column('c', Integer),
               CheckConstraint("c > 5"))
     eq_ignore_whitespace(
         autogenerate.render._add_table(t, self.autogen_context),
         "op.create_table('t',sa.Column('c', sa.Integer(), nullable=True),"
         "sa.CheckConstraint(!U'c > 5', name=op.f('ck_ct_t')))")
Beispiel #2
0
 def test_inline_fk(self):
     t = Table('t', self.metadata, Column('c', Integer, ForeignKey('q.id')))
     eq_ignore_whitespace(
         autogenerate.render._add_table(t, self.autogen_context),
         "op.create_table('t',sa.Column('c', sa.Integer(), nullable=True),"
         "sa.ForeignKeyConstraint(['c'], ['q.id'], "
         "name=op.f('fk_ct_t_c_q')))")
Beispiel #3
0
 def test_implicit_unique_constraint(self):
     t = Table('t', self.metadata, Column('c', Integer, unique=True))
     uq = [c for c in t.constraints if isinstance(c, UniqueConstraint)][0]
     eq_ignore_whitespace(
         autogenerate.render._render_unique_constraint(
             uq, self.autogen_context),
         "sa.UniqueConstraint('c', name=op.f('uq_ct_t_c'))")
Beispiel #4
0
    def test_inline_exclude_constraint_case_sensitive(self):
        from sqlalchemy.dialects.postgresql import ExcludeConstraint

        autogen_context = self.autogen_context

        m = MetaData()
        t = Table(
            "TTable", m, Column("XColumn", String), Column("YColumn", String)
        )
        ExcludeConstraint(
            (t.c.XColumn, ">"),
            using="gist",
            where='"XColumn" != 2',
            name="TExclX",
        )

        op_obj = ops.CreateTableOp.from_table(t)

        eq_ignore_whitespace(
            autogenerate.render_op_text(autogen_context, op_obj),
            "op.create_table('TTable',sa.Column('XColumn', sa.String(), "
            "nullable=True),"
            "sa.Column('YColumn', sa.String(), nullable=True),"
            "postgresql.ExcludeConstraint((sa.column('XColumn'), '>'), "
            "where=sa.text(!U'\"XColumn\" != 2'), using='gist', "
            "name='TExclX'))",
        )
Beispiel #5
0
 def test_schema_type_boolean(self):
     t = Table('t', self.metadata, Column('c', Boolean(name='xyz')))
     eq_ignore_whitespace(
         autogenerate.render._add_column(None, "t", t.c.c,
                                         self.autogen_context),
         "op.add_column('t', "
         "sa.Column('c', sa.Boolean(name='xyz'), nullable=True))")
Beispiel #6
0
 def test_render_add_column_w_schema(self):
     eq_ignore_whitespace(
         autogenerate.render._add_column(
             "foo", "bar", Column("x", Integer, server_default="5"),
             self.autogen_context),
         "op.add_column('bar', sa.Column('x', sa.Integer(), "
         "server_default='5', nullable=True), schema='foo')")
Beispiel #7
0
    def test_add_exclude_constraint_case_sensitive(self):
        from sqlalchemy.dialects.postgresql import ExcludeConstraint

        autogen_context = self.autogen_context

        m = MetaData()
        t = Table(
            "TTAble", m, Column("XColumn", String), Column("YColumn", String)
        )

        op_obj = ops.AddConstraintOp.from_constraint(
            ExcludeConstraint(
                (t.c.XColumn, ">"),
                where=t.c.XColumn != 2,
                using="gist",
                name="t_excl_x",
            )
        )

        eq_ignore_whitespace(
            autogenerate.render_op_text(autogen_context, op_obj),
            "op.create_exclude_constraint('t_excl_x', 'TTAble', "
            "(sa.column('XColumn'), '>'), "
            "where=sa.text(!U'\"XColumn\" != 2'), using='gist')",
        )
 def test_render_non_native_enum(self):
     eq_ignore_whitespace(
         autogenerate.render._repr_type(
             Enum("one", "two", "three", native_enum=False),
             self.autogen_context),
         "sa.Enum('one', 'two', 'three', native_enum=False)"
     )
Beispiel #9
0
    def test_render_add_index_pg_where(self):
        autogen_context = self.autogen_context

        m = MetaData()
        t = Table('t', m, Column('x', String), Column('y', String))

        idx = Index('foo_idx',
                    t.c.x,
                    t.c.y,
                    postgresql_where=(t.c.y == 'something'))

        op_obj = ops.CreateIndexOp.from_index(idx)

        if compat.sqla_08:
            eq_ignore_whitespace(
                autogenerate.render_op_text(autogen_context, op_obj),
                """op.create_index('foo_idx', 't', \
['x', 'y'], unique=False, """
                """postgresql_where=sa.text(!U"y = 'something'"))""")
        else:
            eq_ignore_whitespace(
                autogenerate.render_op_text(autogen_context, op_obj),
                """op.create_index('foo_idx', 't', ['x', 'y'], \
unique=False, """
                """postgresql_where=sa.text(!U't.y = %(y_1)s'))""")
 def test_render_drop_table_w_schema(self):
     eq_ignore_whitespace(
         autogenerate.render._drop_table(
             Table("sometable", MetaData(), schema='foo'),
             self.autogen_context),
         "op.drop_table('sometable', schema='foo')"
     )
    def test_render_add_index_pg_where(self):
        autogen_context = self.pg_autogen_context

        m = MetaData()
        t = Table('t', m,
                  Column('x', String),
                  Column('y', String)
                  )

        idx = Index('foo_idx', t.c.x, t.c.y,
                    postgresql_where=(t.c.y == 'something'))

        if compat.sqla_08:
            eq_ignore_whitespace(
                autogenerate.render._add_index(idx, autogen_context),
                """op.create_index('foo_idx', 't', \
['x', 'y'], unique=False, """
                """postgresql_where=sa.text(!U"t.y = 'something'"))"""
            )
        else:
            eq_ignore_whitespace(
                autogenerate.render._add_index(idx, autogen_context),
                """op.create_index('foo_idx', 't', ['x', 'y'], \
unique=False, """
                """postgresql_where=sa.text(!U't.y = %(y_1)s'))"""
            )
Beispiel #12
0
    def test_inline_exclude_constraint(self):
        from sqlalchemy.dialects.postgresql import ExcludeConstraint

        autogen_context = self.autogen_context

        m = MetaData()
        t = Table(
            "t",
            m,
            Column("x", String),
            Column("y", String),
            ExcludeConstraint(
                (column("x"), ">"),
                using="gist",
                where="x != 2",
                name="t_excl_x",
            ),
        )

        op_obj = ops.CreateTableOp.from_table(t)

        eq_ignore_whitespace(
            autogenerate.render_op_text(autogen_context, op_obj),
            "op.create_table('t',sa.Column('x', sa.String(), nullable=True),"
            "sa.Column('y', sa.String(), nullable=True),"
            "postgresql.ExcludeConstraint((sa.column('x'), '>'), "
            "where=sa.text(!U'x != 2'), using='gist', name='t_excl_x')"
            ")",
        )
Beispiel #13
0
    def test_inline_exclude_constraint(self):
        from sqlalchemy.dialects.postgresql import ExcludeConstraint

        autogen_context = self.autogen_context

        m = MetaData()
        t = Table(
            't', m,
            Column('x', String),
            Column('y', String),
            ExcludeConstraint(
                ('x', ">"),
                using="gist",
                where='x != 2',
                name="t_excl_x"
            )
        )

        op_obj = ops.CreateTableOp.from_table(t)

        eq_ignore_whitespace(
            autogenerate.render_op_text(autogen_context, op_obj),
            "op.create_table('t',sa.Column('x', sa.String(), nullable=True),"
            "sa.Column('y', sa.String(), nullable=True),"
            "postgresql.ExcludeConstraint((!U'x', '>'), "
            "where=sa.text(!U'x != 2'), using='gist', name='t_excl_x')"
            ")"
        )
Beispiel #14
0
    def test_inline_exclude_constraint_case_sensitive(self):
        from sqlalchemy.dialects.postgresql import ExcludeConstraint

        autogen_context = self.autogen_context

        m = MetaData()
        t = Table(
            "TTable", m, Column("XColumn", String), Column("YColumn", String)
        )
        ExcludeConstraint(
            (t.c.XColumn, ">"),
            using="gist",
            where='"XColumn" != 2',
            name="TExclX",
        )

        op_obj = ops.CreateTableOp.from_table(t)

        eq_ignore_whitespace(
            autogenerate.render_op_text(autogen_context, op_obj),
            "op.create_table('TTable',sa.Column('XColumn', sa.String(), "
            "nullable=True),"
            "sa.Column('YColumn', sa.String(), nullable=True),"
            "postgresql.ExcludeConstraint((sa.column('XColumn'), '>'), "
            "where=sa.text(!U'\"XColumn\" != 2'), using='gist', "
            "name='TExclX'))",
        )
Beispiel #15
0
    def test_add_exclude_constraint_case_sensitive(self):
        from sqlalchemy.dialects.postgresql import ExcludeConstraint

        autogen_context = self.autogen_context

        m = MetaData()
        t = Table(
            "TTAble", m, Column("XColumn", String), Column("YColumn", String)
        )

        op_obj = ops.AddConstraintOp.from_constraint(
            ExcludeConstraint(
                (t.c.XColumn, ">"),
                where=t.c.XColumn != 2,
                using="gist",
                name="t_excl_x",
            )
        )

        eq_ignore_whitespace(
            autogenerate.render_op_text(autogen_context, op_obj),
            "op.create_exclude_constraint('t_excl_x', 'TTAble', "
            "(sa.column('XColumn'), '>'), "
            "where=sa.text(!U'\"XColumn\" != 2'), using='gist')",
        )
 def test_inline_pk_constraint(self):
     t = Table('t', self.metadata, Column('c', Integer, primary_key=True))
     eq_ignore_whitespace(
         autogenerate.render._add_table(t, self.autogen_context),
         "op.create_table('t',sa.Column('c', sa.Integer(), nullable=False),"
         "sa.PrimaryKeyConstraint('c', name=op.f('pk_ct_t')))"
     )
    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.render._add_table(
            t, autogen_context
        )
        eq_ignore_whitespace(
            result,
            "sa.create_table('t',"
            "col(x),"
            "render:primary_key)"
        )
    def test_render_table_max_cols(self):
        m = MetaData()
        t = Table(
            'test', m,
            Column('a', Integer),
            Column('b', Integer),
            Column('c', Integer),
            Column('d', Integer),
        )
        eq_ignore_whitespace(
            autogenerate.render._add_table(t, self.autogen_context),
            "op.create_table('test',"
            "*[sa.Column('a', sa.Integer(), nullable=True),"
            "sa.Column('b', sa.Integer(), nullable=True),"
            "sa.Column('c', sa.Integer(), nullable=True),"
            "sa.Column('d', sa.Integer(), nullable=True)])"
        )

        t2 = Table(
            'test2', m,
            Column('a', Integer),
            Column('b', Integer),
            Column('c', Integer),
        )

        eq_ignore_whitespace(
            autogenerate.render._add_table(t2, self.autogen_context),
            "op.create_table('test2',"
            "sa.Column('a', sa.Integer(), nullable=True),"
            "sa.Column('b', sa.Integer(), nullable=True),"
            "sa.Column('c', sa.Integer(), nullable=True))"
        )
Beispiel #19
0
 def test_render_add_column(self):
     eq_ignore_whitespace(
         autogenerate.render._add_column(
             None, "foo", Column("x", Integer, server_default="5"),
             self.autogen_context),
         "op.add_column('foo', sa.Column('x', sa.Integer(), "
         "server_default='5', nullable=True))")
 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.render._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_add_fk_constraint_separate_colkeys(self):
        m = MetaData()
        Table('a', m, Column('id', Integer, key='aid', primary_key=True))
        b = Table('b', m, Column('a_id', Integer, key='baid'))
        fk = ForeignKeyConstraint(['baid'], ['a.aid'], name='fk_a_id')
        b.append_constraint(fk)

        py_code = autogenerate.render._add_table(b, self.autogen_context)

        eq_ignore_whitespace(
            py_code,
            "op.create_table('b',"
            "sa.Column('a_id', sa.Integer(), nullable=True),"
            "sa.ForeignKeyConstraint(['a_id'], ['a.id'], name='fk_a_id'))"
        )

        context = op_fixture()
        eval(py_code)
        context.assert_(
            "CREATE TABLE b (a_id INTEGER, CONSTRAINT "
            "fk_a_id FOREIGN KEY(a_id) REFERENCES a (id))")

        context = op_fixture()
        py_code = autogenerate.render._add_fk_constraint(
            fk, self.autogen_context)

        eq_ignore_whitespace(
            autogenerate.render._add_fk_constraint(fk, self.autogen_context),
            "op.create_foreign_key('fk_a_id', 'b', 'a', ['a_id'], ['id'])"
        )

        eval(py_code)
        context.assert_(
            "ALTER TABLE b ADD CONSTRAINT fk_a_id "
            "FOREIGN KEY(a_id) REFERENCES a (id)")
Beispiel #22
0
 def test_explicit_unique_constraint(self):
     t = Table('t', self.metadata, Column('c', Integer))
     eq_ignore_whitespace(
         autogenerate.render._render_unique_constraint(
             UniqueConstraint(t.c.c, deferrable='XYZ'),
             self.autogen_context),
         "sa.UniqueConstraint('c', deferrable='XYZ', "
         "name=op.f('uq_ct_t_c'))")
Beispiel #23
0
 def test_render_unique_constraint_opts(self):
     m = MetaData()
     t = Table('t', m, Column('c', Integer))
     eq_ignore_whitespace(
         autogenerate.render._render_unique_constraint(
             UniqueConstraint(t.c.c, name='uq_1', deferrable='XYZ'),
             self.autogen_context),
         "sa.UniqueConstraint('c', deferrable='XYZ', name='uq_1')")
 def test_inline_fk(self):
     t = Table('t', self.metadata, Column('c', Integer, ForeignKey('q.id')))
     eq_ignore_whitespace(
         autogenerate.render._add_table(t, self.autogen_context),
         "op.create_table('t',sa.Column('c', sa.Integer(), nullable=True),"
         "sa.ForeignKeyConstraint(['c'], ['q.id'], "
         "name=op.f('fk_ct_t_c_q')))"
     )
 def test_inline_ck_constraint(self):
     t = Table(
         't', self.metadata, Column('c', Integer), CheckConstraint("c > 5"))
     eq_ignore_whitespace(
         autogenerate.render._add_table(t, self.autogen_context),
         "op.create_table('t',sa.Column('c', sa.Integer(), nullable=True),"
         "sa.CheckConstraint(!U'c > 5', name=op.f('ck_ct_t')))"
     )
Beispiel #26
0
    def test_render_table_no_implicit_check(self):
        m = MetaData()
        t = Table('test', m, Column('x', Boolean()))

        eq_ignore_whitespace(
            autogenerate.render._add_table(t, self.autogen_context),
            "op.create_table('test',"
            "sa.Column('x', sa.Boolean(), nullable=True))")
 def test_render_check_constraint_literal(self):
     eq_ignore_whitespace(
         autogenerate.render._render_check_constraint(
             CheckConstraint("im a constraint", name='cc1'),
             self.autogen_context
         ),
         "sa.CheckConstraint(!U'im a constraint', name='cc1')"
     )
Beispiel #28
0
 def test_render_col_autoinc_false_mysql(self):
     c = Column('some_key', Integer, primary_key=True, autoincrement=False)
     Table('some_table', MetaData(), c)
     result = autogenerate.render._render_column(c, self.autogen_context)
     eq_ignore_whitespace(
         result, 'sa.Column(\'some_key\', sa.Integer(), '
         'autoincrement=False, '
         'nullable=False)')
    def test_render_drop_column_w_schema(self):
        eq_ignore_whitespace(
            autogenerate.render._drop_column(
                "foo", "bar", Column("x", Integer, server_default="5"),
                self.autogen_context),

            "op.drop_column('bar', 'x', schema='foo')"
        )
    def test_render_drop_column(self):
        eq_ignore_whitespace(
            autogenerate.render._drop_column(
                None, "foo", Column("x", Integer, server_default="5"),
                self.autogen_context),

            "op.drop_column('foo', 'x')"
        )
 def test_render_add_column_w_schema(self):
     eq_ignore_whitespace(
         autogenerate.render._add_column(
             "foo", "bar", Column("x", Integer, server_default="5"),
             self.autogen_context),
         "op.add_column('bar', sa.Column('x', sa.Integer(), "
         "server_default='5', nullable=True), schema='foo')"
     )
 def test_render_add_column(self):
     eq_ignore_whitespace(
         autogenerate.render._add_column(
             None, "foo", Column("x", Integer, server_default="5"),
             self.autogen_context),
         "op.add_column('foo', sa.Column('x', sa.Integer(), "
         "server_default='5', nullable=True))"
     )
 def test_schema_type_boolean(self):
     t = Table('t', self.metadata, Column('c', Boolean(name='xyz')))
     eq_ignore_whitespace(
         autogenerate.render._add_column(
             None, "t", t.c.c,
             self.autogen_context),
         "op.add_column('t', "
         "sa.Column('c', sa.Boolean(name='xyz'), nullable=True))"
     )
 def test_explicit_named_unique_constraint(self):
     t = Table('t', self.metadata, Column('c', Integer))
     eq_ignore_whitespace(
         autogenerate.render._render_unique_constraint(
             UniqueConstraint(t.c.c, name='q'),
             self.autogen_context
         ),
         "sa.UniqueConstraint('c', name='q')"
     )
 def test_render_modify_type(self):
     eq_ignore_whitespace(
         autogenerate.render._modify_col(
             "sometable", "somecolumn",
             self.autogen_context,
             type_=CHAR(10), existing_type=CHAR(20)),
         "op.alter_column('sometable', 'somecolumn', "
         "existing_type=sa.CHAR(length=20), type_=sa.CHAR(length=10))"
     )
 def test_implicit_unique_constraint(self):
     t = Table('t', self.metadata, Column('c', Integer, unique=True))
     uq = [c for c in t.constraints if isinstance(c, UniqueConstraint)][0]
     eq_ignore_whitespace(
         autogenerate.render._render_unique_constraint(uq,
                                                       self.autogen_context
                                                       ),
         "sa.UniqueConstraint('c', name=op.f('uq_ct_t_c'))"
     )
 def test_render_check_constraint_literal_binds(self):
     c = column('c')
     eq_ignore_whitespace(
         autogenerate.render._render_check_constraint(
             CheckConstraint(and_(c > 5, c < 10)),
             self.autogen_context
         ),
         "sa.CheckConstraint(!U'c > 5 AND c < 10')"
     )
    def test_render_table_no_implicit_check(self):
        m = MetaData()
        t = Table('test', m, Column('x', Boolean()))

        eq_ignore_whitespace(
            autogenerate.render._add_table(t, self.autogen_context),
            "op.create_table('test',"
            "sa.Column('x', sa.Boolean(), nullable=True))"
        )
Beispiel #39
0
    def test_render_add_column_w_on_conflict(self):
        c = Column("int_value", Integer, sqlite_on_conflict_not_null="FAIL")

        result = autogenerate.render._render_column(c, self.autogen_context)
        eq_ignore_whitespace(
            result,
            "sa.Column('int_value', sa.Integer(), "
            "nullable=True, sqlite_on_conflict_not_null='FAIL')",
        )
Beispiel #40
0
    def test_render_server_default_const(self):
        c = Column("int_value", Integer, server_default="5")

        result = autogenerate.render._render_column(c, self.autogen_context)
        eq_ignore_whitespace(
            result,
            "sa.Column('int_value', sa.Integer(), server_default='5', "
            "nullable=True)",
        )
Beispiel #41
0
 def test_jsonb_type(self):
     if config.requirements.sqlalchemy_110.enabled:
         eq_ignore_whitespace(
             autogenerate.render._repr_type(JSONB(), self.autogen_context),
             "postgresql.JSONB(astext_type=sa.Text())")
     else:
         eq_ignore_whitespace(
             autogenerate.render._repr_type(JSONB(), self.autogen_context),
             "postgresql.JSONB()")
 def test_explicit_unique_constraint(self):
     t = Table('t', self.metadata, Column('c', Integer))
     eq_ignore_whitespace(
         autogenerate.render._render_unique_constraint(
             UniqueConstraint(t.c.c, deferrable='XYZ'),
             self.autogen_context
         ),
         "sa.UniqueConstraint('c', deferrable='XYZ', "
         "name=op.f('uq_ct_t_c'))"
     )
 def test_drop_fk_constraint(self):
     m = MetaData()
     Table('a', m, Column('id', Integer, primary_key=True))
     b = Table('b', m, Column('a_id', Integer, ForeignKey('a.id')))
     fk = ForeignKeyConstraint(['a_id'], ['a.id'], name='fk_a_id')
     b.append_constraint(fk)
     eq_ignore_whitespace(
         autogenerate.render._drop_constraint(fk, self.autogen_context),
         "op.drop_constraint('fk_a_id', 'b', type_='foreignkey')"
     )
 def test_render_modify_nullable_w_schema(self):
     eq_ignore_whitespace(
         autogenerate.render._modify_col(
             "sometable", "somecolumn",
             self.autogen_context,
             existing_type=Integer(),
             nullable=True, schema='foo'),
         "op.alter_column('sometable', 'somecolumn', "
         "existing_type=sa.Integer(), nullable=True, schema='foo')"
     )
 def test_render_unique_constraint_opts(self):
     m = MetaData()
     t = Table('t', m, Column('c', Integer))
     eq_ignore_whitespace(
         autogenerate.render._render_unique_constraint(
             UniqueConstraint(t.c.c, name='uq_1', deferrable='XYZ'),
             self.autogen_context
         ),
         "sa.UniqueConstraint('c', deferrable='XYZ', name='uq_1')"
     )
 def test_render_table_w_unicode_name(self):
     m = MetaData()
     t = Table(compat.ue('\u0411\u0435\u0437'), m,
               Column('id', Integer, primary_key=True),
               )
     eq_ignore_whitespace(
         autogenerate.render._add_table(t, self.autogen_context),
         "op.create_table(%r,"
         "sa.Column('id', sa.Integer(), nullable=False),"
         "sa.PrimaryKeyConstraint('id'))" % compat.ue('\u0411\u0435\u0437')
     )
 def test_render_check_constraint_sqlexpr(self):
     c = column('c')
     five = literal_column('5')
     ten = literal_column('10')
     eq_ignore_whitespace(
         autogenerate.render._render_check_constraint(
             CheckConstraint(and_(c > five, c < ten)),
             self.autogen_context
         ),
         "sa.CheckConstraint(!U'c > 5 AND c < 10')"
     )