/+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: 2008-08-23 07:31:12 UTC
  • Revision ID: git-v1:d25980565dd2640f4d700e85f0fab48a685c8ed7
Fixed bug where the module would download twice (#7) (Evan Lezar)

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)
8
12
 
9
13
import pygooglechart as gc
 
14
from pygooglechart import NoDataGivenException
10
15
 
11
16
 
12
17
class TestBase(unittest.TestCase):
26
31
            # nicer looking tests! :)
27
32
            warnings.simplefilter('ignore')
28
33
 
 
34
    def assertChartURL(self, url, query):
 
35
        self.assertTrue(url.endswith(query))
 
36
 
 
37
 
29
38
class TestDataTypes(TestBase):
30
39
 
31
40
    def test_simple_data(self):
71
80
        self.assertEquals(sv(2222, [0, 10000]), 22.22)
72
81
 
73
82
        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])
 
83
        self.assertRaises(UserWarning, sv, -10, [0, 1])
 
84
        self.assertRaises(UserWarning, sv, 30, [0, 1])
77
85
 
78
86
    def test_ext_scale(self):
79
87
        sv = gc.ExtendedData.scale_value
89
97
        self.assertRaises(UserWarning, sv, -10, [0, 1])
90
98
        self.assertRaises(UserWarning, sv, 30, [0, 1])
91
99
 
 
100
 
 
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:AAMzZm__zM')
 
108
 
 
109
class TestQRChart(TestBase):
 
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
 
92
168
class TestGrammar(TestBase):
93
169
 
94
170
    types = ('Venn', 'GroupedHorizontalBar', 'GoogleOMeter', 'Scatter',
95
171
        'StackedVerticalBar', 'Map', 'StackedHorizontalBar', 'SimpleLine',
96
 
        'SparkLine', 'GroupedVerticalBar', 'SplineRadar', 'XYLine', 'Radar')
 
172
        'SparkLine', 'GroupedVerticalBar', 'SplineRadar', 'XYLine', 'Radar',
 
173
        'QR')
97
174
 
98
175
    def test_chart_types(self):
99
176
        ret = gc.ChartGrammar.get_possible_chart_types()
113
190
        }
114
191
        grammar = gc.ChartGrammar()
115
192
        chart = grammar.parse(g)
116
 
        print chart.get_url()
 
193
#        print chart.get_url()
117
194
#        chart.download('meh.png')
118
195
 
119
196
 
120
197
if __name__ == "__main__":
121
198
    unittest.main()
122
199
 
123
 
    suite = unittest.TestSuite()
124
 
    suite.addTest(TestScaling('test_ext_scale'))
125
 
    unittest.TextTestRunner().run(suite)
126