You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@climate.apache.org by ja...@apache.org on 2016/06/14 18:27:40 UTC

climate git commit: CLIMATE-808 - Add tests for plotter module

Repository: climate
Updated Branches:
  refs/heads/master cc54cc801 -> 6b9fae9d5


CLIMATE-808 - Add tests for plotter module

- Add tests to verify computations in plotter module
- Update .coveragerc file to ignore image generation code


Project: http://git-wip-us.apache.org/repos/asf/climate/repo
Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/6b9fae9d
Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/6b9fae9d
Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/6b9fae9d

Branch: refs/heads/master
Commit: 6b9fae9d5fe2432fd9a8c84e33976a78a2e8236e
Parents: cc54cc8
Author: Ibrahim <ja...@gmail.com>
Authored: Tue Jun 14 17:34:17 2016 +0530
Committer: Ibrahim <ja...@gmail.com>
Committed: Tue Jun 14 17:34:17 2016 +0530

----------------------------------------------------------------------
 .coveragerc               | 42 +++++++++++++++++++-
 ocw/tests/test_plotter.py | 88 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 127 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/climate/blob/6b9fae9d/.coveragerc
----------------------------------------------------------------------
diff --git a/.coveragerc b/.coveragerc
index accf369..1763e54 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -26,4 +26,44 @@ exclude_lines =
     else:
     
     # Due to issue https://issues.apache.org/jira/browse/CLIMATE-803
-    def calc_subregion_area_mean_and_std*
+    def calc_subregion_area_mean_and_std
+
+    # The following lines from plotter module are excluded from test 
+    # coverage because they cannot be tested
+    def set_cmap
+
+    # Ignore all draw functions
+    def draw
+
+    # Multi line function definitions need to be ignored in this way
+    # Ignore function draw_taylor_diagram
+    def draw_taylor_diagram
+    gridshape=(1,1), ptitle='', subtitles=None, #pragma: no cover
+    pos='upper right', frameon=True, radmax=1.5
+
+    # Ignore function draw_subregions
+    def draw_subregions
+    parallels=None, meridians=None, subregion_masks=None
+
+    # Ignore function draw_time_series
+    def draw_time_series
+    xlabel='', ylabel='', ptitle='', subtitles=None,
+    label_month=False, yscale='linear', aspect=None
+
+    # Ignore function draw_barchart
+    def draw_barchart
+    xlabel='', ylabel=''
+
+    # Ignore function draw_contour_map
+    def draw_contour_map
+    clabel='', ptitle='', subtitles=None, cmap=None,
+    clevs=None, nlevs=10, parallels=None, meridians=None,
+    extend='neither', aspect=8.5/2.5
+
+    # Ignore function draw_portrait_diagram
+    def draw_portrait_diagram
+    gridshape=(1, 1), xlabel='', ylabel='', clabel='',
+    ptitle='', subtitles=None, cmap=None, clevs=None,
+    nlevs=10, extend='neither', aspect=None
+
+    class TaylorDiagram
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/climate/blob/6b9fae9d/ocw/tests/test_plotter.py
----------------------------------------------------------------------
diff --git a/ocw/tests/test_plotter.py b/ocw/tests/test_plotter.py
index 85e5fe4..f824670 100644
--- a/ocw/tests/test_plotter.py
+++ b/ocw/tests/test_plotter.py
@@ -18,10 +18,94 @@
 '''Unit tests for the plotter.py module'''
 
 import unittest
+import numpy as np
+from ocw import plotter
 
 
-class TestPlotter(unittest.TestCase):
-    pass
+class TestNiceIntervalsFunction(unittest.TestCase):
+    def test_nice_intervals(self):
+        test_array = np.arange(0, 30)
+        expected_array = np.arange(0, 30, 3)[1::]
+        nlev = 10
+        result = plotter._nice_intervals(test_array, nlev)
+        np.testing.assert_array_equal(result, expected_array)
+
+    def test_even_nice_intervals(self):
+        test_array = np.array([-2, 0, 2])
+        expected_array = np.array([-2., -1., 0., 1., 2.])
+        nlev = 4
+        result = plotter._nice_intervals(test_array, nlev)
+        np.testing.assert_array_equal(result, expected_array)
+
+    def test_odd_nice_intervals(self):
+        test_array = np.array([-2, 0, 2])
+        expected_array = np.array([-2., -1., 0., 1., 2.])
+        nlev = 5
+        result = plotter._nice_intervals(test_array, nlev)
+        np.testing.assert_array_equal(result, expected_array)
+
+
+class TestBestGridShapeFunction(unittest.TestCase):
+    def test_returned_shape_small(self):
+        nplots = 2
+        oldshape = (2, 2)
+        expected_shape = (1, 2)
+        new_shape = plotter._best_grid_shape(nplots, oldshape)
+        self.assertEqual(new_shape, expected_shape)
+
+    def test_returned_shape_large(self):
+        nplots = 57
+        oldshape = (220, 12)
+        expected_shape = (5, 12)
+        new_shape = plotter._best_grid_shape(nplots, oldshape)
+        self.assertEqual(new_shape, expected_shape)
+
+    def test_invalid_shape(self):
+        nplots = 2532
+        oldshape = (22, 12)
+        with self.assertRaises(ValueError):
+            plotter._best_grid_shape(nplots, oldshape)
+
+    def test_equal_number_of_plots_and_old_shape(self):
+        nplots = 4
+        oldshape = (2, 2)
+        expected_shape = (2, 2)
+        new_shape = plotter._best_grid_shape(nplots, oldshape)
+        self.assertEqual(new_shape, expected_shape)
+
+
+class TestFigshapeFunction(unittest.TestCase):
+    def test_small_gridshape_size(self):
+        gridshape = (2, 2)
+        expected_width = 8.5
+        expected_height = 5.5
+        width, height = plotter._fig_size(gridshape)
+        self.assertEqual(width, expected_width)
+        self.assertEqual(height, expected_height)
+
+    def test_large_gridshape_size(self):
+        gridshape = (567, 1223)
+        expected_width = 17.0
+        expected_height = 5.5
+        width, height = plotter._fig_size(gridshape)
+        self.assertEqual(width, expected_width)
+        self.assertEqual(height, expected_height)
+
+    def test_small_gridshape_with_aspect(self):
+        gridshape = (2, 2)
+        expected_width = 5.5
+        expected_height = 5.5
+        width, height = plotter._fig_size(gridshape, aspect=(4 / 3))
+        self.assertEqual(width, expected_width)
+        self.assertEqual(height, expected_height)
+
+    def test_large_gridshape_with_aspect(self):
+        gridshape = (567, 1223)
+        expected_width = 11.0
+        expected_height = 5.5
+        width, height = plotter._fig_size(gridshape, aspect=(16 / 9))
+        self.assertEqual(width, expected_width)
+        self.assertEqual(height, expected_height)
 
 if __name__ == '__main__':
     unittest.main()