Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top