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
from ZTUtils import make_query
# XXX make sure that get_request works.
current_app = None
import Products.ERP5Type.Utils
from Products.ERP5Type import Globals
......@@ -34,23 +33,24 @@ from Products.ERP5Type import Globals
original_get_request = Globals.get_request
convertToUpperCase = Products.ERP5Type.Utils.convertToUpperCase
from Testing.ZopeTestCase.connections import registry
def get_context():
if registry:
return registry._conns[-1]
def get_request():
request = original_get_request()
if request is not None:
return request
current_app = get_context()
if current_app is not None:
return current_app.REQUEST
else:
return None
Products.ERP5Type.Utils.get_request = get_request
Globals.get_request = get_request
try:
import itools.zope
def get_context():
return current_app
itools.zope.get_context = get_context
except ImportError:
pass
......@@ -315,10 +315,15 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
def getPortal(self):
"""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()]
# FIXME: Try not to run this call below so often by moving it somewhere
# where it is called exactly once per test.
# 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)
self.REQUEST = portal.REQUEST
setSite(portal)
......@@ -525,9 +530,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
erp5_catalog_storage=erp5_catalog_storage,
use_dummy_mail_host=use_dummy_mail_host)
PortalTestCase.setUp(self)
global current_app
current_app = self.app
self._updateConnectionStrings()
def afterSetUp(self):
'''Called after setUp() has completed. This is
......@@ -551,15 +553,11 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
def _updateConnectionStrings(self):
"""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
for connection_string_name, connection_string in\
_getConnectionStringDict().items():
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):
"""Replace Original Mail Host by Dummy Mail Host.
......@@ -833,7 +831,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
def setUpERP5Site(self,
business_template_list=(),
app=None,
quiet=0,
light_install=1,
create_activities=1,
......@@ -852,17 +849,12 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
raise SetupSiteError(
'Installation of %s already failed, giving up' % portal_name)
try:
if app is None:
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
self.app = app = self._app()
app.test_portal_name = portal_name
global setup_done
if not (hasattr(aq_base(app), portal_name) and
setup_done.get(tuple(business_template_list))):
portal = app._getOb(portal_name, None)
if portal is None or not setup_done.get(tuple(business_template_list)):
setup_done[tuple(business_template_list)] = 1
business_template_list = \
self._getBTPathAndIdList(business_template_list)
......@@ -882,7 +874,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
setattr(app, 'isIndexable', 0)
reindex = 0
portal = getattr(app, portal_name, None)
if portal is None:
if not quiet:
ZopeTestCase._print('Adding %s ERP5 Site ... ' % portal_name)
......@@ -903,19 +894,12 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
reindex=reindex,
create_activities=create_activities,
**extra_constructor_kw )
portal = app[portal_name]
if not quiet:
ZopeTestCase._print('done (%.3fs)\n' % (time.time() - _start))
# Release locks
transaction.commit()
self.portal = portal
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)
self.portal = portal = self.getPortal()
if len(setup_done) == 1: # make sure it is run only once
try:
......@@ -947,8 +931,6 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
if not quiet:
ZopeTestCase._print('Executing setUpOnce ... ')
start = time.time()
# setUpOnce method may use self.app and self.portal
self.app = app
setup_once()
if not quiet:
ZopeTestCase._print('done (%.3fs)\n' % (time.time() - start))
......@@ -977,6 +959,7 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
raise
else:
transaction.commit()
del self.portal, self.app
ZopeTestCase.close(app)
except:
f = StringIO()
......
......@@ -131,6 +131,8 @@ class ProcessingNodeTestCase(backportUnittest.TestCase, ZopeTestCase.TestCase):
def tic(self, verbose=0):
"""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
if 1:
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