[z3-checkins] r18250 - in z3/sqlos/branch/jinty-3.0-fix_the_tests: . ftests testing tests transaction

jinty at codespeak.net jinty at codespeak.net
Fri Oct 7 10:55:35 CEST 2005


Author: jinty
Date: Fri Oct  7 10:55:31 2005
New Revision: 18250

Added:
   z3/sqlos/branch/jinty-3.0-fix_the_tests/testing/
   z3/sqlos/branch/jinty-3.0-fix_the_tests/testing/__init__.py
   z3/sqlos/branch/jinty-3.0-fix_the_tests/testing/sampleperson.py
   z3/sqlos/branch/jinty-3.0-fix_the_tests/testing/testdb.py
Removed:
   z3/sqlos/branch/jinty-3.0-fix_the_tests/tests/sampleperson.py
Modified:
   z3/sqlos/branch/jinty-3.0-fix_the_tests/connection.py
   z3/sqlos/branch/jinty-3.0-fix_the_tests/ftests/test_transaction.py
   z3/sqlos/branch/jinty-3.0-fix_the_tests/transaction/__init__.py
Log:
Apologies for this patch that does a few things at the same time.

They are:

* Create a testing module to support testing.
* Shove the sampleperson module there.
* Create a testdb module which can create temporary SQLite databases in memory
  and make them easily overridable so that the unit tests can be run against
  different databases easily.
* Make the prstgresql and sqlite connection adapters correctly state that they
  support transactions and allow for this in the unit tests. Also, when getting
  connections test for this support and act accordingly.
* Clean up the _connection better in the unit tests.



Modified: z3/sqlos/branch/jinty-3.0-fix_the_tests/connection.py
==============================================================================
--- z3/sqlos/branch/jinty-3.0-fix_the_tests/connection.py	(original)
+++ z3/sqlos/branch/jinty-3.0-fix_the_tests/connection.py	Fri Oct  7 10:55:31 2005
@@ -89,7 +89,12 @@
                 warnings.warn("Couldn't find a rdb connection by the "
                               "name %s. Please verify your setup." % name,
                               SQLObjectWarning, 3)
-            connCache[key] = IZopeSQLConnection(newconn()).transaction()
+            conn = IZopeSQLConnection(newconn())
+            if conn.supportTransactions:
+                connCache[key] = conn.transaction()
+            else: # At least MySQL does not support transactions
+                connCache[key] = conn
+
         return connCache[key]
 
     def releaseConnection(name):

Modified: z3/sqlos/branch/jinty-3.0-fix_the_tests/ftests/test_transaction.py
==============================================================================
--- z3/sqlos/branch/jinty-3.0-fix_the_tests/ftests/test_transaction.py	(original)
+++ z3/sqlos/branch/jinty-3.0-fix_the_tests/ftests/test_transaction.py	Fri Oct  7 10:55:31 2005
@@ -21,7 +21,7 @@
 
 from sqlos import getFactory
 from sqlos.container import SQLObjectContainer
-from sqlos.tests.sampleperson import SamplePerson
+from sqlos.testing.sampleperson import SamplePerson
 
 from psycopgda.adapter import PsycopgAdapter
 

Added: z3/sqlos/branch/jinty-3.0-fix_the_tests/testing/__init__.py
==============================================================================
--- (empty file)
+++ z3/sqlos/branch/jinty-3.0-fix_the_tests/testing/__init__.py	Fri Oct  7 10:55:31 2005
@@ -0,0 +1,11 @@
+##############################################################################
+#
+# Copyright (c) 2004 Enfold Systems LLC. All rights reserved.
+#
+# This software is distributed under the terms of the Zope Public
+# License (ZPL) v2.1. See COPYING.txt for more information.
+#
+##############################################################################
+
+# Change this to change the database used in all unit testing
+from testdb import SQLiteTestDB as TestDB

Added: z3/sqlos/branch/jinty-3.0-fix_the_tests/testing/sampleperson.py
==============================================================================
--- (empty file)
+++ z3/sqlos/branch/jinty-3.0-fix_the_tests/testing/sampleperson.py	Fri Oct  7 10:55:31 2005
@@ -0,0 +1,28 @@
+from zope.interface import implements
+from sqlobject import *
+from sqlos import SQLOS
+from sqlos.interfaces import ISQLSchema
+from zope.schema import TextLine, Text, Datetime
+
+class IPerson(ISQLSchema):
+
+    fullname = TextLine(title=u'Full Name',
+                    description=u"The full name of the user")
+    username = TextLine(title=u'Username',
+                    description=u"The user's login")
+    password = TextLine(title=u'Password',
+                    description=u"The user's password")
+
+class SamplePerson(SQLOS):
+
+    implements(IPerson)
+
+    # Disable cache, so we can see the real contents of the database
+    # during testing.
+    _cacheValues = False
+
+    _columns = [
+        StringCol('fullname', length=50, notNull=1),
+        StringCol('username', length=20, notNull=1),
+        StringCol('password', length=20, notNull=1),
+        ]

