From ac2a641a029bd6cdb3a3f9f65fb23be7cc7ca992 Mon Sep 17 00:00:00 2001 From: davidh-ssec Date: Mon, 22 Jun 2015 21:25:34 -0500 Subject: [PATCH] Added more tests for projectables and updated projectable 3d resample test. 100% coverage of projectable! --- mpop/tests/test_projectable.py | 36 ++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/mpop/tests/test_projectable.py b/mpop/tests/test_projectable.py index 51d6420f..c75297c6 100644 --- a/mpop/tests/test_projectable.py +++ b/mpop/tests/test_projectable.py @@ -61,6 +61,7 @@ def test_arithmetics(self): ds = projectable.Dataset(np.arange(1, 25), foo="bar") ref = np.arange(1, 25) ds2 = ds + 1 + ds3 = projectable.Dataset(np.linspace(0, 1, 24), foo="bar") np.testing.assert_array_equal((ds + 1).data, ref + 1) np.testing.assert_array_equal((1 + ds).data, ref + 1) np.testing.assert_array_equal((ds + ds).data, ref * 2) @@ -76,6 +77,12 @@ def test_arithmetics(self): np.testing.assert_array_equal((-ds).data, -ref) np.testing.assert_array_equal((abs(ds)).data, abs(ref)) np.testing.assert_array_equal((ds ** 2).data, ref ** 2) + np.testing.assert_array_equal((ds ** ds3).data, ref ** ds3.data) + + def test_str_repr(self): + ds = projectable.Dataset(np.arange(1, 25), foo="bar") + ds_str = str(ds) + ds_repr = repr(ds) class TestProjectable(unittest.TestCase): """ @@ -102,6 +109,24 @@ def test_to_image_1D(self): p = projectable.Projectable(np.arange(25)) self.assertRaises(ValueError, p.to_image) + def test_str(self): + # Normal situation + p = projectable.Projectable(np.arange(25), + sensor="fake_sensor", + wavelength_range=500, + resolution=250, + ) + p_str = str(p) + + # Not loaded data + p = projectable.Projectable() + p_str = str(p) + self.assertIn("not loaded", p_str) + + # Data that doesn't have a shape + p = projectable.Projectable(data=tuple()) + p_str = str(p) + @mock.patch('mpop.projectable.GeoImage') def test_to_image_2D(self, mock_geoimage): """ @@ -142,9 +167,14 @@ def test_show_save(self, mock_geoimage): p.show(filename) mock_geoimage.return_value.save.assert_called_once_with(filename) + def test_show_unloaded(self): + p = projectable.Projectable() + self.assertRaises(ValueError, p.show) + @mock.patch('mpop.projectable.resample_kd_tree_nearest') def test_resample_2D(self, mock_resampler): data = np.arange(25).reshape((5, 5)) + mock_resampler.return_value = data p = projectable.Projectable(data) source_area = "here" destination_area = "there" @@ -152,21 +182,23 @@ def test_resample_2D(self, mock_resampler): res = p.resample(destination_area) mock_resampler.assert_called_once_with(source_area, data, destination_area) self.assertTrue(isinstance(res, projectable.Projectable)) - self.assertEqual(res.data, mock_resampler.return_value) + np.testing.assert_array_equal(res.data, mock_resampler.return_value) @mock.patch('mpop.projectable.resample_kd_tree_nearest') def test_resample_3D(self, mock_resampler): data = np.arange(75).reshape((3, 5, 5)) + mock_resampler.return_value = np.rollaxis(data, 0, 3) p = projectable.Projectable(data) source_area = "here" destination_area = "there" p.info["area"] = source_area res = p.resample(destination_area) + self.assertTrue(mock_resampler.called) self.assertEqual(mock_resampler.call_args[0][0], source_area) np.testing.assert_array_equal(np.rollaxis(data, 0, 3), mock_resampler.call_args[0][1]) self.assertEqual(mock_resampler.call_args[0][2], destination_area) self.assertTrue(isinstance(res, projectable.Projectable)) - self.assertEqual(res.data, mock_resampler.return_value) + np.testing.assert_array_equal(res.data, data) def suite():