本文整理汇总了Python中test.lib.testing.assert_raises函数的典型用法代码示例。如果您正苦于以下问题:Python assert_raises函数的具体用法?Python assert_raises怎么用?Python assert_raises使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了assert_raises函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_versioncheck_for_update
def test_versioncheck_for_update(self):
"""query.with_lockmode performs a 'version check' on an already loaded instance"""
Foo = self.classes.Foo
s1 = self._fixture()
f1s1 = Foo(value='f1 value')
s1.add(f1s1)
s1.commit()
s2 = create_session(autocommit=False)
f1s2 = s2.query(Foo).get(f1s1.id)
s2.refresh(f1s2, lockmode='update')
f1s2.value='f1 new value'
assert_raises(
exc.DBAPIError,
s1.refresh, f1s1, lockmode='update_nowait'
)
s1.rollback()
s2.commit()
s1.refresh(f1s1, lockmode='update_nowait')
assert f1s1.version_id == f1s2.version_id
示例2: test_literal_interpretation
def test_literal_interpretation(self):
t = table('test', column('col1'))
assert_raises(exc.ArgumentError, case, [("x", "y")])
self.assert_compile(case([("x", "y")], value=t.c.col1), "CASE test.col1 WHEN :param_1 THEN :param_2 END")
self.assert_compile(case([(t.c.col1==7, "y")], else_="z"), "CASE WHEN (test.col1 = :col1_1) THEN :param_1 ELSE :param_2 END")
示例3: test_pk_violation_with_savepoint
def test_pk_violation_with_savepoint(self):
User, Address = self.classes.User, self.classes.Address
s = self.session()
a1 = Address(email_address='foo')
u1 = User(id=1, name='ed', addresses=[a1])
s.add(u1)
s.commit()
a2 = Address(email_address='bar')
u2 = User(id=1, name='jack', addresses=[a2])
u1.name = 'edward'
a1.email_address = 'foober'
s.begin_nested()
s.add(u2)
assert_raises(sa_exc.FlushError, s.commit)
assert_raises(sa_exc.InvalidRequestError, s.commit)
s.rollback()
assert u2 not in s
assert a2 not in s
assert u1 in s
assert a1 in s
s.commit()
eq_(
s.query(User).all(),
[User(id=1, name='edward',
addresses=[Address(email_address='foober')])]
)
示例4: test_accounting_commit_fails_delete
def test_accounting_commit_fails_delete(self):
User = self.classes.User
sess = create_session(autocommit=True)
fail = False
def fail_fn(*arg, **kw):
if fail:
raise Exception("commit fails")
event.listen(sess, "after_flush_postexec", fail_fn)
u1 = User(name='ed')
sess.add(u1)
sess.flush()
sess.delete(u1)
fail = True
assert_raises(
Exception,
sess.flush
)
fail = False
assert u1 in sess
assert u1 not in sess.deleted
sess.delete(u1)
sess.flush()
assert u1 not in sess
eq_(
sess.query(User.name).order_by(User.name).all(),
[]
)
示例5: test_noninherited_warning
def test_noninherited_warning(self):
A, B, b_table, a_table, Dest, dest_table = (self.classes.A,
self.classes.B,
self.tables.b_table,
self.tables.a_table,
self.classes.Dest,
self.tables.dest_table)
mapper(A, a_table, properties={'some_dest': relationship(Dest)})
mapper(B, b_table, inherits=A, concrete=True)
mapper(Dest, dest_table)
b = B()
dest = Dest()
assert_raises(AttributeError, setattr, b, 'some_dest', dest)
clear_mappers()
mapper(A, a_table, properties={'a_id': a_table.c.id})
mapper(B, b_table, inherits=A, concrete=True)
mapper(Dest, dest_table)
b = B()
assert_raises(AttributeError, setattr, b, 'a_id', 3)
clear_mappers()
mapper(A, a_table, properties={'a_id': a_table.c.id})
mapper(B, b_table, inherits=A, concrete=True)
mapper(Dest, dest_table)
示例6: test_explode_in_initializer
def test_explode_in_initializer(self):
engine = engines.testing_engine()
def broken_initialize(connection):
connection.execute("select fake_stuff from _fake_table")
engine.dialect.initialize = broken_initialize
# raises a DBAPIError, not an AttributeError
assert_raises(exc.DBAPIError, engine.connect)
# dispose connections so we get a new one on
# next go
engine.dispose()
p1 = engine.pool
def is_disconnect(e, conn, cursor):
return True
engine.dialect.is_disconnect = is_disconnect
# invalidate() also doesn't screw up
assert_raises(exc.DBAPIError, engine.connect)
# pool was recreated
assert engine.pool is not p1
示例7: test_rollback_hook
def test_rollback_hook(self):
User, users = self.classes.User, self.tables.users
sess, canary = self._listener_fixture()
mapper(User, users)
u = User(name="u1", id=1)
sess.add(u)
sess.commit()
u2 = User(name="u1", id=1)
sess.add(u2)
assert_raises(sa.orm.exc.FlushError, sess.commit)
sess.rollback()
eq_(
canary,
[
"after_attach",
"before_commit",
"before_flush",
"after_begin",
"after_flush",
"after_flush_postexec",
"after_commit",
"after_attach",
"before_commit",
"before_flush",
"after_begin",
"after_rollback",
"after_soft_rollback",
"after_soft_rollback",
],
)
示例8: test_precolumns
def test_precolumns(self):
dialect = self.__dialect__
def gen(distinct=None, prefixes=None):
kw = {}
if distinct is not None:
kw['distinct'] = distinct
if prefixes is not None:
kw['prefixes'] = prefixes
return str(select(['q'], **kw).compile(dialect=dialect))
eq_(gen(None), 'SELECT q')
eq_(gen(True), 'SELECT DISTINCT q')
assert_raises(
exc.SADeprecationWarning,
gen, 'DISTINCT'
)
eq_(gen(prefixes=['ALL']), 'SELECT ALL q')
eq_(gen(prefixes=['DISTINCTROW']),
'SELECT DISTINCTROW q')
# Interaction with CUBRID prefix extensions
eq_(
gen(None, ['straight_join']),
'SELECT straight_join q')
eq_(
gen(False, ['HIGH_PRIORITY', 'SQL_SMALL_RESULT', 'ALL']),
'SELECT HIGH_PRIORITY SQL_SMALL_RESULT ALL q')
eq_(
gen(True, ['high_priority', sql.text('sql_cache')]),
'SELECT high_priority sql_cache DISTINCT q')
示例9: test_existing_plus_useexisting_raises
def test_existing_plus_useexisting_raises(self):
meta2 = self._useexisting_fixture()
assert_raises(
exc.ArgumentError,
Table, 'users', meta2, useexisting=True,
extend_existing=True
)
示例10: test_can_use_session_in_outer_rollback_hook
def test_can_use_session_in_outer_rollback_hook(self):
User, users = self.classes.User, self.tables.users
mapper(User, users)
sess = Session()
assertions = []
@event.listens_for(sess, "after_soft_rollback")
def do_something(session, previous_transaction):
if session.is_active:
assertions.append('name' not in u.__dict__)
assertions.append(u.name == 'u1')
u = User(name='u1', id=1)
sess.add(u)
sess.commit()
u2 = User(name='u1', id=1)
sess.add(u2)
assert_raises(
sa.orm.exc.FlushError,
sess.commit
)
sess.rollback()
eq_(assertions, [True, True])
示例11: test_clauseelement
def test_clauseelement(self):
metadata = MetaData()
table = Table('test_table', metadata,
Column('foo', Integer))
metadata.create_all(bind=testing.db)
try:
for elem in [
table.select,
lambda **kwargs: sa.func.current_timestamp(**kwargs).select(),
# func.current_timestamp().select,
lambda **kwargs:text("select * from test_table", **kwargs)
]:
for bind in (
testing.db,
testing.db.connect()
):
try:
e = elem(bind=bind)
assert e.bind is bind
e.execute().close()
finally:
if isinstance(bind, engine.Connection):
bind.close()
e = elem()
assert e.bind is None
assert_raises(
exc.UnboundExecutionError,
e.execute
)
finally:
if isinstance(bind, engine.Connection):
bind.close()
metadata.drop_all(bind=testing.db)
示例12: test_deleted_flag
def test_deleted_flag(self):
users, User = self.tables.users, self.classes.User
mapper(User, users)
sess = sessionmaker()()
u1 = User(name="u1")
sess.add(u1)
sess.commit()
sess.delete(u1)
sess.flush()
assert u1 not in sess
assert_raises(sa.exc.InvalidRequestError, sess.add, u1)
sess.rollback()
assert u1 in sess
sess.delete(u1)
sess.commit()
assert u1 not in sess
assert_raises(sa.exc.InvalidRequestError, sess.add, u1)
make_transient(u1)
sess.add(u1)
sess.commit()
eq_(sess.query(User).count(), 1)
示例13: test_rollback_recover
def test_rollback_recover(self):
users, User = self.tables.users, self.classes.User
mapper(User, users)
session = sessionmaker()()
u1, u2, u3= \
User(name='u1'),\
User(name='u2'),\
User(name='u3')
session.add_all([u1, u2, u3])
session.commit()
session.delete(u2)
u4 = User(name='u2')
session.add(u4)
session.flush()
u5 = User(name='u3')
session.add(u5)
assert_raises(orm_exc.FlushError, session.flush)
assert u5 not in session
assert u2 not in session.deleted
session.rollback()
示例14: test_too_long_idx_name
def test_too_long_idx_name(self):
dialect = testing.db.dialect.__class__()
for max_ident, max_index in [(22, None), (256, 22)]:
dialect.max_identifier_length = max_ident
dialect.max_index_name_length = max_index
for tname, cname, exp in [
('sometable', 'this_name_is_too_long', 'ix_sometable_t_09aa'),
('sometable', 'this_name_alsois_long', 'ix_sometable_t_3cf1'),
]:
t1 = Table(tname, MetaData(),
Column(cname, Integer, index=True),
)
ix1 = list(t1.indexes)[0]
self.assert_compile(
schema.CreateIndex(ix1),
"CREATE INDEX %s "
"ON %s (%s)" % (exp, tname, cname),
dialect=dialect
)
dialect.max_identifier_length = 22
dialect.max_index_name_length = None
t1 = Table('t', MetaData(), Column('c', Integer))
assert_raises(
exc.IdentifierError,
schema.CreateIndex(Index(
"this_other_name_is_too_long_for_what_were_doing",
t1.c.c)).compile,
dialect=dialect
)
示例15: test_mapped_managerattr
def test_mapped_managerattr(self):
t = Table('t', MetaData(),
Column('id', Integer, primary_key=True),
Column(instrumentation.ClassManager.MANAGER_ATTR, Integer))
class T(object): pass
assert_raises(KeyError, mapper, T, t)