-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtest_invariant.py
72 lines (62 loc) · 2.47 KB
/
test_invariant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/env python
#Copyright ReportLab Europe Ltd. 2000-2017
#see license.txt for license details
__doc__="""Verfy that if in invariant mode, repeated runs
make identical file. This does NOT test across platforms
or python versions, only a user can do that :-)"""
__version__='3.3.0'
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
setOutDir(__name__)
import unittest
from reportlab.pdfgen.canvas import Canvas
filename = outputfile('test_invariant.pdf')
class InvarTestCase(unittest.TestCase):
"Simplest test that makes PDF"
def test0(self):
import os
from reportlab.lib.testutils import testsFolder
c = Canvas(filename, invariant=1, pageCompression=0)
c.setFont('Helvetica-Bold', 36)
c.drawString(100,700, 'Hello World')
gif = os.path.join(testsFolder,'pythonpowered.gif')
c.drawImage(gif,100,600)
c.save()
with open(filename, 'rb') as f:
raw1 = f.read()
c = Canvas(filename, invariant=1, pageCompression=0)
c.setFont('Helvetica-Bold', 36)
c.drawString(100,700, 'Hello World')
c.drawImage(gif,100,600)
c.save()
with open(filename, 'rb') as f:
raw2 = f.read()
assert raw1 == raw2, 'repeated runs differ!'
def test1(self):
import os
from reportlab.lib.testutils import testsFolder
filename = outputfile('test_no_helvetica.pdf')
c = Canvas(filename, invariant=1, pageCompression=0, initialFontName='Times-Roman')
c.drawString(100,700, 'Hello World')
c.save()
with open(filename, 'rb') as f:
raw = f.read()
assert b'Helvetica' not in raw and b'Times-Roman' in raw, 'Canvas initialFontName expectations not satisfied!'
def makeSuite():
return makeSuiteForClasses(InvarTestCase)
#noruntests
if __name__ == "__main__":
# add some diagnostics, useful in invariant tests
import sys, os
from hashlib import md5
from reportlab.lib.utils import bytestr
verbose = ('-v' in sys.argv)
unittest.TextTestRunner().run(makeSuite())
if verbose:
#tell us about the file we produced
fileSize = os.stat(filename)[6]
raw = open(filename,'rb').read()
digest = md5(bytestr(raw)).hexdigest()
major, minor = sys.version_info[0:2]
print('%s on %s (Python %d.%d):\n %d bytes, digest %s' % (
filename,sys.platform, major, minor, fileSize, digest))
printLocation()