1 |
bh |
596 |
# Copyright (c) 2002, 2003 by Intevation GmbH |
2 |
bh |
292 |
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Support classes and function for the test suite |
10 |
|
|
""" |
11 |
|
|
|
12 |
|
|
__version__ = "$Revision$" |
13 |
|
|
# $Source$ |
14 |
|
|
# $Id$ |
15 |
|
|
|
16 |
|
|
import os, sys |
17 |
|
|
import unittest |
18 |
|
|
|
19 |
|
|
def thuban_dir(): |
20 |
|
|
"""Return the directory containing the Thuban package""" |
21 |
|
|
thisdir = os.path.dirname(__file__) |
22 |
|
|
return os.path.join(thisdir, os.pardir) |
23 |
|
|
|
24 |
bh |
1245 |
def resource_dir(): |
25 |
|
|
return os.path.join(thuban_dir(), "Resources") |
26 |
bh |
292 |
|
27 |
|
|
def add_thuban_dir_to_path(): |
28 |
|
|
"""Insert the Thuban directory at the beginning of the python path. |
29 |
|
|
|
30 |
|
|
If it's already part of the path, remove later occurrences. |
31 |
|
|
""" |
32 |
|
|
dir = thuban_dir() |
33 |
|
|
while 1: |
34 |
|
|
try: |
35 |
|
|
sys.path.remove(dir) |
36 |
|
|
except ValueError: |
37 |
|
|
break |
38 |
|
|
sys.path.insert(0, dir) |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
_initthuban_done = 0 |
42 |
|
|
def initthuban(): |
43 |
|
|
"""Initialize the interpreter for using Thuban modules |
44 |
|
|
""" |
45 |
|
|
global _initthuban_done |
46 |
|
|
if not _initthuban_done: |
47 |
|
|
add_thuban_dir_to_path() |
48 |
|
|
import thubaninit |
49 |
|
|
_initthuban_done = 1 |
50 |
|
|
|
51 |
bh |
596 |
def run_tests(): |
52 |
|
|
"""Frontend for unittest.main that prints some additional debug information |
53 |
bh |
292 |
|
54 |
bh |
596 |
After calling unittest.main, run the garbage collector and print |
55 |
|
|
uncollected objects. Also print any un-unsubscribed messages. |
56 |
|
|
""" |
57 |
|
|
try: |
58 |
|
|
unittest.main() |
59 |
|
|
finally: |
60 |
|
|
# This has to be in a finally clause because unittest.main() |
61 |
|
|
# ends with a sys.exit to make sure that the process exits with |
62 |
|
|
# an appropriate exit code |
63 |
bh |
1245 |
print_additional_summary() |
64 |
bh |
292 |
|
65 |
bh |
1245 |
def print_additional_summary(): |
66 |
|
|
"""Print some additional summary information after tests have been run""" |
67 |
|
|
print_garbage_information() |
68 |
|
|
import xmlsupport |
69 |
|
|
xmlsupport.print_summary_message() |
70 |
|
|
|
71 |
bh |
596 |
def print_garbage_information(): |
72 |
|
|
"""Print information about things that haven't been cleaned up. |
73 |
|
|
|
74 |
|
|
Run the garbage collector and print uncollected objects. Also print |
75 |
|
|
any un-unsubscribed messages. |
76 |
|
|
""" |
77 |
|
|
import gc, Thuban.Lib.connector |
78 |
|
|
gc.collect() |
79 |
|
|
if gc.garbage: |
80 |
|
|
print |
81 |
|
|
print "There are %d uncollected objects:" % len(gc.garbage) |
82 |
|
|
print gc.garbage |
83 |
|
|
Thuban.Lib.connector._the_connector.print_connections() |
84 |
|
|
|
85 |
|
|
|
86 |
bh |
292 |
def create_temp_dir(): |
87 |
|
|
"""Create a temporary directory and return its name. |
88 |
|
|
|
89 |
|
|
The temporary directory is always called temp and is created in the |
90 |
|
|
directory where support module is located. |
91 |
|
|
|
92 |
|
|
If the temp directory already exists, just return the name. |
93 |
|
|
""" |
94 |
|
|
name = os.path.abspath(os.path.join(os.path.dirname(__file__), "temp")) |
95 |
|
|
|
96 |
|
|
# if the directory already exists, we're done |
97 |
|
|
if os.path.isdir(name): |
98 |
|
|
return name |
99 |
|
|
|
100 |
|
|
# create the directory |
101 |
|
|
os.mkdir(name) |
102 |
|
|
return name |
103 |
|
|
|
104 |
|
|
|
105 |
|
|
class FileTestMixin: |
106 |
|
|
|
107 |
|
|
"""Mixin class for tests that use files in the temporary directory |
108 |
|
|
""" |
109 |
|
|
|
110 |
|
|
def temp_file_name(self, basename): |
111 |
|
|
"""Return the full name of the file named basename in the temp. dir""" |
112 |
|
|
return os.path.join(create_temp_dir(), basename) |
113 |
|
|
|
114 |
|
|
def temp_dir(self): |
115 |
|
|
"""Return the name of the directory for the temporary files""" |
116 |
|
|
return create_temp_dir() |
117 |
bh |
324 |
|
118 |
|
|
|
119 |
bh |
956 |
|
120 |
|
|
class FileLoadTestCase(unittest.TestCase, FileTestMixin): |
121 |
|
|
|
122 |
|
|
"""Base class for test case that test loading files. |
123 |
|
|
|
124 |
|
|
This base class provides some common infrastructure for testing the |
125 |
|
|
reading of files. |
126 |
|
|
|
127 |
|
|
Each test case should be its own class derived from this one. There |
128 |
|
|
is one file associated with each class. The contents are defined by |
129 |
|
|
the file_contents class attribute and its name by the filename |
130 |
|
|
method. |
131 |
|
|
|
132 |
|
|
Derived classes usually only have to provide appropriate values for |
133 |
|
|
the file_contents and file_extension class attributes. |
134 |
|
|
""" |
135 |
|
|
|
136 |
|
|
file_contents = None |
137 |
|
|
file_extension = "" |
138 |
|
|
|
139 |
|
|
def filename(self): |
140 |
|
|
"""Return the name of the test file to use. |
141 |
|
|
|
142 |
|
|
The default implementation simply calls self.volatile_file_name |
143 |
|
|
with a basename derived from the class name by stripping off a |
144 |
|
|
leading 'test_' and appending self.file_extension. |
145 |
|
|
""" |
146 |
|
|
name = self.__class__.__name__ |
147 |
|
|
if name.startswith("test_"): |
148 |
|
|
name = name[5:] |
149 |
|
|
return self.temp_file_name(name + self.file_extension) |
150 |
|
|
|
151 |
|
|
def setUp(self): |
152 |
|
|
"""Create the volatile file for the test. |
153 |
|
|
|
154 |
|
|
Write self.contents (which should be a string) to the file named |
155 |
|
|
by self.filename(). |
156 |
|
|
""" |
157 |
|
|
filename = self.filename() |
158 |
|
|
file = open(filename, "w") |
159 |
|
|
file.write(self.file_contents) |
160 |
|
|
file.close() |
161 |
|
|
|
162 |
|
|
|
163 |
jonathan |
1263 |
class FloatComparisonMixin(unittest.TestCase): |
164 |
bh |
324 |
|
165 |
|
|
""" |
166 |
|
|
Mixin class for tests comparing floating point numbers. |
167 |
|
|
|
168 |
|
|
This class provides a few methods for testing floating point |
169 |
|
|
operations. |
170 |
|
|
""" |
171 |
|
|
|
172 |
|
|
fp_epsilon = 1e-6 |
173 |
jonathan |
1263 |
fp_inf = float('1e1000') # FIXME: hack for infinite |
174 |
bh |
324 |
|
175 |
|
|
def assertFloatEqual(self, test, value): |
176 |
|
|
"""Assert equality of test and value with some tolerance. |
177 |
|
|
|
178 |
|
|
Assert that the absolute difference between test and value is |
179 |
|
|
less than self.fp_epsilon. |
180 |
|
|
""" |
181 |
jonathan |
1263 |
if abs(test) == self.fp_inf: |
182 |
|
|
self.assertEqual(test, value) |
183 |
|
|
else: |
184 |
|
|
self.assert_(self.fp_epsilon > abs(test - value), |
185 |
bh |
342 |
"abs(%g - %g) >= %g" % (test, value, self.fp_epsilon)) |
186 |
bh |
324 |
|
187 |
|
|
def assertFloatSeqEqual(self, test, value, epsilon = None): |
188 |
|
|
"""Assert equality of the sequences test and value with some tolerance. |
189 |
|
|
|
190 |
|
|
Assert that the absolute difference between each corresponding |
191 |
|
|
value in test and value is less than the optional parameter |
192 |
|
|
epsilon. If epsilon is not given use self.fp_epsilon. |
193 |
|
|
""" |
194 |
|
|
if epsilon is None: |
195 |
|
|
epsilon = self.fp_epsilon |
196 |
|
|
for i in range(len(test)): |
197 |
bh |
342 |
self.assert_(epsilon > abs(test[i] - value[i]), |
198 |
|
|
"abs(%g - %g) >= %g" % (test[i], value[i], epsilon)) |
199 |
bh |
324 |
|
200 |
|
|
|
201 |
|
|
class SubscriberMixin: |
202 |
|
|
|
203 |
|
|
"""Mixin class for tests for messages sent through the Connector |
204 |
|
|
|
205 |
|
|
The SubscriberMixin has some methods that can be used as subscribers |
206 |
|
|
of events that when called append information about the message into |
207 |
|
|
a list of messages received. |
208 |
|
|
|
209 |
|
|
A derived class should call the clear_messages() method in both its |
210 |
|
|
setUp and tearDown methods to clear the list of messages received. |
211 |
|
|
""" |
212 |
|
|
|
213 |
|
|
def clear_messages(self): |
214 |
|
|
"""Clear the list of received messages. |
215 |
|
|
|
216 |
|
|
Call this at least in the tests setUp and tearDown methods. It's |
217 |
|
|
important to do it in tearDown too because otherwise there may |
218 |
|
|
be cyclic references. |
219 |
|
|
""" |
220 |
|
|
self.received_messages = [] |
221 |
|
|
|
222 |
|
|
def subscribe_no_params(self): |
223 |
|
|
"""Method for subscriptions without parameters. |
224 |
|
|
|
225 |
|
|
Add an empty tuple to the list of received messages. |
226 |
|
|
""" |
227 |
|
|
self.received_messages.append(()) |
228 |
|
|
|
229 |
|
|
def subscribe_with_params(self, *args): |
230 |
|
|
"""Method for subscriptions with parameters. |
231 |
|
|
|
232 |
|
|
Append the tuple will all arguments to this function (except for |
233 |
|
|
the self argument) to the list of received messages. |
234 |
|
|
""" |
235 |
|
|
self.received_messages.append(args) |
236 |
|
|
|
237 |
|
|
def check_messages(self, messages): |
238 |
|
|
"""Check whether the messages received match the list messages""" |
239 |
|
|
self.assertEquals(messages, self.received_messages) |
240 |
jonathan |
904 |
|