root/trunk/tests/chart.py

Revision 174, 9.4 kB (checked in by lgs, 3 years ago)

Add a test for the interval option. Closes #28

Line 
1# Copyright(c) 2007-2009 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
2#
3# This file is part of PyCha.
4#
5# PyCha is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# PyCha is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public License
16# along with PyCha.  If not, see <http://www.gnu.org/licenses/>.
17
18import unittest
19
20import cairo
21
22import pycha.chart
23
24
25class FunctionsTests(unittest.TestCase):
26
27    def test_uniqueIndices(self):
28        arr = (range(10), range(5), range(20), range(30))
29        self.assertEqual(pycha.chart.uniqueIndices(arr), range(30))
30
31        arr = (range(30), range(20), range(5), range(10))
32        self.assertEqual(pycha.chart.uniqueIndices(arr), range(30))
33
34        arr = (range(4), )
35        self.assertEqual(pycha.chart.uniqueIndices(arr), range(4))
36
37        arr = (range(0), )
38        self.assertEqual(pycha.chart.uniqueIndices(arr), [])
39
40
41class AreaTests(unittest.TestCase):
42
43    def test_area(self):
44        area = pycha.chart.Area(10, 20, 100, 300)
45        self.assertEqual(area.x, 10)
46        self.assertEqual(area.y, 20)
47        self.assertEqual(area.w, 100)
48        self.assertEqual(area.h, 300)
49        self.assertEqual(area.origin, 0.0)
50        msg = "<pycha.chart.Area@(10.00, 20.00) 100.00 x 300.00 Origin: 0.00>"
51        self.assertEqual(str(area), msg)
52
53
54class OptionTests(unittest.TestCase):
55
56    def test_options(self):
57        opt = pycha.chart.Option(a=1, b=2, c=3)
58        self.assertEqual(opt.a, opt['a'])
59        self.assertEqual(opt.b, 2)
60        self.assertEqual(opt['c'], 3)
61
62        opt = pycha.chart.Option({'a': 1, 'b': 2, 'c': 3})
63        self.assertEqual(opt.a, opt['a'])
64        self.assertEqual(opt.b, 2)
65        self.assertEqual(opt['c'], 3)
66
67    def test_merge(self):
68        opt = pycha.chart.Option(a=1, b=2,
69                                 c=pycha.chart.Option(d=4, e=5))
70        self.assertEqual(opt.c.d, 4)
71        opt.merge(dict(c=pycha.chart.Option(d=7, e=8, f=9)))
72        self.assertEqual(opt.c.d, 7)
73        # new attributes not present in original option are not merged
74        self.assertRaises(AttributeError, getattr, opt.c, 'f')
75
76        opt.merge(pycha.chart.Option(a=10, b=20))
77        self.assertEqual(opt.a, 10)
78        self.assertEqual(opt.b, 20)
79
80
81class ChartTests(unittest.TestCase):
82
83    def test_init(self):
84        ch = pycha.chart.Chart(None)
85        self.assertEqual(ch.resetFlag, False)
86        self.assertEqual(ch.datasets, [])
87        self.assertEqual(ch.area, None)
88        self.assertEqual(ch.minxval, None)
89        self.assertEqual(ch.maxxval, None)
90        self.assertEqual(ch.minyval, None)
91        self.assertEqual(ch.maxyval, None)
92        self.assertEqual(ch.xscale, 1.0)
93        self.assertEqual(ch.yscale, 1.0)
94        self.assertEqual(ch.xrange, None)
95        self.assertEqual(ch.yrange, None)
96        self.assertEqual(ch.xticks, [])
97        self.assertEqual(ch.yticks, [])
98        self.assertEqual(ch.options, pycha.chart.DEFAULT_OPTIONS)
99
100    def test_datasets(self):
101        ch = pycha.chart.Chart(None)
102        d1 = ('dataset1', ([0, 0], [1, 2], [2, 1.5]))
103        d2 = ('dataset2', ([0, 1], [1, 2], [2, 2.4]))
104        d3 = ('dataset3', ([0, 4], [1, 3], [2, 0.5]))
105        ch.addDataset((d1, d2, d3))
106        self.assertEqual(ch._getDatasetsKeys(),
107                         ['dataset1', 'dataset2', 'dataset3'])
108        self.assertEqual(ch._getDatasetsValues(),
109                         [d1[1], d2[1], d3[1]])
110
111    def test_options(self):
112        ch = pycha.chart.Chart(None)
113        opt = pycha.chart.Option(shouldFill=False)
114        ch.setOptions(opt)
115        self.assertEqual(ch.options.shouldFill, False)
116
117        opt = {'pieRadius': 0.8}
118        ch.setOptions(opt)
119        self.assertEqual(ch.options.pieRadius, 0.8)
120
121    def test_reset(self):
122        ch = pycha.chart.Chart(None, options={'shouldFill': False})
123        self.assertEqual(ch.resetFlag, False)
124        self.assertEqual(ch.options.shouldFill, False)
125        dataset = (('dataset1', ([0, 1], [1, 1])), )
126        ch.addDataset(dataset)
127        self.assertEqual(ch._getDatasetsKeys(), ['dataset1'])
128        ch.reset()
129        defaultFill = pycha.chart.DEFAULT_OPTIONS.shouldFill
130        self.assertEqual(ch.options.shouldFill, defaultFill)
131        self.assertEqual(ch.datasets, [])
132        self.assertEqual(ch.resetFlag, True)
133
134    def test_colorscheme(self):
135        options = {'colorScheme': {'name': 'gradient',
136                                   'args': {'initialColor': '#000000'}}}
137        ch = pycha.chart.Chart(None, options)
138        dataset = (('dataset1', ([0, 1], [1, 1])), )
139        ch.addDataset(dataset)
140        ch._setColorscheme()
141        self.assert_(isinstance(ch.colorScheme, dict))
142        self.assertEqual(ch.colorScheme, {'dataset1': (0.0, 0.0, 0.0)})
143
144        options = {'colorScheme': {'name': 'foo'}}
145        ch = pycha.chart.Chart(None, options)
146        ch.addDataset(dataset)
147        self.assertRaises(ValueError, ch._setColorscheme)
148
149    def test_updateXY(self):
150        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
151        opt = {'padding': dict(left=10, right=10, top=10, bottom=10)}
152        dataset = (
153            ('dataset1', ([0, 1], [1, 1], [2, 3])),
154            ('dataset2', ([0, 2], [1, 0], [3, 4])),
155            )
156        ch = pycha.chart.Chart(surface, opt)
157        ch.addDataset(dataset)
158        ch._updateXY()
159        self.assertEqual((ch.area.x, ch.area.y, ch.area.w, ch.area.h),
160                         (10, 10, 480, 480))
161        self.assertEqual(ch.minxval, 0.0)
162        self.assertEqual(ch.maxxval, 3)
163        self.assertEqual(ch.xrange, 3)
164        self.assertEqual(ch.xscale, 1/3.0)
165
166        self.assertEqual(ch.minyval, 0)
167        self.assertEqual(ch.maxyval, 4)
168        self.assertEqual(ch.yrange, 4)
169        self.assertEqual(ch.yscale, 1/4.0)
170        # TODO: test with different options (axis.range, ...)
171
172    def test_updateTicks(self):
173        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
174        opt = {'padding': dict(left=10, right=10, top=10, bottom=10)}
175        dataset = (
176            ('dataset1', ([0, 1], [1, 1], [2, 3])),
177            ('dataset2', ([0, 2], [1, 0], [3, 4])),
178            )
179        ch = pycha.chart.Chart(surface, opt)
180        ch.addDataset(dataset)
181        ch._updateXY()
182        ch._updateTicks()
183        xticks = [(0.0, 0), (1/3.0, 1), (2/3.0, 2)]
184        for i in range(len(xticks)):
185            self.assertAlmostEqual(ch.xticks[i][0], xticks[i][0], 4)
186            self.assertAlmostEqual(ch.xticks[i][1], xticks[i][1], 4)
187
188        yticks = [(1 - 0.1 * i, 0.4*i)
189                  for i in range(ch.options.axis.y.tickCount + 1)]
190        self.assertEqual(len(ch.yticks), len(yticks))
191        for i in range(len(yticks)):
192            self.assertAlmostEqual(ch.yticks[i][0], yticks[i][0], 4)
193            self.assertAlmostEqual(ch.yticks[i][1], yticks[i][1], 4)
194
195    def _test_updateExplicitTicks(self):
196        """Test for bug #7"""
197        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
198        yticks = [dict(v=i, label=str(i)) for i in range(0, 3)]
199        opt = {'axis': {'y': {'ticks': yticks}}}
200        dataset = (
201            ('dataset1', ([0, 1], [1, 1], [2, 3])),
202            )
203        ch = pycha.chart.Chart(surface, opt)
204        ch.addDataset(dataset)
205        ch._updateXY()
206        ch._updateTicks()
207        self.assertAlmostEqual(ch.yticks[0][0], 1.0, 4)
208        self.assertAlmostEqual(ch.yticks[1][0], 2/3.0, 4)
209        self.assertAlmostEqual(ch.yticks[2][0], 1/3.0, 4)
210
211    def test_abstractChart(self):
212        ch = pycha.chart.Chart(None)
213        self.assertRaises(NotImplementedError, ch._updateChart)
214        self.assertRaises(NotImplementedError, ch._renderChart, None)
215
216    def test_range(self):
217        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
218        opt = {'axis': {'x': {'range': (1, 10)}, 'y': {'range': (1.0, 10.0)}}}
219        ch = pycha.chart.Chart(surface, opt)
220        dataset = (
221            ('dataset1', ([0, 1], [1, 1], [2, 3])),
222            )
223        ch.addDataset(dataset)
224        ch._updateXY()
225        self.assertAlmostEqual(ch.xrange, 9, 4)
226        self.assertAlmostEqual(ch.yrange, 9, 4)
227        self.assertAlmostEqual(ch.xscale, 0.1111, 4)
228        self.assertAlmostEqual(ch.yscale, 0.1111, 4)
229
230    def test_interval(self):
231        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
232        opt = {'axis': {'y': {'interval': 2.5}}}
233        ch = pycha.chart.Chart(surface, opt)
234        dataset = (
235            ('dataset1', ([0, 1], [1, 4], [2, 10])),
236            )
237        ch.addDataset(dataset)
238        ch._updateXY()
239        ch._updateTicks()
240        yticks = ((0.75, 2.5), (0.5, 5.0),
241                  (0.25, 7.5), (0.0, 10.0))
242
243        self.assertEqual(len(yticks), len(ch.yticks))
244        for i, (pos, label) in enumerate(yticks):
245            tick = ch.yticks[i]
246            self.assertAlmostEqual(tick[0], pos, 2)
247            self.assertAlmostEqual(tick[1], label, 2)
248
249
250def test_suite():
251    return unittest.TestSuite((
252        unittest.makeSuite(FunctionsTests),
253        unittest.makeSuite(AreaTests),
254        unittest.makeSuite(OptionTests),
255        unittest.makeSuite(ChartTests),
256    ))
257
258
259if __name__ == '__main__':
260    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the browser.