From f213618316d46d90de37a4b83336abdc84bd3e06 Mon Sep 17 00:00:00 2001
From: Leonardo Rochael Almeida <leonardo@nexedi.com>
Date: Thu, 21 Jan 2010 13:20:42 +0000
Subject: [PATCH] Better test isolation. Fix regressions in TestCRMMailSend
 when all test classes are run toghether

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@31873 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/tests/testCRM.py | 142 ++++++++++++++++++----------------
 1 file changed, 74 insertions(+), 68 deletions(-)

diff --git a/product/ERP5/tests/testCRM.py b/product/ERP5/tests/testCRM.py
index ea7b692e97..9c7eddc2c6 100644
--- a/product/ERP5/tests/testCRM.py
+++ b/product/ERP5/tests/testCRM.py
@@ -44,8 +44,42 @@ TEST_HOME = os.path.dirname(__file__)
 def openTestFile(filename):
   return file(os.path.join(TEST_HOME, 'test_data', 'crm_emails', filename))
 
+clear_module_names = """
+campaign_module
+event_module
+meeting_module
+organisation_module
+person_module
+sale_opportunity_module
+""".strip().splitlines()
 
-class TestCRM(ERP5TypeTestCase):
+class BaseTestCRM(ERP5TypeTestCase):
+
+  def afterSetUp(self):
+    super(BaseTestCRM, self).afterSetUp()
+    # add a dummy mailhost not to send real messages
+    self.oldMailHost = getattr(self.portal, 'MailHost', None)
+    if self.oldMailHost is not None:
+      self.portal.manage_delObjects(['MailHost'])
+      self.portal._setObject('MailHost', DummyMailHost('MailHost'))
+
+  def beforeTearDown(self):
+    transaction.abort()
+    self.getActivityTool().manageClearActivities()
+    # restore the original MailHost
+    if self.oldMailHost is not None:
+      self.portal.manage_delObjects(['MailHost'])
+      self.portal._setObject('MailHost', DummyMailHost('MailHost'))
+    # clear modules if necessary
+    for module_name in clear_module_names:
+      module = getattr(self.portal, module_name)
+      module.manage_delObjects(list(module.objectIds()))
+
+    transaction.commit()
+    self.tic()
+    super(BaseTestCRM, self).beforeTearDown()
+
+class TestCRM(BaseTestCRM):
   def getTitle(self):
     return "CRM"
 
@@ -331,7 +365,7 @@ class TestCRM(ERP5TypeTestCase):
       self.assertEqual(new_event.getTitle(), 'Re: Event Title')
 
 
-class TestCRMMailIngestion(ERP5TypeTestCase):
+class TestCRMMailIngestion(BaseTestCRM):
   """Test Mail Ingestion for standalone CRM.
   """
   def getTitle(self):
@@ -346,46 +380,34 @@ class TestCRMMailIngestion(ERP5TypeTestCase):
             )
 
   def afterSetUp(self):
+    super(TestCRMMailIngestion, self).afterSetUp()
     portal = self.portal
 
     # create customer organisation and person
-    if 'customer' not in portal.organisation_module.objectIds():
-      portal.organisation_module.newContent(
-              id='customer',
-              portal_type='Organisation',
-              title='Customer')
+    portal.organisation_module.newContent(
+            id='customer',
+            portal_type='Organisation',
+            title='Customer')
     customer_organisation = portal.organisation_module.customer
-    if 'sender' not in portal.person_module.contentIds():
-      portal.person_module.newContent(
-              id='sender',
-              title='Sender',
-              subordination_value=customer_organisation,
-              default_email_text='sender@customer.com')
+    portal.person_module.newContent(
+            id='sender',
+            title='Sender',
+            subordination_value=customer_organisation,
+            default_email_text='sender@customer.com')
     # also create the recipients
