| 1 | # -*- encoding: utf-8 -*- |
|---|
| 2 | ############################################################################### |
|---|
| 3 | # |
|---|
| 4 | # Copyright (c) 2007, 2008 OpenHex SPRL. (http://openhex.com) All Rights |
|---|
| 5 | # Reserved. |
|---|
| 6 | # |
|---|
| 7 | # This program is free software; you can redistribute it and/or modify it under |
|---|
| 8 | # the terms of the GNU General Public License as published by the Free Software |
|---|
| 9 | # Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 10 | # version. |
|---|
| 11 | # |
|---|
| 12 | # This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 13 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 14 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|---|
| 15 | # details. |
|---|
| 16 | # |
|---|
| 17 | # You should have received a copy of the GNU General Public License along with |
|---|
| 18 | # this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 19 | # |
|---|
| 20 | ############################################################################### |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | import os |
|---|
| 24 | import re |
|---|
| 25 | from cStringIO import StringIO |
|---|
| 26 | |
|---|
| 27 | import lxml.etree |
|---|
| 28 | from nose.tools import * |
|---|
| 29 | import genshi |
|---|
| 30 | from genshi.filters import Translator |
|---|
| 31 | from genshi.core import PI |
|---|
| 32 | from genshi.template.eval import UndefinedError |
|---|
| 33 | |
|---|
| 34 | from templates.opendocument import Template, GENSHI_EXPR |
|---|
| 35 | |
|---|
| 36 | def pseudo_gettext(string): |
|---|
| 37 | catalog = {'Mes collègues sont:': 'My colleagues are:', |
|---|
| 38 | 'Bonjour,': 'Hello,', |
|---|
| 39 | 'Je suis un test de templating en odt.': |
|---|
| 40 | 'I am an odt templating test', |
|---|
| 41 | 'Felix da housecat': unicode('Félix le chat de la maison', |
|---|
| 42 | 'utf8'), |
|---|
| 43 | 'We sell stuff': u'On vend des choses', |
|---|
| 44 | } |
|---|
| 45 | return catalog.get(string, string) |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | class TestOOTemplating(object): |
|---|
| 49 | |
|---|
| 50 | def setup(self): |
|---|
| 51 | thisdir = os.path.dirname(__file__) |
|---|
| 52 | filepath = os.path.join(thisdir, 'test.odt') |
|---|
| 53 | self.oot = Template(file(filepath), filepath) |
|---|
| 54 | self.data = {'first_name': u'Trente', |
|---|
| 55 | 'last_name': unicode('Møller', 'utf8'), |
|---|
| 56 | 'ville': unicode('Liège', 'utf8'), |
|---|
| 57 | 'friends': [{'first_name': u'Camille', |
|---|
| 58 | 'last_name': u'Salauhpe'}, |
|---|
| 59 | {'first_name': u'Mathias', |
|---|
| 60 | 'last_name': u'Lechat'}], |
|---|
| 61 | 'hobbies': [u'Music', u'Dancing', u'DJing'], |
|---|
| 62 | 'animals': [u'Felix da housecat', u'Dog eat Dog'], |
|---|
| 63 | 'images': [(file(os.path.join(thisdir, 'one.jpg')), |
|---|
| 64 | 'image/jpeg'), |
|---|
| 65 | (file(os.path.join(thisdir, 'two.png')), |
|---|
| 66 | 'image/png')], |
|---|
| 67 | 'oeuf': file(os.path.join(thisdir, 'egg.jpg')), |
|---|
| 68 | 'footer': u'We sell stuff'} |
|---|
| 69 | |
|---|
| 70 | def test_init(self): |
|---|
| 71 | "Testing the correct handling of the styles.xml and content.xml files" |
|---|
| 72 | ok_(isinstance(self.oot.stream, list)) |
|---|
| 73 | eq_(self.oot.stream[0], (PI, ('relatorio', 'content.xml'), None)) |
|---|
| 74 | ok_((PI, ('relatorio', 'content.xml'), None) in self.oot.stream) |
|---|
| 75 | |
|---|
| 76 | def test_directives(self): |
|---|
| 77 | "Testing the directives interpolation" |
|---|
| 78 | xml = '''<b:a xmlns:b="urn:b" xmlns:text="%s" xmlns:draw="urn:draw" |
|---|
| 79 | xmlns:table="urn:table" xmlns:office="urn:office" |
|---|
| 80 | xmlns:xlink="urn:xlink"> |
|---|
| 81 | <text:a xlink:href="relatorio://foo">foo</text:a> |
|---|
| 82 | </b:a>''' % 'urn:text' |
|---|
| 83 | interpolated = self.oot.insert_directives(xml) |
|---|
| 84 | root_interpolated = lxml.etree.parse(interpolated).getroot() |
|---|
| 85 | root_attrs = root_interpolated[0].attrib |
|---|
| 86 | eq_(root_attrs['{http://genshi.edgewall.org/}replace'], 'foo') |
|---|
| 87 | |
|---|
| 88 | def test_styles(self): |
|---|
| 89 | "Testing that styles get rendered" |
|---|
| 90 | stream = self.oot.generate(**self.data) |
|---|
| 91 | rendered = stream.events.render() |
|---|
| 92 | ok_('We sell stuff' in rendered) |
|---|
| 93 | |
|---|
| 94 | dico = self.data.copy() |
|---|
| 95 | del dico['footer'] |
|---|
| 96 | stream = self.oot.generate(**dico) |
|---|
| 97 | assert_raises(UndefinedError, lambda: stream.events.render()) |
|---|
| 98 | |
|---|
| 99 | def test_generate(self): |
|---|
| 100 | "Testing that content get rendered" |
|---|
| 101 | stream = self.oot.generate(**self.data) |
|---|
| 102 | rendered = stream.events.render() |
|---|
| 103 | ok_('Bonjour,' in rendered) |
|---|
| 104 | ok_('Trente' in rendered) |
|---|
| 105 | ok_('Møller' in rendered) |
|---|
| 106 | ok_('Dog eat Dog' in rendered) |
|---|
| 107 | ok_('Felix da housecat' in rendered) |
|---|
| 108 | |
|---|
| 109 | def test_filters(self): |
|---|
| 110 | "Testing the filters with the Translator filter" |
|---|
| 111 | stream = self.oot.generate(**self.data) |
|---|
| 112 | translated = stream.filter(Translator(pseudo_gettext)) |
|---|
| 113 | translated_xml = translated.events.render() |
|---|
| 114 | ok_("Hello," in translated_xml) |
|---|
| 115 | ok_("I am an odt templating test" in translated_xml) |
|---|
| 116 | ok_('Felix da housecat' not in translated_xml) |
|---|
| 117 | ok_('Félix le chat de la maison' in translated_xml) |
|---|
| 118 | ok_('We sell stuff' not in translated_xml) |
|---|
| 119 | ok_('On vend des choses' in translated_xml) |
|---|
| 120 | |
|---|
| 121 | def test_images(self): |
|---|
| 122 | "Testing the image replacement directive" |
|---|
| 123 | stream = self.oot.generate(**self.data) |
|---|
| 124 | rendered = stream.events.render() |
|---|
| 125 | styles_idx = rendered.find('<?relatorio styles.xml?>') |
|---|
| 126 | tree = lxml.etree.parse(StringIO(rendered[25:styles_idx])) |
|---|
| 127 | root = tree.getroot() |
|---|
| 128 | images = root.xpath('//draw:frame', namespaces=self.oot.namespaces) |
|---|
| 129 | eq_(len(images), 3) |
|---|
| 130 | eq_(images[0].attrib['{%s}name' % self.oot.namespaces['draw']], |
|---|
| 131 | "image: (oeuf, 'image/png')") |
|---|
| 132 | eq_(images[1].attrib['{%s}name' % self.oot.namespaces['draw']], |
|---|
| 133 | 'image: img') |
|---|
| 134 | eq_(images[1].attrib['{%s}width' % self.oot.namespaces['svg']], |
|---|
| 135 | '1.732cm') |
|---|
| 136 | eq_(images[1].attrib['{%s}height' % self.oot.namespaces['svg']], |
|---|
| 137 | '1.513cm') |
|---|
| 138 | eq_(images[2].attrib['{%s}width' % self.oot.namespaces['svg']], |
|---|
| 139 | '1.732cm') |
|---|
| 140 | eq_(images[2].attrib['{%s}height' % self.oot.namespaces['svg']], |
|---|
| 141 | '1.513cm') |
|---|
| 142 | |
|---|
| 143 | def test_regexp(self): |
|---|
| 144 | "Testing the regexp used to find relatorio tags" |
|---|
| 145 | # a valid expression |
|---|
| 146 | group = GENSHI_EXPR.match('for each="foo in bar"').groups() |
|---|
| 147 | eq_(group, (None, 'for', 'each', 'foo in bar')) |
|---|
| 148 | |
|---|
| 149 | # invalid expr |
|---|
| 150 | group = GENSHI_EXPR.match('foreach="foo in bar"').groups() |
|---|
| 151 | eq_(group, (None, None, None, None)) |
|---|
| 152 | |
|---|
| 153 | # valid closing tags |
|---|
| 154 | group = GENSHI_EXPR.match('/for').groups() |
|---|
| 155 | eq_(group, ('/', 'for', None, None)) |
|---|
| 156 | group = GENSHI_EXPR.match('/for ').groups() |
|---|
| 157 | eq_(group, ('/', 'for', None, None)) |
|---|
| 158 | |
|---|
| 159 | # another non matching expr |
|---|
| 160 | group = GENSHI_EXPR.match('formatLang("en")').groups() |
|---|
| 161 | eq_(group, (None, None, None, None)) |
|---|
| 162 | |
|---|
| 163 | def test_str(self): |
|---|
| 164 | "Testing that a RelatorioStream str returns a bitstream" |
|---|
| 165 | stream = str(self.oot.generate(**self.data)) |
|---|
| 166 | ok_(isinstance(stream, str)) |
|---|