web/test/unittest_pdf.py
author Alexandre Fayolle <alexandre.fayolle@logilab.fr>
Wed, 07 Apr 2010 11:56:30 +0200
branchstable
changeset 5171 1c2567af37dd
parent 5053 2021b3237015
child 5421 8167de96c523
permissions -rw-r--r--
merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2952
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
     1
import os.path as osp
5053
2021b3237015 use lgc.testlib.TestCase
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5031
diff changeset
     2
from tempfile import NamedTemporaryFile
2021b3237015 use lgc.testlib.TestCase
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5031
diff changeset
     3
from subprocess import Popen as sub
2952
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
     4
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
     5
5053
2021b3237015 use lgc.testlib.TestCase
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5031
diff changeset
     6
from logilab.common.testlib import TestCase, unittest_main
2952
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
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
     9
from cubicweb.ext.xhtml2fo import ReportTransformer
2952
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    10
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    11
DATADIR = osp.join(osp.dirname(__file__), 'data')
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    12
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    13
class PDFTC(TestCase):
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    14
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    15
    def test_xhtml_to_fop_to_pdf(self):
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    16
        if not can_do_pdf_conversion():
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    17
            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
    18
        xmltree = ElementTree()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    19
        xmltree.parse(osp.join(DATADIR, 'sample1.xml'))
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    20
        foptree = ReportTransformer(u'contentmain').transform(xmltree)
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    21
        # next
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    22
        foptmp = NamedTemporaryFile()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    23
        foptree.write(foptmp)
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    24
        foptmp.flush()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    25
        pdftmp = NamedTemporaryFile()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    26
        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
    27
        fopproc.wait()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    28
        del foptmp
4676
b0937a191f94 [test] skip test when fop failed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3259
diff changeset
    29
        if fopproc.returncode:
b0937a191f94 [test] skip test when fop failed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3259
diff changeset
    30
            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
    31
        pdftmp.seek(0) # a bit superstitious
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    32
        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
    33
        output = pdftmp.read()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    34
        # 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
    35
        self.assertEquals( len(output), len(reference) )
2c5c1f434640 ensure unittest_pdf passes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3080
diff changeset
    36
        # cut begin & end 'cause they contain variyng data
2c5c1f434640 ensure unittest_pdf passes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3080
diff changeset
    37
        self.assertTextEquals(output[150:1500], reference[150:1500])
5031
60c4dea96afa __missing__ main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4676
diff changeset
    38
60c4dea96afa __missing__ main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4676
diff changeset
    39
if __name__ == '__main__':
60c4dea96afa __missing__ main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4676
diff changeset
    40
    unittest_main()
60c4dea96afa __missing__ main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4676
diff changeset
    41