web/test/unittest_pdf.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 20 Jan 2010 11:31:38 +0100
changeset 4290 c2f15166f019
parent 3259 2c5c1f434640
child 4676 b0937a191f94
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
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
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    30
        pdftmp.seek(0) # a bit superstitious
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    31
        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
    32
        output = pdftmp.read()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    33
        # 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
    34
        self.assertEquals( len(output), len(reference) )
2c5c1f434640 ensure unittest_pdf passes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3080
diff changeset
    35
        # cut begin & end 'cause they contain variyng data
2c5c1f434640 ensure unittest_pdf passes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3080
diff changeset
    36
        self.assertTextEquals(output[150:1500], reference[150:1500])