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

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

Added a module to fix the import Image / from PIL import Image problem.

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