trac-ticketlinks / trac /

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# Copyright (C) 2003-2005 Edgewall Software
# Copyright (C) 2003-2005 Jonas Borgström <>
# Copyright (C) 2005 Christopher Lenz <>
# 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
# This software consists of voluntary contributions made by many
# individuals. For exact contribution history, see the revision
# history and logs, available at
# Author: Jonas Borgström <>
#         Christopher Lenz <>

from trac.core import ComponentManager
from trac.db import SQLiteConnection

import unittest

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')

    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'
    '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 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
        for table in schema:
            for stmt in SQLiteConnection.to_sql(table):


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

    def __init__(self, default_data=False, enable=None):
        self.enabled_components = enable
        self.db = InMemoryDatabase()

        from trac.config import Configuration
        self.config = Configuration(None)

        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('')

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

    def component_activated(self, component):
        component.env = self
        component.config = self.config
        component.log = self.log

    def is_component_enabled(self, cls):
        if self.enabled_components is None:
            return True
        return cls in self.enabled_components

    def get_db_cnx(self):
        return self.db

def suite():
    import trac.tests
    import trac.scripts.tests
    import trac.ticket.tests
    import trac.versioncontrol.tests
    import trac.web.tests

    suite = unittest.TestSuite()

    return suite

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