source: qesdi/geoplot/trunk/lib/geoplot/tests/unit/test_layer_drawer.py @ 5636

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/geoplot/trunk/lib/geoplot/tests/unit/test_layer_drawer.py@5636
Revision 5636, 3.7 KB checked in by pnorton, 11 years ago (diff)

Fixed a problem with imshow drawing a grid when there was no data to draw. Also fixed some of the tests.

Line 
1__test__=True
2
3import unittest
4import nose.tools as nt
5import logging
6import Image
7import StringIO
8import time
9import cdms2 as cdms
10import numpy
11import nose
12
13from matplotlib.figure import Figure
14from matplotlib.backends.backend_agg import FigureCanvasAgg
15from matplotlib.patches import Rectangle
16
17from mock import patch, patch_object, Mock, sentinel
18
19from geoplot.layer_drawer import LayerDrawerBase
20
21import geoplot.layer_drawer as layer_drawer
22import geoplot.layer_drawer_grid as layer_drawer_grid
23import geoplot.utils as geoplot_utils
24
25
26
27log = logging.getLogger(__name__)
28
29class TestLayerDrawer(unittest.TestCase):
30
31    def setUp(self):
32        pass
33   
34    def tearDown(self):
35        pass
36
37    def test_001_ExceptionWhenUnknownGridType(self):
38        ld = layer_drawer.LayerDrawerBase()
39       
40        nt.assert_raises(Exception,
41                         setattr,
42                         ld, 'gridType', 'notAValidGridType')
43
44    def test_002_makeImage(self):
45        #create a mock grid builder
46        mockGridBuilderLatLon = Mock(spec=layer_drawer.GridBuilderLatLon)
47        mockGridBuilderLatLon.return_value = mockGridBuilderLatLon
48        #create a mock grid drawer
49        mockGridDrawer = Mock(spec=layer_drawer_grid.GridDrawer)
50        mockGridDrawer.return_value = mockGridDrawer
51       
52        #create a function that draws something on an axis
53        def drawBoxOnAxis(axes, grid, **kwargs):
54            axes.add_patch(Rectangle((0.1, 0.25), 0.2, 0.5, edgecolor='b', fill=False, linewidth=1))
55               
56        #use this function to create a test image
57        width = 150; height=100;
58        fig = Figure((width / 100.0, height / 100.0), dpi=100, facecolor='w')
59        axes = fig.add_axes([0.0, 0.0, 1.0, 1.0], xticks=[], yticks=[], frameon=False)
60        drawBoxOnAxis(axes, None)
61        #axes.add_patch(Rectangle((0.1, 0.30), 0.2, 0.5, edgecolor='b', fill=False, linewidth=1))
62       
63        testImage = geoplot_utils.figureToImage(fig)
64                       
65        #draw the box on the axis instead of the grid
66        mockGridDrawer.draw.side_effect = drawBoxOnAxis
67       
68        #apply the mocks and run the code
69        @apply
70        @patch('geoplot.grid_factory.GridBuilderLatLon', mockGridBuilderLatLon)
71        @patch('geoplot.layer_drawer_grid.GridDrawer',mockGridDrawer)
72        def do_test():
73            ld = layer_drawer_grid.LayerDrawerGrid(sentinel)
74            im = ld.makeImage(width=width, height=height)
75           
76            assert issubclass(im.__class__, Image.Image)
77            testImage.save('/tmp/test_image.png')
78            im.save('/tmp/resulting_image.png')
79            assert compareImages(testImage, im)
80   
81   
82    def test_003_limitsSetOnFigure(self):
83       
84        #create a cdms variable with a lat-lon range       
85        lat = cdms.createAxis([0,2,4,6,8,10], id='lat')
86        lon = cdms.createAxis([0,1,2,3,4,5], id='lon')
87       
88        arr = numpy.zeros((len(lat),len(lon)))
89        v = cdms.createVariable(arr, axes=[lat,lon])
90        ld = layer_drawer_grid.LayerDrawerGrid(v)
91        xLims = (-5,2)
92        yLims = (6,11)
93       
94        fig = ld._getFigure(width=100, height=100, dpi=100)
95       
96        axes = ld._addAxes(fig)
97        ld._drawToAxes(axes, xLimits=xLims, yLimits=yLims)
98        ld._resetAxes(axes,  xLimits=xLims, yLimits=yLims)
99       
100        print dir(fig)
101       
102        nt.assert_equal(axes.get_xlim(), xLims)
103        nt.assert_equal(axes.get_ylim(), yLims)
104
105def compareImages(image1, image2):
106    return image1.tostring() == image2.tostring()
107   
108def suite():
109    suite = unittest.TestSuite()
110    suite.addTest(unittest.makeSuite(TestLayerDrawer))
111    return suite
112
113if __name__ == "__main__":
114    nose.runmodule()
115   
116#    unittest.TextTestRunner(verbosity=1).run(suite())
Note: See TracBrowser for help on using the repository browser.