Commits

Olemis Lang committed bfd998b

TracDutest : Migrate environment stub class from trachacks:TracGVizPlugin

Comments (0)

Files changed (2)

trac-dev/testing/tracdutest/__init__.py

 import tempfile
 
 #------------------------------------------------------
+#    Trac environments used for testing purposes
+#------------------------------------------------------
+
+class EnvironmentStub(EnvironmentStub):
+    r"""Enhanced stub of the trac.env.Environment object for testing.
+    """
+
+    # Dont break lazy evaluation. Otherwise RPC calls misteriously fail.
+    @property
+    def _abs_href(self):
+        return self.abs_href
+
+    def enable_component(self, clsdef):
+        r"""Enable a plugin temporarily at testing time.
+        """
+        self.config.set('components', clsdef, 'enabled')
+        self.enabled.clear()
+        try:
+            del self._rules
+        except AttributeError:
+            pass
+
+    def disable_component(self, clsdef):
+        r"""Disable a plugin temporarily at testing time.
+        """
+        self.config.set('components', clsdef, 'disabled')
+        self.enabled.clear()
+        try:
+            del self._rules
+        except AttributeError:
+            pass
+
+    def rip_component(self, cls):
+        r"""Disable a plugin forever and RIP it using the super-laser beam.
+        """
+        self.disable_component(cls)
+        for reg in ComponentMeta._registry.itervalues():
+            try:
+                reg.remove(cls)
+            except ValueError :
+                pass
+
+    if not hasattr(EnvironmentStub, 'reset_db'):
+
+        # Copycat trac.test.EnvironmentStub.reset_db (Trac=0.11.5)
+        def reset_db(self, default_data=None):
+            r"""Remove all data from Trac tables, keeping the tables themselves.
+            :param default_data: after clean-up, initialize with default data
+            :return: True upon success
+            """
+            from trac import db_default
+
+            db = self.get_db_cnx()
+            db.rollback() # make sure there's no transaction in progress
+            cursor = db.cursor()
+
+            defdata = list(db_default.get_data(db))
+
+            for table, cols, vals in defdata:
+                cursor.execute("DELETE FROM %s" % (table,) )
+
+            # Delete tickets as well
+            cursor.execute("DELETE FROM ticket")
+            db.commit()
+
+            if default_data:
+                for table, cols, vals in defdata:
+                    cursor.executemany("INSERT INTO %s (%s) VALUES (%s)"
+                                       % (table, ','.join(cols),
+                                          ','.join(['%s' for c in cols])),
+                                       vals)
+            else:
+                cursor.execute("INSERT INTO system (name, value) "
+                               "VALUES (%s, %s)",
+                               ('database_version', str(db_default.db_version)))
+            db.commit()
+
+#------------------------------------------------------
 #    Minimalistic testing framework for Trac
 #------------------------------------------------------
 

trac-dev/testing/tracdutest/rpc.py

             globs = self.globalns
             for rpch in self.rpcsys.method_handlers :
                 self.env.log.debug('Matching RPC namespace %s %s', rpcns, \
-                                    rpch.xmlrpc_namespace() )
-              if rpch.xmlrpc_namespace() == rpcns :
-                  globs['rpcobj'] = RpcDirectProxy(rpch, globs['req'])
-                  globs['auth_rpcobj'] = RpcDirectProxy(rpch, globs['auth_req'])
-                  break
+                                   rpch.xmlrpc_namespace() )
+                if rpch.xmlrpc_namespace() == rpcns :
+                    globs['rpcobj'] = RpcDirectProxy(rpch, globs['req'])
+                    globs['auth_rpcobj'] = RpcDirectProxy(rpch, globs['auth_req'])
+                    break
             else :
                   raise RuntimeError('Cannot load RPC handler for %s' % rpcns)