Skip to content

Commit

Permalink
Added more tests for projectables and updated projectable 3d resample…
Browse files Browse the repository at this point in the history
… test. 100% coverage of projectable!
  • Loading branch information
djhoese committed Jun 23, 2015
1 parent 332d82c commit ac2a641
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions mpop/tests/test_projectable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -142,31 +167,38 @@ 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"
p.info["area"] = source_area
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():
Expand Down

0 comments on commit ac2a641

Please sign in to comment.