climate-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jarifibra...@apache.org
Subject [2/3] climate git commit: Add tests for local module
Date Tue, 14 Jun 2016 06:40:58 GMT
Add tests for local module

- Add new tests for local module
- Add .coveragerc file to remove redundant code from test coverage


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

Branch: refs/heads/master
Commit: 80664b66ba5d40610e670fdba7fe74536e82a4bb
Parents: a4777fb
Author: Ibrahim <jarifibrahim@gmail.com>
Authored: Mon Jun 13 13:15:15 2016 +0530
Committer: Ibrahim <jarifibrahim@gmail.com>
Committed: Tue Jun 14 01:17:11 2016 +0530

----------------------------------------------------------------------
 .coveragerc             |   5 +++
 ocw/tests/test_local.py | 104 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/climate/blob/80664b66/.coveragerc
----------------------------------------------------------------------
diff --git a/.coveragerc b/.coveragerc
index 4e4a627..fa45ba4 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -18,3 +18,8 @@
 [report]
 exclude_lines = 
     if __name__ == .__main__.:
+    # These lines will never be executed under any circumstances
+    if level_index == 0:
+    elif level_index == 1:
+    elif level_index == 2:
+    else:

http://git-wip-us.apache.org/repos/asf/climate/blob/80664b66/ocw/tests/test_local.py
----------------------------------------------------------------------
diff --git a/ocw/tests/test_local.py b/ocw/tests/test_local.py
index 6137563..1c747f9 100644
--- a/ocw/tests/test_local.py
+++ b/ocw/tests/test_local.py
@@ -40,6 +40,12 @@ class test_load_file(unittest.TestCase):
     def tearDown(self):
         os.remove(self.file_path)
 
+    def test_load_invalid_file_path(self):
+        self.invalid_netcdf_path = '/invalid/path'
+        with self.assertRaises(ValueError):
+            local.load_file(file_path=self.invalid_netcdf_path,
+                            variable_name='test variable')
+
     def test_function_load_file_lats(self):
         """To test load_file function for latitudes"""
         self.assertItemsEqual(local.load_file(
@@ -76,6 +82,104 @@ class test_load_file(unittest.TestCase):
         self.assertEqual(ds.origin['source'], 'local')
 
 
+class TestLoadMultipleFiles(unittest.TestCase):
+    def setUp(self):
+        # Read netCDF file
+        self.file_path = create_netcdf_object()
+        self.netCDF_file = netCDF4.Dataset(self.file_path, 'r')
+        self.latitudes = self.netCDF_file.variables['latitude'][:]
+        self.longitudes = self.netCDF_file.variables['longitude'][:]
+        self.values = self.netCDF_file.variables['value'][:]
+        self.variable_name_list = ['latitude',
+                                   'longitude', 'time', 'level', 'value']
+        self.possible_value_name = ['latitude', 'longitude', 'time', 'level']
+
+    def tearDown(self):
+        os.remove(self.file_path)
+
+    def test_function_load_multiple_files_data_name(self):
+        dataset, data_name = local.load_multiple_files(self.file_path, "value")
+        self.assertEqual(data_name, ['model'])
+
+    def test_function_load_multiple_files_lons(self):
+        """To test load_multiple_file function for longitudes"""
+        dataset, data_name = local.load_multiple_files(self.file_path, "value")
+        self.assertItemsEqual(dataset[0].lons, self.longitudes)
+
+    def test_function_load_multiple_files_times(self):
+        """To test load_multiple_files function for times"""
+        dataset, data_name = local.load_multiple_files(self.file_path, "value")
+
+        newTimes = datetime.datetime(2001, 01, 01), datetime.datetime(
+            2001, 02, 01), datetime.datetime(2001, 03, 01)
+        self.assertItemsEqual(dataset[0].times, newTimes)
+
+    def test_function_load_multiple_files_values(self):
+        """To test load_multiple_files function for values"""
+        new_values = self.values[:, 0, :, :]
+        dataset, data_name = local.load_multiple_files(
+            self.file_path, "value")
+        self.assertTrue(numpy.allclose(dataset[0].values, new_values))
+
+    def test_load_multiple_files_custom_dataset_name(self):
+        """Test adding a custom name to a dataset"""
+        dataset, data_name = local.load_multiple_files(self.file_path,
+                                                       "value",
+                                                       dataset_name='foo')
+        self.assertEqual(dataset[0].name, 'foo')
+
+    def test_dataset_origin(self):
+        dataset, data_name = local.load_multiple_files(self.file_path, 'value')
+        expected_keys = set(['source', 'path', 'lat_name', 'lon_name',
+                             'time_name'])
+        self.assertEqual(set(dataset[0].origin.keys()), expected_keys)
+        self.assertEqual(dataset[0].origin['source'], 'local')
+
+
+class TestLoadDatasetFromMultipleNetcdfFiles(unittest.TestCase):
+    def setUp(self):
+        self.file_path = create_netcdf_object()
+        self.netCDF_file = netCDF4.Dataset(self.file_path, 'r+')
+        self.latitudes = self.netCDF_file.variables['latitude'][:]
+        self.longitudes = self.netCDF_file.variables['longitude'][:]
+        self.values = self.netCDF_file.variables['value'][:]
+        self.variable_name_list = ['latitude',
+                                   'longitude', 'time', 'level', 'value']
+        self.possible_value_name = ['latitude', 'longitude', 'time', 'level']
+        self.dataset = local.load_dataset_from_multiple_netcdf_files(
+            variable_name='value',
+            file_path='',
+            filename_pattern=[
+                self.file_path])
+
+    def tearDown(self):
+        os.remove(self.file_path)
+
+    def test_variable_name(self):
+        self.assertEqual(self.dataset.variable, 'value')
+
+    def test_function_load_dataset_from_multiple_netcdf_files_lats(self):
+        """To test load_multiple_files function for times"""
+        _, self.latitudes = numpy.meshgrid(self.longitudes, self.latitudes)
+        numpy.testing.assert_array_equal(self.dataset.lats, self.latitudes)
+
+    def test_function_load_dataset_from_multiple_netcdf_files_lons(self):
+        """To test load_multiple_files function for times"""
+        self.longitudes, _ = numpy.meshgrid(self.longitudes, self.latitudes)
+        numpy.testing.assert_array_equal(self.dataset.lons, self.longitudes)
+
+    def test_function_load_dataset_from_multiple_netcdf_files_times(self):
+        """To test load_multiple_files function for times"""
+        newTimes = datetime.datetime(2001, 01, 01), datetime.datetime(
+            2001, 02, 01), datetime.datetime(2001, 03, 01)
+        self.assertItemsEqual(self.dataset.times, newTimes)
+
+    def test_function_load_dataset_from_multiple_netcdf_files_values(self):
+        """To test load_multiple_files function for values"""
+        new_values = self.values[:, 0, :, :]
+        self.assertTrue(numpy.allclose(self.dataset.values, new_values))
+
+
 class test_get_netcdf_variable_names(unittest.TestCase):
     file_path = "http://zipper.jpl.nasa.gov/dist/"
     test_model = "AFRICA_KNMI-RACMO2.2b_CTL_ERAINT_MM_50km_1989-2008_tasmax.nc"


Mime
View raw message