-    if 'me' not in portal.person_module.contentIds():
-      portal.person_module.newContent(
-              id='me',
-              title='Me',
-              default_email_text='me@erp5.org')
-    if 'he' not in portal.person_module.contentIds():
-      portal.person_module.newContent(
-              id='he',
-              title='He',
-              default_email_text='he@erp5.org')
+    portal.person_module.newContent(
+            id='me',
+            title='Me',
+            default_email_text='me@erp5.org')
+    portal.person_module.newContent(
+            id='he',
+            title='He',
+            default_email_text='he@erp5.org')
 
     # make sure customers are available to catalog
     transaction.commit()
     self.tic()
 
-  def beforeTearDown(self):
-    transaction.abort()
-    # clear modules if necessary
-    for module in (self.portal.event_module,
-                   self.portal.campaign_module):
-      module.manage_delObjects(list(module.objectIds()))
-    transaction.commit()
-    self.tic()
-
   def _readTestData(self, filename):
     """read test data from data directory."""
     return file(os.path.join(os.path.dirname(__file__),
@@ -604,7 +626,7 @@ class TestCRMMailIngestion(ERP5TypeTestCase):
 ##    event = self._ingestMail('with_attachements')
 ##
 
-class TestCRMMailSend(ERP5TypeTestCase):
+class TestCRMMailSend(BaseTestCRM):
   """Test Mail Sending for CRM
   """
   def getTitle(self):
@@ -622,31 +644,29 @@ class TestCRMMailSend(ERP5TypeTestCase):
             )
 
   def afterSetUp(self):
+    super(TestCRMMailSend, self).afterSetUp()
     portal = self.portal
 
     # create customer organisation and person
-    if 'customer' not in portal.organisation_module.objectIds():
-      portal.organisation_module.newContent(
-              id='customer',
-              portal_type='Organisation',
-              title='Customer')
+    portal.organisation_module.newContent(
+            id='customer',
+            portal_type='Organisation',
+            title='Customer')
     customer_organisation = portal.organisation_module.customer
-    if 'recipient' not in portal.person_module.contentIds():
-      portal.person_module.newContent(
-              id='recipient',
-              # The ',' below is to force quoting of the name in e-mail
-              # addresses on Zope 2.12
-              title='Recipient,',
-              subordination_value=customer_organisation,
-              default_email_text='recipient@example.com')
-    if 'me' not in portal.person_module.contentIds():
-      # also create the sender
-      portal.person_module.newContent(
-              id='me',
-              # The ',' below is to force quoting of the name in e-mail
-              # addresses on Zope 2.12
-              title='Me,',
-              default_email_text='me@erp5.org')
+    portal.person_module.newContent(
+            id='recipient',
+            # The ',' below is to force quoting of the name in e-mail
+            # addresses on Zope 2.12
+            title='Recipient,',
+            subordination_value=customer_organisation,
+            default_email_text='recipient@example.com')
+    # also create the sender
+    portal.person_module.newContent(
+            id='me',
+            # The ',' below is to force quoting of the name in e-mail
+            # addresses on Zope 2.12
+            title='Me,',
+            default_email_text='me@erp5.org')
 
     # set preference
     default_pref = self.portal.portal_preferences.default_site_preference
@@ -657,24 +677,10 @@ class TestCRMMailSend(ERP5TypeTestCase):
     if default_pref.getPreferenceState() == 'disabled':
       default_pref.enable()
 
-    # add a dummy mailhost not to send real messages
-    if 'MailHost' in self.portal.objectIds():
-      self.portal.manage_delObjects(['MailHost'])
-      self.portal._setObject('MailHost', DummyMailHost('MailHost'))
-
     # make sure customers are available to catalog
     transaction.commit()
     self.tic()
 
-  def beforeTearDown(self):
-    transaction.abort()
-    # clear modules if necessary
-    for module in (self.portal.event_module,
-                   self.portal.campaign_module,):
-      module.manage_delObjects(list(module.objectIds()))
-    transaction.commit()
-    self.tic()
-
   def test_MailFromMailMessageEvent(self):
     # passing start_action transition on event workflow will send an email to the
     # person as destination
-- 
2.30.9