Commit 39cc1f00 authored by Julien Muchembled's avatar Julien Muchembled

ERP5TypeTestCase: cleanup

- remove 'current_app' global variable
- clean initialization of self.app and self.portal

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@38638 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent c8003029
...@@ -26,7 +26,6 @@ from warnings import warn ...@@ -26,7 +26,6 @@ from warnings import warn
from ZTUtils import make_query from ZTUtils import make_query
# XXX make sure that get_request works. # XXX make sure that get_request works.
current_app = None
import Products.ERP5Type.Utils import Products.ERP5Type.Utils
from Products.ERP5Type import Globals from Products.ERP5Type import Globals
...@@ -34,23 +33,24 @@ from Products.ERP5Type import Globals ...@@ -34,23 +33,24 @@ from Products.ERP5Type import Globals
original_get_request = Globals.get_request original_get_request = Globals.get_request
convertToUpperCase = Products.ERP5Type.Utils.convertToUpperCase convertToUpperCase = Products.ERP5Type.Utils.convertToUpperCase
from Testing.ZopeTestCase.connections import registry
def get_context():
if registry:
return registry._conns[-1]
def get_request(): def get_request():
request = original_get_request() request = original_get_request()
if request is not None: if request is not None:
return request return request
current_app = get_context()
if current_app is not None: if current_app is not None:
return current_app.REQUEST return current_app.REQUEST
else:
return None
Products.ERP5Type.Utils.get_request = get_request Products.ERP5Type.Utils.get_request = get_request
Globals.get_request = get_request Globals.get_request = get_request
try: try:
import itools.zope import itools.zope
def get_context():
return current_app
itools.zope.get_context = get_context itools.zope.get_context = get_context
except ImportError: except ImportError:
pass pass
...@@ -315,10 +315,15 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase): ...@@ -315,10 +315,15 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
def getPortal(self): def getPortal(self):
"""Returns the portal object, i.e. the "fixture root". """Returns the portal object, i.e. the "fixture root".
It also does some initialization, as if the portal was accessed for the
first time for the current request.
For performance reason, this should be used in only 3 places:
'setUpERP5Site', 'tic' and 'PortalTestCase._portal'
""" """
portal = self.app[self.getPortalName()] portal = self.app[self.getPortalName()]
# FIXME: Try not to run this call below so often by moving it somewhere # Make sure skins are correctly set-up (it's not implicitly set up
# where it is called exactly once per test. # by Acquisition on Zope 2.12 as it is on 2.8)
portal.setupCurrentSkin(portal.REQUEST) portal.setupCurrentSkin(portal.REQUEST)
self.REQUEST = portal.REQUEST self.REQUEST = portal.REQUEST
setSite(portal) setSite(portal)
...@@ -525,9 +530,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase): ...@@ -525,9 +530,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
erp5_catalog_storage=erp5_catalog_storage, erp5_catalog_storage=erp5_catalog_storage,
use_dummy_mail_host=use_dummy_mail_host) use_dummy_mail_host=use_dummy_mail_host)
PortalTestCase.setUp(self) PortalTestCase.setUp(self)
global current_app
current_app = self.app
self._updateConnectionStrings()
def afterSetUp(self): def afterSetUp(self):
'''Called after setUp() has completed. This is '''Called after setUp() has completed. This is
...@@ -551,15 +553,11 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase): ...@@ -551,15 +553,11 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
def _updateConnectionStrings(self): def _updateConnectionStrings(self):
"""Update connection strings with values passed by the testRunner """Update connection strings with values passed by the testRunner
""" """
global current_app
if current_app is not None:
self.app = current_app
portal = self.getPortal()
# update connection strings # update connection strings
for connection_string_name, connection_string in\ for connection_string_name, connection_string in\
_getConnectionStringDict().items(): _getConnectionStringDict().items():
connection_name = connection_string_name.replace('_string', '') connection_name = connection_string_name.replace('_string', '')
getattr(portal, connection_name).edit('', connection_string) getattr(self.portal, connection_name).edit('', connection_string)
def _setUpDummyMailHost(self): def _setUpDummyMailHost(self):
"""Replace Original Mail Host by Dummy Mail Host. """Replace Original Mail Host by Dummy Mail Host.
...@@ -833,7 +831,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase): ...@@ -833,7 +831,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
def setUpERP5Site(self, def setUpERP5Site(self,
business_template_list=(), business_template_list=(),
app=None,
quiet=0, quiet=0,
light_install=1, light_install=1,
create_activities=1, create_activities=1,
...@@ -852,17 +849,12 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase): ...@@ -852,17 +849,12 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
raise SetupSiteError( raise SetupSiteError(
'Installation of %s already failed, giving up' % portal_name) 'Installation of %s already failed, giving up' % portal_name)
try: try:
if app is None: self.app = app = self._app()
app = ZopeTestCase.app()
# this app will be closed after setUp, but keep an reference anyway, to
# make it's REQUEST available during setup
global current_app
current_app = app
app.test_portal_name = portal_name app.test_portal_name = portal_name
global setup_done global setup_done
if not (hasattr(aq_base(app), portal_name) and portal = app._getOb(portal_name, None)
setup_done.get(tuple(business_template_list))): if portal is None or not setup_done.get(tuple(business_template_list)):
setup_done[tuple(business_template_list)] = 1 setup_done[tuple(business_template_list)] = 1
business_template_list = \ business_template_list = \
self._getBTPathAndIdList(business_template_list) self._getBTPathAndIdList(business_template_list)
...@@ -882,7 +874,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase): ...@@ -882,7 +874,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
setattr(app, 'isIndexable', 0) setattr(app, 'isIndexable', 0)
reindex = 0 reindex = 0
portal = getattr(app, portal_name, None)
if portal is None: if portal is None:
if not quiet: if not quiet:
ZopeTestCase._print('Adding %s ERP5 Site ... ' % portal_name) ZopeTestCase._print('Adding %s ERP5 Site ... ' % portal_name)
...@@ -903,19 +894,12 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase): ...@@ -903,19 +894,12 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
reindex=reindex, reindex=reindex,
create_activities=create_activities, create_activities=create_activities,
**extra_constructor_kw ) **extra_constructor_kw )
portal = app[portal_name]
if not quiet: if not quiet:
ZopeTestCase._print('done (%.3fs)\n' % (time.time() - _start)) ZopeTestCase._print('done (%.3fs)\n' % (time.time() - _start))
# Release locks # Release locks
transaction.commit() transaction.commit()
self.portal = portal self.portal = portal = self.getPortal()
setSite(portal)
# Make sure skins are correctly set-up (it's not implicitly set up
# by Acquisition on Zope 2.12 as it is on 2.8)
portal.setupCurrentSkin(portal.REQUEST)
if len(setup_done) == 1: # make sure it is run only once if len(setup_done) == 1: # make sure it is run only once
try: try:
...@@ -947,8 +931,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase): ...@@ -947,8 +931,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
if not quiet: if not quiet:
ZopeTestCase._print('Executing setUpOnce ... ') ZopeTestCase._print('Executing setUpOnce ... ')
start = time.time() start = time.time()
# setUpOnce method may use self.app and self.portal
self.app = app
setup_once() setup_once()
if not quiet: if not quiet:
ZopeTestCase._print('done (%.3fs)\n' % (time.time() - start)) ZopeTestCase._print('done (%.3fs)\n' % (time.time() - start))
...@@ -977,6 +959,7 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase): ...@@ -977,6 +959,7 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
raise raise
else: else:
transaction.commit() transaction.commit()
del self.portal, self.app
ZopeTestCase.close(app) ZopeTestCase.close(app)
except: except:
f = StringIO() f = StringIO()
......
...@@ -131,6 +131,8 @@ class ProcessingNodeTestCase(backportUnittest.TestCase, ZopeTestCase.TestCase): ...@@ -131,6 +131,8 @@ class ProcessingNodeTestCase(backportUnittest.TestCase, ZopeTestCase.TestCase):
def tic(self, verbose=0): def tic(self, verbose=0):
"""Execute pending activities""" """Execute pending activities"""
# Some tests like testDeferredStyle require that we use self.getPortal()
# instead of self.portal in order to setup current skin.
portal_activities = self.getPortal().portal_activities portal_activities = self.getPortal().portal_activities
if 1: if 1:
if verbose: if verbose:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment