root/relatorio/tests/test_api.py @ 60:deb52535750d

Revision 60:deb52535750d, 3.7 kB (checked in by Nicolas ?vrard <nicoe@…>, 5 years ago)

Use the same signature for generate and for report.call

Using coverage.py to complete the tests

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 reporting import (ReportRepository, Report, MIMETemplateLoader,
26                       DefaultFactory, _absolute)
27
28
29class StubObject(object):
30
31    def __init__(self, **kwargs):
32        for key, val in kwargs.iteritems():
33            setattr(self, key, val)
34
35def setup():
36    MIMETemplateLoader.load_template_engines()
37
38class TestRepository(object):
39
40    def test_register(self):
41        "Testing the registration"
42        reporting = ReportRepository()
43        reporting.add_report(StubObject, 'text/plain', 
44                             os.path.join('templates', 'test.tmpl'))
45
46        assert_true(StubObject in reporting.reports)
47        assert_true('default' in reporting.reports[StubObject])
48        assert_true('text/plain' in reporting.reports[StubObject])
49
50        report, mime = reporting.reports[StubObject]['default']
51        eq_(mime, 'text/plain')
52        eq_(report.mimetype, 'text/plain')
53        assert_true(report.fpath.endswith(os.path.join('templates', 
54                                                       'test.tmpl')))
55
56        name, report2 = reporting.reports[StubObject]['text/plain'][0]
57        eq_(name, 'default')
58        eq_(report, report2)
59
60    def abspath_helper(self, path):
61        return _absolute(path)
62
63    def test_absolute(self):
64        "Test the absolute path calculation"
65        eq_("/home/nicoe/python/mock.py",
66            _absolute("/home/nicoe/python/mock.py"))
67
68        our_dir, _ = os.path.split(__file__)
69        # We use this because me go up by two frames
70        new_path = self.abspath_helper(os.path.join('brol', 'toto'))
71        eq_(os.path.join(our_dir, 'brol', 'toto'), new_path)
72
73
74class TestReport(object):
75
76    def setup(self):
77        self.loader = MIMETemplateLoader()
78        our_dir, _ = os.path.split(__file__)
79        self.report = Report(os.path.join(our_dir, 'templates', 'test.tmpl'),
80                             'text/plain', DefaultFactory(), self.loader)
81
82    def test_report(self):
83        "Testing the report generation"
84        a = StubObject(name='OpenHex')
85        eq_(self.report(o=a).render(), 'Hello OpenHex.\n')
86
87    def test_factory(self):
88        "Testing the data factory"
89        class MyFactory:
90            def __call__(self, o, time, y=1):
91                d = dict()
92                d['o'] = o
93                d['y'] = y
94                d['time'] = time
95                d['func'] = lambda x: x+1
96                return d
97
98        our_dir, _ = os.path.split(__file__)
99        report = Report(os.path.join(our_dir, 'templates', 'time.tmpl'),
100                        'text/plain', MyFactory(), self.loader)
101
102        a = StubObject(name='Foo')
103        eq_(report(o=a, time="One o'clock").render(), 
104            "Hi Foo,\nIt's One o'clock to 2 !\n")
105        eq_(report(o=a, time="One o'clock", y=4).render(), 
106            "Hi Foo,\nIt's One o'clock to 5 !\n")
107        assert_raises(TypeError, report, a) 
Note: See TracBrowser for help on using the browser.