root/tags/0.5.0/tests/pie.py

Revision 139, 5.4 kB (checked in by lgs, 3 years ago)

Index the ticks by xval and not name. This fixes a broken example

Line 
1# Copyright (c) 2007-2008 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/>.
17import math
18import unittest
19
20import cairo
21
22import pycha.pie
23
24class SliceTests(unittest.TestCase):
25
26    def test_init(self):
27        slice = pycha.pie.Slice('test', 3/5.0, 0, 4, 1/4.0)
28        self.assertEqual(slice.name, 'test')
29        self.assertEqual(slice.fraction, 3/5.0)
30        self.assertEqual(slice.xval, 0)
31        self.assertEqual(slice.yval, 4)
32        self.assertEqual(slice.startAngle, math.pi / 2)
33        self.assertEqual(slice.endAngle, 1.7 * math.pi)
34
35    def test_isBigEnough(self):
36        slice = pycha.pie.Slice('test 1', 3/5.0, 0, 4, 1/4.0)
37        self.assertEqual(slice.isBigEnough(), True)
38
39        slice = pycha.pie.Slice('test 2', 1/10000.0, 0, 4, 1/4.0)
40        self.assertEqual(slice.isBigEnough(), False)
41
42    def test_normalisedAngle(self):
43        # First quadrant
44        slice = pycha.pie.Slice('test 1', 1/6.0, 0, 4, 0)
45        self.assertAlmostEqual(slice.getNormalisedAngle(), 1/6.0 * math.pi, 4)
46
47        # Second quadrant
48        slice = pycha.pie.Slice('test 1', 1/6.0, 0, 4, 1/4.0)
49        self.assertAlmostEqual(slice.getNormalisedAngle(), 2/3.0 * math.pi, 4)
50
51        # Third quadrant
52        slice = pycha.pie.Slice('test 1', 1/6.0, 0, 4, 1/2.0)
53        self.assertAlmostEqual(slice.getNormalisedAngle(), 7/6.0 * math.pi, 4)
54
55        # Fouth quadrant
56        slice = pycha.pie.Slice('test 1', 1/6.0, 0, 4, 3/4.0)
57        self.assertAlmostEqual(slice.getNormalisedAngle(), 10/6.0 * math.pi, 4)
58
59        # Bigger than a circle
60        slice = pycha.pie.Slice('test 1', 2/3.0, 0, 4, 3/4.0)
61        self.assertAlmostEqual(slice.getNormalisedAngle(), 1/6.0 * math.pi, 4)
62
63        # Negative angle
64        slice = pycha.pie.Slice('test 1', -1/6.0, 0, 4, 0)
65        self.assertAlmostEqual(slice.getNormalisedAngle(), 11/6.0 * math.pi, 4)
66
67class PieTests(unittest.TestCase):
68
69    def test_init(self):
70        ch = pycha.pie.PieChart(None)
71        self.assertEqual(ch.slices, [])
72        self.assertEqual(ch.centerx, 0)
73        self.assertEqual(ch.centery, 0)
74        self.assertEqual(ch.radius, 0)
75
76    def test_updateChart(self):
77        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
78        dataset = (
79            ('dataset1', ([0, 10],)),
80            ('dataset2', ([0, 20],)),
81            ('dataset3', ([0, 70],)),
82            )
83        opt = {'padding': {'left': 0, 'right': 0, 'top': 0, 'bottom': 0},
84               'pieRadius': 0.5}
85        ch = pycha.pie.PieChart(surface, opt)
86        ch.addDataset(dataset)
87        ch._updateXY()
88        ch._updateChart()
89        self.assertEqual(ch.centerx, 250)
90        self.assertEqual(ch.centery, 250)
91        self.assertEqual(ch.radius, 250)
92
93        slices = (
94            pycha.pie.Slice('dataset1', 0.1, 0, 10, 0),
95            pycha.pie.Slice('dataset2', 0.2, 1, 20, 0.1),
96            pycha.pie.Slice('dataset3', 0.7, 2, 70, 0.3),
97            )
98
99        for i, slice in enumerate(slices):
100            s1, s2 = ch.slices[i], slice
101            self.assertEqual(s1.name, s2.name)
102            self.assertAlmostEqual(s1.fraction, s2.fraction, 4)
103            self.assertAlmostEqual(s1.startAngle, s2.startAngle, 4)
104            self.assertAlmostEqual(s1.endAngle, s2.endAngle, 4)
105            self.assertEqual(s1.xval, s2.xval)
106            self.assertEqual(s1.yval, s2.yval)
107
108    def test_updateTicks(self):
109        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
110        dataset = (
111            ('dataset1', ([0, 10],)),
112            ('dataset2', ([0, 20],)),
113            ('dataset3', ([0, 70],)),
114            )
115        opt = {'padding': {'left': 0, 'right': 0, 'top': 0, 'bottom': 0},
116               'pieRadius': 0.5}
117        ch = pycha.pie.PieChart(surface, opt)
118        ch.addDataset(dataset)
119        ch._updateXY()
120        ch._updateChart()
121        ch._updateTicks()
122        self.assertEqual(ch.xticks, [(0, 'dataset1 (10.0%)'),
123                                     (1, 'dataset2 (20.0%)'),
124                                     (2, 'dataset3 (70.0%)')])
125
126        ticks = [{'v': 0, 'label': 'First dataset'},
127                 {'v': 1, 'label': 'Second dataset'},
128                 {'v': 2, 'label': 'Third dataset'}]
129        opt = {'axis': {'x': {'ticks': ticks},},}
130        ch = pycha.pie.PieChart(surface, opt)
131        ch.addDataset(dataset)
132        ch._updateXY()
133        ch._updateChart()
134        ch._updateTicks()
135        self.assertEqual(ch.xticks, [(0, 'First dataset (10.0%)'),
136                                     (1, 'Second dataset (20.0%)'),
137                                     (2, 'Third dataset (70.0%)')])
138
139
140def test_suite():
141    return unittest.TestSuite((
142        unittest.makeSuite(SliceTests),
143        unittest.makeSuite(PieTests),
144    ))
145
146if __name__ == '__main__':
147    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the browser.