On Aug 12, 2014, at 7:26 AM, alchemy1 <veerukrish...@hotmail.com> wrote:

> I've tried to simplify the code but it still isn't working:
> 
> 
> from sqlalchemy.orm import scoped_session, sessionmaker
> from zope.sqlalchemy import ZopeTransactionExtension
> DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
> engine = create_engine('postgresql+psycopg2://......')
> 
> class DatabaseTest:
> 
>     def setup_method(self, method):
>         self.config = testing.setUp()
>         self.connection = engine.connect()
>         self.trans = self.connection.begin()


this line here:

>         self.session = DBSession(bind=self.connection)


creates a Session() instance, which is bound to your Connection.  OK, great.   
DBSession is a scoped_session, so subsequent access to this object will give 
you that same Session object.  It's a registry, the session will stay there 
until remove() is called.  

then in this line:

> 
> def my_view(request):
>     one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()
>     return {'one': one}
> 

a view somewhere is calling into DBSession and there's no bind.  But, where is 
this?  This view is in your controllers modules somewhere?   Where is it 
getting DBSession from?   Your test module?   "DBSession" is just a plain 
Python object like any other - if the one that you're setting up is in your 
test module, then you'd have to patch it in so that your view sees the same 
object.

What I do is, in the test fixture, locate *the actual DBSession*, and inject 
the connection into it.   Making another scoped_session somewhere else isn't 
very useful if your actual code isn't using it.

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to