/+junk/pygooglechart-py3k

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/pygooglechart-py3k
38 by gak
- Added support for QR Code chart (#8)
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
36 by gak
- Really added initial unit tests
4
import unittest
5
import sys
6
import os
7
import warnings
38 by gak
- Added support for QR Code chart (#8)
8
import urllib
36 by gak
- Really added initial unit tests
9
10
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
11
sys.path.insert(0, ROOT)
12
13
import pygooglechart as gc
38 by gak
- Added support for QR Code chart (#8)
14
from pygooglechart import NoDataGivenException
36 by gak
- Really added initial unit tests
15
16
17
class TestBase(unittest.TestCase):
18
19
    def setUp(self):
20
21
        # All tests require warnings to be raised
22
        self.raise_warnings(True)
23
42 by gak
- Minor test fixes
24
        self.temp_image = 'temp.png'
25
26
    def tearDown(self):
44 by gak
- woops
27
        if os.path.exists(self.temp_image):
42 by gak
- Minor test fixes
28
            os.unlink(self.temp_image)
29
36 by gak
- Really added initial unit tests
30
    def raise_warnings(self, rw):
31
        gc._reset_warnings()
32
33
        if rw:
34
            warnings.simplefilter('error')
35
        else:
36
            # Don't print out warnings if we're expecting them--so we can have
37
            # nicer looking tests! :)
38
            warnings.simplefilter('ignore')
39
38 by gak
- Added support for QR Code chart (#8)
40
    def assertChartURL(self, url, query):
39 by gak
- Bug fixed when automatic scaling is on and None values are in a data set (#5) (Alec Thomas)
41
        self.assertTrue(url.endswith(query))
38 by gak
- Added support for QR Code chart (#8)
42
37 by gak
- Added "colours within series" option to chart. (chco=xxx|xxx) (Steve Brandt)
43
36 by gak
- Really added initial unit tests
44
class TestDataTypes(TestBase):
45
46
    def test_simple_data(self):
47
        s = gc.SimpleData([range(0, 62), [0, 1, 60, 61]])
48
        self.assertEquals(repr(s),
49
            'chd=s:ABCDEFGHIJKLMNOPQRSTUVWXYZ'
50
            'abcdefghijklmnopqrstuvwxyz0123456789,AB89')
51
52
    def test_text_data(self):
53
        s = gc.TextData([[0, 1, 99.9]])
54
        self.assertEquals(repr(s), 'chd=t:0.0,1.0,99.9')
55
56
    def test_ext_data(self):
57
        s = gc.ExtendedData([[0, 1, 4095]])
58
        self.assertEquals(repr(s), 'chd=e:AAAB..')
59
60
61
class TestScaling(TestBase):
62
63
    def test_simple_scale(self):
64
        sv = gc.SimpleData.scale_value
65
66
        self.raise_warnings(False)  # We know some of these give warnings
67
        self.assertEquals(sv(-10, [0, 1]), 0)
68
        self.assertEquals(sv(0, [0, 1]), 0)
69
        self.assertEquals(sv(.5, [0, 1]), 31)
70
        self.assertEquals(sv(30, [0, 1]), 61)
71
        self.assertEquals(sv(2222, [0, 10000]), 14)
72
73
        # Test for warnings
74
        self.raise_warnings(True)
75
        self.assertRaises(UserWarning, sv, -10, [0, 1])
76
        self.assertRaises(UserWarning, sv, 30, [0, 1])
77
78
    def test_text_scale(self):
79
        sv = gc.TextData.scale_value
80
81
        self.raise_warnings(False)
82
        self.assertEquals(sv(-10, [0, 1]), 0)
83
        self.assertEquals(sv(0, [0, 1]), 0)
84
        self.assertEquals(sv(.5, [0, 1]), 50)
85
        self.assertEquals(sv(30, [0, 1]), 100)
86
        self.assertEquals(sv(2222, [0, 10000]), 22.22)
87
88
        self.raise_warnings(True)
37 by gak
- Added "colours within series" option to chart. (chco=xxx|xxx) (Steve Brandt)
89
        self.assertRaises(UserWarning, sv, -10, [0, 1])
90
        self.assertRaises(UserWarning, sv, 30, [0, 1])
36 by gak
- Really added initial unit tests
91
92
    def test_ext_scale(self):
93
        sv = gc.ExtendedData.scale_value
94
95
        self.raise_warnings(False)
96
        self.assertEquals(sv(-10, [0, 1]), 0)
97
        self.assertEquals(sv(0, [0, 1]), 0)
98
        self.assertEquals(sv(.5, [0, 1]), 2048)
99
        self.assertEquals(sv(30, [0, 1]), 4095)
100
        self.assertEquals(sv(2222, [0, 10000]), 910)
101
102
        self.raise_warnings(True)
103
        self.assertRaises(UserWarning, sv, -10, [0, 1])
104
        self.assertRaises(UserWarning, sv, 30, [0, 1])
105
37 by gak
- Added "colours within series" option to chart. (chco=xxx|xxx) (Steve Brandt)
106
39 by gak
- Bug fixed when automatic scaling is on and None values are in a data set (#5) (Alec Thomas)
107
class TestLineChart(TestBase):
108
109
    def test_none_data(self):
110
        chart = gc.SimpleLineChart(300, 100)
111
        chart.add_data([1, 2, 3, None, 5])
112
        self.assertChartURL(chart.get_url(), \
40 by gak
- Fixed a bug with auto-scaling, where the minimum was always 0. (#6) (Rohit Jenveja)
113
            '?cht=lc&chs=300x100&chd=e:AAMzZm__zM')
39 by gak
- Bug fixed when automatic scaling is on and None values are in a data set (#5) (Alec Thomas)
114
43 by gak
115
39 by gak
- Bug fixed when automatic scaling is on and None values are in a data set (#5) (Alec Thomas)
116
class TestQRChart(TestBase):
38 by gak
- Added support for QR Code chart (#8)
117
118
    def assertQRImage(self, chart, text):
119
        try:
120
            import PyQrcodec
121
        except ImportError:
122
            print 'PyQrCodec not installed. Can not test QR code image'
123
            return
124
42 by gak
- Minor test fixes
125
        chart.download(self.temp_image)
126
        status, string = PyQrcodec.decode(self.temp_image)
38 by gak
- Added support for QR Code chart (#8)
127
        self.assertTrue(status)
128
        self.assertEquals(text, string)
129
130
    def test_simple(self):
131
        text = 'Hello World'
132
        chart = gc.QRChart(100, 150)
133
        chart.add_data(text)
134
        self.assertChartURL(chart.get_url(), \
135
            '?cht=qr&chs=100x150&chl=Hello%20World')
136
137
    def test_encoding(self):
138
        chart = gc.QRChart(100, 100)
139
        chart.add_data('Hello World')
140
        self.assertChartURL(chart.get_url(), \
141
            '?cht=qr&chs=100x100&chl=Hello%20World')
142
143
    def test_no_data(self):
144
        chart = gc.QRChart(100, 100)
145
        self.assertRaises(NoDataGivenException, chart.get_url)
146
147
    def test_validate_image(self):
148
        text = 'Hello World'
149
        chart = gc.QRChart(100, 100)
150
        chart.add_data(text)
151
        chart.set_ec('H', 0)  # PyQrcodec seems to only work on higher EC
152
        self.assertQRImage(chart, text)
153
154
    def test_validate_utf8(self):
42 by gak
- Minor test fixes
155
        text = 'こんにちは世界'  # Hello world in Japanese UTF-8
38 by gak
- Added support for QR Code chart (#8)
156
        chart = gc.QRChart(100, 100)
157
        chart.add_data(text)
158
        chart.set_ec('H', 0)
159
        self.assertQRImage(chart, text)
160
161
    def test_validate_shift_jis(self):
162
        # XXX: It looks like PyQrcodec doesn't do shift_jis?
163
        text = unicode('こんにちは世界', 'utf-8').encode('shift_jis')
164
        chart = gc.QRChart(100, 100)
165
        chart.add_data(text)
166
        chart.set_ec('H', 0)
167
        chart.set_encoding('Shift_JIS')
168
        self.assertChartURL(chart.get_url(), \
169
            '?cht=qr&chs=100x100&chl=%82%B1%82%F1%82%C9' \
170
            '%82%BF%82%CD%90%A2%8AE&choe=Shift_JIS&chld=H|0')
42 by gak
- Minor test fixes
171
        chart.download(self.temp_image)
38 by gak
- Added support for QR Code chart (#8)
172
173
36 by gak
- Really added initial unit tests
174
class TestGrammar(TestBase):
175
176
    types = ('Venn', 'GroupedHorizontalBar', 'GoogleOMeter', 'Scatter',
177
        'StackedVerticalBar', 'Map', 'StackedHorizontalBar', 'SimpleLine',
38 by gak
- Added support for QR Code chart (#8)
178
        'SparkLine', 'GroupedVerticalBar', 'SplineRadar', 'XYLine', 'Radar',
179
        'QR')
36 by gak
- Really added initial unit tests
180
181
    def test_chart_types(self):
182
        ret = gc.ChartGrammar.get_possible_chart_types()
183
        diff = set(ret).symmetric_difference(set(TestGrammar.types))
184
        self.assert_(not diff)
185
186
    def test_google_chart(self):
187
        g = {
188
            'type': 'GoogleOMeter',
189
            'w': 100,
190
            'h': 100,
191
            'auto_scale': True,
192
            'x_range': [ 0, 10 ],
193
            'data': [
194
                [ 1, 5, 10 ]
195
            ],
196
        }
197
        grammar = gc.ChartGrammar()
198
        chart = grammar.parse(g)
37 by gak
- Added "colours within series" option to chart. (chco=xxx|xxx) (Steve Brandt)
199
#        print chart.get_url()
36 by gak
- Really added initial unit tests
200
#        chart.download('meh.png')
201
202
203
if __name__ == "__main__":
204
    unittest.main()
205