Author: barry.warsaw
Date: Sun Mar 20 13:59:42 2005
New Revision: 36

Added:
   trunk/mailman/src/mailman/database/tests/
   trunk/mailman/src/mailman/database/tests/__init__.py
   trunk/mailman/src/mailman/database/tests/test_roster.py
   trunk/mailman/src/mailman/utils/descriptors.py
      - copied, changed from r30, trunk/mailman/src/mailman/utils/txnprop.py
   trunk/mailman/test.py   (contents, props changed)
      - copied, changed from r30, tags/mailman/rewrite-1/test.py
Removed:
   trunk/mailman/src/mailman/utils/txnprop.py
Modified:
   trunk/mailman/src/mailman/database/__init__.py
   trunk/mailman/src/mailman/database/schema.py
Log:
Added a test case for simple Roster insertion.

Created the Database class for simplified opening of the database (and not
tied to SQLite directly).

Moved txnprop to a generic descriptor module, and added a readonly descriptor.

Added test.py runner.


Modified: trunk/mailman/src/mailman/database/__init__.py
==============================================================================
--- trunk/mailman/src/mailman/database/__init__.py      (original)
+++ trunk/mailman/src/mailman/database/__init__.py      Sun Mar 20 13:59:42 2005
@@ -0,0 +1,42 @@
+# Copyright (C) 2005 Barry A. Warsaw
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; either version 2 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+# Place - Suite 330, Boston, MA 02111-1307, USA.
+
+"""The SQLite/SQLObject database."""
+
+from sqlobject import SQLObject, connectionForURI
+
+from mailman.utils.descriptors import readonly
+
+
+
+class Database:
+    def open(self, connection_url):
+        self._conn = connectionForURI(connection_url)
+        # Dynamically set the connection object and create if necessary, all
+        # SQLObject tables.
+        import mailman.database.schema
+        for k in dir(mailman.database.schema):
+            obj = getattr(mailman.database.schema, k)
+            if isinstance(obj, type) and issubclass(obj, SQLObject):
+                obj.setConnection(self._conn)
+                obj.createTable(ifNotExists=True)
+
+    def close(self):
+        self._conn.close()
+
+    @readonly
+    def conn(self):
+        return self._conn

Modified: trunk/mailman/src/mailman/database/schema.py
==============================================================================
--- trunk/mailman/src/mailman/database/schema.py        (original)
+++ trunk/mailman/src/mailman/database/schema.py        Sun Mar 20 13:59:42 2005
@@ -21,8 +21,8 @@
 import datetime
 
 from sqlobject import *
-from sqlobject.inheritance import InheritableSQLObject
 from sqlobject.col import *
+from sqlobject.inheritance import InheritableSQLObject
 
 log = logging.getLogger('mailman.queues')
 
@@ -160,8 +160,7 @@
 
 if __name__ == '__main__':
     from sqlobject.sqlite import builder
-    b = builder()
-    conn = b(sys.argv[1], debug=True)
+    conn = builder()(sys.argv[1], debug=True)
     for table in (MailingList, Domain, RosterEntry, EmailAddress, Roster,
                   RosterReference):
         table.setConnection(conn)

Added: trunk/mailman/src/mailman/database/tests/__init__.py
==============================================================================

Added: trunk/mailman/src/mailman/database/tests/test_roster.py
==============================================================================
--- (empty file)
+++ trunk/mailman/src/mailman/database/tests/test_roster.py     Sun Mar 20 
13:59:42 2005
@@ -0,0 +1,55 @@
+# Copyright (C) 2005 Barry A. Warsaw
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; either version 2 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+# Place - Suite 330, Boston, MA 02111-1307, USA.
+
+"""Roster tests."""
+
+import os
+import tempfile
+import unittest
+
+from mailman.database import Database
+from mailman.database import schema
+
+
+
+class TestRoster(unittest.TestCase):
+    def setUp(self):
+        fd, self._dbfile = tempfile.mkstemp('.db', text=False)
+        filename = os.path.abspath(self._dbfile)
+        self._db = Database()
+        self._db.open('sqlite://' + filename)
+
+    def tearDown(self):
+        self._db.close()
+        os.remove(self._dbfile)
+
+    def test_simple_roster(self):
+        eq = self.assertEqual
+        roster = schema.Roster()
+        addr = schema.EmailAddress(
+            address='[EMAIL PROTECTED]',
+            realname='Adam Ant',
+            member_of=roster)
+        entries = list(schema.EmailAddress.select(
+            schema.EmailAddress.q.address == '[EMAIL PROTECTED]'))
+        eq(len(entries), 1)
+        eq(entries[0].address, '[EMAIL PROTECTED]')
+
+
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(TestRoster))
+    return suite

Copied: trunk/mailman/src/mailman/utils/descriptors.py (from r30, 
trunk/mailman/src/mailman/utils/txnprop.py)
==============================================================================
--- trunk/mailman/src/mailman/utils/txnprop.py  (original)
+++ trunk/mailman/src/mailman/utils/descriptors.py      Sun Mar 20 13:59:42 2005
@@ -1,4 +1,4 @@
-# Copyright (C) 2004 Barry A. Warsaw
+# Copyright (C) 2004-2005 Barry A. Warsaw
 #
 # This program is free software; you can redistribute it and/or modify it
 # under the terms of the GNU General Public License as published by the Free
@@ -14,14 +14,18 @@
 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 # Place - Suite 330, Boston, MA 02111-1307, USA.
 
-"""A transaction wrapping property."""
-
-# A common pattern is to write a non-public method which does the basic logic
-# of the operation, along with public method which wraps the first in a
-# transaction or a lock acquire/release.  This descriptor class makes this
-# pattern much more consistent.  Use txnprop when you want to both acquire the
-# lock and do the operation in a transaction (i.e. for mutator methods).
+"""Various useful descriptors."""
 
+
+# This descriptor works hand-in-hand with the associated object, which must
+# provide a _withtxn() method that takes as arguments the object, the method
+# to call, as well as the *positional and **keyword arguments.  _withtxn()
+# then should start a transaction, call the method providing the cursor as the
+# first argument, and then commit or abort the transaction as necessary.
+#
+# Transaction-protected methods then can be written to be largely ignorant of
+# transactions, except that they should use the cursor object provided in the
+# first argument.
 class txnprop(object):
     def __init__(self, func):
         # func is going to be a function not a method (even unbound)
@@ -34,3 +38,15 @@
         def wrapper(*args, **kws):
             return obj._withtxn(self._func.__get__(obj), *args, **kws)
         return wrapper
+
+
+
+# Use this descriptor for read-only properties.  The method should be named
+# the same as the attribute, and it should do whatever is necessary to 'get'
+# the attribute's value.
+def readonly(get):
+    def set(self, val):
+        raise AttributeError('read-only attribute')
+    def delete(self):
+        raise AttributeError('read-only attribute')
+    return property(get, set, delete)

Copied: trunk/mailman/test.py (from r30, tags/mailman/rewrite-1/test.py)
==============================================================================
--- tags/mailman/rewrite-1/test.py      (original)
+++ trunk/mailman/test.py       Sun Mar 20 13:59:42 2005
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+#! /usr/bin/env python2.4
 
 """
 test.py [-aBbcdDfgGhLmprtTuv] [modfilter [testfilter]]
_______________________________________________
Mailman-checkins mailing list
[email protected]
Unsubscribe: 
http://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to