From b35a5ce45ab979f238ad1ab2457b6c6589ff691f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com> Date: Mon, 18 Sep 2006 08:52:54 +0000 Subject: [PATCH] added support to run unit tests on an existing Data.fs, just pass the path to the Data.fs and the id of the portal. catalog will be recreated unless you pass --recreate_catalog=0 argument. Note that DemoStorage cannot use read only FileStorage as a base and that FileStorage uses a lock file, so it's impossible to run tests on a Data.fs that's already in use. git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@10077 20353a03-c40f-0410-a6d1-a30d3c3de9de --- product/ERP5Type/tests/ERP5TypeTestCase.py | 86 +++++++++++++++++----- product/ERP5Type/tests/custom_zodb.py | 9 ++- product/ERP5Type/tests/runUnitTest.py | 21 +++++- 3 files changed, 95 insertions(+), 21 deletions(-) diff --git a/product/ERP5Type/tests/ERP5TypeTestCase.py b/product/ERP5Type/tests/ERP5TypeTestCase.py index 006ac7ac31..47ef8aae5b 100644 --- a/product/ERP5Type/tests/ERP5TypeTestCase.py +++ b/product/ERP5Type/tests/ERP5TypeTestCase.py @@ -146,6 +146,29 @@ portal_name = 'erp5_portal' # prevent replaying the same failing setup step for each test. failed_portal_installation = {} +def _getConnectionStringDict(): + """Returns the connection strings used for this test. + """ + connection_string_dict = {} + erp5_sql_connection_string = os.environ.get( + 'erp5_sql_connection_string') + if erp5_sql_connection_string: + connection_string_dict['erp5_sql_connection_string'] = \ + erp5_sql_connection_string + cmf_activity_sql_connection_string = os.environ.get( + 'cmf_activity_sql_connection_string', + os.environ.get('erp5_sql_connection_string')) + if cmf_activity_sql_connection_string: + connection_string_dict['cmf_activity_sql_connection_string'] = \ + cmf_activity_sql_connection_string + erp5_sql_deferred_connection_string = os.environ.get( + 'erp5_sql_deferred_connection_string', + os.environ.get('erp5_sql_connection_string')) + if erp5_sql_deferred_connection_string: + connection_string_dict['erp5_sql_deferred_connection_string'] = \ + erp5_sql_deferred_connection_string + return connection_string_dict + class ERP5TypeTestCase(PortalTestCase): def getTitle(self): @@ -158,7 +181,12 @@ class ERP5TypeTestCase(PortalTestCase): Return the name of a portal for this test case. This is necessary for each test case to use a different portal built by different business templates. + The test runner can set `erp5_tests_portal_id` environnement variable + to force a portal id. """ + forced_portal_id = os.environ.get('erp5_tests_portal_id') + if forced_portal_id: + return str(forced_portal_id) m = md5.new() m.update(repr(self.getBusinessTemplateList())+ self.getTitle()) uid = m.hexdigest() @@ -271,6 +299,8 @@ class ERP5TypeTestCase(PortalTestCase): create_activities=create_activities, hot_reindexing=hot_reindexing) PortalTestCase.setUp(self) + self._updateConnectionStrings() + self._recreateCatalog() def afterSetUp(self): '''Called after setUp() has completed. This is @@ -291,6 +321,39 @@ class ERP5TypeTestCase(PortalTestCase): ZopeTestCase._print('\n%s ' % message) LOG('Testing ... ', DEBUG, message) + def _updateConnectionStrings(self): + """Update connection strings with values passed by the testRunner + """ + portal = self.getPortal() + # update connection strings + for connection_string_name, connection_string in\ + _getConnectionStringDict().items(): + connection_name = connection_string_name.replace('_string', '') + getattr(portal, connection_name).edit('', connection_string) + + def _recreateCatalog(self, quiet=0): + """Clear activities and catalog and recatalog everything. + Test runner can set `erp5_tests_recreate_catalog` environnement variable, + in that case we have to clear catalog. """ + portal = self.getPortal() + if int(os.environ.get('erp5_tests_recreate_catalog', 0)): + try: + self.login() + _start = time.time() + if not quiet: + ZopeTestCase._print('\nRecreating catalog ... ') + portal.portal_activities.manageClearActivities() + portal.portal_catalog.manage_catalogClear() + get_transaction().commit() + portal.ERP5Site_reindexAll() + get_transaction().commit() + self.tic() + if not quiet: + ZopeTestCase._print('done (%.3fs)\n' % (time.time() - _start,)) + finally: + os.environ['erp5_tests_recreate_catalog'] = '0' + noSecurityManager() + # Utility methods specific to ERP5Type def getTemplateTool(self): return getToolByName(self.getPortal(), 'portal_templates', None) @@ -389,7 +452,7 @@ def setupERP5Site( business_template_list=(), ''' Creates an ERP5 site. business_template_list must be specified correctly - (e.g. '("erp5_common", )'). + (e.g. '("erp5_base", )'). ''' if portal_name in failed_portal_installation: raise RuntimeError, 'Installation of %s already failed, giving up'\ @@ -417,24 +480,7 @@ def setupERP5Site( business_template_list=(), if not quiet: ZopeTestCase._print('Adding %s ERP5 Site ... ' % portal_name) - extra_constructor_kw = {} - erp5_sql_connection_string = os.environ.get( - 'erp5_sql_connection_string') - if erp5_sql_connection_string: - extra_constructor_kw['erp5_sql_connection_string'] = \ - erp5_sql_connection_string - cmf_activity_sql_connection_string = os.environ.get( - 'cmf_activity_sql_connection_string', - os.environ.get('erp5_sql_connection_string')) - if cmf_activity_sql_connection_string: - extra_constructor_kw['cmf_activity_sql_connection_string'] = \ - cmf_activity_sql_connection_string - erp5_sql_deferred_connection_string = os.environ.get( - 'erp5_sql_deferred_connection_string', - os.environ.get('erp5_sql_connection_string')) - if erp5_sql_deferred_connection_string: - extra_constructor_kw['erp5_sql_deferred_connection_string'] = \ - erp5_sql_deferred_connection_string + extra_constructor_kw = _getConnectionStringDict() email_from_address = os.environ.get('email_from_address') if email_from_address is not None: extra_constructor_kw['email_from_address'] = email_from_address @@ -517,6 +563,8 @@ def setupERP5Site( business_template_list=(), ZopeTestCase._print(f.getvalue()) f.close() failed_portal_installation[portal_name] = 1 + ZopeTestCase._print('Ran Unit test of %s (installation failed)\n' + % title) # run_unit_test depends on this string. raise def optimize(): diff --git a/product/ERP5Type/tests/custom_zodb.py b/product/ERP5Type/tests/custom_zodb.py index c80bcac617..b44e14b872 100644 --- a/product/ERP5Type/tests/custom_zodb.py +++ b/product/ERP5Type/tests/custom_zodb.py @@ -1,4 +1,11 @@ import ZODB +import os from ZODB.DemoStorage import DemoStorage +from ZODB.FileStorage import FileStorage + +data_fs_path = os.environ.get('erp5_tests_data_fs_path') +if data_fs_path: + Storage = DemoStorage(base=FileStorage(data_fs_path), quota=(1<<20)) +else: + Storage = DemoStorage(quota=(1<<20)) -Storage = DemoStorage(quota=(1<<20)) diff --git a/product/ERP5Type/tests/runUnitTest.py b/product/ERP5Type/tests/runUnitTest.py index 2a143aaf25..7d1108971f 100755 --- a/product/ERP5Type/tests/runUnitTest.py +++ b/product/ERP5Type/tests/runUnitTest.py @@ -10,6 +10,15 @@ usage: %(program)s [options] [UnitTest1[:TestClass1[:TestClass2]] [UnitTest2]] Options: -v, --verbose produce verbose output -h, --help this help screen + --portal_id=STRING force id of the portal. Usefull when using + --data_fs_path to run tests on an existing + Data.fs + --data_fs_path=STRING Path to the orginal Data.fs to run tests on an + existing environment. The Data.fs is openned read + only + --recreate_catalog=0 or 1 recreate the content of the sql catalog. Defaults + is to recreate, when using an existing Data.fs + --erp5_sql_connection_string=STRING ZSQL Connection string for erp5_sql_connection, by default, it will use "test test" @@ -177,7 +186,8 @@ def usage(stream, msg=None): def main(): try: opts, args = getopt.getopt(sys.argv[1:], - "hv", ["help", "verbose", "erp5_sql_connection_string=", + "hv", ["help", "verbose", "portal_id=", "data_fs_path=", + "recreate_catalog=", "erp5_sql_connection_string=", "cmf_activity_sql_connection_string=", "erp5_deferred_sql_connection_string=", "email_from_address="] ) @@ -185,12 +195,21 @@ def main(): usage(sys.stderr, msg) sys.exit(2) + os.environ["erp5_tests_recreate_catalog"] = "0" + for opt, arg in opts: if opt in ("-v", "--verbose"): os.environ['VERBOSE'] = "1" elif opt in ("-h", "--help"): usage(sys.stdout) sys.exit() + elif opt == '--portal_id': + os.environ["erp5_tests_portal_id"] = arg + elif opt == '--data_fs_path': + os.environ["erp5_tests_data_fs_path"] = arg + os.environ["erp5_tests_recreate_catalog"] = "1" + elif opt == '--recreate_catalog': + os.environ["erp5_tests_recreate_catalog"] = arg elif opt == "--erp5_sql_connection_string": os.environ["erp5_sql_connection_string"] = arg elif opt == "--cmf_activity_sql_connection_string": -- 2.30.9