web/test/unittest_pdf.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 29 Mar 2010 10:20:01 +0200
branchstable
changeset 5054 cb066d29166a
parent 5053 2021b3237015
child 5421 8167de96c523
permissions -rw-r--r--
fix dataimport for 3.7.2

import os.path as osp
from tempfile import NamedTemporaryFile
from subprocess import Popen as sub
from xml.etree.cElementTree import ElementTree, fromstring, tostring, dump

from logilab.common.testlib import TestCase, unittest_main

from cubicweb.utils import can_do_pdf_conversion
from cubicweb.ext.xhtml2fo import ReportTransformer

DATADIR = osp.join(osp.dirname(__file__), 'data')

class PDFTC(TestCase):

    def test_xhtml_to_fop_to_pdf(self):
        if not can_do_pdf_conversion():
            self.skip('dependencies not available : check pysixt and fop')
        xmltree = ElementTree()
        xmltree.parse(osp.join(DATADIR, 'sample1.xml'))
        foptree = ReportTransformer(u'contentmain').transform(xmltree)
        # next
        foptmp = NamedTemporaryFile()
        foptree.write(foptmp)
        foptmp.flush()
        pdftmp = NamedTemporaryFile()
        fopproc = sub(['/usr/bin/fop', foptmp.name, pdftmp.name])
        fopproc.wait()
        del foptmp
        if fopproc.returncode:
            self.skip('fop returned status %s' % fopproc.returncode)
        pdftmp.seek(0) # a bit superstitious
        reference = open(osp.join(DATADIR, 'sample1.pdf'), 'r').read()
        output = pdftmp.read()
        # XXX almost equals due to ID, creation date, so it seems to fail
        self.assertEquals( len(output), len(reference) )
        # cut begin & end 'cause they contain variyng data
        self.assertTextEquals(output[150:1500], reference[150:1500])

if __name__ == '__main__':
    unittest_main()