【发布时间】:2022-01-26 14:20:35
【问题描述】:
我为 CRUD 应用程序编写测试,我想确保在永久或暂时丢失与数据库的连接时,它的行为在不同情况下是正确的。
现在我在会话中间使用 OperationalError side_effect 修补 session.execute。有没有更好或更真实的连接中断模拟?
我想要达到的效果是这样的:
def test_interrupted_connection(self, client):
query = text("SELECT 1")
with sessionmaker.begin() as s:
# Successful execute
s.execute(query)
# Failed execute
with pytest.raises(OperationalError):
# I guess here should be some monkeypatching or mocking
# which will make SQLAlchemy think that connection
# is lost during .execute() call below
# But I don't know how it should look like.
s.execute(query).
【问题讨论】:
-
什么不起作用?
-
我需要什么或如何模拟“with pytest.raises(...)”和 s.execute(query) 之间的断开连接
-
展示你的尝试。
-
也许可以看看 SQLAlchmey 的test suite 中的测试场景
标签: python unit-testing sqlalchemy pytest