Ignore:
Timestamp:
13/10/09 10:44:23 (11 years ago)
Author:
pnorton
Message:

Improved the performance of the getUniqueValues method on the grid, also cached the result to avoid the cost of re-calculation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • qesdi/geoplot/trunk/lib/geoplot/tests/unit/test_grid.py

    r5403 r5835  
     1from __future__ import with_statement 
     2 
    13#/urs/bin/env python 
    24""" 
     
    46""" 
    57import nose 
     8import nose.tools as nt 
    69import numpy as N 
    710 
    8 import geoplot.grid 
    911from geoplot.grid import Grid 
     12from mock import Mock, patch 
    1013 
    1114class Test_Grid(object): 
     
    1821        self.vals = N.array([[1.5, 2.0, 2.5, 3.0], [3.5, 4.0, 4.5, 5.0],  
    1922                             [5.5, 6.0, 6.5, 7.0], [7.5, 8.0, 8.5, 9.0]]) 
     23         
    2024        self.grd = Grid(self.boundX, self.boundY, self.midX, self.midY, self.vals) 
    2125         
    22      
    2326    def tearDown(self): 
    2427        self.grd = None 
     
    3134        nose.tools.assert_equal(self.vals.tolist(), self.grd.values.tolist()) 
    3235     
     36    @patch('numpy.unique') 
     37    def test_002_checkUniqueValuesAreOnlyCalculatedOnce(self, mockUniqueFunction): 
     38         
     39        mockUniqueFunction.return_value = N.array([1., 2.5, 3.6, 5.0 ]) 
     40        uniqueVals = self.grd.getUniqueValues() 
     41         
     42        #check that the expected value was returned 
     43        nt.assert_equal(id(uniqueVals), id(mockUniqueFunction.return_value)) 
     44         
     45        #check that the mock was called with the values 
     46        (args, kwargs) = mockUniqueFunction.call_args 
     47        nt.assert_equal(len(args), 1) 
     48        nt.assert_equal(id(args[0]), id(self.grd.values) ) 
     49        nt.assert_equal(kwargs, {}) 
     50         
     51        uniqueVals = self.grd.getUniqueValues() 
     52         
     53        #check it was only called once 
     54        nt.assert_equal(mockUniqueFunction.call_count, 1) 
     55             
     56    def test_003_checkUniqueValuesAreRecalculatedForDifferentVals(self): 
     57         
     58        with patch('numpy.unique') as mockUniqueFunction: 
     59            mockUniqueFunction.return_value = N.array([1., 2.5, 3.6, 5.0 ]) 
     60             
     61            uniqueVals = self.grd.getUniqueValues() 
     62            nt.assert_equal(mockUniqueFunction.call_count, 1) 
     63            nt.assert_equal(id(uniqueVals), id(mockUniqueFunction.return_value)) 
     64             
     65            #change the grid values 
     66            self.grd.values = Mock() 
     67            mockUniqueFunction.return_value = N.array([13.6, 5.0 ]) 
     68            uniqueVals = self.grd.getUniqueValues() 
     69            nt.assert_equal(id(uniqueVals), id(mockUniqueFunction.return_value)) 
     70             
     71            #check it was only called again 
     72            nt.assert_equal(mockUniqueFunction.call_count, 2) 
     73             
     74            #check that the mock was called with the new grid values 
     75            (args, kwargs) = mockUniqueFunction.call_args 
     76            nt.assert_equal(len(args), 1) 
     77            nt.assert_equal(id(args[0]), id(self.grd.values) ) 
     78            nt.assert_equal(kwargs, {})             
     79             
     80     
    3381if __name__ == '__main__': 
    3482 
    35     import geoplot.log_util 
    36     geoplot.log_util.setGeoplotHandlerToStdOut() 
    37  
    3883    nose.runmodule() 
Note: See TracChangeset for help on using the changeset viewer.