I write tests for CRUD application and i want to be sure that its behaviour is correct during different scenarios when connection to DB permanently or temporarily lost.
For now I patch session.execute in the middle of session with OperationalError side_effect. Is there better or\and more realistic mocking of connection interruption?
What I want to achive looks like that:
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).