On 12/20/2015 07:16 PM, Gerald Thibault wrote:
> I've been struggling with this for a few days now, and I've tried a
> whole slew of different approaches, but I'm just missing something.
> 
> I started with the example
> at 
> http://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#joining-a-session-into-an-external-transaction-such-as-for-test-suites
> and have been trying variations on it to suit my goals.
> 
> We have a webapp which, in simplest form, could be represented as this:
> 
> # in the db setup file
> 
> dsn = 'mysql://...t'
> engine = create_engine(dsn)
> session_factory = sessionmaker(bind=engine)
> scoped = scoped_session(session_factory)
> 
> # the app
> 
> class Webapp(object):
> 
>     def dispatch(self, name):
>         view = getattr(self, name)
>         session = scoped()
>         try:
>             rsp = view()
>             session.commit()
>             return rsp
>         except:
>             session.rollback()
>             return 'error'
>         finally:
>             session.close()
>             pass
> 
> # the views files
> 
>     def add_user(self, session):
>         user = User(username='test')
>         session = scoped()
>         session.add(user)
>         session.flush()
>         return 'user %s' % user.id
> 
> The app uses commit, rollback, and close on a scoped session.
> 
> I'd like to know how to adjust the example at the above URL so the test
> is able to "enclose" the webapp, so the calls to sessionmaker, and all
> operation that occur within the webapp, are within the scope of the test
> transaction, so everything that happens in the app can be rolled back at
> the end of the test. 

When I've done this I use WebTest: http://webtest.readthedocs.org/en/latest/

although Pyramid has some of its own similar techniques within the
pyramid.testing package.    I have an example of it used within the
"test inside a transaction" technique from my Pycon 2014 talk:

https://bitbucket.org/zzzeek/pycon2014_atmcraft/src/f50cbe745a197ea7db83569283b703c418481222/atmcraft/tests/test_views_transactional.py?at=master&fileviewer=file-view-default

https://bitbucket.org/zzzeek/pycon2014_atmcraft/src/f50cbe745a197ea7db83569283b703c418481222/atmcraft/tests/__init__.py?at=master&fileviewer=file-view-default#__init__.py-20

you should be able to clone this repo and run the whole thing.






The example at the url acquires a connection, and
> binds the session to the connection, and I'm not sure how to force the
> sessionmaker to work with that. Could 'scopefunc' be used to somehow
> force the returned session to be within the context of the transaction?
> 
> There is also fixture loading code, which starts by acquiring a session
> from the scoped_session, adding fixtures, then committing. A simple
> example could be this function:
> 
> def load_fixture(username):
>     session = scoped()
>     session.add(User(username=username))
>     session.commit()
> 
> I'd like to use this in unittests, and have it rolled back along with
> the transaction.
> 
> I attached a full (nonworking) example, can you tell me what I am doing
> wrong? Is this even possible with a scope session, or is this sort of
> testing limited to to sessions bound to connections? Would I need to
> rewrite the session acquisition method to return a globally stored
> connection-bound session before defaulting to the scoped (engine-bound)
> session?
> 
> An example I saw
> at 
> https://web.archive.org/web/20140419235219/http://sontek.net/blog/detail/writing-tests-for-pyramid-and-sqlalchemy
> seems to indicate that person overwrote the app session from within the
> unittest, to ensure the app used that session. Is that the approach I
> would need to take?
> 
> -- 
> 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]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at https://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 [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