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"""Tests for common.py."""
18
19import warnings
20
21from graphy import common
22from graphy import graphy_test
23from graphy.backends import google_chart_api
24
25
26class CommonTest(graphy_test.GraphyTest):
27
28  def setUp(self):
29    self.chart = google_chart_api.LineChart()
30
31  def tearDown(self):
32    warnings.resetwarnings()
33
34  def testDependentAxis(self):
35    self.assertTrue(self.chart.left is self.chart.GetDependentAxis())
36    self.assertTrue(self.chart.bottom is self.chart.GetIndependentAxis())
37
38  def testAxisAssignment(self):
39    """Make sure axis assignment works properly"""
40    new_axis = common.Axis()
41    self.chart.top = new_axis
42    self.assertTrue(self.chart.top is new_axis)
43    new_axis = common.Axis()
44    self.chart.bottom = new_axis
45    self.assertTrue(self.chart.bottom is new_axis)
46    new_axis = common.Axis()
47    self.chart.left = new_axis
48    self.assertTrue(self.chart.left is new_axis)
49    new_axis = common.Axis()
50    self.chart.right = new_axis
51    self.assertTrue(self.chart.right is new_axis)
52
53  def testAxisConstruction(self):
54    axis = common.Axis()
55    self.assertTrue(axis.min is None)
56    self.assertTrue(axis.max is None)
57    axis = common.Axis(-2, 16)
58    self.assertEqual(axis.min, -2)
59    self.assertEqual(axis.max, 16)
60
61  def testGetDependentIndependentAxes(self):
62    c = self.chart
63    self.assertEqual([c.left, c.right], c.GetDependentAxes())
64    self.assertEqual([c.top, c.bottom], c.GetIndependentAxes())
65    right2 = c.AddAxis(common.AxisPosition.RIGHT, common.Axis())
66    bottom2 = c.AddAxis(common.AxisPosition.BOTTOM, common.Axis())
67    self.assertEqual([c.left, c.right, right2], c.GetDependentAxes())
68    self.assertEqual([c.top, c.bottom, bottom2], c.GetIndependentAxes())
69
70  # TODO: remove once AddSeries is deleted
71  def testAddSeries(self):
72    warnings.filterwarnings('ignore')
73    chart = common.BaseChart()
74    chart.AddSeries(points=[1, 2, 3], style='foo',
75                    markers='markers', label='label')
76    series = chart.data[0]
77    self.assertEqual(series.data, [1, 2, 3])
78    self.assertEqual(series.style, 'foo')
79    self.assertEqual(series.markers, 'markers')
80    self.assertEqual(series.label, 'label')
81
82  # TODO: remove once the deprecation warning is removed
83  def testDataSeriesStyles(self):
84    # Deprecated approach
85    warnings.filterwarnings('error')
86    self.assertRaises(DeprecationWarning, common.DataSeries, [1, 2, 3],
87      color='0000FF')
88    warnings.filterwarnings('ignore')
89    d = common.DataSeries([1, 2, 3], color='0000FF')
90    self.assertEqual('0000FF', d.color)
91    d.color = 'F00'
92    self.assertEqual('F00', d.color)
93
94  # TODO: remove once the deprecation warning is removed
95  def testDataSeriesArgumentOrder(self):
96    # Deprecated approach
97    warnings.filterwarnings('error')
98    self.assertRaises(DeprecationWarning, common.DataSeries, [1, 2, 3],
99      '0000FF', 'style')
100
101    # New order
102    style = common._BasicStyle('0000FF')
103    d = common.DataSeries([1, 2, 3], 'label', style)
104    self.assertEqual('label', d.label)
105    self.assertEqual(style, d.style)
106
107if __name__ == '__main__':
108  graphy_test.main()
109