本文整理汇总了Python中sqlalchemy.test.testing.assert_raises函数的典型用法代码示例。如果您正苦于以下问题:Python assert_raises函数的具体用法?Python assert_raises怎么用?Python assert_raises使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了assert_raises函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_notcallable
def test_notcallable(self):
class Point(object):
pass
table = self.point_map(Point)
alias = aliased(Point)
assert_raises(TypeError, alias)
示例2: test_basic
def test_basic(self):
carrier = util.ExtensionCarrier()
assert 'translate_row' not in carrier
assert carrier.translate_row() is interfaces.EXT_CONTINUE
assert 'translate_row' not in carrier
assert_raises(AttributeError, lambda: carrier.snickysnack)
class Partial(object):
def __init__(self, marker):
self.marker = marker
def translate_row(self, row):
return self.marker
carrier.append(Partial('end'))
assert 'translate_row' in carrier
assert carrier.translate_row(None) == 'end'
carrier.push(Partial('front'))
assert carrier.translate_row(None) == 'front'
assert 'populate_instance' not in carrier
carrier.append(interfaces.MapperExtension)
# Py3K
#assert 'populate_instance' not in carrier
# Py2K
assert 'populate_instance' in carrier
# end Py2K
assert carrier.interface
for m in carrier.interface:
assert getattr(interfaces.MapperExtension, m)
示例3: test_deleted_flag
def test_deleted_flag(self):
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)
示例4: test_basic
def test_basic(self):
carrier = util.ExtensionCarrier()
assert "translate_row" not in carrier
assert carrier.translate_row() is interfaces.EXT_CONTINUE
assert "translate_row" not in carrier
assert_raises(AttributeError, lambda: carrier.snickysnack)
class Partial(object):
def __init__(self, marker):
self.marker = marker
def translate_row(self, row):
return self.marker
carrier.append(Partial("end"))
assert "translate_row" in carrier
assert carrier.translate_row(None) == "end"
carrier.push(Partial("front"))
assert carrier.translate_row(None) == "front"
assert "populate_instance" not in carrier
carrier.append(interfaces.MapperExtension)
assert "populate_instance" in carrier
assert carrier.interface
for m in carrier.interface:
assert getattr(interfaces.MapperExtension, m)
示例5: test_nonreflected_fk_raises
def test_nonreflected_fk_raises(self):
"""test that a NoReferencedColumnError is raised when reflecting
a table with an FK to another table which has not included the target
column in its reflection.
"""
meta = MetaData(testing.db)
a1 = Table('a', meta,
Column('x', sa.Integer, primary_key=True),
Column('z', sa.Integer),
test_needs_fk=True
)
b1 = Table('b', meta,
Column('y', sa.Integer, sa.ForeignKey('a.x')),
test_needs_fk=True
)
meta.create_all()
try:
m2 = MetaData(testing.db)
a2 = Table('a', m2, include_columns=['z'], autoload=True)
b2 = Table('b', m2, autoload=True)
assert_raises(tsa.exc.NoReferencedColumnError, a2.join, b2)
finally:
meta.drop_all()
示例6: test_single_parent_raise
def test_single_parent_raise(self):
sess = create_session()
y = T2(data='T2a')
x = T1(data='T1a', t2=y)
assert_raises(sa_exc.InvalidRequestError, T1, data='T1b', t2=y)
示例7: test_empty_insert_pk2
def test_empty_insert_pk2(self):
assert_raises(
exc.DBAPIError,
self._test_empty_insert,
Table('b', MetaData(testing.db),
Column('x', Integer, primary_key=True),
Column('y', Integer, primary_key=True)))
示例8: test_inheriting
def test_inheriting(self):
mapper(A, a_table, properties={
'some_dest': relationship(Dest,back_populates='many_a')
})
mapper(B, b_table, inherits=A, concrete=True,
properties={
'some_dest': relationship(Dest, back_populates='many_b')
})
mapper(Dest, dest_table, properties={
'many_a': relationship(A,back_populates='some_dest'),
'many_b': relationship(B,back_populates='some_dest')
})
sess = sessionmaker()()
dest1 = Dest(name='c1')
dest2 = Dest(name='c2')
a1 = A(some_dest=dest1, aname='a1')
a2 = A(some_dest=dest2, aname='a2')
b1 = B(some_dest=dest1, bname='b1')
b2 = B(some_dest=dest1, bname='b2')
assert_raises(AttributeError, setattr, b1, 'aname', 'foo')
assert_raises(AttributeError, getattr, A, 'bname')
assert dest2.many_a == [a2]
assert dest1.many_a == [a1]
assert dest1.many_b == [b1, b2]
sess.add_all([dest1, dest2])
sess.commit()
assert sess.query(Dest).filter(Dest.many_a.contains(a2)).one() is dest2
assert dest2.many_a == [a2]
assert dest1.many_a == [a1]
assert dest1.many_b == [b1, b2]
assert sess.query(B).filter(B.bname == 'b1').one() is b1
示例9: test_noninherited_warning
def test_noninherited_warning(self):
mapper(A, a_table, properties={
'some_c':relation(C)
})
mapper(B, b_table,inherits=A, concrete=True)
mapper(C, c_table)
b = B()
c = C()
assert_raises(AttributeError, setattr, b, 'some_c', c)
clear_mappers()
mapper(A, a_table, properties={
'a_id':a_table.c.id
})
mapper(B, b_table,inherits=A, concrete=True)
mapper(C, c_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(C, c_table)
示例10: 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):
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
示例11: test_child_row_switch_two
def test_child_row_switch_two(self):
Session = sessionmaker()
sess1 = Session()
sess1.add(P(id=1, data='P version 1'))
sess1.commit()
sess1.close()
p1 = sess1.query(P).first()
sess2 = Session()
p2 = sess2.query(P).first()
sess1.delete(p1)
sess1.commit()
sess1.add(P(id='P1', data='P version 2'))
sess1.commit()
p2.data = 'P overwritten by concurrent tx'
assert_raises(
orm.exc.ConcurrentModificationError,
sess2.commit
)
示例12: 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
)
示例13: test_versioncheck
def test_versioncheck(self):
"""query.with_lockmode performs a 'version check' on an already loaded instance"""
s1 = create_session(autocommit=False)
mapper(Foo, version_table, version_id_col=version_table.c.version_id)
f1s1 = Foo(value='f1 value')
s1.add(f1s1)
s1.commit()
s2 = create_session(autocommit=False)
f1s2 = s2.query(Foo).get(f1s1.id)
f1s2.value='f1 new value'
s2.commit()
# load, version is wrong
assert_raises(
sa.orm.exc.ConcurrentModificationError,
s1.query(Foo).with_lockmode('read').get, f1s1.id
)
# reload it - this expires the old version first
s1.refresh(f1s1, lockmode='read')
# now assert version OK
s1.query(Foo).with_lockmode('read').get(f1s1.id)
# assert brand new load is OK too
s1.close()
s1.query(Foo).with_lockmode('read').get(f1s1.id)
示例14: test_default_constructor_state_not_shared
def test_default_constructor_state_not_shared(self):
scope = scoped_session(sa.orm.sessionmaker())
class A(object):
pass
class B(object):
def __init__(self):
pass
scope.mapper(A, table1)
scope.mapper(B, table2)
A(foo='bar')
assert_raises(TypeError, B, foo='bar')
scope = scoped_session(sa.orm.sessionmaker())
class C(object):
def __init__(self):
pass
class D(object):
pass
scope.mapper(C, table1)
scope.mapper(D, table2)
assert_raises(TypeError, C, foo='bar')
D(foo='bar')
示例15: 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")