/+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
14
15
16
class TestBase(unittest.TestCase):
17
18
    def setUp(self):
19
20
        # All tests require warnings to be raised
21
        self.raise_warnings(True)
22
42 by gak
- Minor test fixes
23
        self.temp_image = 'temp.png'
24
25
    def tearDown(self):
44 by gak
- woops
26
        if os.path.exists(self.temp_image):
42 by gak
- Minor test fixes
27
            os.unlink(self.temp_image)
28
36 by gak
- Really added initial unit tests
29
    def raise_warnings(self, rw):
30
        gc._reset_warnings()
31
32
        if rw:
33
            warnings.simplefilter('error')
34
        else:
35
            # Don't print out warnings if we're expecting them--so we can have
36
            # nicer looking tests! :)
37
            warnings.simplefilter('ignore')
38
38 by gak
- Added support for QR Code chart (#8)
39
    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)
40
        self.assertTrue(url.endswith(query))
38 by gak
- Added support for QR Code chart (#8)
41
37 by gak
- Added "colours within series" option to chart. (chco=xxx|xxx) (Steve Brandt)
42
36 by gak
- Really added initial unit tests
43
class TestDataTypes(TestBase):
44
45
    def test_simple_data(self):
46
        s = gc.SimpleData([range(0, 62), [0, 1, 60, 61]])
47
        self.assertEquals(repr(s),
48
            'chd=s:ABCDEFGHIJKLMNOPQRSTUVWXYZ'
49
            'abcdefghijklmnopqrstuvwxyz0123456789,AB89')
50
51
    def test_text_data(self):
52
        s = gc.TextData([[0, 1, 99.9]])
53
        self.assertEquals(repr(s), 'chd=t:0.0,1.0,99.9')
54
55
    def test_ext_data(self):
56
        s = gc.ExtendedData([[0, 1, 4095]])
57
        self.assertEquals(repr(s), 'chd=e:AAAB..')
58
59
60
class TestScaling(TestBase):
61
62
    def test_simple_scale(self):
63
        sv = gc.SimpleData.scale_value
64
65
        self.raise_warnings(False)  # We know some of these give warnings
66
        self.assertEquals(sv(-10, [0, 1]), 0)
67
        self.assertEquals(sv(0, [0, 1]), 0)
68
        self.assertEquals(sv(.5, [0, 1]), 31)
69
        self.assertEquals(sv(30, [0, 1]), 61)
70
        self.assertEquals(sv(2222, [0, 10000]), 14)
71
72
        # Test for warnings
73
        self.raise_warnings(True)
74
        self.assertRaises(UserWarning, sv, -10, [0, 1])
75
        self.assertRaises(UserWarning, sv, 30, [0, 1])
76
77
    def test_text_scale(self):
78
        sv = gc.TextData.scale_value
79
80
        self.raise_warnings(False)
81
        self.assertEquals(sv(-10, [0, 1]), 0)
82
        self.assertEquals(sv(0, [0, 1]), 0)
83
        self.assertEquals(sv(.5, [0, 1]), 50)
84
        self.assertEquals(sv(30, [0, 1]), 100)
85
        self.assertEquals(sv(2222, [0, 10000]), 22.22)
86
87
        self.raise_warnings(True)
37 by gak
- Added "colours within series" option to chart. (chco=xxx|xxx) (Steve Brandt)
88
        self.assertRaises(UserWarning, sv, -10, [0, 1])
89
        self.assertRaises(UserWarning, sv, 30, [0, 1])
36 by gak
- Really added initial unit tests
90
91
    def test_ext_scale(self):
92
        sv = gc.ExtendedData.scale_value
93
94
        self.raise_warnings(False)
95
        self.assertEquals(sv(-10, [0, 1]), 0)
96
        self.assertEquals(sv(0, [0, 1]), 0)
97
        self.assertEquals(sv(.5, [0, 1]), 2048)
98
        self.assertEquals(sv(30, [0, 1]), 4095)
99
        self.assertEquals(sv(2222, [0, 10000]), 910)
100
101
        self.raise_warnings(True)
102
        self.assertRaises(UserWarning, sv, -10, [0, 1])
103
        self.assertRaises(UserWarning, sv, 30, [0, 1])
104
37 by gak
- Added "colours within series" option to chart. (chco=xxx|xxx) (Steve Brandt)
105
39 by gak
- Bug fixed when automatic scaling is on and None values are in a data set (#5) (Alec Thomas)
106
class TestLineChart(TestBase):
107
108
    def test_none_data(self):
109
        chart = gc.SimpleLineChart(300, 100)
110
        chart.add_data([1, 2, 3, None, 5])
111
        self.assertChartURL(chart.get_url(), \
40 by gak
- Fixed a bug with auto-scaling, where the minimum was always 0. (#6) (Rohit Jenveja)
112
            '?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)
113
43 by gak
114
39 by gak
- Bug fixed when automatic scaling is on and None values are in a data set (#5) (Alec Thomas)
115
class TestQRChart(TestBase):
38 by gak
- Added support for QR Code chart (#8)
116
117
    def assertQRImage(self, chart, text):
118
        try:
119
            import PyQrcodec
120
        except ImportError:
121
            print 'PyQrCodec not installed. Can not test QR code image'
122
            return
123
42 by gak
- Minor test fixes
124
        chart.download(self.temp_image)
125
        status, string = PyQrcodec.decode(self.temp_image)
38 by gak
- Added support for QR Code chart (#8)
126
        self.assertTrue(status)
127
        self.assertEquals(text, string)
128
129
    def test_simple(self):
130
        text = 'Hello World'
131
        chart = gc.QRChart(100, 150)
132
        chart.add_data(text)
133
        self.assertChartURL(chart.get_url(), \
134
            '?cht=qr&chs=100x150&chl=Hello%20World')
135
136
    def test_encoding(self):
137
        chart = gc.QRChart(100, 100)
138
        chart.add_data('Hello World')
139
        self.assertChartURL(chart.get_url(), \
140
            '?cht=qr&chs=100x100&chl=Hello%20World')
141
142
    def test_no_data(self):
143
        chart = gc.QRChart(100, 100)
52 by gak
- Few fixes for python 3.0
144
        self.assertRaises(gc.NoDataGivenException, chart.get_url)
38 by gak
- Added support for QR Code chart (#8)
145
146
    def test_validate_image(self):
147
        text = 'Hello World'
148
        chart = gc.QRChart(100, 100)
149
        chart.add_data(text)
150
        chart.set_ec('H', 0)  # PyQrcodec seems to only work on higher EC
151
        self.assertQRImage(chart, text)
152
153
    def test_validate_utf8(self):
42 by gak
- Minor test fixes
154
        text = 'こんにちは世界'  # Hello world in Japanese UTF-8
38 by gak
- Added support for QR Code chart (#8)
155
        chart = gc.QRChart(100, 100)
156
        chart.add_data(text)
157
        chart.set_ec('H', 0)
158
        self.assertQRImage(chart, text)
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