Mercurial > genshi > mirror
comparison examples/trac/trac/test.py @ 39:93b4dcbafd7b trunk
Copy Trac to main branch.
author | cmlenz |
---|---|
date | Mon, 03 Jul 2006 18:53:27 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
38:ee669cb9cccc | 39:93b4dcbafd7b |
---|---|
1 #!/usr/bin/env python | |
2 # -*- coding: utf-8 -*- | |
3 # | |
4 # Copyright (C) 2003-2005 Edgewall Software | |
5 # Copyright (C) 2003-2005 Jonas Borgström <jonas@edgewall.com> | |
6 # Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de> | |
7 # All rights reserved. | |
8 # | |
9 # This software is licensed as described in the file COPYING, which | |
10 # you should have received as part of this distribution. The terms | |
11 # are also available at http://trac.edgewall.com/license.html. | |
12 # | |
13 # This software consists of voluntary contributions made by many | |
14 # individuals. For exact contribution history, see the revision | |
15 # history and logs, available at http://projects.edgewall.com/trac/. | |
16 # | |
17 # Author: Jonas Borgström <jonas@edgewall.com> | |
18 # Christopher Lenz <cmlenz@gmx.de> | |
19 | |
20 import unittest | |
21 | |
22 from trac.core import Component, ComponentManager, ExtensionPoint | |
23 from trac.env import Environment | |
24 from trac.db.sqlite_backend import SQLiteConnection | |
25 | |
26 | |
27 def Mock(bases=(), *initargs, **kw): | |
28 """ | |
29 Simple factory for dummy classes that can be used as replacement for the | |
30 real implementation in tests. | |
31 | |
32 Base classes for the mock can be specified using the first parameter, which | |
33 must be either a tuple of class objects or a single class object. If the | |
34 bases parameter is omitted, the base class of the mock will be object. | |
35 | |
36 So to create a mock that is derived from the builtin dict type, you can do: | |
37 | |
38 >>> mock = Mock(dict) | |
39 >>> mock['foo'] = 'bar' | |
40 >>> mock['foo'] | |
41 'bar' | |
42 | |
43 Attributes of the class are provided by any additional keyword parameters. | |
44 | |
45 >>> mock = Mock(foo='bar') | |
46 >>> mock.foo | |
47 'bar' | |
48 | |
49 Objects produces by this function have the special feature of not requiring | |
50 the 'self' parameter on methods, because you should keep data at the scope | |
51 of the test function. So you can just do: | |
52 | |
53 >>> mock = Mock(add=lambda x,y: x+y) | |
54 >>> mock.add(1, 1) | |
55 2 | |
56 | |
57 To access attributes from the mock object from inside a lambda function, | |
58 just access the mock itself: | |
59 | |
60 >>> mock = Mock(dict, do=lambda x: 'going to the %s' % mock[x]) | |
61 >>> mock['foo'] = 'bar' | |
62 >>> mock.do('foo') | |
63 'going to the bar' | |
64 | |
65 Because assignments or other types of statements don't work in lambda | |
66 functions, assigning to a local variable from a mock function requires some | |
67 extra work: | |
68 | |
69 >>> myvar = [None] | |
70 >>> mock = Mock(set=lambda x: myvar.__setitem__(0, x)) | |
71 >>> mock.set(1) | |
72 >>> myvar[0] | |
73 1 | |
74 """ | |
75 if not isinstance(bases, tuple): | |
76 bases = (bases,) | |
77 cls = type('Mock', bases, {}) | |
78 mock = cls(*initargs) | |
79 for k,v in kw.items(): | |
80 setattr(mock, k, v) | |
81 return mock | |
82 | |
83 | |
84 class TestSetup(unittest.TestSuite): | |
85 """ | |
86 Test suite decorator that allows a fixture to be setup for a complete | |
87 suite of test cases. | |
88 """ | |
89 def setUp(self): | |
90 pass | |
91 | |
92 def tearDown(self): | |
93 pass | |
94 | |
95 def __call__(self, result): | |
96 self.setUp() | |
97 unittest.TestSuite.__call__(self, result) | |
98 self.tearDown() | |
99 return result | |
100 | |
101 | |
102 class InMemoryDatabase(SQLiteConnection): | |
103 """ | |
104 DB-API connection object for an SQLite in-memory database, containing all | |
105 the default Trac tables but no data. | |
106 """ | |
107 def __init__(self): | |
108 SQLiteConnection.__init__(self, ':memory:') | |
109 cursor = self.cnx.cursor() | |
110 | |
111 from trac.db_default import schema | |
112 from trac.db.sqlite_backend import _to_sql | |
113 for table in schema: | |
114 for stmt in _to_sql(table): | |
115 cursor.execute(stmt) | |
116 | |
117 self.cnx.commit() | |
118 | |
119 | |
120 class EnvironmentStub(Environment): | |
121 """A stub of the trac.env.Environment object for testing.""" | |
122 | |
123 def __init__(self, default_data=False, enable=None): | |
124 ComponentManager.__init__(self) | |
125 Component.__init__(self) | |
126 self.enabled_components = enable | |
127 self.db = InMemoryDatabase() | |
128 | |
129 from trac.config import Configuration | |
130 self.config = Configuration(None) | |
131 | |
132 from trac.log import logger_factory | |
133 self.log = logger_factory('test') | |
134 | |
135 from trac.web.href import Href | |
136 self.href = Href('/trac.cgi') | |
137 self.abs_href = Href('http://example.org/trac.cgi') | |
138 | |
139 from trac import db_default | |
140 if default_data: | |
141 cursor = self.db.cursor() | |
142 for table, cols, vals in db_default.data: | |
143 cursor.executemany("INSERT INTO %s (%s) VALUES (%s)" | |
144 % (table, ','.join(cols), | |
145 ','.join(['%s' for c in cols])), | |
146 vals) | |
147 self.db.commit() | |
148 | |
149 self.known_users = [] | |
150 | |
151 def is_component_enabled(self, cls): | |
152 if self.enabled_components is None: | |
153 return True | |
154 return cls in self.enabled_components | |
155 | |
156 def get_db_cnx(self): | |
157 return self.db | |
158 | |
159 def get_templates_dir(self): | |
160 return None | |
161 | |
162 def get_known_users(self, db): | |
163 return self.known_users | |
164 | |
165 | |
166 def suite(): | |
167 import trac.tests | |
168 import trac.db.tests | |
169 import trac.mimeview.tests | |
170 import trac.scripts.tests | |
171 import trac.ticket.tests | |
172 import trac.util.tests | |
173 import trac.versioncontrol.tests | |
174 import trac.versioncontrol.web_ui.tests | |
175 import trac.web.tests | |
176 import trac.wiki.tests | |
177 | |
178 suite = unittest.TestSuite() | |
179 suite.addTest(trac.tests.suite()) | |
180 suite.addTest(trac.db.tests.suite()) | |
181 suite.addTest(trac.mimeview.tests.suite()) | |
182 suite.addTest(trac.scripts.tests.suite()) | |
183 suite.addTest(trac.ticket.tests.suite()) | |
184 suite.addTest(trac.util.tests.suite()) | |
185 suite.addTest(trac.versioncontrol.tests.suite()) | |
186 suite.addTest(trac.versioncontrol.web_ui.tests.suite()) | |
187 suite.addTest(trac.web.tests.suite()) | |
188 suite.addTest(trac.wiki.tests.suite()) | |
189 | |
190 return suite | |
191 | |
192 if __name__ == '__main__': | |
193 import doctest, sys | |
194 doctest.testmod(sys.modules[__name__]) | |
195 unittest.main(defaultTest='suite') |