1#!/usr/bin/python2.4 2# 3# Copyright 2008 Google Inc. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17"""Unittest for Graphy and Google Chart API backend.""" 18 19import warnings 20 21from graphy import graphy_test 22from graphy import pie_chart 23from graphy.backends import google_chart_api 24from graphy.backends.google_chart_api import base_encoder_test 25 26 27# Extend BaseChartTest so that we pick up & repeat all the line tests which 28# Pie Charts should continue to satisfy 29class PieChartTest(base_encoder_test.BaseChartTest): 30 31 def tearDown(self): 32 warnings.resetwarnings() 33 super(PieChartTest, self).tearDown() 34 35 def GetChart(self, *args, **kwargs): 36 return google_chart_api.PieChart(*args, **kwargs) 37 38 def AddToChart(self, chart, points, color=None, label=None): 39 return chart.AddSegment(points[0], color=color, label=label) 40 41 def testCanRemoveDefaultFormatters(self): 42 # Override this test, as pie charts don't have default formatters. 43 pass 44 45 def testChartType(self): 46 self.chart.display.is3d = False 47 self.assertEqual(self.Param('cht'), 'p') 48 self.chart.display.is3d = True 49 self.assertEqual(self.Param('cht'), 'p3') 50 51 def testEmptyChart(self): 52 self.assertEqual(self.Param('chd'), 's:') 53 self.assertEqual(self.Param('chco'), '') 54 self.assertEqual(self.Param('chl'), '') 55 56 def testChartCreation(self): 57 self.chart = self.GetChart([1,2,3], ['Mouse', 'Cat', 'Dog']) 58 self.assertEqual(self.Param('chd'), 's:Up9') 59 self.assertEqual(self.Param('chl'), 'Mouse|Cat|Dog') 60 self.assertEqual(self.Param('cht'), 'p') 61 # TODO: Get 'None' labels to work and test them 62 63 def testAddSegment(self): 64 self.chart = self.GetChart([1,2,3], ['Mouse', 'Cat', 'Dog']) 65 self.chart.AddSegment(4, label='Horse') 66 self.assertEqual(self.Param('chd'), 's:Pfu9') 67 self.assertEqual(self.Param('chl'), 'Mouse|Cat|Dog|Horse') 68 69 # TODO: Remove this when AddSegments is removed 70 def testAddMultipleSegments(self): 71 warnings.filterwarnings('ignore') 72 self.chart.AddSegments([1,2,3], 73 ['Mouse', 'Cat', 'Dog'], 74 ['ff0000', '00ff00', '0000ff']) 75 self.assertEqual(self.Param('chd'), 's:Up9') 76 self.assertEqual(self.Param('chl'), 'Mouse|Cat|Dog') 77 self.assertEqual(self.Param('chco'), 'ff0000,00ff00,0000ff') 78 # skip two colors 79 self.chart.AddSegments([4,5,6], ['Horse', 'Moose', 'Elephant'], ['cccccc']) 80 self.assertEqual(self.Param('chd'), 's:KUfpz9') 81 self.assertEqual(self.Param('chl'), 'Mouse|Cat|Dog|Horse|Moose|Elephant') 82 self.assertEqual(self.Param('chco'), 'ff0000,00ff00,0000ff,cccccc') 83 84 def testMultiplePies(self): 85 self.chart.AddPie([1,2,3], 86 ['Mouse', 'Cat', 'Dog'], 87 ['ff0000', '00ff00', '0000ff']) 88 self.assertEqual(self.Param('chd'), 's:Up9') 89 self.assertEqual(self.Param('chl'), 'Mouse|Cat|Dog') 90 self.assertEqual(self.Param('chco'), 'ff0000,00ff00,0000ff') 91 self.assertEqual(self.Param('cht'), 'p') 92 # skip two colors 93 self.chart.AddPie([4,5,6], ['Horse', 'Moose', 'Elephant'], ['cccccc']) 94 self.assertEqual(self.Param('chd'), 's:KUf,pz9') 95 self.assertEqual(self.Param('chl'), 'Mouse|Cat|Dog|Horse|Moose|Elephant') 96 self.assertEqual(self.Param('chco'), 'ff0000,00ff00,0000ff,cccccc') 97 self.assertEqual(self.Param('cht'), 'pc') 98 99 def testMultiplePiesNo3d(self): 100 chart = self.GetChart([1,2,3], ['Mouse', 'Cat', 'Dog']) 101 chart.AddPie([4,5,6], ['Horse', 'Moose', 'Elephant']) 102 chart.display.is3d = True 103 warnings.filterwarnings('error') 104 self.assertRaises(RuntimeWarning, chart.display.Url, 320, 240) 105 106 def testAddSegmentByIndex(self): 107 self.chart = self.GetChart([1,2,3], ['Mouse', 'Cat', 'Dog']) 108 self.chart.AddSegment(4, 'Horse', pie_index=0) 109 self.assertEqual(self.Param('chd'), 's:Pfu9') 110 self.assertEqual(self.Param('chl'), 'Mouse|Cat|Dog|Horse') 111 self.chart.AddPie([4,5], ['Apple', 'Orange'], []) 112 self.chart.AddSegment(6, 'Watermelon', pie_index=1) 113 self.assertEqual(self.Param('chd'), 's:KUfp,pz9') 114 115 def testSetColors(self): 116 self.assertEqual(self.Param('chco'), '') 117 self.chart.AddSegment(1, label='Mouse') 118 self.chart.AddSegment(5, label='Moose') 119 self.chart.SetColors('000033', '0000ff') 120 self.assertEqual(self.Param('chco'), '000033,0000ff') 121 self.chart.AddSegment(6, label='Elephant') 122 self.assertEqual(self.Param('chco'), '000033,0000ff') 123 124 def testHugeSegmentSizes(self): 125 self.chart = self.GetChart([1000000000000000L,3000000000000000L], 126 ['Big', 'Uber']) 127 self.assertEqual(self.Param('chd'), 's:U9') 128 self.chart.display.enhanced_encoding = True 129 self.assertEqual(self.Param('chd'), 'e:VV..') 130 131 def testSetSegmentSize(self): 132 segment1 = self.chart.AddSegment(1) 133 segment2 = self.chart.AddSegment(2) 134 self.assertEqual(self.Param('chd'), 's:f9') 135 segment2.size = 3 136 self.assertEquals(segment1.size, 1) 137 self.assertEquals(segment2.size, 3) 138 self.assertEqual(self.Param('chd'), 's:U9') 139 140 def testChartAngle(self): 141 self.assertTrue('chp' not in self.chart.display._Params(self.chart)) 142 self.chart.display.angle = 3.1415 143 self.assertEqual(self.Param('chp'), '3.1415') 144 self.chart.display.angle = 0 145 self.assertTrue('chp' not in self.chart.display._Params(self.chart)) 146 147 148if __name__ == '__main__': 149 graphy_test.main() 150