views:

641

answers:

2

I want to use a SQLite in memory (":memory:") DB for the tests in my webapp. I'm using nosetests for the tests, and webpy as framework.

I want to populate the DB in the setup() function, and then run all my tests. My problem is that webpy closes all the open DB connections after each request, and the SQLite :memory: DB only lasts until you close the connection, so only the first test is actually run correctly and all the others fail.

My choices are either to run the tests on a disk backed DB, or to recreate the entire DB in memory at the beginning of each individual test.

Do you know how can I prevent webpy from closing DB connections after each request? Can you think of any other way to get an in memory SQLite DB that lasts for more than one request using webpy?

+2  A: 

Maybe you could run the tests on a DB stored on the disk, but using a RAM disk. In Windows, you can install a driver to set up a RAM disk (some instructions here). In Linux, I believe you want to set up tmpfs.

A ram disk will act exactly like a hard disk, but will operate completely from memory, so that you will lose some of the overhead of loading files to/from the hard disk.

Mark Rushakoff
That's a good suggestion, but it'll complicate the set up for a random tester. In my case it's not worth it.
Joaquin Cuenca Abela
+1  A: 

Untested:

class NoCloseDB(web.db.SqliteDB):
  def _unload_context(self):
    pass # this keeps the _ctx.db attribute alive
web.db.register_database('sqlite',NoCloseDB) # overrides the previous registration

Notice that this can only work if you run web.py in a way that uses only one operating system process. If a request is dispatched across multiple processes, each one will still get its own database.

Martin v. Löwis
It doesn't work, _unload_context is not explicitly call at the end of the request, only after an explicit commit or rollback. Digging a bit more, it turns out the entire _ctx object (along with any ThreadedDict attached to this thread) is actually deleted in web.application._cleanup. I'm thinking now that maybe the best way to solve this is to install DBUtils and to connect using a pool of 1 connection.
Joaquin Cuenca Abela