1. Alex Willmer
  2. trac-ticketlinks


trac-ticketlinks / trac / test.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2003-2006 Edgewall Software
# Copyright (C) 2003-2005 Jonas Borgström <jonas@edgewall.com>
# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
# All rights reserved.
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
# Author: Jonas Borgström <jonas@edgewall.com>
#         Christopher Lenz <cmlenz@gmx.de>

import os
import unittest
import sys
import pkg_resources
from fnmatch import fnmatch

from trac.config import Configuration
from trac.core import Component, ComponentManager, ExtensionPoint
from trac.env import Environment
from trac.db.sqlite_backend import SQLiteConnection
from trac.ticket.default_workflow import load_workflow_config_snippet

def Mock(bases=(), *initargs, **kw):
    Simple factory for dummy classes that can be used as replacement for the 
    real implementation in tests.
    Base classes for the mock can be specified using the first parameter, which
    must be either a tuple of class objects or a single class object. If the
    bases parameter is omitted, the base class of the mock will be object.

    So to create a mock that is derived from the builtin dict type, you can do:

    >>> mock = Mock(dict)
    >>> mock['foo'] = 'bar'
    >>> mock['foo']

    Attributes of the class are provided by any additional keyword parameters.

    >>> mock = Mock(foo='bar')
    >>> mock.foo

    Objects produces by this function have the special feature of not requiring
    the 'self' parameter on methods, because you should keep data at the scope
    of the test function. So you can just do:

    >>> mock = Mock(add=lambda x,y: x+y)
    >>> mock.add(1, 1)

    To access attributes from the mock object from inside a lambda function,
    just access the mock itself:

    >>> mock = Mock(dict, do=lambda x: 'going to the %s' % mock[x])
    >>> mock['foo'] = 'bar'
    >>> mock.do('foo')
    'going to the bar'

    Because assignments or other types of statements don't work in lambda
    functions, assigning to a local variable from a mock function requires some
    extra work:

    >>> myvar = [None]
    >>> mock = Mock(set=lambda x: myvar.__setitem__(0, x))
    >>> mock.set(1)
    >>> myvar[0]
    if not isinstance(bases, tuple):
        bases = (bases,)
    cls = type('Mock', bases, {})
    mock = cls(*initargs)
    for k,v in kw.items():
        setattr(mock, k, v)
    return mock

class MockPerm(object):
    """Fake permission class. Necessary as Mock can not be used with operator
    def has_permission(self, x):
        return True
    __contains__ = has_permission

    def __call__(self, *a, **kw):
        return self

    def require(self, *a, **kw):
    assert_permission = require

class TestSetup(unittest.TestSuite):
    Test suite decorator that allows a fixture to be setup for a complete
    suite of test cases.
    def setUp(self):

    def tearDown(self):

    def __call__(self, result):
        unittest.TestSuite.__call__(self, result)
        return result

class InMemoryDatabase(SQLiteConnection):
    DB-API connection object for an SQLite in-memory database, containing all
    the default Trac tables but no data.
    def __init__(self):
        SQLiteConnection.__init__(self, ':memory:')
        cursor = self.cnx.cursor()

        from trac.db_default import schema
        from trac.db.sqlite_backend import _to_sql
        for table in schema:
            for stmt in _to_sql(table):


class EnvironmentStub(Environment):
    """A stub of the trac.env.Environment object for testing."""

    href = abs_href = None

    def __init__(self, default_data=False, enable=None):
        """Construct a new Envuronment stub object.

        default_data: If True, populate the database with some defaults.
        enable: A list of component classes or name globs to activate in the
                stub environment.
        self.enabled_components = enable or ['trac.*']
        self.db = InMemoryDatabase()
        self.systeminfo = [('Python', sys.version)]

        import trac
        self.path = os.path.dirname(trac.__file__)
        if not os.path.isabs(self.path):
            self.path = os.path.join(os.getcwd(), self.path)

        self.config = Configuration(None)
        # We have to have a ticket-workflow config for ''lots'' of things to
        # work.  So insert the basic-workflow config here.  There may be a
        # better solution than this.
        load_workflow_config_snippet(self.config, 'basic-workflow.ini')

        from trac.log import logger_factory
        self.log = logger_factory('test')

        from trac.web.href import Href
        self.href = Href('/trac.cgi')
        self.abs_href = Href('http://example.org/trac.cgi')

        from trac import db_default
        if default_data:
            cursor = self.db.cursor()
            for table, cols, vals in db_default.get_data(self.db):
                cursor.executemany("INSERT INTO %s (%s) VALUES (%s)"
                                   % (table, ','.join(cols),
                                      ','.join(['%s' for c in cols])),
        self.known_users = []

    def is_component_enabled(self, cls):
        for component in self.enabled_components:
            if component is cls:
                return True
            if isinstance(component, basestring) and \
                fnmatch(cls.__module__ + '.' + cls.__name__, component):
                return True
        return False

    def get_db_cnx(self):
        return self.db

    def get_known_users(self, db):
        return self.known_users

def locate(fn):
    """Locates a binary on the path.

    Returns the fully-qualified path, or None.
    import os
    exec_suffix = os.name == 'nt' and '.exe' or ''
    for p in ["."] + os.environ['PATH'].split(os.pathsep):
        f = os.path.join(p, fn + exec_suffix)
        if os.path.exists(f):
            return f
    return None

def suite():
    import trac.tests
    import trac.admin.tests
    import trac.db.tests
    import trac.mimeview.tests
    import trac.ticket.tests
    import trac.util.tests
    import trac.versioncontrol.tests
    import trac.versioncontrol.web_ui.tests
    import trac.web.tests
    import trac.wiki.tests

    suite = unittest.TestSuite()

    return suite

if __name__ == '__main__':
    import doctest, sys