web/test/unittest_pdf.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 28 Apr 2010 12:15:52 +0200
brancholdstable
changeset 5424 8ecbcbff9777
parent 5421 8167de96c523
permissions -rw-r--r--
replace logilab-common by CubicWeb in disclaimer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5053
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
2952
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    18
import os.path as osp
5053
2021b3237015 use lgc.testlib.TestCase
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5031
diff changeset
    19
from tempfile import NamedTemporaryFile
2021b3237015 use lgc.testlib.TestCase
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5031
diff changeset
    20
from subprocess import Popen as sub
2952
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    21
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
    22
5053
2021b3237015 use lgc.testlib.TestCase
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5031
diff changeset
    23
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
    24
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    25
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
    26
from cubicweb.ext.xhtml2fo import ReportTransformer
2952
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    27
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    28
DATADIR = osp.join(osp.dirname(__file__), 'data')
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    29
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    30
class PDFTC(TestCase):
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    31
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    32
    def test_xhtml_to_fop_to_pdf(self):
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    33
        if not can_do_pdf_conversion():
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    34
            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
    35
        xmltree = ElementTree()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    36
        xmltree.parse(osp.join(DATADIR, 'sample1.xml'))
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    37
        foptree = ReportTransformer(u'contentmain').transform(xmltree)
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    38
        # next
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    39
        foptmp = NamedTemporaryFile()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    40
        foptree.write(foptmp)
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    41
        foptmp.flush()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    42
        pdftmp = NamedTemporaryFile()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    43
        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
    44
        fopproc.wait()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    45
        del foptmp
4676
b0937a191f94 [test] skip test when fop failed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3259
diff changeset
    46
        if fopproc.returncode:
b0937a191f94 [test] skip test when fop failed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3259
diff changeset
    47
            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
    48
        pdftmp.seek(0) # a bit superstitious
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    49
        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
    50
        output = pdftmp.read()
6e871df30058 [pdf,tests] test the pdf tranformation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    51
        # 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
    52
        self.assertEquals( len(output), len(reference) )
2c5c1f434640 ensure unittest_pdf passes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3080
diff changeset
    53
        # cut begin & end 'cause they contain variyng data
2c5c1f434640 ensure unittest_pdf passes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3080
diff changeset
    54
        self.assertTextEquals(output[150:1500], reference[150:1500])
5031
60c4dea96afa __missing__ main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4676
diff changeset
    55
60c4dea96afa __missing__ main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4676
diff changeset
    56
if __name__ == '__main__':
60c4dea96afa __missing__ main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4676
diff changeset
    57
    unittest_main()
60c4dea96afa __missing__ main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4676
diff changeset
    58