Added: z3/sqlos/branch/jinty-3.0-fix_the_tests/testing/testdb.py
==============================================================================
--- (empty file)
+++ z3/sqlos/branch/jinty-3.0-fix_the_tests/testing/testdb.py	Fri Oct  7 10:55:31 2005
@@ -0,0 +1,46 @@
+##############################################################################
+#
+# Copyright (c) 2004 Enfold Systems LLC. All rights reserved.
+#
+# This software is distributed under the terms of the Zope Public
+# License (ZPL) v2.1. See COPYING.txt for more information.
+#
+##############################################################################
+"""
+$Id: __init__.py 18249 2005-10-07 08:02:49Z jinty $
+"""
+
+__metaclass__ = type
+
+import os
+import tempfile
+import urllib
+import urlparse
+
+import sqlite
+from zope.app.rdb import ZopeDatabaseAdapter
+
+from sqlos.adapter import SQLiteAdapter
+
+
+class SQLiteda(ZopeDatabaseAdapter):
+    """A not very fancy SQLLite database adapter."""
+    def _connection_factory(self):
+        path = urlparse.urlparse(self.dsn)[2]
+        if path == '//:memory:':
+            return sqlite.connect(':memory:')
+        file = urllib.pathname2url(path)
+        return sqlite.connect(file)
+
+
+class SQLiteTestDB:
+    """Returns a DB suitable for unit tests."""
+
+    def __init__(self):
+        self.zda = SQLiteda('dbi://:memory:')
+
+    def connectionAdapterFactory(self):
+        return SQLiteAdapter(self.zda())
+
+    def tearDown(self):
+        pass

Deleted: /z3/sqlos/branch/jinty-3.0-fix_the_tests/tests/sampleperson.py
==============================================================================
--- /z3/sqlos/branch/jinty-3.0-fix_the_tests/tests/sampleperson.py	Fri Oct  7 10:55:31 2005
+++ (empty file)
@@ -1,28 +0,0 @@
-from zope.interface import implements
-from sqlobject import *
-from sqlos import SQLOS
-from sqlos.interfaces import ISQLSchema
-from zope.schema import TextLine, Text, Datetime
-
-class IPerson(ISQLSchema):
-
-    fullname = TextLine(title=u'Full Name',
-                    description=u"The full name of the user")
-    username = TextLine(title=u'Username',
-                    description=u"The user's login")
-    password = TextLine(title=u'Password',
-                    description=u"The user's password")
-
-class SamplePerson(SQLOS):
-
-    implements(IPerson)
-
-    # Disable cache, so we can see the real contents of the database
-    # during testing.
-    _cacheValues = False
-
-    _columns = [
-        StringCol('fullname', length=50, notNull=1),
-        StringCol('username', length=20, notNull=1),
-        StringCol('password', length=20, notNull=1),
-        ]

Modified: z3/sqlos/branch/jinty-3.0-fix_the_tests/transaction/__init__.py
==============================================================================
--- z3/sqlos/branch/jinty-3.0-fix_the_tests/transaction/__init__.py	(original)
+++ z3/sqlos/branch/jinty-3.0-fix_the_tests/transaction/__init__.py	Fri Oct  7 10:55:31 2005
@@ -40,13 +40,6 @@
     >>> from zope.app.tests.placelesssetup import setUp, tearDown
     >>> setUp()
 
-    Make a testing db:
-
-    >>> import tempfile
-    >>> import urllib
-    >>> fh, testdb_file = tempfile.mkstemp()
-    >>> testdb_dbi = 'dbi://' + urllib.pathname2url(testdb_file)
-
     First, register the subscribers:
 
     >>> from zope.app.tests import ztapi
@@ -61,23 +54,25 @@
     >>> ztapi.handle([IObjectCreatedEvent], sqlobjectCreatedSubscriber)
     >>> ztapi.handle([IObjectModifiedEvent], sqlobjectModifiedSubscriber)
 
+    Make a testing db:
+
+    >>> from sqlos.testing import TestDB
+    >>> testdb = TestDB()
+
     Then, create a connection:
 
-    >>> import sqlite
-    >>> from sqlos.adapter import SQLiteAdapter
-    >>> from zope.app.rdb import ZopeDatabaseAdapter
-    >>> class SQLiteda(ZopeDatabaseAdapter):
-    ...     def _connection_factory(self):
-    ...         return sqlite.connect(testdb_file)
-    >>> zda = SQLiteda(testdb_dbi)
-    >>> adapter = SQLiteAdapter(zda())
+    >>> adapter = testdb.connectionAdapterFactory()
 
     Stuff the connection into the test class and initialize the table
     if it doesn't exists yet:
 
-    >>> from sqlos.tests.sampleperson import SamplePerson
-    >>> SamplePerson._connection = adapter
-    >>> SamplePerson.createTable(ifNotExists=True)
+    >>> from sqlos.testing.sampleperson import SamplePerson
+    >>> oldconn = SamplePerson._connection
+    >>> if adapter.supportTransactions == True:
+    ...     SamplePerson._connection = adapter.transaction()
+    ... else:
+    ...     SamplePerson._connection = adapter
+    >>> SamplePerson.createTable()
 
     Now create a SamplePerson for testing:
 
@@ -236,11 +231,11 @@
     >>> del SamplePerson._expire_called
     >>> SQLObjectTransactionManager.commit = oldcommit
     >>> ZopeConnection.registerForTxn = oldregister
+    >>> SamplePerson._connection = oldconn
 
     And finally call tearDown and cleanup:
 
-    >>> import os
-    >>> os.remove(testdb_file)
+    >>> testdb.tearDown()
     >>> tearDown()
     """
 


More information about the z3-checkins mailing list