summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBarry Warsaw2012-07-25 21:50:42 -0400
committerBarry Warsaw2012-07-25 21:50:42 -0400
commitfc7e14405afd9a89ae1b6cc8cabd861ec4e72ee8 (patch)
tree9d4d13881af752fb42d6389fccf2fc10ce9c4881 /src
parent12b1c4ca12668e6269d367886f8141005ae8c112 (diff)
downloadmailman-fc7e14405afd9a89ae1b6cc8cabd861ec4e72ee8.tar.gz
mailman-fc7e14405afd9a89ae1b6cc8cabd861ec4e72ee8.tar.zst
mailman-fc7e14405afd9a89ae1b6cc8cabd861ec4e72ee8.zip
Diffstat (limited to 'src')
-rw-r--r--src/mailman/database/factory.py36
-rw-r--r--src/mailman/database/postgresql.py39
-rw-r--r--src/mailman/database/sqlite.py2
-rw-r--r--src/mailman/testing/testing.cfg6
4 files changed, 38 insertions, 45 deletions
diff --git a/src/mailman/database/factory.py b/src/mailman/database/factory.py
index 80ec09b36..0ea2f4776 100644
--- a/src/mailman/database/factory.py
+++ b/src/mailman/database/factory.py
@@ -31,8 +31,9 @@ import os
import types
import shutil
import tempfile
-import functools
+from functools import partial
+from urlparse import urlsplit, urlunsplit
from zope.interface import implementer
from zope.interface.verify import verifyObject
@@ -64,7 +65,9 @@ def _reset(self):
"""See `IDatabase`."""
from mailman.database.model import ModelMeta
self.store.rollback()
+ self._pre_reset(self.store)
ModelMeta._reset(self.store)
+ self._post_reset(self.store)
self.store.commit()
@@ -91,6 +94,12 @@ def _sqlite_cleanup(self, tempdir):
shutil.rmtree(tempdir)
+def _postgresql_cleanup(self, database, tempdb_name):
+ database.store.rollback()
+ database.store.close()
+ config.db.store.execute('DROP DATABASE {0}'.format(tempdb_name))
+
+
@implementer(IDatabaseFactory)
class DatabaseTemporaryFactory:
"""Create a temporary database for some of the migration tests."""
@@ -107,9 +116,32 @@ class DatabaseTemporaryFactory:
with configuration('database', url=url):
database.initialize()
database._cleanup = types.MethodType(
- functools.partial(_sqlite_cleanup, tempdir=tempdir), database)
+ partial(_sqlite_cleanup, tempdir=tempdir), database)
# bool column values in SQLite must be integers.
database.FALSE = 0
database.TRUE = 1
+ elif database.TAG == 'postgres':
+ parts = urlsplit(config.database.url)
+ assert parts.scheme == 'postgres'
+ new_parts = list(parts)
+ new_parts[2] = '/mmtest'
+ url = urlunsplit(new_parts)
+ # Use the existing database connection to create a new testing
+ # database.
+ config.db.store.execute('ABORT;')
+ config.db.store.execute('CREATE DATABASE mmtest;')
+ with configuration('database', url=url):
+ database.initialize()
+ database._cleanup = types.MethodType(
+ partial(_postgresql_cleanup,
+ database=database,
+ tempdb_name='mmtest'),
+ database)
+ # bool column values in PostgreSQL.
+ database.FALSE = 'False'
+ database.TRUE = 'True'
+ else:
+ raise RuntimeError(
+ 'Unsupported test database: {0}'.format(database.TAG))
# Don't load the migrations.
return database
diff --git a/src/mailman/database/postgresql.py b/src/mailman/database/postgresql.py
index 4200ae4fe..1fb831b3b 100644
--- a/src/mailman/database/postgresql.py
+++ b/src/mailman/database/postgresql.py
@@ -17,7 +17,7 @@
"""PostgreSQL database support."""
-from __future__ import absolute_import, unicode_literals
+from __future__ import absolute_import, print_function, unicode_literals
__metaclass__ = type
__all__ = [
@@ -26,29 +26,11 @@ __all__ = [
from operator import attrgetter
-from urlparse import urlsplit, urlunsplit
-from mailman.config import config
from mailman.database.base import StormBaseDatabase
-
-class _TestDB:
- # For the test suite; bool column values.
- TRUE = 'True'
- FALSE = 'False'
-
- def __init__(self, database):
- self.database = database
-
- def cleanup(self):
- self.database.store.rollback()
- self.database.store.close()
- config.db.store.execute('DROP DATABASE mmtest;')
-
-
-
class PostgreSQLDatabase(StormBaseDatabase):
"""Database class for PostgreSQL."""
@@ -81,22 +63,3 @@ class PostgreSQLDatabase(StormBaseDatabase):
max("id") IS NOT null)
FROM "{0}";
""".format(model_class.__storm_table__))
-
- @staticmethod
- def _make_testdb():
- from mailman.testing.helpers import configuration
- parts = urlsplit(config.database.url)
- assert parts.scheme == 'postgres'
- new_parts = list(parts)
- new_parts[2] = '/mmtest'
- url = urlunsplit(new_parts)
- # Use the existing database connection to create a new testing
- # database. Create a savepoint, which will make it easy to reset
- # after the test.
- config.db.store.execute('ABORT;')
- config.db.store.execute('CREATE DATABASE mmtest;')
- # Now create a new, test database.
- database = PostgreSQLDatabase()
- with configuration('database', url=url):
- database.initialize()
- return _TestDB(database)
diff --git a/src/mailman/database/sqlite.py b/src/mailman/database/sqlite.py
index 199feed9e..305fa006e 100644
--- a/src/mailman/database/sqlite.py
+++ b/src/mailman/database/sqlite.py
@@ -26,8 +26,6 @@ __all__ = [
import os
-import shutil
-import tempfile
from urlparse import urlparse
diff --git a/src/mailman/testing/testing.cfg b/src/mailman/testing/testing.cfg
index 141d74a8f..0be01298b 100644
--- a/src/mailman/testing/testing.cfg
+++ b/src/mailman/testing/testing.cfg
@@ -18,9 +18,9 @@
# A testing configuration.
# For testing against PostgreSQL.
-# [database]
-# class: mailman.database.postgresql.PostgreSQLDatabase
-# url: postgres://barry:barry@localhost/mailman
+[database]
+class: mailman.database.postgresql.PostgreSQLDatabase
+url: postgres://barry:barry@localhost/mailman
[mailman]
site_owner: noreply@example.com