tests/test_lib_utils.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3668 d8a42e00c079
child 3823 c8133d91487a
permissions -rw-r--r--
second stage of port to Python 3.3; working hello world
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3326
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2012
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
     2
#see license.txt for license details
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     3
"""Tests for reportlab.lib.utils
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     4
"""
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
     5
__version__=''' $Id$ '''
2987
711910106e3a tests: more changes to testsFolder and friends
rgbecker
parents: 2984
diff changeset
     6
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, printLocation
2984
c63f149d55aa tests.utils --> reportlab.lib.testutils
rgbecker
parents: 2966
diff changeset
     7
setOutDir(__name__)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     8
import os
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     9
import reportlab
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    10
import unittest
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    11
from reportlab.lib import colors
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    12
from reportlab.lib.utils import recursiveImport, recursiveGetAttr, recursiveSetAttr, rl_isfile, \
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    13
                                isCompactDistro
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    14
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    15
def _rel_open_and_read(fn):
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    16
    from reportlab.lib.utils import open_and_read
2987
711910106e3a tests: more changes to testsFolder and friends
rgbecker
parents: 2984
diff changeset
    17
    from reportlab.lib.testutils import testsFolder
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    18
    cwd = os.getcwd()
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    19
    os.chdir(testsFolder)
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    20
    try:
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    21
        return open_and_read(fn)
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    22
    finally:
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    23
        os.chdir(cwd)
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    24
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    25
class ImporterTestCase(unittest.TestCase):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    26
    "Test import utilities"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    27
    count = 0
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    28
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    29
    def setUp(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    30
        from time import time
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    31
        from reportlab.lib.utils import get_rl_tempdir
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 2987
diff changeset
    32
        s = repr(int(time())) + repr(self.count)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    33
        self.__class__.count += 1
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    34
        self._tempdir = get_rl_tempdir('reportlab_test','tmp_%s' % s)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    35
        _testmodulename = os.path.join(self._tempdir,'test_module_%s.py' % s)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    36
        f = open(_testmodulename,'w')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    37
        f.write('__all__=[]\n')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    38
        f.close()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    39
        self._testmodulename = os.path.splitext(os.path.basename(_testmodulename))[0]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    40
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    41
    def tearDown(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    42
        from shutil import rmtree
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    43
        rmtree(self._tempdir,1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    44
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    45
    def test1(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    46
        "try stuff known to be in the path"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    47
        m1 = recursiveImport('reportlab.pdfgen.canvas')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    48
        import reportlab.pdfgen.canvas
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    49
        assert m1 == reportlab.pdfgen.canvas
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    50
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    51
    def test2(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    52
        "try under a well known directory NOT on the path"
2987
711910106e3a tests: more changes to testsFolder and friends
rgbecker
parents: 2984
diff changeset
    53
        from reportlab.lib.testutils import testsFolder
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    54
        D = os.path.join(testsFolder,'..','tools','pythonpoint')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    55
        fn = os.path.join(D,'stdparser.py')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    56
        if rl_isfile(fn) or rl_isfile(fn+'c') or rl_isfile(fn+'o'):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    57
            m1 = recursiveImport('stdparser', baseDir=D)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    58
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    59
    def test3(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    60
        "ensure CWD is on the path"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    61
        try:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    62
            cwd = os.getcwd()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    63
            os.chdir(self._tempdir)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    64
            m1 = recursiveImport(self._testmodulename)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    65
        finally:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    66
            os.chdir(cwd)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    67
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    68
    def test4(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    69
        "ensure noCWD removes current dir from path"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    70
        try:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    71
            cwd = os.getcwd()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    72
            os.chdir(self._tempdir)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    73
            import sys
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    74
            try:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    75
                del sys.modules[self._testmodulename]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    76
            except KeyError:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    77
                pass
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    78
            self.assertRaises(ImportError,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    79
                              recursiveImport,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    80
                              self._testmodulename,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    81
                              noCWD=1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    82
        finally:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    83
            os.chdir(cwd)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    84
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    85
    def test5(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    86
        "recursive attribute setting/getting on modules"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    87
        import reportlab.lib.units
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    88
        inch = recursiveGetAttr(reportlab, 'lib.units.inch')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    89
        assert inch == 72
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    90
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    91
        recursiveSetAttr(reportlab, 'lib.units.cubit', 18*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    92
        cubit = recursiveGetAttr(reportlab, 'lib.units.cubit')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    93
        assert cubit == 18*inch
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    94
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    95
    def test6(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    96
        "recursive attribute setting/getting on drawings"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    97
        from reportlab.graphics.charts.barcharts import sampleH1
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    98
        drawing = sampleH1()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    99
        recursiveSetAttr(drawing, 'barchart.valueAxis.valueMax', 72)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   100
        theMax = recursiveGetAttr(drawing, 'barchart.valueAxis.valueMax')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   101
        assert theMax == 72
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   102
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   103
    def test7(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   104
        "test open and read of a simple relative file"
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
   105
        b = _rel_open_and_read('../docs/images/Edit_Prefs.gif')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   106
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   107
    def test8(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   108
        "test open and read of a relative file: URL"
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
   109
        b = _rel_open_and_read('file:../docs/images/Edit_Prefs.gif')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   110
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   111
    def test9(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   112
        "test open and read of an http: URL"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   113
        from reportlab.lib.utils import open_and_read
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   114
        b = open_and_read('http://www.reportlab.com/rsrc/encryption.gif')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   115
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   116
    def test10(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   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
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   120
        b = open_and_read(b)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   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
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   148
def makeSuite():
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   149
    return makeSuiteForClasses(ImporterTestCase)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   150
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   151
if __name__ == "__main__": #noruntests
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   152
    unittest.TextTestRunner().run(makeSuite())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   153
    printLocation()