1 |
# Copyright (c) 2002, 2003 by Intevation GmbH |
# Copyright (c) 2002, 2003, 2004 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
15 |
|
|
16 |
import os, sys |
import os, sys |
17 |
import unittest |
import unittest |
18 |
|
import traceback |
19 |
|
|
20 |
|
import postgissupport |
21 |
|
|
22 |
|
|
23 |
def thuban_dir(): |
def thuban_dir(): |
24 |
"""Return the directory containing the Thuban package""" |
"""Return the directory containing the Thuban package""" |
48 |
""" |
""" |
49 |
global _initthuban_done |
global _initthuban_done |
50 |
if not _initthuban_done: |
if not _initthuban_done: |
51 |
|
# Thuban uses gettext to translate some strings. Some of these |
52 |
|
# strings are tested for equality in some test cases. So we |
53 |
|
# unset any LANG environment setting to make sure only the |
54 |
|
# untranslated messages are used. |
55 |
|
try: |
56 |
|
del os.environ["LANG"] |
57 |
|
except KeyError: |
58 |
|
pass |
59 |
add_thuban_dir_to_path() |
add_thuban_dir_to_path() |
60 |
import thubaninit |
import thubaninit |
61 |
|
|
62 |
|
# Install a dummy translation function so that importing |
63 |
|
# Thuban.UI doesn't install a wx specific one for which would |
64 |
|
# need to import wxPython |
65 |
|
import Thuban |
66 |
|
Thuban.install_translation_function(lambda s: s) |
67 |
|
|
68 |
_initthuban_done = 1 |
_initthuban_done = 1 |
69 |
|
|
70 |
|
|
117 |
self.stream.writeln() |
self.stream.writeln() |
118 |
self.stream.writeln("Skipped tests:") |
self.stream.writeln("Skipped tests:") |
119 |
for reason, tests in self.skipped_tests.items(): |
for reason, tests in self.skipped_tests.items(): |
120 |
self.stream.writeln("%s:" % reason) |
self.stream.writeln(" %s:" % reason) |
121 |
for test in tests: |
for test in tests: |
122 |
self.stream.writeln(" " + test.id()) |
self.stream.writeln(" " + test.id()) |
123 |
unittest._TextTestResult.printErrors(self) |
unittest._TextTestResult.printErrors(self) |
124 |
|
|
125 |
|
def getDescription(self, test): |
126 |
|
return test.id() |
127 |
|
|
128 |
|
|
129 |
class ThubanTestRunner(unittest.TextTestRunner): |
class ThubanTestRunner(unittest.TextTestRunner): |
130 |
|
|
141 |
|
|
142 |
def runTests(self): |
def runTests(self): |
143 |
"""Extend inherited method so that we use a ThubanTestRunner""" |
"""Extend inherited method so that we use a ThubanTestRunner""" |
|
print "ThubanTestProgram.runTests" |
|
144 |
self.testRunner = ThubanTestRunner(verbosity = self.verbosity) |
self.testRunner = ThubanTestRunner(verbosity = self.verbosity) |
145 |
unittest.TestProgram.runTests(self) |
unittest.TestProgram.runTests(self) |
146 |
|
|
147 |
|
|
148 |
def run_tests(): |
def execute_as_testsuite(callable, *args, **kw): |
149 |
"""Frontend for unittest.main that prints some additional debug information |
"""Call callable with args as if it were the entry point to the test suite |
150 |
|
|
151 |
After calling unittest.main, run the garbage collector and print |
Return watever callable returns. |
152 |
uncollected objects. Also print any un-unsubscribed messages. |
|
153 |
|
This is a helper function for run_tests and runtests.py. Call |
154 |
|
callable in a try-finally block and run some cleanup and print some |
155 |
|
additional information in the finally block. |
156 |
|
|
157 |
|
The additionaly information include: |
158 |
|
|
159 |
|
- A list of uncollected objects (after an explicit garbage |
160 |
|
collector call) |
161 |
|
|
162 |
|
- any unsubscribed messages |
163 |
""" |
""" |
164 |
try: |
try: |
165 |
ThubanTestProgram() |
return callable(*args, **kw) |
166 |
finally: |
finally: |
167 |
# This has to be in a finally clause because unittest.main() |
# This has to be in a finally clause because unittest.main() |
168 |
# ends with a sys.exit to make sure that the process exits with |
# ends with a sys.exit to make sure that the process exits with |
169 |
# an appropriate exit code |
# an appropriate exit code |
170 |
|
|
171 |
|
# Shutdown the postgis server if it's running |
172 |
|
try: |
173 |
|
postgissupport.shutdown_test_server() |
174 |
|
except: |
175 |
|
traceback.print_exc() |
176 |
|
|
177 |
|
# Print additional information |
178 |
print_additional_summary() |
print_additional_summary() |
179 |
|
|
180 |
|
def run_tests(): |
181 |
|
"""Frontend for unittest.main that prints some additional debug information |
182 |
|
|
183 |
|
After calling unittest.main, run the garbage collector and print |
184 |
|
uncollected objects. Also print any un-unsubscribed messages. |
185 |
|
""" |
186 |
|
execute_as_testsuite(ThubanTestProgram) |
187 |
|
|
188 |
|
|
189 |
def print_additional_summary(): |
def print_additional_summary(): |
190 |
"""Print some additional summary information after tests have been run""" |
"""Print some additional summary information after tests have been run""" |
191 |
print_garbage_information() |
print_garbage_information() |
198 |
Run the garbage collector and print uncollected objects. Also print |
Run the garbage collector and print uncollected objects. Also print |
199 |
any un-unsubscribed messages. |
any un-unsubscribed messages. |
200 |
""" |
""" |
201 |
|
# this function may be called indirectly from test cases that test |
202 |
|
# test support modules which do not use anything from thuban itself, |
203 |
|
# so we call initthuban so that we can import the connector module |
204 |
|
initthuban() |
205 |
import gc, Thuban.Lib.connector |
import gc, Thuban.Lib.connector |
206 |
gc.collect() |
gc.collect() |
207 |
if gc.garbage: |
if gc.garbage: |
268 |
def filename(self): |
def filename(self): |
269 |
"""Return the name of the test file to use. |
"""Return the name of the test file to use. |
270 |
|
|
271 |
The default implementation simply calls self.volatile_file_name |
The default implementation simply calls self.temp_file_name with |
272 |
with a basename derived from the class name by stripping off a |
a basename derived from the class name by stripping off a |
273 |
leading 'test_' and appending self.file_extension. |
leading 'test_' and appending self.file_extension. |
274 |
""" |
""" |
275 |
name = self.__class__.__name__ |
name = self.__class__.__name__ |
300 |
|
|
301 |
fp_epsilon = 1e-6 |
fp_epsilon = 1e-6 |
302 |
fp_inf = float('1e1000') # FIXME: hack for infinite |
fp_inf = float('1e1000') # FIXME: hack for infinite |
303 |
|
|
304 |
def assertFloatEqual(self, test, value, epsilon = None): |
def assertFloatEqual(self, test, value, epsilon = None): |
305 |
"""Assert equality of test and value with some tolerance. |
"""Assert equality of test and value with some tolerance. |
306 |
|
|
322 |
value in test and value is less than the optional parameter |
value in test and value is less than the optional parameter |
323 |
epsilon. If epsilon is not given use self.fp_epsilon. |
epsilon. If epsilon is not given use self.fp_epsilon. |
324 |
""" |
""" |
325 |
|
self.assertEquals(len(test), len(value)) |
326 |
for i in range(len(test)): |
for i in range(len(test)): |
327 |
self.assertFloatEqual(test[i], value[i], epsilon) |
self.assertFloatEqual(test[i], value[i], epsilon) |
328 |
|
|
329 |
|
def assertPointListEquals(self, test, value): |
330 |
|
"""Assert equality of two lists of lists of tuples of float |
331 |
|
|
332 |
|
This assertion is usually used to compare the geometry of shapes |
333 |
|
as returned by a Shape object's Points() method, hence the name. |
334 |
|
""" |
335 |
|
for i in range(len(test)): |
336 |
|
self.assertEquals(len(test[i]), len(value[i])) |
337 |
|
for j in range(len(test[i])): |
338 |
|
self.assertFloatSeqEqual(test[i][j], value[i][j]) |
339 |
|
|
340 |
|
|
341 |
class SubscriberMixin: |
class SubscriberMixin: |
342 |
|
|
343 |
"""Mixin class for tests for messages sent through the Connector |
"""Mixin class for tests for messages sent through the Connector |