/+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-05-03 07:30:58 UTC
  • Revision ID: git-v1:9342edb8666dde7e843e3eb438f1f6a717aa32fc
- Really added initial unit tests
- Converted setup.py to unix file format
- warnings made when data is being clipped and when data scaling is incorrect
- max_value is now a variable
- pie and google-o-meter chart data is now on the x-axis
- More grammar work

Show diffs side-by-side

added added

removed removed

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