diff options
| author | Barry Warsaw | 2012-07-25 19:10:40 -0400 |
|---|---|---|
| committer | Barry Warsaw | 2012-07-25 19:10:40 -0400 |
| commit | 12b1c4ca12668e6269d367886f8141005ae8c112 (patch) | |
| tree | fd69adbeb8eea065e154391c4107e552fe94532c /src | |
| parent | 26bd306b3dbca3cda0ba8eb2e34e2b1261902a6e (diff) | |
| download | mailman-12b1c4ca12668e6269d367886f8141005ae8c112.tar.gz mailman-12b1c4ca12668e6269d367886f8141005ae8c112.tar.zst mailman-12b1c4ca12668e6269d367886f8141005ae8c112.zip | |
Add a temporary database factory so we can remove more cruft from the
individual database classes themselves. This does mean that the temporary
factory must know something special about the sqlite and postgresql databases,
but I think that's an acceptable trade-off.
This should make the test code cleaner, and allow me to write better support
for temporary PostgreSQL databases.
Diffstat (limited to 'src')
| -rw-r--r-- | src/mailman/config/configure.zcml | 6 | ||||
| -rw-r--r-- | src/mailman/database/factory.py | 35 | ||||
| -rw-r--r-- | src/mailman/database/sqlite.py | 24 | ||||
| -rw-r--r-- | src/mailman/database/tests/test_migrations.py | 26 |
4 files changed, 52 insertions, 39 deletions
diff --git a/src/mailman/config/configure.zcml b/src/mailman/config/configure.zcml index 9e0d7c786..481ef6d0b 100644 --- a/src/mailman/config/configure.zcml +++ b/src/mailman/config/configure.zcml @@ -45,6 +45,12 @@ /> <utility + provides="mailman.interfaces.database.IDatabaseFactory" + factory="mailman.database.factory.DatabaseTemporaryFactory" + name="temporary" + /> + + <utility provides="mailman.interfaces.domain.IDomainManager" factory="mailman.model.domain.DomainManager" /> diff --git a/src/mailman/database/factory.py b/src/mailman/database/factory.py index 4783410cc..80ec09b36 100644 --- a/src/mailman/database/factory.py +++ b/src/mailman/database/factory.py @@ -22,17 +22,23 @@ from __future__ import absolute_import, print_function, unicode_literals __metaclass__ = type __all__ = [ 'DatabaseFactory', + 'DatabaseTemporaryFactory', 'DatabaseTestingFactory', ] +import os import types +import shutil +import tempfile +import functools from zope.interface import implementer from zope.interface.verify import verifyObject from mailman.config import config from mailman.interfaces.database import IDatabase, IDatabaseFactory +from mailman.testing.helpers import configuration from mailman.utilities.modules import call_name @@ -78,3 +84,32 @@ class DatabaseTestingFactory: # Make _reset() a bound method of the database instance. database._reset = types.MethodType(_reset, database) return database + + + +def _sqlite_cleanup(self, tempdir): + shutil.rmtree(tempdir) + + +@implementer(IDatabaseFactory) +class DatabaseTemporaryFactory: + """Create a temporary database for some of the migration tests.""" + + @staticmethod + def create(): + """See `IDatabaseFactory`.""" + database_class_name = config.database['class'] + database = call_name(database_class_name) + verifyObject(IDatabase, database) + if database.TAG == 'sqlite': + tempdir = tempfile.mkdtemp() + url = 'sqlite:///' + os.path.join(tempdir, 'mailman.db') + with configuration('database', url=url): + database.initialize() + database._cleanup = types.MethodType( + functools.partial(_sqlite_cleanup, tempdir=tempdir), database) + # bool column values in SQLite must be integers. + database.FALSE = 0 + database.TRUE = 1 + # Don't load the migrations. + return database diff --git a/src/mailman/database/sqlite.py b/src/mailman/database/sqlite.py index fc1037301..199feed9e 100644 --- a/src/mailman/database/sqlite.py +++ b/src/mailman/database/sqlite.py @@ -35,20 +35,6 @@ from mailman.database.base import StormBaseDatabase -class _TestDB: - # For the test suite; bool column values. - TRUE = 1 - FALSE = 0 - - def __init__(self, database, tempdir): - self.database = database - self._tempdir = tempdir - - def cleanup(self): - shutil.rmtree(self._tempdir) - - - class SQLiteDatabase(StormBaseDatabase): """Database class for SQLite.""" @@ -70,13 +56,3 @@ class SQLiteDatabase(StormBaseDatabase): # Ignore errors if fd > 0: os.close(fd) - - @staticmethod - def _make_testdb(): - from mailman.testing.helpers import configuration - tempdir = tempfile.mkdtemp() - url = 'sqlite:///' + os.path.join(tempdir, 'mailman.db') - database = SQLiteDatabase() - with configuration('database', url=url): - database.initialize() - return _TestDB(database, tempdir) diff --git a/src/mailman/database/tests/test_migrations.py b/src/mailman/database/tests/test_migrations.py index 595a673fc..c0ff80f74 100644 --- a/src/mailman/database/tests/test_migrations.py +++ b/src/mailman/database/tests/test_migrations.py @@ -33,14 +33,13 @@ from pkg_resources import resource_string from storm.exceptions import DatabaseError from zope.component import getUtility -from mailman.config import config +from mailman.interfaces.database import IDatabaseFactory from mailman.interfaces.domain import IDomainManager from mailman.interfaces.archiver import ArchivePolicy from mailman.interfaces.listmanager import IListManager from mailman.interfaces.mailinglist import IAcceptableAliasSet from mailman.testing.helpers import temporary_db from mailman.testing.layers import ConfigLayer -from mailman.utilities.modules import call_name @@ -62,13 +61,10 @@ class MigrationTestBase(unittest.TestCase): layer = ConfigLayer def setUp(self): - database_class_name = config.database['class'] - self._database_class = call_name(database_class_name) - self._testdb = self._database_class._make_testdb() - self._database = self._testdb.database + self._database = getUtility(IDatabaseFactory, 'temporary').create() def tearDown(self): - self._testdb.cleanup() + self._database._cleanup() @@ -137,7 +133,7 @@ class TestMigration20120407Data(MigrationTestBase): # Load the previous schema's sample data. sample_data = resource_string( 'mailman.database.tests.data', - 'migration_{0}_1.sql'.format(self._database_class.TAG)) + 'migration_{0}_1.sql'.format(self._database.TAG)) self._database.load_sql(self._database.store, sample_data) # Update to the current migration we're testing. self._database.load_migrations('20120407000000') @@ -200,7 +196,7 @@ class TestMigration20120407ArchiveData(MigrationTestBase): # Load the previous schema's sample data. sample_data = resource_string( 'mailman.database.tests.data', - 'migration_{0}_1.sql'.format(self._database_class.TAG)) + 'migration_{0}_1.sql'.format(self._database.TAG)) self._database.load_sql(self._database.store, sample_data) def _upgrade(self): @@ -213,7 +209,7 @@ class TestMigration20120407ArchiveData(MigrationTestBase): # ignored. This test sets it to 0 to ensure it's ignored. self._database.store.execute( 'UPDATE mailinglist SET archive = {0}, archive_private = {0} ' - 'WHERE id = 1;'.format(self._testdb.FALSE)) + 'WHERE id = 1;'.format(self._database.FALSE)) # Complete the migration self._upgrade() with temporary_db(self._database): @@ -226,8 +222,8 @@ class TestMigration20120407ArchiveData(MigrationTestBase): # ignored. This test sets it to 1 to ensure it's ignored. self._database.store.execute( 'UPDATE mailinglist SET archive = {0}, archive_private = {1} ' - 'WHERE id = 1;'.format(self._testdb.FALSE, - self._testdb.TRUE)) + 'WHERE id = 1;'.format(self._database.FALSE, + self._database.TRUE)) # Complete the migration self._upgrade() with temporary_db(self._database): @@ -239,7 +235,7 @@ class TestMigration20120407ArchiveData(MigrationTestBase): # private archives. self._database.store.execute( 'UPDATE mailinglist SET archive = {0}, archive_private = {0} ' - 'WHERE id = 1;'.format(self._testdb.TRUE)) + 'WHERE id = 1;'.format(self._database.TRUE)) # Complete the migration self._upgrade() with temporary_db(self._database): @@ -251,8 +247,8 @@ class TestMigration20120407ArchiveData(MigrationTestBase): # public archives. self._database.store.execute( 'UPDATE mailinglist SET archive = {1}, archive_private = {0} ' - 'WHERE id = 1;'.format(self._testdb.FALSE, - self._testdb.TRUE)) + 'WHERE id = 1;'.format(self._database.FALSE, + self._database.TRUE)) # Complete the migration self._upgrade() with temporary_db(self._database): |
