author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 05 Mar 2010 09:24:55 +0100 | |
branch | stable |
changeset 4809 | 1e983e926cca |
parent 4676 | b0937a191f94 |
child 5031 | 60c4dea96afa |
permissions | -rw-r--r-- |
2952
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
1 |
from unittest import TestCase |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
2 |
import os.path as osp |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
3 |
from xml.etree.cElementTree import ElementTree, fromstring, tostring, dump |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
4 |
|
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
5 |
from tempfile import NamedTemporaryFile |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
6 |
from subprocess import Popen as sub |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
7 |
|
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
8 |
from cubicweb.utils import can_do_pdf_conversion |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
9 |
|
3080
32c2b6a34ab2
move xhtml2fo to ext, allow to specify the section in url/get param
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2952
diff
changeset
|
10 |
from cubicweb.ext.xhtml2fo import ReportTransformer |
2952
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
11 |
|
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
12 |
DATADIR = osp.join(osp.dirname(__file__), 'data') |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
13 |
|
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
14 |
class PDFTC(TestCase): |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
15 |
|
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
16 |
def test_xhtml_to_fop_to_pdf(self): |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
17 |
if not can_do_pdf_conversion(): |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
18 |
self.skip('dependencies not available : check pysixt and fop') |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
19 |
xmltree = ElementTree() |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
20 |
xmltree.parse(osp.join(DATADIR, 'sample1.xml')) |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
21 |
foptree = ReportTransformer(u'contentmain').transform(xmltree) |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
22 |
# next |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
23 |
foptmp = NamedTemporaryFile() |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
24 |
foptree.write(foptmp) |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
25 |
foptmp.flush() |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
26 |
pdftmp = NamedTemporaryFile() |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
27 |
fopproc = sub(['/usr/bin/fop', foptmp.name, pdftmp.name]) |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
28 |
fopproc.wait() |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
29 |
del foptmp |
4676
b0937a191f94
[test] skip test when fop failed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3259
diff
changeset
|
30 |
if fopproc.returncode: |
b0937a191f94
[test] skip test when fop failed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3259
diff
changeset
|
31 |
self.skip('fop returned status %s' % fopproc.returncode) |
2952
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
32 |
pdftmp.seek(0) # a bit superstitious |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
33 |
reference = open(osp.join(DATADIR, 'sample1.pdf'), 'r').read() |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
34 |
output = pdftmp.read() |
6e871df30058
[pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff
changeset
|
35 |
# XXX almost equals due to ID, creation date, so it seems to fail |
3259
2c5c1f434640
ensure unittest_pdf passes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3080
diff
changeset
|
36 |
self.assertEquals( len(output), len(reference) ) |
2c5c1f434640
ensure unittest_pdf passes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3080
diff
changeset
|
37 |
# cut begin & end 'cause they contain variyng data |
2c5c1f434640
ensure unittest_pdf passes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3080
diff
changeset
|
38 |
self.assertTextEquals(output[150:1500], reference[150:1500]) |