Have you seen this?

koo.fi/blog/2015/10/22/flask-sqlalchemy-and-postgresql-unit-testing-with-transaction-savepoints/

He's using Flask-SQLAlchemy, but the underlying principles should be the
same. It's not quite the same since IIRC he uses nested savepoints for
everything rather than scoped session but it still might be useful.

I'm doing something similar although with Pytest, although I haven't yet
set it up so the nested savepoints enclose creating/dropping the tables...
I've been meaning to write a blogpost explaining it over the holidays, if I
get to it I'll send over a link.
ᐧ

On Sun, Dec 20, 2015 at 4:16 PM, Gerald Thibault <dieselmach...@gmail.com>
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. 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 sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>



-- 

*Jeff Widman*
jeffwidman.com <http://www.jeffwidman.com/> | 740-WIDMAN-J (943-6265)
<><

-- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to