From b60f54fedab835f214f3c88e990ff3bb098e6cad Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Wed, 25 Jul 2012 23:06:30 -0400 Subject: The final bit of refactoring puts the specifics of making a temporary database into the hands of the database modules, by using ZCA adapters. --- src/mailman/database/sqlite.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/mailman/database/sqlite.py') diff --git a/src/mailman/database/sqlite.py b/src/mailman/database/sqlite.py index 305fa006e..a9683457a 100644 --- a/src/mailman/database/sqlite.py +++ b/src/mailman/database/sqlite.py @@ -22,14 +22,20 @@ from __future__ import absolute_import, unicode_literals __metaclass__ = type __all__ = [ 'SQLiteDatabase', + 'make_temporary', ] import os +import types +import shutil +import tempfile +from functools import partial from urlparse import urlparse from mailman.database.base import StormBaseDatabase +from mailman.testing.helpers import configuration @@ -54,3 +60,25 @@ class SQLiteDatabase(StormBaseDatabase): # Ignore errors if fd > 0: os.close(fd) + + + +# Test suite adapter for ITemporaryDatabase. + +def _cleanup(self, tempdir): + shutil.rmtree(tempdir) + + +def make_temporary(database): + """Adapts by monkey patching an existing SQLite IDatabase.""" + tempdir = tempfile.mkdtemp() + url = 'sqlite:///' + os.path.join(tempdir, 'mailman.db') + with configuration('database', url=url): + database.initialize() + database._cleanup = types.MethodType( + partial(_cleanup, tempdir=tempdir), + database) + # bool column values in SQLite must be integers. + database.FALSE = 0 + database.TRUE = 1 + return database -- cgit v1.2.3-70-g09d2