[CalendarServer-changes] [9719] CalendarServer/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Thu Aug 16 14:49:55 PDT 2012
Revision: 9719
http://trac.macosforge.org/projects/calendarserver/changeset/9719
Author: glyph at apple.com
Date: 2012-08-16 14:49:55 -0700 (Thu, 16 Aug 2012)
Log Message:
-----------
add an explicitly supported, directly tested test fixture for other code to use
Modified Paths:
--------------
CalendarServer/trunk/twext/enterprise/dal/test/test_record.py
Added Paths:
-----------
CalendarServer/trunk/twext/enterprise/fixtures.py
CalendarServer/trunk/twext/enterprise/test/test_fixtures.py
Property Changed:
----------------
CalendarServer/trunk/
Modified: CalendarServer/trunk/twext/enterprise/dal/test/test_record.py
===================================================================
--- CalendarServer/trunk/twext/enterprise/dal/test/test_record.py 2012-08-16 21:49:54 UTC (rev 9718)
+++ CalendarServer/trunk/twext/enterprise/dal/test/test_record.py 2012-08-16 21:49:55 UTC (rev 9719)
@@ -18,8 +18,6 @@
Test cases for L{twext.enterprise.dal.record}.
"""
-import sqlite3
-
from twisted.internet.defer import inlineCallbacks
from twisted.trial.unittest import TestCase
@@ -27,12 +25,10 @@
from twext.enterprise.dal.record import (
Record, fromTable, ReadOnly, NoSuchRecord
)
-from twext.enterprise.dal.syntax import SQLITE_DIALECT
-
from twext.enterprise.dal.test.test_parseschema import SchemaTestHelper
-from twext.enterprise.adbapi2 import ConnectionPool
from twext.enterprise.dal.syntax import SchemaSyntax
from twisted.internet.defer import gatherResults
+from twext.enterprise.fixtures import buildConnectionPool
# from twext.enterprise.dal.syntax import
@@ -75,22 +71,7 @@
"""
def setUp(self):
- sqlitename = self.mktemp()
- seqs = {}
- def connectionFactory(label=self.id()):
- conn = sqlite3.connect(sqlitename)
- def nextval(seq):
- result = seqs[seq] = seqs.get(seq, 0) + 1
- return result
- conn.create_function("nextval", 1, nextval)
- return conn
- con = connectionFactory()
- con.executescript(schemaString)
- con.commit()
- self.pool = ConnectionPool(connectionFactory, paramstyle='numeric',
- dialect=SQLITE_DIALECT)
- self.pool.startService()
- self.addCleanup(self.pool.stopService)
+ self.pool = buildConnectionPool(self, schemaString)
@inlineCallbacks
Added: CalendarServer/trunk/twext/enterprise/fixtures.py
===================================================================
--- CalendarServer/trunk/twext/enterprise/fixtures.py (rev 0)
+++ CalendarServer/trunk/twext/enterprise/fixtures.py 2012-08-16 21:49:55 UTC (rev 9719)
@@ -0,0 +1,55 @@
+# -*- test-case-name: twext.enterprise.test.test_fixtures -*-
+##
+# Copyright (c) 2012 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+"""
+Fixtures for testing code that uses ADBAPI2.
+"""
+
+import sqlite3
+
+from twext.enterprise.adbapi2 import ConnectionPool
+from twext.enterprise.ienterprise import SQLITE_DIALECT
+
+def buildConnectionPool(testCase, schemaText="", dialect=SQLITE_DIALECT):
+ """
+ Build a L{ConnectionPool} for testing purposes, with the given C{testCase}.
+
+ @param testCase: the test case to attach the resulting L{ConnectionPool}
+ to.
+ @type testCase: L{twisted.trial.unittest.TestCase}
+
+ @param schemaText: The text of the schema with which to initialize the
+ database.
+ @type schemaText: L{str}
+ """
+ sqlitename = testCase.mktemp()
+ seqs = {}
+ def connectionFactory(label=testCase.id()):
+ conn = sqlite3.connect(sqlitename)
+ def nextval(seq):
+ result = seqs[seq] = seqs.get(seq, 0) + 1
+ return result
+ conn.create_function("nextval", 1, nextval)
+ return conn
+ con = connectionFactory()
+ con.executescript(schemaText)
+ con.commit()
+ pool = ConnectionPool(connectionFactory, paramstyle='numeric',
+ dialect=SQLITE_DIALECT)
+ pool.startService()
+ testCase.addCleanup(pool.stopService)
+ return pool
Added: CalendarServer/trunk/twext/enterprise/test/test_fixtures.py
===================================================================
--- CalendarServer/trunk/twext/enterprise/test/test_fixtures.py (rev 0)
+++ CalendarServer/trunk/twext/enterprise/test/test_fixtures.py 2012-08-16 21:49:55 UTC (rev 9719)
@@ -0,0 +1,51 @@
+##
+# Copyright (c) 2012 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+"""
+Tests for L{twext.enterprise.fixtures}.
+
+Quis custodiet ipsos custodes? This module, that's who.
+"""
+
+from twext.enterprise.fixtures import buildConnectionPool
+
+from twisted.trial.unittest import TestCase
+from twisted.trial.reporter import TestResult
+from twext.enterprise.adbapi2 import ConnectionPool
+
+class PoolTests(TestCase):
+ """
+ Tests for fixtures that create a connection pool.
+ """
+
+ def test_buildConnectionPool(self):
+ """
+ L{buildConnectionPool} returns a L{ConnectionPool} which will be
+ running only for the duration of the test.
+ """
+ collect = []
+ class SampleTest(TestCase):
+ def setUp(self):
+ self.pool = buildConnectionPool(self)
+ def test_sample(self):
+ collect.append(self.pool.running)
+ def tearDown(self):
+ collect.append(self.pool.running)
+ r = TestResult()
+ t = SampleTest("test_sample")
+ t.run(r)
+ self.assertIsInstance(t.pool, ConnectionPool)
+ self.assertEqual([True, False], collect)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120816/35b82662/attachment-0001.html>
More information about the calendarserver-changes
mailing list