/[thuban]/branches/WIP-pyshapelib-bramz/test/support.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/test/support.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1736 - (hide annotations)
Mon Sep 22 18:46:56 2003 UTC (21 years, 5 months ago) by bh
Original Path: trunk/thuban/test/support.py
File MIME type: text/x-python
File size: 11656 byte(s)
(initthuban): Make sure to unset the LANG env.
var. so that tests that compare translated strings work.

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 bh 1601 import traceback
19 bh 292
20 bh 1605 import postgissupport
21    
22    
23 bh 292 def thuban_dir():
24     """Return the directory containing the Thuban package"""
25     thisdir = os.path.dirname(__file__)
26     return os.path.join(thisdir, os.pardir)
27    
28 bh 1245 def resource_dir():
29     return os.path.join(thuban_dir(), "Resources")
30 bh 292
31     def add_thuban_dir_to_path():
32     """Insert the Thuban directory at the beginning of the python path.
33    
34     If it's already part of the path, remove later occurrences.
35     """
36     dir = thuban_dir()
37     while 1:
38     try:
39     sys.path.remove(dir)
40     except ValueError:
41     break
42     sys.path.insert(0, dir)
43    
44    
45     _initthuban_done = 0
46     def initthuban():
47     """Initialize the interpreter for using Thuban modules
48     """
49     global _initthuban_done
50     if not _initthuban_done:
51 bh 1736 # 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 bh 292 add_thuban_dir_to_path()
60     import thubaninit
61     _initthuban_done = 1
62    
63 bh 1555
64     #
65     # Special test runner and result that support skipping tests
66     #
67    
68     class SkipTest(Exception):
69     """Exception to raise in tests that are skipped for some reason
70    
71     For instance, since gdal support is optional, test cases that
72     require gdal raise this exception to indicate that they are skipped.
73     Skipped is different from failure or error in that it is expected
74     under certain circumstances.
75     """
76    
77     class ThubanTestResult(unittest._TextTestResult):
78    
79     def __init__(self, stream, descriptions, verbosity):
80     unittest._TextTestResult.__init__(self, stream, descriptions,
81     verbosity)
82     self.skipped_tests = {}
83    
84     def add_skipped_test(self, test, exc):
85     reason = str(exc)
86     self.skipped_tests.setdefault(reason, []).append(test)
87    
88     def count_skipped(self):
89     sum = 0
90     for tests in self.skipped_tests.values():
91     sum += len(tests)
92     return sum
93    
94     def addError(self, test, err):
95     """Extend inherited method to handle SkipTest exceptions specially
96     """
97     #print "addError", test, err
98     if isinstance(err[1], SkipTest):
99     self.add_skipped_test(test, err[1])
100     if self.showAll:
101     self.stream.writeln("skipped")
102     elif self.dots:
103     self.stream.write('S')
104     else:
105     unittest._TextTestResult.addError(self, test, err)
106    
107     def printErrors(self):
108     if self.skipped_tests:
109     if self.dots or self.showAll:
110     self.stream.writeln()
111     self.stream.writeln("Skipped tests:")
112     for reason, tests in self.skipped_tests.items():
113 bh 1601 self.stream.writeln(" %s:" % reason)
114 bh 1555 for test in tests:
115     self.stream.writeln(" " + test.id())
116     unittest._TextTestResult.printErrors(self)
117    
118    
119     class ThubanTestRunner(unittest.TextTestRunner):
120    
121     def _makeResult(self):
122     return ThubanTestResult(self.stream, self.descriptions, self.verbosity)
123    
124     def run(self, test):
125     result = unittest.TextTestRunner.run(self, test)
126     self.stream.writeln("skipped = %d" % result.count_skipped())
127     return result
128    
129    
130     class ThubanTestProgram(unittest.TestProgram):
131    
132     def runTests(self):
133     """Extend inherited method so that we use a ThubanTestRunner"""
134     self.testRunner = ThubanTestRunner(verbosity = self.verbosity)
135     unittest.TestProgram.runTests(self)
136    
137    
138 bh 1601 def execute_as_testsuite(callable, *args, **kw):
139     """Call callable with args as if it were the entry point to the test suite
140 bh 292
141 bh 1601 Return watever callable returns.
142    
143     This is a helper function for run_tests and runtests.py. Call
144     callable in a try-finally block and run some cleanup and print some
145     additional information in the finally block.
146    
147     The additionaly information include:
148    
149     - A list of uncollected objects (after an explicit garbage
150     collector call)
151    
152     - any unsubscribed messages
153 bh 596 """
154     try:
155 bh 1601 return callable(*args, **kw)
156 bh 596 finally:
157     # This has to be in a finally clause because unittest.main()
158     # ends with a sys.exit to make sure that the process exits with
159     # an appropriate exit code
160 bh 1601
161 bh 1605 # Shutdown the postgis server if it's running
162     try:
163     postgissupport.shutdown_test_server()
164     except:
165     traceback.print_exc()
166    
167 bh 1601 # Print additional information
168 bh 1245 print_additional_summary()
169 bh 292
170 bh 1601 def run_tests():
171     """Frontend for unittest.main that prints some additional debug information
172    
173     After calling unittest.main, run the garbage collector and print
174     uncollected objects. Also print any un-unsubscribed messages.
175     """
176     execute_as_testsuite(ThubanTestProgram)
177    
178    
179 bh 1245 def print_additional_summary():
180     """Print some additional summary information after tests have been run"""
181     print_garbage_information()
182     import xmlsupport
183     xmlsupport.print_summary_message()
184    
185 bh 596 def print_garbage_information():
186     """Print information about things that haven't been cleaned up.
187    
188     Run the garbage collector and print uncollected objects. Also print
189     any un-unsubscribed messages.
190     """
191 bh 1673 # this function may be called indirectly from test cases that test
192     # test support modules which do not use anything from thuban itself,
193     # so we call initthuban so that we can import the connector module
194     initthuban()
195 bh 596 import gc, Thuban.Lib.connector
196     gc.collect()
197     if gc.garbage:
198     print
199     print "There are %d uncollected objects:" % len(gc.garbage)
200     print gc.garbage
201     Thuban.Lib.connector._the_connector.print_connections()
202    
203 bh 1555 #
204 bh 596
205 bh 292 def create_temp_dir():
206     """Create a temporary directory and return its name.
207    
208     The temporary directory is always called temp and is created in the
209     directory where support module is located.
210    
211     If the temp directory already exists, just return the name.
212     """
213     name = os.path.abspath(os.path.join(os.path.dirname(__file__), "temp"))
214    
215     # if the directory already exists, we're done
216     if os.path.isdir(name):
217     return name
218    
219     # create the directory
220     os.mkdir(name)
221     return name
222    
223    
224     class FileTestMixin:
225    
226     """Mixin class for tests that use files in the temporary directory
227     """
228    
229     def temp_file_name(self, basename):
230     """Return the full name of the file named basename in the temp. dir"""
231     return os.path.join(create_temp_dir(), basename)
232    
233     def temp_dir(self):
234     """Return the name of the directory for the temporary files"""
235     return create_temp_dir()
236 bh 324
237    
238 bh 956
239     class FileLoadTestCase(unittest.TestCase, FileTestMixin):
240    
241     """Base class for test case that test loading files.
242    
243     This base class provides some common infrastructure for testing the
244     reading of files.
245    
246     Each test case should be its own class derived from this one. There
247     is one file associated with each class. The contents are defined by
248     the file_contents class attribute and its name by the filename
249     method.
250    
251     Derived classes usually only have to provide appropriate values for
252     the file_contents and file_extension class attributes.
253     """
254    
255     file_contents = None
256     file_extension = ""
257    
258     def filename(self):
259     """Return the name of the test file to use.
260    
261     The default implementation simply calls self.volatile_file_name
262     with a basename derived from the class name by stripping off a
263     leading 'test_' and appending self.file_extension.
264     """
265     name = self.__class__.__name__
266     if name.startswith("test_"):
267     name = name[5:]
268     return self.temp_file_name(name + self.file_extension)
269    
270     def setUp(self):
271     """Create the volatile file for the test.
272    
273     Write self.contents (which should be a string) to the file named
274     by self.filename().
275     """
276     filename = self.filename()
277     file = open(filename, "w")
278     file.write(self.file_contents)
279     file.close()
280    
281    
282 bh 1421 class FloatComparisonMixin:
283 bh 324
284     """
285     Mixin class for tests comparing floating point numbers.
286    
287     This class provides a few methods for testing floating point
288     operations.
289     """
290    
291     fp_epsilon = 1e-6
292 jonathan 1263 fp_inf = float('1e1000') # FIXME: hack for infinite
293 bh 1574
294 jonathan 1397 def assertFloatEqual(self, test, value, epsilon = None):
295 bh 324 """Assert equality of test and value with some tolerance.
296    
297     Assert that the absolute difference between test and value is
298     less than self.fp_epsilon.
299     """
300 jonathan 1397 if epsilon is None:
301     epsilon = self.fp_epsilon
302 jonathan 1263 if abs(test) == self.fp_inf:
303     self.assertEqual(test, value)
304     else:
305 jonathan 1397 self.assert_(epsilon > abs(test - value),
306     "abs(%g - %g) >= %g" % (test, value, epsilon))
307 bh 324
308     def assertFloatSeqEqual(self, test, value, epsilon = None):
309     """Assert equality of the sequences test and value with some tolerance.
310    
311     Assert that the absolute difference between each corresponding
312     value in test and value is less than the optional parameter
313     epsilon. If epsilon is not given use self.fp_epsilon.
314     """
315     for i in range(len(test)):
316 jonathan 1397 self.assertFloatEqual(test[i], value[i], epsilon)
317 bh 324
318 bh 1574 def assertPointListEquals(self, test, value):
319     """Assert equality of two lists of lists of tuples of float
320    
321     This assertion is usually used to compare the geometry of shapes
322     as returned by a Shape object's Points() method, hence the name.
323     """
324     for i in range(len(test)):
325     self.assertEquals(len(test[i]), len(value[i]))
326     for j in range(len(test[i])):
327     self.assertFloatSeqEqual(test[i][j], value[i][j])
328    
329    
330 bh 324 class SubscriberMixin:
331    
332     """Mixin class for tests for messages sent through the Connector
333    
334     The SubscriberMixin has some methods that can be used as subscribers
335     of events that when called append information about the message into
336     a list of messages received.
337    
338     A derived class should call the clear_messages() method in both its
339     setUp and tearDown methods to clear the list of messages received.
340     """
341    
342     def clear_messages(self):
343     """Clear the list of received messages.
344    
345     Call this at least in the tests setUp and tearDown methods. It's
346     important to do it in tearDown too because otherwise there may
347     be cyclic references.
348     """
349     self.received_messages = []
350    
351     def subscribe_no_params(self):
352     """Method for subscriptions without parameters.
353    
354     Add an empty tuple to the list of received messages.
355     """
356     self.received_messages.append(())
357    
358     def subscribe_with_params(self, *args):
359     """Method for subscriptions with parameters.
360    
361     Append the tuple will all arguments to this function (except for
362     the self argument) to the list of received messages.
363     """
364     self.received_messages.append(args)
365    
366     def check_messages(self, messages):
367     """Check whether the messages received match the list messages"""
368     self.assertEquals(messages, self.received_messages)
369 jonathan 904

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26