bloodhound-mq / t355 / t355_r1442601_trac_test_env_cmp_enabled.diff

# HG changeset patch
# Parent 25f5af82c0ce83f1318165a3a2ffce8bdc4f705d
BH Multiproduct #355 : ProductEnvironment.is_conponent_enabled enhanced to cope with trac.* built-in components

diff -r 25f5af82c0ce bloodhound_multiproduct/multiproduct/config.py
--- a/bloodhound_multiproduct/multiproduct/config.py	Thu Feb 07 14:50:28 2013 +0000
+++ b/bloodhound_multiproduct/multiproduct/config.py	Fri Feb 08 03:21:30 2013 -0500
@@ -298,7 +298,7 @@
                 setting.delete()
             else:
                 # Update existing record
-                setting.value = value
+                setting._data['value'] = value
                 setting.update()
 
     # Helper methods
diff -r 25f5af82c0ce bloodhound_multiproduct/multiproduct/dbcursor.py
--- a/bloodhound_multiproduct/multiproduct/dbcursor.py	Thu Feb 07 14:50:28 2013 +0000
+++ b/bloodhound_multiproduct/multiproduct/dbcursor.py	Fri Feb 08 03:21:30 2013 -0500
@@ -65,6 +65,8 @@
                                                                  TRANSLATE_TABLES,
                                                                  PRODUCT_COLUMN,
                                                                  product_prefix)
+        if self.log:
+            self.log.debug('Original SQl: %s', sql)
         return self._translator.translate(sql) if (self._translator is not None) else sql
 
     def execute(self, sql, args=None):
diff -r 25f5af82c0ce bloodhound_multiproduct/multiproduct/env.py
--- a/bloodhound_multiproduct/multiproduct/env.py	Thu Feb 07 14:50:28 2013 +0000
+++ b/bloodhound_multiproduct/multiproduct/env.py	Fri Feb 08 03:21:30 2013 -0500
@@ -117,6 +117,42 @@
         except OperationalError:
             pass
 
+    @staticmethod
+    def enable_component_in_config(env, cls):
+        """Keep track of enabled state in configuration as well 
+        during test runs. This is closer to reality than 
+        inherited `enable_component` method.
+        """
+        env.config['components'].set(env._component_name(cls), 'enabled')
+        env.enabled.clear()
+        env.components.pop(cls, None)
+        try:
+            del env._rules
+        except AttributeError:
+            pass
+        # FIXME: Shall we ?
+        #env.config.save()
+
+    @staticmethod
+    def disable_component_in_config(env, component):
+        """Keep track of disabled state in configuration as well 
+        during test runs. This is closer to reality than 
+        inherited `disable_component` method.
+        """
+        if isinstance(component, type):
+            cls = component
+        else:
+            cls = component.__class__
+        env.config['components'].set(env._component_name(cls), 'disabled')
+        env.enabled.clear()
+        env.components.pop(cls, None)
+        try:
+            del env._rules
+        except AttributeError:
+            pass
+        # FIXME: Shall we ?
+        #env.config.save()
+
     def reset_db(self, default_data=None):
         from multiproduct.api import DB_VERSION
         schema_version = -1
@@ -127,6 +163,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 +374,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 25f5af82c0ce bloodhound_multiproduct/tests/config.py
--- a/bloodhound_multiproduct/tests/config.py	Thu Feb 07 14:50:28 2013 +0000
+++ b/bloodhound_multiproduct/tests/config.py	Fri Feb 08 03:21:30 2013 -0500
@@ -188,6 +188,12 @@
             self.assertEquals(u"Voilà l'été", config2.get('a', 'option2'))
         self._test_with_inherit(testcb)
 
+    def test_overwrite(self):
+        config = self._read()
+        config.set('a', 'option', 'value1')
+        self.assertEquals('value1', config.get('a', 'option'))
+        config.set('a', 'option', 'value2')
+        self.assertEquals('value2', config.get('a', 'option'))
 
 def test_suite():
     return unittest.makeSuite(ProductConfigTestCase,'test')
diff -r 25f5af82c0ce bloodhound_multiproduct/tests/env.py
--- a/bloodhound_multiproduct/tests/env.py	Thu Feb 07 14:50:28 2013 +0000
+++ b/bloodhound_multiproduct/tests/env.py	Fri Feb 08 03:21:30 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)
@@ -215,6 +219,13 @@
 
     def tearDown(self):
         # Release reference to transient environment mock object
+        if self.env is not None:
+            try:
+                self.env.reset_db()
+            except OperationalError:
+                # "Database not found ...",
+                # "OperationalError: no such table: system" or the like
+                pass
         self.env = None
         self.product_env = None
 
@@ -303,53 +314,99 @@
 
         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):
+            cname = global_env._component_name(cls)
+            disable_component_in_config = global_env.disable_component_in_config
+            enable_component_in_config = global_env.enable_component_in_config
 
-        clear_component_rules(global_env)
-        clear_component_rules(product_env)
+            # cls initially disabled in both envs
+            disable_component_in_config(global_env, cls)
+            disable_component_in_config(product_env, cls)
 
-        # 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)
+            expected_rules = {
+                    'multiproduct' : True,
+                    'trac' : True,
+                    'trac.db' : True,
+                    cname : False,
+                }
+            self.assertEquals(expected_rules, global_env._component_rules)
+            self.assertEquals(expected_rules, product_env._component_rules)
 
-        clear_component_rules(global_env)
-        clear_component_rules(product_env)
+            self.assertFalse(global_env.is_component_enabled(cls))
+            self.assertFalse(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)
+            # cls enabled in product env but not in global env
+            disable_component_in_config(global_env, cls)
+            enable_component_in_config(product_env, cls)
 
-        clear_component_rules(global_env)
-        clear_component_rules(product_env)
+            expected_rules[cname] = False
+            self.assertEquals(expected_rules, global_env._component_rules)
+            expected_rules[cname] = True
+            self.assertEquals(expected_rules, product_env._component_rules)
 
-        # 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)
+            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)
+
+            # cls enabled in both envs
+            enable_component_in_config(global_env, cls)
+            enable_component_in_config(product_env, cls)
+
+            expected_rules[cname] = True
+            self.assertEquals(expected_rules, global_env._component_rules)
+            expected_rules[cname] = True
+            self.assertEquals(expected_rules, product_env._component_rules)
+
+            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)
+
+            # cls enabled in global env but not in product env
+            enable_component_in_config(global_env, cls)
+            disable_component_in_config(product_env, cls)
+
+            expected_rules[cname] = True
+            self.assertEquals(expected_rules, global_env._component_rules)
+            expected_rules[cname] = False
+            self.assertEquals(expected_rules, product_env._component_rules)
+
+            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"""
         self.assertEqual(self.product_env.path, 
                 os.path.join(self.env.path, 'products', self.default_product))
 
+    def test_env_config_inheritance(self):
+        """Testing env.config"""
+        global_config = self.env.config
+        product_config = self.product_env.config
+
+        # By default inherit global settings ...
+        global_config['section'].set('key', 'value1')
+        self.assertEquals('value1', global_config['section'].get('key'))
+        self.assertEquals('value1', product_config['section'].get('key'))
+
+        # ... but allow for overrides in product scope
+        product_config['section'].set('key', 'value2')
+        self.assertEquals('value1', global_config['section'].get('key'))
+        self.assertEquals('value2', product_config['section'].get('key'))
+
+
 def test_suite():
     return unittest.TestSuite([
             unittest.makeSuite(ProductEnvTestCase,'test'),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.