Source

bloodhound-mq / t355 / t355_r1442601_trac_test_ticket_conversion.diff

Full commit
# HG changeset patch
# Parent 8e42500c130bb34e9a19ab940ba85afbfc762913
BH Multiproduct #355 : Rewrite Trac test cases for MIME conversions so as to run them against product environments

diff -r 8e42500c130b bloodhound_multiproduct/multiproduct/env.py
--- a/bloodhound_multiproduct/multiproduct/env.py	Wed Feb 06 10:33:38 2013 +0000
+++ b/bloodhound_multiproduct/multiproduct/env.py	Thu Feb 07 03:23:00 2013 -0500
@@ -127,6 +127,7 @@
             with self.db_direct_transaction as db:
                 self.mpsystem._update_db_version(db, DB_VERSION)
 
+
 # replace trac.test.EnvironmentStub
 trac.test.EnvironmentStub = EnvironmentStub
 
@@ -337,7 +338,7 @@
         `None`, the component only gets activated if it is located in
         the `plugins` directory of the environment.
         """
-        if not self.parent.is_component_enabled(cls):
+        if self.parent[cls] is None:
             return False
         return self.is_component_enabled_local(cls)
 
diff -r 8e42500c130b bloodhound_multiproduct/tests/env.py
--- a/bloodhound_multiproduct/tests/env.py	Wed Feb 06 10:33:38 2013 +0000
+++ b/bloodhound_multiproduct/tests/env.py	Thu Feb 07 03:23:00 2013 -0500
@@ -38,6 +38,8 @@
 from trac.env import Environment
 from trac.test import EnvironmentStub
 from trac.tests.env import EnvironmentTestCase
+from trac.ticket.report import ReportModule
+from trac.ticket.web_ui import TicketModule
 
 from multiproduct.api import MultiProductSystem
 from multiproduct.env import ProductEnvironment
@@ -159,6 +161,8 @@
     def _upgrade_mp(self, env):
         r"""Apply multi product upgrades
         """
+        env.disable_component(TicketModule)
+        env.disable_component(ReportModule)
         self.mpsystem = MultiProductSystem(env)
         try:
             self.mpsystem.upgrade_environment(env.db_transaction)
@@ -303,47 +307,61 @@
 
         global_env = self.env
         product_env = self.product_env
-        
+
         def clear_component_rules(env):
             del env._rules
             env.enabled.clear()
 
-        # C initially disabled in both envs
-        self.assertFalse(global_env.is_component_enabled(C))
-        self.assertFalse(product_env.is_component_enabled_local(C))
-        self.assertIs(global_env[C], None)
-        self.assertIs(product_env[C], None)
+        def _test_component_enabled(cls):
+            is_in_trac = global_env._component_name(cls).startswith('trac.')
 
-        clear_component_rules(global_env)
-        clear_component_rules(product_env)
+            # cls initially disabled in both envs
+            global_env.disable_component(cls)
+            product_env.disable_component(cls)
+            self.assertEquals(global_env.is_component_enabled(cls), 
+                    True if is_in_trac else None)
+            self.assertEquals(product_env.is_component_enabled_local(cls),
+                    True if is_in_trac else None)
+            self.assertIs(global_env[cls], None)
+            self.assertIs(product_env[cls], None)
 
-        # C enabled in product env but not in global env
-        product_env.enable_component(C)
-        self.assertFalse(global_env.is_component_enabled(C))
-        self.assertTrue(product_env.is_component_enabled_local(C))
-        self.assertIs(global_env[C], None)
-        self.assertIs(product_env[C], None)
+            clear_component_rules(global_env)
+            clear_component_rules(product_env)
 
-        clear_component_rules(global_env)
-        clear_component_rules(product_env)
+            # cls enabled in product env but not in global env
+            global_env.disable_component(cls)
+            product_env.enable_component(cls)
+            self.assertFalse(global_env.is_component_enabled(cls))
+            self.assertTrue(product_env.is_component_enabled_local(cls))
+            self.assertIs(global_env[cls], None)
+            self.assertIs(product_env[cls], None)
 
-        # C enabled in both envs
-        product_env.enable_component(C)
-        global_env.enable_component(C)
-        self.assertTrue(global_env.is_component_enabled(C))
-        self.assertTrue(product_env.is_component_enabled_local(C))
-        self.assertIsNot(global_env[C], None)
-        self.assertIsNot(product_env[C], None)
+            clear_component_rules(global_env)
+            clear_component_rules(product_env)
 
-        clear_component_rules(global_env)
-        clear_component_rules(product_env)
+            # cls enabled in both envs
+            global_env.enable_component(cls)
+            product_env.enable_component(cls)
+            self.assertTrue(global_env.is_component_enabled(cls))
+            self.assertTrue(product_env.is_component_enabled_local(cls))
+            self.assertIsNot(global_env[cls], None)
+            self.assertIsNot(product_env[cls], None)
 
-        # C enabled in global env but not in product env
-        global_env.enable_component(C)
-        self.assertTrue(global_env.is_component_enabled(C))
-        self.assertFalse(product_env.is_component_enabled_local(C))
-        self.assertIsNot(global_env[C], None)
-        self.assertIs(product_env[C], None)
+            clear_component_rules(global_env)
+            clear_component_rules(product_env)
+
+            # cls enabled in global env but not in product env
+            global_env.enable_component(cls)
+            product_env.disable_component(cls)
+            self.assertTrue(global_env.is_component_enabled(cls))
+            self.assertFalse(product_env.is_component_enabled_local(cls))
+            self.assertIsNot(global_env[cls], None)
+            self.assertIs(product_env[cls], None)
+
+        # Test the rules against custom , external component
+        _test_component_enabled(C)
+        # Test the rules against Trac component class
+        _test_component_enabled(TicketModule)
 
     def test_path(self):
         """Testing env.path"""
diff -r 8e42500c130b bloodhound_multiproduct/tests/ticket/conversion.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bloodhound_multiproduct/tests/ticket/conversion.py	Thu Feb 07 03:23:00 2013 -0500
@@ -0,0 +1,84 @@
+
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+
+"""Tests for Apache(TM) Bloodhound's MIME conversions for tickets in
+product environments"""
+
+import os.path
+import unittest
+
+from trac.test import Mock
+from trac.mimeview.api import Mimeview
+from trac.ticket.tests.conversion import TicketConversionTestCase
+from trac.web.href import Href
+
+from multiproduct.ticket.web_ui import ProductTicketModule
+from multiproduct.env import ProductEnvironment
+from tests.env import MultiproductTestCase
+
+class ProductTicketConversionTestCase(TicketConversionTestCase, \
+        MultiproductTestCase):
+
+    def setUp(self):
+        self._mp_setup()
+        self.global_env = self.env
+        self.env = ProductEnvironment(self.global_env, self.default_product)
+
+        self.env.config.set('trac', 'templates_dir',
+                            os.path.join(os.path.dirname(self.env.path),
+                                         'templates'))
+        self.ticket_module = ProductTicketModule(self.env)
+        self.mimeview = Mimeview(self.env)
+        self.req = Mock(base_path='/trac.cgi', path_info='',
+                        href=Href('/trac.cgi'), chrome={'logo': {}},
+                        abs_href=Href('http://example.org/trac.cgi'),
+                        environ={}, perm=[], authname='-', args={}, tz=None,
+                        locale='', session=None, form_token=None)
+
+    def test_csv_conversion(self):
+        ticket = self._create_a_ticket()
+        csv = self.mimeview.convert_content(self.req, 'trac.ticket.Ticket',
+                                            ticket, 'csv')
+        self.assertEqual(('\xef\xbb\xbf'
+                          'id,summary,reporter,owner,description,status,'
+                          'product,keywords,cc\r'
+                          '\n1,Foo,santa,,Bar,,,,\r\n',
+                          'text/csv;charset=utf-8', 'csv'), csv)
+
+    def test_tab_conversion(self):
+        ticket = self._create_a_ticket()
+        csv = self.mimeview.convert_content(self.req, 'trac.ticket.Ticket',
+                                            ticket, 'tab')
+        self.assertEqual(('\xef\xbb\xbf'
+                          'id\tsummary\treporter\towner\tdescription\tstatus\t'
+                          'product\tkeywords\tcc\r\n'
+                          '1\tFoo\tsanta\t\tBar\t\t\t\t\r\n',
+                          'text/tab-separated-values;charset=utf-8', 'tsv'),
+                         csv)
+
+    def tearDown(self):
+        self.global_env.reset_db()
+
+def test_suite():
+    return unittest.TestSuite([
+            unittest.makeSuite(ProductTicketConversionTestCase,'test'),
+        ])
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')
+