|
[Webware-checkins] r8044 - in Webware/trunk: . UserKit/Tests
UserKit/Tests/UserManagerTest.mkmodel
From: <updates@we...> - 2009-08-02 10:06
|
Author: chrisz
Date: Sun Aug 2 04:06:37 2009
New Revision: 8044
Modified:
Webware/trunk/AllTests.py
Webware/trunk/UserKit/Tests/UserManagerTest.mkmodel/Settings.config
Webware/trunk/UserKit/Tests/UserManagerTest.py
Log:
Use SQLite instead of MySQL for testing UserKit with MiddleKit. Removed the testing config mechanism that was actually only needed for MySQL.
Modified: Webware/trunk/AllTests.py
==============================================================================
--- Webware/trunk/AllTests.py (original)
+++ Webware/trunk/AllTests.py Sun Aug 2 04:06:37 2009
@@ -9,9 +9,6 @@
python AllTests.py - Runs all the unittests
python AllTests.py mypackage.MyFile - Runs the tests in 'mypackage/MyFile'
-This module also has a test-wide configuration file which can be accessed by
-the function AllTests.config().
-
"""
@@ -44,103 +41,11 @@
]
-import os
import site
import sys
import unittest
import logging
-from MiscUtils.Configurable import Configurable
-
-_alltestConfig = None
-_log = logging.getLogger(__name__)
-
-
-class _AllTestsConfig(Configurable):
- """Configuration for tests.
-
- E.g. which DBs to test, where to find DB utilities.
-
- If individual tests need some configuration, put it here so it
- is easy for a new user to configure all the tests in one place.
-
- """
-
- _defaultConfig = '''
-{ # Edit this file to activate more tests
-
- # Turn on tests that use MySQL?
- 'hasMysql': False,
-
- # If hasMysql is true, then these are used to connect:
- 'mysqlTestInfo' : {
-
- # Where is MySQLdb lib located?
- # 'extraSysPath': ['/somewhere/MySQL-python-1.2.2/build/lib'],
- 'extraSysPath': [],
-
- # Where is the MySQL client located (if not on the path)?
- # 'mysqlClient': '/usr/local/mysql/bin/mysql',
- # 'mysqlClient': 'c:/progra~1/mysql/mysqls~1.0/bin/mysql.exe',
- 'mysqlClient': 'mysql',
-
- # The name of the MySQL database to be used:
- 'database': 'test', # Test case uses this,
- # but UserManagerTest.mkmodel/Settings.config also defines it.
-
- # This is passed to MySQLObjectStore():
- 'DatabaseArgs': {
- 'host': 'localhost',
- 'port': 3306,
- 'user': 'test', # should have all database privileges
- 'passwd': '',
- },
- }
-}
-'''
-
- def configFilename(self):
- theFilename = os.path.join(os.path.dirname(__file__), 'AllTests.config')
- # The first time we are run, write a new configuration file.
- if not os.path.exists(theFilename):
- _log.info(' Creating new configuration file at "%s".'
- ' You can customize it to run more tests.', theFilename)
- fp = open(theFilename, 'w')
- fp.write(_AllTestsConfig._defaultConfig)
- fp.close()
- return theFilename
-
- def defaultConfig(self):
- default = eval(_AllTestsConfig._defaultConfig)
- return default
-
-def config():
- """Return singleton of configuration file."""
- global _alltestConfig
- if _alltestConfig is None:
- _alltestConfig = _AllTestsConfig()
- return _alltestConfig
-
-
-def checkAndAddPaths(listOfPaths):
- """Check paths.
-
- Pass me a list of paths, and I will check that each one exists and
- add it to sys.paths. This is used by tests which need to use some
- required library such as database drivers.
-
- """
- numBadPaths = 0
- for p in listOfPaths:
- p = os.path.abspath(p)
- if os.path.exists(p):
- site.addsitedir(p)
- else:
- numBadPaths += 1
- print 'WARNING: Trying to add paths to sys.path,'
- print ' but could not find "%s".' % p
- return numBadPaths # 0 = all were found
-
if __name__ == '__main__':
# Configure logging
@@ -165,13 +70,13 @@
# We could just use defaultTestLoader.loadTestsFromNames(),
# but it doesn't give a good error message when it cannot load a test.
# So we load all tests individually and raise appropriate exceptions.
- for t in testnames:
+ for test in testnames:
try:
- tests.addTest(unittest.defaultTestLoader.loadTestsFromName(t))
+ tests.addTest(unittest.defaultTestLoader.loadTestsFromName(test))
except Exception:
- print 'ERROR: Skipping tests from "%s".' % t
- try:
- __import__(t) # just try to import the test after loadig failed
+ print 'ERROR: Skipping tests from "%s".' % test
+ try: # just try to import the test after loadig failed
+ __import__(test)
except ImportError:
print 'Could not import the test module.'
else:
Modified: Webware/trunk/UserKit/Tests/UserManagerTest.mkmodel/Settings.config
==============================================================================
--- Webware/trunk/UserKit/Tests/UserManagerTest.mkmodel/Settings.config (original)
+++ Webware/trunk/UserKit/Tests/UserManagerTest.mkmodel/Settings.config Sun Aug 2 04:06:37 2009
@@ -1,8 +1,4 @@
{
- 'Package': 'UserKit.Tests.mk_MySQL',
+ 'Package': 'UserKit.Tests.mk_SQLite',
'UsePickledClassesCache': False,
- 'Database': 'test',
-
- # These are set in AllTests.config:
- # 'DatabaseArgs': {'user': 'test', 'passwd': ''},
}
Modified: Webware/trunk/UserKit/Tests/UserManagerTest.py
==============================================================================
--- Webware/trunk/UserKit/Tests/UserManagerTest.py (original)
+++ Webware/trunk/UserKit/Tests/UserManagerTest.py Sun Aug 2 04:06:37 2009
@@ -1,7 +1,8 @@
"""This module tests UserManagers in different permutations.
UserManagers can save their data to files, or to a MiddleKit database.
-For MiddleKit, the database can be MySQL, PostgreSQL, MSSQL or SQLite.
+For MiddleKit, the database can be MySQL, PostgreSQL, MSSQL or SQLite,
+but we test only with the SQLite database.
"""
@@ -171,14 +172,14 @@
def setUpUserDir(self, mgr):
path = 'Users'
if os.path.exists(path):
- shutil.rmtree(path, ignore_errors=1)
+ shutil.rmtree(path, ignore_errors=True)
os.mkdir(path)
mgr.setUserDir(path)
def tearDown(self):
path = 'Users'
if os.path.exists(path):
- shutil.rmtree(path, ignore_errors=1)
+ shutil.rmtree(path, ignore_errors=True)
_UserManagerToSomewhereTest.tearDown(self)
@@ -190,64 +191,42 @@
def setUp(self):
_UserManagerToSomewhereTest.setUp(self)
+ self._mgr = self._store = None
self._output = []
self._stdout, self._stderr = sys.stdout, sys.stderr
sys.stdout = sys.stderr = self
try:
-
# Generate Python and SQL from our test MiddleKit Model
from MiddleKit.Design.Generate import Generate
generator = Generate()
-
modelFileName = os.path.join(testDir, 'UserManagerTest.mkmodel')
- generationDir = os.path.join(testDir, 'mk_MySQL')
- # _log.debug('model: %s',modelFileName)
- # @@ 2001-02-18 ce: woops: hard coding MySQL
-
- args = 'Generate.py --db MySQL --model %s --outdir %s' % (
+ # We test only with SQLite for ease of setup
+ generationDir = os.path.join(testDir, 'mk_SQLite')
+ args = 'Generate.py --db SQLite --model %s --outdir %s' % (
modelFileName, generationDir)
Generate().main(args.split())
+ createScript = os.path.join(generationDir, 'GeneratedSQL', 'Create.sql')
- create_sql = os.path.join(generationDir, 'GeneratedSQL', 'Create.sql')
-
- assert os.path.exists(create_sql), (
+ assert os.path.exists(createScript), (
'The generation process should create some SQL files.')
assert os.path.exists(os.path.join(generationDir,
'UserForMKTest.py')), ('The generation process'
' should create some Python files.')
- self._mysqlTestInfo = AllTests.config().setting('mysqlTestInfo')
- # _log.info('mysqlTestInfo=%s', self._mysqlTestInfo)
-
- # Create our test database using info from AllTests.config
-
- self._mysqlClient = self._mysqlTestInfo['mysqlClient']
- mysqlClientName = os.path.basename(self._mysqlClient)
- assert mysqlClientName == 'mysql' or mysqlClientName == 'mysql.exe'
- self._mysqlClient = ' '.join([self._mysqlClient] + ['--%s="%s"'
- % (p.replace('passwd', 'password'), v) for (p, v)
- in self._mysqlTestInfo['DatabaseArgs'].items() if v])
- executeSqlCmd = '%s < %s' % (self._mysqlClient, create_sql)
-
- # _log.debug('running: %s', executeSqlCmd)
- f = os.popen(executeSqlCmd)
- self._output.append(f.read())
- if f.close():
- raise OSError('Error running: %s' % executeSqlCmd)
-
- # Create store, and connect to database
-
- from MiddleKit.Run.MySQLObjectStore import MySQLObjectStore
+ from MiddleKit.Run.SQLiteObjectStore import SQLiteObjectStore
- store = MySQLObjectStore(**self._mysqlTestInfo['DatabaseArgs'])
- store.readModelFileNamed(modelFileName)
+ # Create store and connect to database
+ databaseFile = os.path.join(generationDir, 'test.db')
+ self._store = SQLiteObjectStore(database=databaseFile)
+ self._store.executeSQLScript(open(createScript).read())
+ self._store.readModelFileNamed(modelFileName)
from MiddleKit.Run.MiddleObject import MiddleObject
from UserKit.UserManagerToMiddleKit import UserManagerToMiddleKit
- from UserKit.Tests.mk_MySQL.UserForMKTest import UserForMKTest
+ from UserKit.Tests.mk_SQLite.UserForMKTest import UserForMKTest
assert issubclass(UserForMKTest, MiddleObject)
from UserKit.User import User
if not issubclass(UserForMKTest, User):
@@ -258,10 +237,12 @@
base1 = self.__class__.__bases__[0]
base2 = self.__class__.__bases__[1]
base1.__init__(self)
- base2.__init__(self, manager=manager, name=name, password=password)
+ base2.__init__(self,
+ manager=manager, name=name, password=password)
UserForMKTest.__init__ = __init__
- self._mgr = self.userManagerClass()(userClass=UserForMKTest, store=store)
+ self._mgr = self.userManagerClass()(
+ userClass=UserForMKTest, store=self._store)
except Exception:
sys.stdout, sys.stderr = self._stdout, self._stderr
@@ -282,26 +263,20 @@
self._output = []
self._stdout, self._stderr = sys.stdout, sys.stderr
sys.stdout = sys.stderr = self
-
try:
+ # close user manager and object store
+ if self._mgr:
+ self._mgr.shutDown()
+ self._mgr = None
+ if self._store:
+ self._store.discardEverything()
+ if self._store._connected:
+ self._store._connection.close()
+ self._store = None
# clean out generated files
- path = os.path.join(testDir, 'mk_MySQL')
+ path = os.path.join(testDir, 'mk_SQLite')
if os.path.exists(path):
- shutil.rmtree(path, ignore_errors=1)
-
- # Drop tables from database
- sqlDropTables = "drop table UserForMKTest, _MKClassIds"
-
- db = self._mysqlTestInfo['database']
-
- executeSqlCmd = '%s %s -e "%s"' % (self._mysqlClient, db, sqlDropTables)
-
- # _log.debug('running: %s', executeSqlCmd)
- f = os.popen(executeSqlCmd)
- self._output.append(f.read())
- if f.close():
- raise OSError('Error running: %s' % executeSqlCmd)
-
+ shutil.rmtree(path, ignore_errors=True)
except Exception:
sys.stdout, sys.stderr = self._stdout, self._stderr
print "Error in %s.SetUp." % self.__class__.__name__
@@ -333,20 +308,8 @@
def makeTestSuite():
testClasses = [
- UserManagerTest, UserManagerToFileTest, RoleUserManagerToFileTest]
-
- # See if AllTests.conf has been configured for MySQL
- if AllTests.config().setting('hasMysql'):
- mysqlTestInfo = AllTests.config().setting('mysqlTestInfo')
- _log.info('Adding MySQL tests for UserKit.')
- # Add paths for MySQL-python driver
- numBadPaths = AllTests.checkAndAddPaths(mysqlTestInfo['extraSysPath'])
- # Add the tests that require MySQL
- testClasses.extend([UserManagerToMiddleKitTest, RoleUserManagerToMiddleKitTest])
- else:
- _log.info('Skipping MySQL tests. MySQL is not configured in AllTests.config.')
-
- make = unittest.makeSuite
+ UserManagerTest, UserManagerToFileTest, RoleUserManagerToFileTest,
+ UserManagerToMiddleKitTest, RoleUserManagerToMiddleKitTest]
tests = [unittest.makeSuite(klass) for klass in testClasses]
return unittest.TestSuite(tests)
|
| Thread | Author | Date |
|---|---|---|
| [Webware-checkins] r8044 - in Webware/trunk: . UserKit/Tests UserKit/Tests/UserManagerTest.mkmodel | <updates@we...> |