root/relatorio/tests/test_api.py @ 89:757b097c61a5

Revision 89:757b097c61a5, 4.0 kB (checked in by Ga?tan de Menten <ged@…>, 4 years ago)

- simplified and optimized opening/closing tags matching code, and moved it

into the _relatorio_statements method

- optimized genshi tags replacement loop (common ancestor block)

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