def test_no_embed_in_sql(self): """Using a DefaultGenerator, Sequence, DefaultClause in the columns, where clause of a select, or in the values clause of insert, update, raises an informative error""" for const in ( sa.Sequence('y'), sa.ColumnDefault('y'), sa.DefaultClause('y') ): assert_raises_message( sa.exc.ArgumentError, "SQL expression object or string expected, got object of type " "<.* 'list'> instead", t.select, [const] ) assert_raises_message( sa.exc.InvalidRequestError, "cannot be used directly as a column expression.", str, t.insert().values(col4=const) ) assert_raises_message( sa.exc.InvalidRequestError, "cannot be used directly as a column expression.", str, t.update().values(col4=const) )
def test_no_embed_in_sql(self): """Using a DefaultGenerator, Sequence, DefaultClause in the columns, where clause of a select, or in the values clause of insert, update, raises an informative error""" t = Table("some_table", MetaData(), Column("id", Integer)) for const in ( sa.Sequence("y"), sa.ColumnDefault("y"), sa.DefaultClause("y"), ): assert_raises_message( sa.exc.ArgumentError, r"SQL expression for WHERE/HAVING role expected, " r"got \[(?:Sequence|ColumnDefault|DefaultClause)\('y'.*\)\]", t.select, [const], ) assert_raises_message( sa.exc.ArgumentError, "SQL expression element expected, got %s" % const.__class__.__name__, t.insert().values, col4=const, ) assert_raises_message( sa.exc.ArgumentError, "SQL expression element expected, got %s" % const.__class__.__name__, t.update().values, col4=const, )
def test_arg_signature(self): def fn1(): pass def fn2(): pass def fn3(x=1): eq_(x, 1) def fn4(x=1, y=2, z=3): eq_(x, 1) fn5 = list class fn6a(object): def __init__(self, x): eq_(x, "context") class fn6b(object): def __init__(self, x, y=3): eq_(x, "context") class FN7(object): def __call__(self, x): eq_(x, "context") fn7 = FN7() class FN8(object): def __call__(self, x, y=3): eq_(x, "context") fn8 = FN8() for fn in fn1, fn2, fn3, fn4, fn5, fn6a, fn6b, fn7, fn8: c = sa.ColumnDefault(fn) c.arg("context")
def test_no_embed_in_sql(self): """Using a DefaultGenerator, Sequence, DefaultClause in the columns, where clause of a select, or in the values clause of insert, update, raises an informative error""" t = Table( "some_table", MetaData(), Column("id", Integer), Column("col4", String()), ) for const in ( sa.Sequence("y"), sa.ColumnDefault("y"), sa.DefaultClause("y"), ): assert_raises_message( sa.exc.ArgumentError, "SQL expression object expected, got object of type " "<.* 'list'> instead", t.select, [const], ) assert_raises_message( sa.exc.InvalidRequestError, "cannot be used directly as a column expression.", str, t.insert().values(col4=const), ) assert_raises_message( sa.exc.InvalidRequestError, "cannot be used directly as a column expression.", str, t.update().values(col4=const), )
def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; bind meta = sql.MetaData() meta.bind = migrate_engine token = sql.Table('token', meta, autoload=True) # creating the column immediately with nullable=False fails with # PostgreSQL (LP 1068181), so do it in two steps instead valid = sql.Column( 'valid', sql.Boolean(), sql.ColumnDefault(True), nullable=True) valid.create(token, populate_default=True) valid.alter(type=sql.Boolean(), default=True, nullable=False)
def test_arg_signature(self): def fn1(): pass def fn2(): pass def fn3(x=1): pass def fn4(x=1, y=2, z=3): pass fn5 = list class fn6(object): def __init__(self, x): pass class fn6(object): def __init__(self, x, y=3): pass class FN7(object): def __call__(self, x): pass fn7 = FN7() class FN8(object): def __call__(self, x, y=3): pass fn8 = FN8() for fn in fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8: c = sa.ColumnDefault(fn)
# dynamically create sa classes d = dict() # create users # ndx = (sa.Index('idx_usr_name', 'name'), sa.Index('idx_usr_fullname', 'fullname'),{'schema':'abc'},) l = ['name', 'password'] ndx = [sa.Index('idx_usr_name', *l), sa.Index('idx_usr_fullname', 'fullname'), ] ndx = tuple(ndx) def defval(t): return 10 x = sa.Column(sa.Numeric(10,3)) x.default = sa.ColumnDefault(defval) id = sa.Column(sa.Integer, primary_key=True) id.__dqf__ = 'id' t = {'__tablename__': "users", 'id': id, 'name': sa.Column(sa.String), 'fullname': sa.Column(sa.String), 'password': sa.Column(sa.String), 'cost': x, '__table_args__': ndx, '__dqt__': 'usr', } d['usr'] = type('User',(Base,),t)