root/trunk/tests/stackedbar.py

Revision 168, 7.7 kB (checked in by lgs, 3 years ago)

Finish basic tests for stacked bar charts. Closes #27

Line 
1# Copyright (c) 2009 by Yaco S.L. <lgs@yaco.es>
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.stackedbar
23
24
25class StackedBarTests(unittest.TestCase):
26
27    def test_init(self):
28        ch = pycha.stackedbar.StackedBarChart(None)
29        self.assertEqual(ch.barWidth, 0.0)
30
31    def test_updateXY(self):
32        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
33        dataset = (
34            ('dataset1', ((0, 1), (1, 2))),
35            ('dataset2', ((0, 3), (1, 1))),
36        )
37        ch = pycha.stackedbar.StackedBarChart(surface)
38        ch.addDataset(dataset)
39        ch._updateXY()
40
41        self.assertEqual(ch.yrange, 4.0)
42        self.assertAlmostEqual(ch.yscale, 0.25)
43
44    def test_updateChart(self):
45        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
46        dataset = (
47            ('dataset1', ((0, 1), (1, 2))),
48            ('dataset2', ((0, 3), (1, 1))),
49        )
50        ch = pycha.stackedbar.StackedBarChart(surface)
51        ch.addDataset(dataset)
52        ch._updateXY()
53        ch._updateChart()
54
55        self.assertEqual(ch.minxdelta, 1)
56        self.assertAlmostEqual(ch.barWidth, 0.375, 3)
57        self.assertAlmostEqual(ch.barMargin, 0.0625, 4)
58
59
60class StackedVerticalBarTests(unittest.TestCase):
61
62    def test_updateChart(self):
63        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
64        dataset = (
65            ('dataset1', ([0, 3], [1, 4], [2, 2], [3, 5], [4, 3.5])),
66            ('dataset2', ([0, 2], [1, 3], [2, 1], [3, 5], [4, 2.5])),
67            )
68        ch = pycha.stackedbar.StackedVerticalBarChart(surface)
69        ch.addDataset(dataset)
70        ch._updateXY()
71        ch._updateChart()
72        self.assertEqual(ch.minxval, 0)
73        self.assertEqual(ch.maxxval, 4)
74        self.assertEqual(ch.xrange, 4)
75        self.assertAlmostEqual(ch.xscale, 0.20, 4)
76        self.assertEqual(ch.minyval, 0)
77        self.assertEqual(ch.maxyval, 5)
78        self.assertEqual(ch.yrange, 10)
79        self.assertAlmostEqual(ch.yscale, 0.10, 4)
80        self.assertEqual(ch.minxdelta, 1)
81        self.assertAlmostEqual(ch.barWidth, 0.150, 4)
82        self.assertAlmostEqual(ch.barMargin, 0.025, 4)
83
84        R = pycha.bar.Rect
85        bars = (
86            R(0.025, 0.700, 0.150, 0.300, 0, 3, 'dataset1'),
87            R(0.225, 0.600, 0.150, 0.400, 1, 4, 'dataset1'),
88            R(0.425, 0.800, 0.150, 0.200, 2, 2, 'dataset1'),
89            R(0.625, 0.500, 0.150, 0.500, 3, 5, 'dataset1'),
90            R(0.825, 0.650, 0.150, 0.350, 4, 3.5, 'dataset1'),
91
92            R(0.025, 0.500, 0.150, 0.200, 0, 2, 'dataset2'),
93            R(0.225, 0.300, 0.150, 0.300, 1, 3, 'dataset2'),
94            R(0.425, 0.700, 0.150, 0.100, 2, 1, 'dataset2'),
95            R(0.625, 0.000, 0.150, 0.500, 3, 5, 'dataset2'),
96            R(0.825, 0.400, 0.150, 0.250, 4, 2.5, 'dataset2'),
97            )
98
99        for i, bar in enumerate(bars):
100            b1, b2 = ch.bars[i], bar
101            self.assertAlmostEqual(b1.x, b2.x, 4)
102            self.assertAlmostEqual(b1.y, b2.y, 4)
103            self.assertAlmostEqual(b1.w, b2.w, 4)
104            self.assertAlmostEqual(b1.h, b2.h, 4)
105            self.assertEqual(b1.xval, b2.xval)
106            self.assertEqual(b1.yval, b2.yval)
107            self.assertEqual(b1.name, b2.name)
108
109    def test_updateTicks(self):
110        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
111        dataset = (
112            ('dataset1', ([0, 1], [1, 1], [2, 3])),
113            ('dataset2', ([0, 2], [1, 0], [3, 4])),
114            )
115        ch = pycha.stackedbar.StackedVerticalBarChart(surface)
116        ch.addDataset(dataset)
117        ch._updateXY()
118        ch._updateChart()
119        ch._updateTicks()
120        xticks = [(0.125, 0), (0.375, 1), (0.625, 2)]
121        for i in range(len(xticks)):
122            self.assertAlmostEqual(ch.xticks[i][0], xticks[i][0], 4)
123            self.assertAlmostEqual(ch.xticks[i][1], xticks[i][1], 4)
124
125        yticks = [
126            (1.0, 0.0), (0.9, 0.7), (0.8, 1.4), (0.7, 2.1), (0.6, 2.8),
127            (0.5, 3.5), (0.4, 4.2), (0.3, 4.9), (0.2, 5.6),
128            (0.1, 6.3), (0.0, 7.0),
129            ]
130        for i in range(len(yticks)):
131            self.assertAlmostEqual(ch.yticks[i][0], yticks[i][0], 4)
132            self.assertAlmostEqual(ch.yticks[i][1], yticks[i][1], 4)
133
134
135class StackedHorizontalBarTests(unittest.TestCase):
136
137    def test_updateChart(self):
138        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
139        dataset = (
140            ('dataset1', ([0, 1], [1, 1], [2, 3])),
141            ('dataset2', ([0, 2], [1, 0], [2, 4])),
142            )
143        ch = pycha.stackedbar.StackedHorizontalBarChart(surface)
144        ch.addDataset(dataset)
145        ch._updateXY()
146        ch._updateChart()
147        self.assertEqual(ch.xrange, 2)
148        self.assertAlmostEqual(ch.xscale, 0.3333, 4)
149        self.assertAlmostEqual(ch.yscale, 0.1429, 4)
150        self.assertEqual(ch.minxdelta, 1)
151        self.assertAlmostEqual(ch.barWidth, 0.25, 4)
152        self.assertAlmostEqual(ch.barMargin, 0.0417, 4)
153
154        bars = (
155            pycha.bar.Rect(0, 0.0417, 0.1429, 0.25, 0, 1, 'dataset1'),
156            pycha.bar.Rect(0, 0.3750, 0.1429, 0.25, 1, 1, 'dataset1'),
157            pycha.bar.Rect(0, 0.7083, 0.4286, 0.25, 2, 3, 'dataset1'),
158
159            pycha.bar.Rect(0.1429, 0.0417, 0.2857, 0.25, 0, 2, 'dataset2'),
160            pycha.bar.Rect(0.1429, 0.3750, 0.0000, 0.25, 1, 0, 'dataset2'),
161            pycha.bar.Rect(0.4286, 0.7083, 0.5714, 0.25, 2, 4, 'dataset2'),
162            )
163
164        for i, bar in enumerate(bars):
165            b1, b2 = ch.bars[i], bar
166            self.assertAlmostEqual(b1.x, b2.x, 4)
167            self.assertAlmostEqual(b1.y, b2.y, 4)
168            self.assertAlmostEqual(b1.w, b2.w, 4)
169            self.assertAlmostEqual(b1.h, b2.h, 4)
170            self.assertEqual(b1.xval, b2.xval)
171            self.assertEqual(b1.yval, b2.yval)
172            self.assertEqual(b1.name, b2.name)
173
174    def test_updateTicks(self):
175        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
176        dataset = (
177            ('dataset1', ([0, 1], [1, 1], [2, 3])),
178            ('dataset2', ([0, 2], [1, 0], [2, 4])),
179            )
180        ch = pycha.stackedbar.StackedHorizontalBarChart(surface)
181        ch.addDataset(dataset)
182        ch._updateXY()
183        ch._updateChart()
184        ch._updateTicks()
185
186        xticks = [
187            (0.0, 0.0), (0.1, 0.7), (0.2, 1.4), (0.3, 2.1),
188            (0.4, 2.8), (0.5, 3.5), (0.6, 4.2), (0.7, 4.9),
189            (0.8, 5.6), (0.9, 6.3), (1.0, 7.0),
190            ]
191        for i in range(len(xticks)):
192            self.assertAlmostEqual(ch.xticks[i][0], xticks[i][0], 4)
193            self.assertAlmostEqual(ch.xticks[i][1], xticks[i][1], 4)
194
195        yticks = [(0.1667, 0), (0.5, 1), (0.8333, 2)]
196        for i in range(len(yticks)):
197            self.assertAlmostEqual(ch.yticks[i][0], yticks[i][0], 4)
198            self.assertAlmostEqual(ch.yticks[i][1], yticks[i][1], 4)
199
200
201def test_suite():
202    return unittest.TestSuite((
203        unittest.makeSuite(StackedBarTests),
204        unittest.makeSuite(StackedVerticalBarTests),
205        unittest.makeSuite(StackedHorizontalBarTests),
206    ))
207
208if __name__ == '__main__':
209    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the browser.