Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
What is the best way to simulate disconnection within SQLAlchemy?

I write tests for CRUD application and I want to be sure that its behavior is correct in different scenarios when connection to database is lost permanently or temporarily.

For now, I patch session.execute in the middle of the session with OperationalError side_effect. Is there a better or more realistic simulation of connection interruption?

What I want to achieve looks like this:

 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).
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can invalidate the SQL connection using

 #db = create_engine('auth_url') #conn = db.connect() conn.invalidate() db.dispose(

But if you want to know how your application will behave in case of database failure, the best way to kill all your database connections. This usually happens when you scale your rds (there is a little downtime) and you will get No active database found error and then you can check your application behavior.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!