Skip to content

Commit

Permalink
#91 NA-50 added some tests for Nansat class
Browse files Browse the repository at this point in the history
  • Loading branch information
jeong1park committed Jan 25, 2018
1 parent b3293a9 commit 34078cf
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions nansat/tests/test_nansat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import json
import sys
from xml.sax.saxutils import unescape
from mock import patch, PropertyMock
from mock import patch, PropertyMock, MagicMock

import numpy as np

Expand Down Expand Up @@ -249,10 +249,14 @@ def test_bands(self):
self.assertTrue('name' in bands[1])
self.assertEqual(bands[1]['name'], 'L_645')

def test_has_band(self):
def test_has_band_if_name_matches(self):
n = Nansat(self.test_file_gcps, log_level=40)
hb = n.has_band('L_645')

self.assertTrue(hb)

def test_has_band_if_standard_name_matches(self):
n = Nansat(self.test_file_gcps, log_level=40)
hb = n.has_band('surface_upwelling_spectral_radiance_in_air_emerging_from_sea_water')
self.assertTrue(hb)

def test_write_fig_tif(self):
Expand All @@ -265,6 +269,10 @@ def test_write_fig_tif(self):
# present after opening the image
self.assertTrue(np.allclose(n.get_corners(), nn.get_corners()))

def test_resize_eResampleAlg_is_given(self):
n = Nansat(self.test_file_gcps, log_level=40)
n.resize(pixelsize=500, eResampleAlg=5)

def test_resize_by_pixelsize(self):
n = Nansat(self.test_file_gcps, log_level=40)
n.resize(pixelsize=500, resample_alg=1)
Expand Down Expand Up @@ -345,6 +353,20 @@ def test_get_GDALRasterBand(self):

self.assertEqual(type(b), gdal.Band)
self.assertEqual(type(arr), np.ndarray)

def test_get_GDALRasterBand_if_bandID_is_given(self):
n = Nansat(self.test_file_gcps, log_level=40)
b = n.get_GDALRasterBand(bandID=1)
arr = b.ReadAsArray()

self.assertEqual(type(b), gdal.Band)
self.assertEqual(type(arr), np.ndarray)

def test_list_bands_true(self):
n = Nansat(self.test_file_gcps, log_level=40)
lb = n.list_bands(True)

self.assertEqual(lb, None)

def test_list_bands_false(self):
n = Nansat(self.test_file_gcps, log_level=40)
Expand Down Expand Up @@ -538,6 +560,10 @@ def test_set_metadata_bandid(self):

self.assertEqual(m, 'newVal')

def test_get_band_number(self):
n1 = Nansat(self.test_file_stere, log_level=40)
self.assertEqual(n1._get_band_number(1), 1)

@unittest.skipUnless(MATPLOTLIB_IS_INSTALLED, 'Matplotlib is required')
def test_get_transect(self):
plt.switch_backend('agg')
Expand Down Expand Up @@ -736,13 +762,13 @@ def test_repr_basic(self):
self.assertIn('72', n_repr)
self.assertIn('35', n_repr)
self.assertIn('70', n_repr)
'''
@patch('nansat.nansat.Nansat.get_GDALRasterBand')

@patch.object(Nansat, 'get_GDALRasterBand')
def test_getitem(self, mock_Nansat):
mock_Nansat.GetMetadata = MagicMock(return_value = {'a':1})
mock_Nansat.ReadAsArray.return_value = None
Nansat(self.test_file_stere).__getitem__(1)
'''
type(mock_Nansat()).GetMetadata = MagicMock(return_value={'a':1})
type(mock_Nansat()).ReadAsArray = MagicMock(return_value=None)
with self.assertRaises(NansatGDALError):
Nansat(self.test_file_stere).__getitem__(1)


if __name__ == "__main__":
Expand Down

0 comments on commit 34078cf

Please sign in to comment.