/+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):
35
        return url.endswith(query)
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
38 by gak
- Added support for QR Code chart (#8)
101
class TestChartQR(TestBase):
102
103
    def assertQRImage(self, chart, text):
104
        try:
105
            import PyQrcodec
106
        except ImportError:
107
            print 'PyQrCodec not installed. Can not test QR code image'
108
            return
109
110
        fn = 'temp.png'
111
        chart.download(fn)
112
        status, string = PyQrcodec.decode(fn)
113
        self.assertTrue(status)
114
        self.assertEquals(text, string)
115
116
    def test_simple(self):
117
        text = 'Hello World'
118
        chart = gc.QRChart(100, 150)
119
        chart.add_data(text)
120
        self.assertChartURL(chart.get_url(), \
121
            '?cht=qr&chs=100x150&chl=Hello%20World')
122
123
    def test_encoding(self):
124
        chart = gc.QRChart(100, 100)
125
        chart.add_data('Hello World')
126
        self.assertChartURL(chart.get_url(), \
127
            '?cht=qr&chs=100x100&chl=Hello%20World')
128
129
    def test_no_data(self):
130
        chart = gc.QRChart(100, 100)
131
        self.assertRaises(NoDataGivenException, chart.get_url)
132
133
    def test_validate_image(self):
134
        text = 'Hello World'
135
        chart = gc.QRChart(100, 100)
136
        chart.add_data(text)
137
        chart.set_ec('H', 0)  # PyQrcodec seems to only work on higher EC
138
        self.assertQRImage(chart, text)
139
140
    def test_validate_utf8(self):
141
        text = 'こんにちは世界'  # Hello world in Japanese UTF8
142
        chart = gc.QRChart(100, 100)
143
        chart.add_data(text)
144
        chart.set_ec('H', 0)
145
        self.assertQRImage(chart, text)
146
147
    def test_validate_shift_jis(self):
148
        # XXX: It looks like PyQrcodec doesn't do shift_jis?
149
        text = unicode('こんにちは世界', 'utf-8').encode('shift_jis')
150
        chart = gc.QRChart(100, 100)
151
        chart.add_data(text)
152
        chart.set_ec('H', 0)
153
        chart.set_encoding('Shift_JIS')
154
        self.assertChartURL(chart.get_url(), \
155
            '?cht=qr&chs=100x100&chl=%82%B1%82%F1%82%C9' \
156
            '%82%BF%82%CD%90%A2%8AE&choe=Shift_JIS&chld=H|0')
157
        chart.download('temp.png')
158
159
36 by gak
- Really added initial unit tests
160
class TestGrammar(TestBase):
161
162
    types = ('Venn', 'GroupedHorizontalBar', 'GoogleOMeter', 'Scatter',
163
        'StackedVerticalBar', 'Map', 'StackedHorizontalBar', 'SimpleLine',
38 by gak
- Added support for QR Code chart (#8)
164
        'SparkLine', 'GroupedVerticalBar', 'SplineRadar', 'XYLine', 'Radar',
165
        'QR')
36 by gak
- Really added initial unit tests
166
167
    def test_chart_types(self):
168
        ret = gc.ChartGrammar.get_possible_chart_types()
169
        diff = set(ret).symmetric_difference(set(TestGrammar.types))
170
        self.assert_(not diff)
171
172
    def test_google_chart(self):
173
        g = {
174
            'type': 'GoogleOMeter',
175
            'w': 100,
176
            'h': 100,
177
            'auto_scale': True,
178
            'x_range': [ 0, 10 ],
179
            'data': [
180
                [ 1, 5, 10 ]
181
            ],
182
        }
183
        grammar = gc.ChartGrammar()
184
        chart = grammar.parse(g)
37 by gak
- Added "colours within series" option to chart. (chco=xxx|xxx) (Steve Brandt)
185
#        print chart.get_url()
36 by gak
- Really added initial unit tests
186
#        chart.download('meh.png')
187
188
189
if __name__ == "__main__":
190
    unittest.main()
191