Hi All,
Sorry to top post but people have been and I missed the start of the
thread...
This sounds like the kind of thing that mortar_rdb helps with:
http://packages.python.org/mortar_rdb/use.html
See the testing section in the above docs...
cheers,
Chris
On 28/07/2011 17:20, Joe Dallago wrote:
I would have to agree with Carsten, that I think the problem lies with
the DBSession object. If you are simply creating your session with
sessionmaker(), it will return a new session each time. Try using
scoped_session()
<http://www.sqlalchemy.org/docs/orm/session.html#sqlalchemy.orm.scoped_session>
and see if that fixes your problems immediately. Here is another
example of a good way of doing handler/view testing
https://github.com/jayd3e/StudentUnderground/blob/master/studentunderground/tests/test_handlers.py.
In my example I completely abstract the process of setting up a database
into an initialize_db() function that takes an engine as an argument.
That way you tests and your actual application can use the same
machinery in order to setup an identical Session object. This is how a
lot of people around here do it, and it works out really nicely.
On Mon, Jul 25, 2011 at 5:17 AM, Carsten Senger <sen...@rehfisch.de
<mailto:sen...@rehfisch.de>> wrote:
--On Donnerstag, Juli 21, 2011 05:50:12 -0700 Whitiz
<schugsc...@gmail.com <mailto:schugsc...@gmail.com>> wrote:
import unittest
from pyramid.config import Configurator
from pyramid import testing
from tutorial.models import MyModel, DBSession, Base
import transaction
def _initTestingDB():
print "initTestingDB"
from sqlalchemy import create_engine
engine = create_engine('sqlite://')
DBSession.configure(bind=__engine)
Base.metadata.bind = engine
Base.metadata.drop_all(engine)
Base.metadata.create_all(__engine)
create_models(DBSession)
return DBSession
Here you return "DBSession", which may be a sessionmaker object.
def create_models(dbSession):
session = dbSession()
model = MyModel(name=u'root', value=55)
session.add(model)
session.flush()
transaction.commit()
There you create a new 'session' object from your DBSession
factory. But _initTestingDB() returns the DBSession object, not
this 'session' object.
def clear_models(dbSession):
session = dbSession()
old_object = session.query(MyModel)
session.delete(old_object)
session.flush()
transaction.commit()
print "clear_models"
The transaction is probably the transaction used by repoze.tm
<http://repoze.tm>[2]. Not sure
how that works in tests.
class TestMyView(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
self.dbSession=_initTestingDB(__)
def tearDown(self):
testing.tearDown()
clear_models(self.dbSession)
print "rollback"
self.dbSession.rollback()
As said self.dbSession is not the session you used in create_models().
Sqlalchemy has a neat receipt how to set up an extra session to roll
back to
in tests:
<http://www.sqlalchemy.org/__docs/orm/session.html#joining-__a-session-into-an-external-__transaction
<http://www.sqlalchemy.org/docs/orm/session.html#joining-a-session-into-an-external-transaction>>
In a project where I implemented that pattern I had to replace the
scoped session with
a normal session to make it work:
<https://bitbucket.org/liqd/__adhocracy/src/93d614ae2fb5/__adhocracy/tests/__init__.py
<https://bitbucket.org/liqd/adhocracy/src/93d614ae2fb5/adhocracy/tests/__init__.py>>
..Carsten
--
You received this message because you are subscribed to the Google
Groups "pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com
<mailto:pylons-devel@googlegroups.com>.
To unsubscribe from this group, send email to
pylons-devel+unsubscribe@__googlegroups.com
<mailto:pylons-devel%2bunsubscr...@googlegroups.com>.
For more options, visit this group at
http://groups.google.com/__group/pylons-devel?hl=en
<http://groups.google.com/group/pylons-devel?hl=en>.
--
You received this message because you are subscribed to the Google
Groups "pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/pylons-devel?hl=en.
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
You received this message because you are subscribed to the Google Groups
"pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/pylons-devel?hl=en.