/+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 06:05:53 UTC
  • Revision ID: git-v1:11733abbf5cbcf4721836a92ad389aae6d8f2087
 - Added support for QR Code chart (#8)

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
        return url.endswith(query)
 
36
 
29
37
 
30
38
class TestDataTypes(TestBase):
31
39
 
90
98
        self.assertRaises(UserWarning, sv, 30, [0, 1])
91
99
 
92
100
 
 
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
 
93
160
class TestGrammar(TestBase):
94
161
 
95
162
    types = ('Venn', 'GroupedHorizontalBar', 'GoogleOMeter', 'Scatter',
96
163
        'StackedVerticalBar', 'Map', 'StackedHorizontalBar', 'SimpleLine',
97
 
        'SparkLine', 'GroupedVerticalBar', 'SplineRadar', 'XYLine', 'Radar')
 
164
        'SparkLine', 'GroupedVerticalBar', 'SplineRadar', 'XYLine', 'Radar',
 
165
        'QR')
98
166
 
99
167
    def test_chart_types(self):
100
168
        ret = gc.ChartGrammar.get_possible_chart_types()