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