On May 20, 5:47 pm, Matt Wilson <[email protected]> wrote:
> I like the idea of using a truncate statement with all my tables to
> wipe out all the rows and then reloading them. I don't know how to
> use a rollback with controller tests because cherrypy (or TG) seems to
> issue a commit at the end of each page rendering.
>
> Can you post some of the code you're using to do this truncate and
> reload?
It simply goes like this:
> class DBReloadingTest(unittest.TestCase):
> def setUp(self, *args, **kw):
> super(DBReloadingTest, self).setUp(*args, **kw)
> session.expunge_all()
> adm.truncate_all()
> run_fixture()
where 'adm' is a class that connects with different permissions and
executes:
> def truncate_all(self):
> self.engine.execute("TRUNCATE TABLE %s" % ','.join(self.tablenames()))
>
> def tablenames(self, exclude=[]):
> retrievesql = "SELECT tablename FROM pg_tables WHERE
> schemaname='public'"
> return [ name for (name,) in self.engine.execute(retrievesql) if name
> not in exclude ]
The above is needed for the controller tests, because as you said the
TG 1.1 machinery likes to handle transactions implicitly.
For the model tests, the code below is way, WAY faster and runs 25
tests per second on my laptop.
Keep in mind that all queries (esp. inserts and updates) should go
through the session transaction, so if you have plaintext or
explicitly constructed queries, as opposed to ORM-level ones, you
should execute them via session.bind(User).execute(...) - where User
can be any mapped class that maps a table in the same db.
> class DBTestCase(unittest.TestCase):
> def setUp(self, *args, **kw):
> super(DBTestCase, self).setUp(*args, **kw)
> session.begin()
> session.expunge_all()
>
> def tearDown(self, *args, **kw):
> super(DBTestCase, self).tearDown(*args, **kw)
> session.rollback()
replace session.expunge_all() with session.clear() if using SA 0.4
I need to have a deeper look at the issue, because Postgres + SA 0.5
should support session.begin_nested() with SAVEPOINTs and there's no
reason we can't use them for functional tests.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---