author | rptlab |
Tue, 30 Apr 2013 14:28:14 +0100 | |
branch | py33 |
changeset 3723 | 99aa837b6703 |
parent 3668 | d8a42e00c079 |
child 3823 | c8133d91487a |
permissions | -rw-r--r-- |
3617 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2012 |
2966 | 2 |
#see license.txt for license details |
2963 | 3 |
"""Tests for reportlab.lib.utils |
4 |
""" |
|
2966 | 5 |
__version__=''' $Id$ ''' |
2987 | 6 |
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, printLocation |
2984 | 7 |
setOutDir(__name__) |
2963 | 8 |
import os |
9 |
import reportlab |
|
2966 | 10 |
import unittest |
2963 | 11 |
from reportlab.lib import colors |
12 |
from reportlab.lib.utils import recursiveImport, recursiveGetAttr, recursiveSetAttr, rl_isfile, \ |
|
13 |
isCompactDistro |
|
14 |
||
2966 | 15 |
def _rel_open_and_read(fn): |
16 |
from reportlab.lib.utils import open_and_read |
|
2987 | 17 |
from reportlab.lib.testutils import testsFolder |
2966 | 18 |
cwd = os.getcwd() |
19 |
os.chdir(testsFolder) |
|
20 |
try: |
|
21 |
return open_and_read(fn) |
|
22 |
finally: |
|
23 |
os.chdir(cwd) |
|
24 |
||
2963 | 25 |
class ImporterTestCase(unittest.TestCase): |
26 |
"Test import utilities" |
|
27 |
count = 0 |
|
28 |
||
29 |
def setUp(self): |
|
30 |
from time import time |
|
31 |
from reportlab.lib.utils import get_rl_tempdir |
|
3326 | 32 |
s = repr(int(time())) + repr(self.count) |
2963 | 33 |
self.__class__.count += 1 |
34 |
self._tempdir = get_rl_tempdir('reportlab_test','tmp_%s' % s) |
|
35 |
_testmodulename = os.path.join(self._tempdir,'test_module_%s.py' % s) |
|
36 |
f = open(_testmodulename,'w') |
|
37 |
f.write('__all__=[]\n') |
|
38 |
f.close() |
|
39 |
self._testmodulename = os.path.splitext(os.path.basename(_testmodulename))[0] |
|
40 |
||
41 |
def tearDown(self): |
|
42 |
from shutil import rmtree |
|
43 |
rmtree(self._tempdir,1) |
|
44 |
||
45 |
def test1(self): |
|
46 |
"try stuff known to be in the path" |
|
47 |
m1 = recursiveImport('reportlab.pdfgen.canvas') |
|
48 |
import reportlab.pdfgen.canvas |
|
49 |
assert m1 == reportlab.pdfgen.canvas |
|
50 |
||
51 |
def test2(self): |
|
52 |
"try under a well known directory NOT on the path" |
|
2987 | 53 |
from reportlab.lib.testutils import testsFolder |
2966 | 54 |
D = os.path.join(testsFolder,'..','tools','pythonpoint') |
2963 | 55 |
fn = os.path.join(D,'stdparser.py') |
56 |
if rl_isfile(fn) or rl_isfile(fn+'c') or rl_isfile(fn+'o'): |
|
57 |
m1 = recursiveImport('stdparser', baseDir=D) |
|
58 |
||
59 |
def test3(self): |
|
60 |
"ensure CWD is on the path" |
|
61 |
try: |
|
62 |
cwd = os.getcwd() |
|
63 |
os.chdir(self._tempdir) |
|
64 |
m1 = recursiveImport(self._testmodulename) |
|
65 |
finally: |
|
66 |
os.chdir(cwd) |
|
67 |
||
68 |
def test4(self): |
|
69 |
"ensure noCWD removes current dir from path" |
|
70 |
try: |
|
71 |
cwd = os.getcwd() |
|
72 |
os.chdir(self._tempdir) |
|
73 |
import sys |
|
74 |
try: |
|
75 |
del sys.modules[self._testmodulename] |
|
76 |
except KeyError: |
|
77 |
pass |
|
78 |
self.assertRaises(ImportError, |
|
79 |
recursiveImport, |
|
80 |
self._testmodulename, |
|
81 |
noCWD=1) |
|
82 |
finally: |
|
83 |
os.chdir(cwd) |
|
84 |
||
85 |
def test5(self): |
|
86 |
"recursive attribute setting/getting on modules" |
|
87 |
import reportlab.lib.units |
|
88 |
inch = recursiveGetAttr(reportlab, 'lib.units.inch') |
|
89 |
assert inch == 72 |
|
90 |
||
91 |
recursiveSetAttr(reportlab, 'lib.units.cubit', 18*inch) |
|
92 |
cubit = recursiveGetAttr(reportlab, 'lib.units.cubit') |
|
93 |
assert cubit == 18*inch |
|
94 |
||
95 |
def test6(self): |
|
96 |
"recursive attribute setting/getting on drawings" |
|
97 |
from reportlab.graphics.charts.barcharts import sampleH1 |
|
98 |
drawing = sampleH1() |
|
99 |
recursiveSetAttr(drawing, 'barchart.valueAxis.valueMax', 72) |
|
100 |
theMax = recursiveGetAttr(drawing, 'barchart.valueAxis.valueMax') |
|
101 |
assert theMax == 72 |
|
102 |
||
103 |
def test7(self): |
|
104 |
"test open and read of a simple relative file" |
|
2966 | 105 |
b = _rel_open_and_read('../docs/images/Edit_Prefs.gif') |
2963 | 106 |
|
107 |
def test8(self): |
|
108 |
"test open and read of a relative file: URL" |
|
2966 | 109 |
b = _rel_open_and_read('file:../docs/images/Edit_Prefs.gif') |
2963 | 110 |
|
111 |
def test9(self): |
|
112 |
"test open and read of an http: URL" |
|
113 |
from reportlab.lib.utils import open_and_read |
|
114 |
b = open_and_read('http://www.reportlab.com/rsrc/encryption.gif') |
|
115 |
||
116 |
def test10(self): |
|
117 |
"test open and read of a simple relative file" |
|
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3668
diff
changeset
|
118 |
from reportlab.lib.utils import open_and_read, getBytesIO |
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3668
diff
changeset
|
119 |
b = getBytesIO(_rel_open_and_read('../docs/images/Edit_Prefs.gif')) |
2963 | 120 |
b = open_and_read(b) |
121 |
||
3668
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
122 |
|
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
123 |
def testRecursiveImportErrors(self): |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
124 |
"check we get useful error messages" |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
125 |
try: |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
126 |
m1 = recursiveImport('reportlab.pdfgen.brush') |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
127 |
self.fail("Imported a nonexistent module") |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
128 |
except ImportError, e: |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
129 |
self.assertEquals(e.message, "Could not import 'reportlab.pdfgen.brush'") |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
130 |
|
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
131 |
try: |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
132 |
m1 = recursiveImport('totally.non.existent') |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
133 |
self.fail("Imported a nonexistent module") |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
134 |
except ImportError, e: |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
135 |
self.assertEquals(e.message, "Could not import 'totally.non.existent'") |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
136 |
|
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
137 |
try: |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
138 |
#import a module in the 'tests' directory with a bug |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
139 |
m1 = recursiveImport('unimportable') |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
140 |
self.fail("Imported a buggy module") |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
141 |
except ImportError, e: |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
142 |
self.assert_('integer division or modulo by zero' in e.message) |
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
143 |
|
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
144 |
|
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
145 |
|
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
146 |
|
d8a42e00c079
recursiveImport now has much more helpful error messages
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
147 |
|
2963 | 148 |
def makeSuite(): |
149 |
return makeSuiteForClasses(ImporterTestCase) |
|
150 |
||
151 |
if __name__ == "__main__": #noruntests |
|
152 |
unittest.TextTestRunner().run(makeSuite()) |
|
153 |
printLocation() |