Hi,

I am passing a custom creator to sqlalchemy.create_engine().
And, I want to simulate the following scenario in automated test without 
using actual database connection:

1. Request a connection from pool: connects to DB.
2. DB goes down.
3. Request a connection from pool: connection is invalid.
4. Request a connection from pool: connects to DB again.

I'm trying the following testcase:

        def mycreator():
                print('called')
                # return psycopg2.connect(....)
                return unittest.mock.MagicMock()

        engine = create_engine('postgresql://', creator=mycreator, 
pool_size=1)
        c = engine.connect()  # calls mycreator
        c.close()             # connection returned to pool
        c = engine.connect()  # does not call mycreator (reuses connection 
in pool)
        c.invalidate()        
        c.close()
        c = engine.connect()  # calls mycreator (because connection is 
invalidated)


But, it throws exception because MagicMock isn't cursor or connection.
What kind of mock or fake connection object should mycreator return in the 
test
so that above snippet will behave as I expect it to, calling mycreator 
twice?
(Using actual connection, psycopg2.connect(), works as expected).

Thanks.
Sam

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to