/usr/share/python-relatorio/tests/test_api.py is in python-relatorio 0.6.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | ###############################################################################
#
# Copyright (c) 2014 Cedric Krier.
# Copyright (c) 2007, 2008 OpenHex SPRL. (http://openhex.com) All Rights
# Reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
import os
import unittest
from relatorio.reporting import (ReportRepository, Report, MIMETemplateLoader,
DefaultFactory, _absolute, _guess_type)
class StubObject(object):
def __init__(self, **kwargs):
for key, val in kwargs.iteritems():
setattr(self, key, val)
class TestRepository(unittest.TestCase):
def test_register(self):
"Testing the registration"
reporting = ReportRepository()
reporting.add_report(StubObject, 'text/plain',
os.path.join('templates', 'test.tmpl'),
description='Test report')
self.assertTrue(StubObject in reporting.classes)
self.assertTrue('default' in reporting.classes[StubObject].ids)
self.assertTrue(
'text/plain' in reporting.classes[StubObject].mimetypes)
report, mime, desc = reporting.classes[StubObject].ids['default']
self.assertEqual(mime, 'text/plain')
self.assertEqual(desc, 'Test report')
self.assertEqual(report.mimetype, 'text/plain')
self.assertTrue(report.fpath.endswith(os.path.join('templates',
'test.tmpl')))
report2, name = (reporting.classes[StubObject]
.mimetypes['text/plain'][0])
self.assertEqual(name, 'default')
self.assertEqual(report, report2)
def test_mimeguesser(self):
self.assertEqual(_guess_type('application/pdf'), 'pdf')
self.assertEqual(_guess_type('text/plain'), 'text')
self.assertEqual(_guess_type('text/xhtml'), 'markup')
self.assertEqual(
_guess_type('application/vnd.oasis.opendocument.text'), 'oo.org')
def abspath_helper(self, path):
return _absolute(path)
def test_absolute(self):
"Test the absolute path calculation"
self.assertEqual("/home/nicoe/python/mock.py",
_absolute("/home/nicoe/python/mock.py"))
our_dir, _ = os.path.split(__file__)
# We use this because me go up by two frames
new_path = self.abspath_helper(os.path.join('brol', 'toto'))
self.assertEqual(os.path.join(our_dir, 'brol', 'toto'), new_path)
class TestReport(unittest.TestCase):
def setUp(self):
self.loader = MIMETemplateLoader()
our_dir, _ = os.path.split(__file__)
self.report = Report(os.path.join(our_dir, 'templates', 'test.tmpl'),
'text/plain', DefaultFactory(), self.loader)
def test_report(self):
"Testing the report generation"
a = StubObject(name='OpenHex')
self.assertEqual(self.report(o=a).render(), 'Hello OpenHex.\n')
def test_factory(self):
"Testing the data factory"
class MyFactory:
def __call__(self, o, time, y=1):
d = dict()
d['o'] = o
d['y'] = y
d['time'] = time
d['func'] = lambda x: x + 1
return d
our_dir, _ = os.path.split(__file__)
report = Report(os.path.join(our_dir, 'templates', 'time.tmpl'),
'text/plain', MyFactory(), self.loader)
a = StubObject(name='Foo')
self.assertEqual(report(o=a, time="One o'clock").render(),
"Hi Foo,\nIt's One o'clock to 2 !\n")
self.assertEqual(report(o=a, time="One o'clock", y=4).render(),
"Hi Foo,\nIt's One o'clock to 5 !\n")
self.assertRaises(TypeError, report, a)
class TestReportInclude(unittest.TestCase):
def test_include(self):
our_dir = os.path.dirname(__file__)
template_path = os.path.join(our_dir, 'templates')
relative_report = Report(os.path.join(template_path, 'include.tmpl'),
'text/plain')
self.assertEqual(relative_report().render(), 'Another Hello.\n\n')
|