DBSession is like this (not even sure if I need to do it this way, just 
following the examples)
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension
()))


If change to bind=connection

        self.session = DBSession(bind=connection)


then I get an error "sqlalchemy.exc.InvalidRequestError: Scoped session is 
already present; no new arguments may be specified." when a second test 
method is added to the test class.

Instead of connection.begin_nested should I be using session.begin_nested?


On Monday, August 11, 2014 10:49:18 PM UTC+2, Michael Bayer wrote:
>
>  
> On 08/11/2014 04:37 PM, alchemy1 wrote:
>  
> I have combined several examples I've found to try to get the 
> 'transactional-style' of unit tests to work, where you roll back the 
> database after each test. However when I run this, the test fails when 
> trying to insert the object with DBSession.add, complaining that the tables 
> don't exist. 
>
>
> are you setting up the Connection in DBSession?   I see you doing 
> something with "self.session", but that is not the same session as 
> "DBSession".   If DBSession is a scoped session you'd want to say 
> DBSession(bind=connection) for each test.
>
>
>  I thought Base.metadata.create_all(connection) would create the tables? 
> I'd like to create the tables within the setup_module and roll it back in 
> teardown_module so that the testdb database always goes back to being 
> empty. This is to ensure that the tests are always running against a known 
> state. (Start with empty db, create the tables, do the tests, then empty 
> the db)
>
> Also, since a lot of copy-pasting was involved in creating this, could you 
> please take a look and see what isn't necessary? I'm just trying to do 
> simple tests (in Pyramid). For example is self.session = Session(
> connection) required? And is using the global variables the way I am a 
> good way of doing it? Quite new to this so just trying to learn the best 
> practices.
>
>
>  from sqlalchemy.engine import create_engine
> from sqlalchemy.orm.session import Session
>
> from pyramid import testing
>
> from .models import Base
> from .models import DBSession
>
> transaction = None
> connection = None
> engine = None
>
>
> def setup_module():
>     global transaction, connection, engine
>
>     engine = create_engine(
> 'postgresql+psycopg2://username:password@host:5432/testdb')
>     DBSession.configure(bind=engine)
>     connection = engine.connect()
>     transaction = connection.begin()
>     Base.metadata.create_all(connection)
>
>
> def teardown_module():
>     global transaction, connection, engine
>     transaction.rollback()
>     connection.close()
>     engine.dispose()
>
>
> class DatabaseTest(object):
>
>     def setup(self):
>         self.__transaction = connection.begin_nested()
>         self.session = Session(connection)
>
>     def teardown(self):
>         self.session.close()
>         self.__transaction.rollback()
>
>
> class TestCustom(DatabaseTest):
>
>     def test_passing_view(self):
>         from .models import MyModel
>         model = MyModel(name='one', value=55)
>         DBSession.add(model)
>
>         from .views import my_view
>         request = testing.DummyRequest()
>         info = my_view(request)
>         assert info['one'].name == 'one'
>  
>
>
>
>
>  -- 
> 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+...@googlegroups.com <javascript:>.
> To post to this group, send email to sqlal...@googlegroups.com 
> <javascript:>.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>
>
>  

-- 
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