1 |
# Copyright (c) 2002 by Intevation GmbH |
# Copyright (c) 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
46 |
import thubaninit |
import thubaninit |
47 |
_initthuban_done = 1 |
_initthuban_done = 1 |
48 |
|
|
49 |
def run_suite(suite): |
def run_tests(): |
50 |
"""Run the test suite suite and return the result""" |
"""Frontend for unittest.main that prints some additional debug information |
51 |
runner = unittest.TextTestRunner(verbosity = 2) |
|
52 |
return runner.run(suite) |
After calling unittest.main, run the garbage collector and print |
53 |
|
uncollected objects. Also print any un-unsubscribed messages. |
54 |
|
""" |
55 |
|
try: |
56 |
|
unittest.main() |
57 |
|
finally: |
58 |
|
# This has to be in a finally clause because unittest.main() |
59 |
|
# ends with a sys.exit to make sure that the process exits with |
60 |
|
# an appropriate exit code |
61 |
|
print_garbage_information() |
62 |
|
|
63 |
|
def print_garbage_information(): |
64 |
|
"""Print information about things that haven't been cleaned up. |
65 |
|
|
66 |
|
Run the garbage collector and print uncollected objects. Also print |
67 |
|
any un-unsubscribed messages. |
68 |
|
""" |
69 |
|
import gc, Thuban.Lib.connector |
70 |
|
gc.collect() |
71 |
|
if gc.garbage: |
72 |
|
print |
73 |
|
print "There are %d uncollected objects:" % len(gc.garbage) |
74 |
|
print gc.garbage |
75 |
|
Thuban.Lib.connector._the_connector.print_connections() |
76 |
|
|
77 |
|
|
78 |
def create_temp_dir(): |
def create_temp_dir(): |
106 |
def temp_dir(self): |
def temp_dir(self): |
107 |
"""Return the name of the directory for the temporary files""" |
"""Return the name of the directory for the temporary files""" |
108 |
return create_temp_dir() |
return create_temp_dir() |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
class FileLoadTestCase(unittest.TestCase, FileTestMixin): |
113 |
|
|
114 |
|
"""Base class for test case that test loading files. |
115 |
|
|
116 |
|
This base class provides some common infrastructure for testing the |
117 |
|
reading of files. |
118 |
|
|
119 |
|
Each test case should be its own class derived from this one. There |
120 |
|
is one file associated with each class. The contents are defined by |
121 |
|
the file_contents class attribute and its name by the filename |
122 |
|
method. |
123 |
|
|
124 |
|
Derived classes usually only have to provide appropriate values for |
125 |
|
the file_contents and file_extension class attributes. |
126 |
|
""" |
127 |
|
|
128 |
|
file_contents = None |
129 |
|
file_extension = "" |
130 |
|
|
131 |
|
def filename(self): |
132 |
|
"""Return the name of the test file to use. |
133 |
|
|
134 |
|
The default implementation simply calls self.volatile_file_name |
135 |
|
with a basename derived from the class name by stripping off a |
136 |
|
leading 'test_' and appending self.file_extension. |
137 |
|
""" |
138 |
|
name = self.__class__.__name__ |
139 |
|
if name.startswith("test_"): |
140 |
|
name = name[5:] |
141 |
|
return self.temp_file_name(name + self.file_extension) |
142 |
|
|
143 |
|
def setUp(self): |
144 |
|
"""Create the volatile file for the test. |
145 |
|
|
146 |
|
Write self.contents (which should be a string) to the file named |
147 |
|
by self.filename(). |
148 |
|
""" |
149 |
|
filename = self.filename() |
150 |
|
file = open(filename, "w") |
151 |
|
file.write(self.file_contents) |
152 |
|
file.close() |
153 |
|
|
154 |
|
|
155 |
|
class FloatComparisonMixin: |
156 |
|
|
157 |
|
""" |
158 |
|
Mixin class for tests comparing floating point numbers. |
159 |
|
|
160 |
|
This class provides a few methods for testing floating point |
161 |
|
operations. |
162 |
|
""" |
163 |
|
|
164 |
|
fp_epsilon = 1e-6 |
165 |
|
|
166 |
|
def assertFloatEqual(self, test, value): |
167 |
|
"""Assert equality of test and value with some tolerance. |
168 |
|
|
169 |
|
Assert that the absolute difference between test and value is |
170 |
|
less than self.fp_epsilon. |
171 |
|
""" |
172 |
|
self.assert_(self.fp_epsilon > abs(test - value), |
173 |
|
"abs(%g - %g) >= %g" % (test, value, self.fp_epsilon)) |
174 |
|
|
175 |
|
def assertFloatSeqEqual(self, test, value, epsilon = None): |
176 |
|
"""Assert equality of the sequences test and value with some tolerance. |
177 |
|
|
178 |
|
Assert that the absolute difference between each corresponding |
179 |
|
value in test and value is less than the optional parameter |
180 |
|
epsilon. If epsilon is not given use self.fp_epsilon. |
181 |
|
""" |
182 |
|
if epsilon is None: |
183 |
|
epsilon = self.fp_epsilon |
184 |
|
for i in range(len(test)): |
185 |
|
self.assert_(epsilon > abs(test[i] - value[i]), |
186 |
|
"abs(%g - %g) >= %g" % (test[i], value[i], epsilon)) |
187 |
|
|
188 |
|
|
189 |
|
class SubscriberMixin: |
190 |
|
|
191 |
|
"""Mixin class for tests for messages sent through the Connector |
192 |
|
|
193 |
|
The SubscriberMixin has some methods that can be used as subscribers |
194 |
|
of events that when called append information about the message into |
195 |
|
a list of messages received. |
196 |
|
|
197 |
|
A derived class should call the clear_messages() method in both its |
198 |
|
setUp and tearDown methods to clear the list of messages received. |
199 |
|
""" |
200 |
|
|
201 |
|
def clear_messages(self): |
202 |
|
"""Clear the list of received messages. |
203 |
|
|
204 |
|
Call this at least in the tests setUp and tearDown methods. It's |
205 |
|
important to do it in tearDown too because otherwise there may |
206 |
|
be cyclic references. |
207 |
|
""" |
208 |
|
self.received_messages = [] |
209 |
|
|
210 |
|
def subscribe_no_params(self): |
211 |
|
"""Method for subscriptions without parameters. |
212 |
|
|
213 |
|
Add an empty tuple to the list of received messages. |
214 |
|
""" |
215 |
|
self.received_messages.append(()) |
216 |
|
|
217 |
|
def subscribe_with_params(self, *args): |
218 |
|
"""Method for subscriptions with parameters. |
219 |
|
|
220 |
|
Append the tuple will all arguments to this function (except for |
221 |
|
the self argument) to the list of received messages. |
222 |
|
""" |
223 |
|
self.received_messages.append(args) |
224 |
|
|
225 |
|
def check_messages(self, messages): |
226 |
|
"""Check whether the messages received match the list messages""" |
227 |
|
self.assertEquals(messages, self.received_messages) |
228 |
|
|
229 |
|
class FloatTestCase(unittest.TestCase): |
230 |
|
"""TestCase with methods for testing floating point values""" |
231 |
|
|
232 |
|
fp_epsilon = 1e-6 |
233 |
|
fp_inf = float('1e1000') # FIXME: hack for infinite |
234 |
|
|
235 |
|
def assertFloatEqual(self, first, second, msg=None): |
236 |
|
"""Fail if one float is greater than the other + fp_epsilon""" |
237 |
|
if abs(first) == self.fp_inf: |
238 |
|
self.assertEqual(first, second, msg) |
239 |
|
else: |
240 |
|
self.assert_(self.fp_epsilon > abs(first - second), msg) |
241 |
|
|