/+junk/pygooglechart-py3k

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/pygooglechart-py3k

« back to all changes in this revision

Viewing changes to test/test.py

  • Committer: gak
  • Date: 2009-03-15 07:41:54 UTC
  • Revision ID: git-v1:af07747db18840d2b5e1dd9be42f20009f922f06
Added mapchart example

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
 
1
4
import unittest
2
5
import sys
3
6
import os
4
7
import warnings
 
8
import urllib
5
9
 
6
10
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
7
11
sys.path.insert(0, ROOT)
16
20
        # All tests require warnings to be raised
17
21
        self.raise_warnings(True)
18
22
 
 
23
        self.temp_image = 'temp.png'
 
24
 
 
25
    def tearDown(self):
 
26
        if os.path.exists(self.temp_image):
 
27
            os.unlink(self.temp_image)
 
28
 
19
29
    def raise_warnings(self, rw):
20
30
        gc._reset_warnings()
21
31
 
26
36
            # nicer looking tests! :)
27
37
            warnings.simplefilter('ignore')
28
38
 
 
39
    def assertChartURL(self, url, query):
 
40
        self.assertTrue(url.endswith(query))
 
41
 
 
42
 
29
43
class TestDataTypes(TestBase):
30
44
 
31
45
    def test_simple_data(self):
71
85
        self.assertEquals(sv(2222, [0, 10000]), 22.22)
72
86
 
73
87
        self.raise_warnings(True)
74
 
        sv(-10, [0, 1])
75
 
#        self.assertRaises(UserWarning, sv, -10, [0, 1])
76
 
#        self.assertEquals(UserWarning, sv, 30, [0, 1])
 
88
        self.assertRaises(UserWarning, sv, -10, [0, 1])
 
89
        self.assertRaises(UserWarning, sv, 30, [0, 1])
77
90
 
78
91
    def test_ext_scale(self):
79
92
        sv = gc.ExtendedData.scale_value
89
102
        self.assertRaises(UserWarning, sv, -10, [0, 1])
90
103
        self.assertRaises(UserWarning, sv, 30, [0, 1])
91
104
 
 
105
 
 
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(), \
 
112
            '?cht=lc&chs=300x100&chd=e:AAMzZm__zM')
 
113
 
 
114
 
 
115
class TestQRChart(TestBase):
 
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
 
 
124
        chart.download(self.temp_image)
 
125
        status, string = PyQrcodec.decode(self.temp_image)
 
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)
 
144
        self.assertRaises(gc.NoDataGivenException, chart.get_url)
 
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):
 
154
        text = 'こんにちは世界'  # Hello world in Japanese UTF-8
 
155
        chart = gc.QRChart(100, 100)
 
156
        chart.add_data(text)
 
157
        chart.set_ec('H', 0)
 
158
        self.assertQRImage(chart, text)
 
159
 
92
160
class TestGrammar(TestBase):
93
161
 
94
162
    types = ('Venn', 'GroupedHorizontalBar', 'GoogleOMeter', 'Scatter',
95
163
        'StackedVerticalBar', 'Map', 'StackedHorizontalBar', 'SimpleLine',
96
 
        'SparkLine', 'GroupedVerticalBar', 'SplineRadar', 'XYLine', 'Radar')
 
164
        'SparkLine', 'GroupedVerticalBar', 'SplineRadar', 'XYLine', 'Radar',
 
165
        'QR')
97
166
 
98
167
    def test_chart_types(self):
99
168
        ret = gc.ChartGrammar.get_possible_chart_types()
113
182
        }
114
183
        grammar = gc.ChartGrammar()
115
184
        chart = grammar.parse(g)
116
 
        print chart.get_url()
 
185
#        print chart.get_url()
117
186
#        chart.download('meh.png')
118
187
 
119
188
 
120
189
if __name__ == "__main__":
121
190
    unittest.main()
122
191
 
123
 
    suite = unittest.TestSuite()
124
 
    suite.addTest(TestScaling('test_ext_scale'))
125
 
    unittest.TextTestRunner().run(suite)
126