Skip to content

Commit

Permalink
Fix Nightlight and LitPop tests
Browse files Browse the repository at this point in the history
* Move file checks to integration tests. They require file downloads.
* Fix assertion for almost equal arrays.
  • Loading branch information
peanutfun committed Aug 17, 2023
1 parent 36f2ed4 commit 4335e22
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 52 deletions.
2 changes: 1 addition & 1 deletion climada/entity/exposures/test/test_litpop.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def test_gridpoints_core_calc_offsets_exp_rescale(self):
self.assertEqual(result_array.shape, results_check.shape)
self.assertAlmostEqual(result_array.sum(), tot)
self.assertEqual(result_array[1,2], results_check[1,2])
np.testing.assert_array_almost_equal_nulp(result_array, results_check)
np.testing.assert_allclose(result_array, results_check)

def test_grp_read_pass(self):
"""test _grp_read() to pass and return either dict with admin1 values or None"""
Expand Down
52 changes: 1 addition & 51 deletions climada/entity/exposures/test/test_nightlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,6 @@ def test_required_files(self):
self.assertRaises(ValueError, nightlight.get_required_nl_files,
(-90, 90))

def test_check_files_exist(self):
"""Test check_nightlight_local_file_exists"""
# If invalid directory is supplied it has to fail
try:
nightlight.check_nl_local_file_exists(
np.ones(np.count_nonzero(BM_FILENAMES)), 'Invalid/path')[0]
raise Exception("if the path is not valid, check_nl_local_file_exists should fail")
except ValueError:
pass
files_exist = nightlight.check_nl_local_file_exists(
np.ones(np.count_nonzero(BM_FILENAMES)), SYSTEM_DIR)
self.assertTrue(
files_exist.sum() > 0,
f'{files_exist} {BM_FILENAMES}'
)

def test_download_nightlight_files(self):
"""Test check_nightlight_local_file_exists"""
# Not the same length of arguments
Expand Down Expand Up @@ -117,41 +101,7 @@ def test_get_required_nl_files(self):
req_files = nightlight.get_required_nl_files(bounds = bounds_c3)
bool = np.array_equal(np.array([0, 0, 0, 0, 0, 0, 1, 0]), req_files)
self.assertTrue(bool)

def test_check_nl_local_file_exists(self):
""" Test that an array with the correct number of already existing files
is produced, the LOGGER messages logged and the ValueError raised. """

# check logger messages by giving a to short req_file
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='WARNING') as cm:
nightlight.check_nl_local_file_exists(required_files = np.array([0, 0, 1, 1]))
self.assertIn('The parameter \'required_files\' was too short and is ignored',
cm.output[0])

# check logger message: not all files are available
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='DEBUG') as cm:
nightlight.check_nl_local_file_exists()
self.assertIn('Not all satellite files available. '
f'Found 5 out of 8 required files in {Path(SYSTEM_DIR)}', cm.output[0])

# check logger message: no files found in checkpath
check_path = Path('climada/entity/exposures')
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='INFO') as cm:
# using a random path where no files are stored
nightlight.check_nl_local_file_exists(check_path=check_path)
self.assertIn(f'No satellite files found locally in {check_path}',
cm.output[0])

# test raises with wrong path
check_path = Path('/random/wrong/path')
with self.assertRaises(ValueError) as cm:
nightlight.check_nl_local_file_exists(check_path=check_path)
self.assertEqual(f'The given path does not exist: {check_path}',
str(cm.exception))

# test that files_exist is correct
files_exist = nightlight.check_nl_local_file_exists()
self.assertEqual(int(sum(files_exist)), 5)


# Execute Tests
if __name__ == "__main__":
Expand Down
51 changes: 51 additions & 0 deletions climada/test/test_nightlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,57 @@ def test_untar_noaa_stable_nighlight(self):
self.assertIn('found more than one potential intensity file in', cm.output[0])
path_tar.unlink()

def test_check_nl_local_file_exists(self):
""" Test that an array with the correct number of already existing files
is produced, the LOGGER messages logged and the ValueError raised. """

# check logger messages by giving a to short req_file
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='WARNING') as cm:
nightlight.check_nl_local_file_exists(required_files = np.array([0, 0, 1, 1]))
self.assertIn('The parameter \'required_files\' was too short and is ignored',
cm.output[0])

# check logger message: not all files are available
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='DEBUG') as cm:
nightlight.check_nl_local_file_exists()
self.assertIn("Not all satellite files available.", cm.output[0])
self.assertIn(f"files in {Path(SYSTEM_DIR)}", cm.output[0])

# check logger message: no files found in checkpath
check_path = Path('climada/entity/exposures')
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='INFO') as cm:
# using a random path where no files are stored
nightlight.check_nl_local_file_exists(check_path=check_path)
self.assertIn(f'No satellite files found locally in {check_path}',
cm.output[0])

# test raises with wrong path
check_path = Path('/random/wrong/path')
with self.assertRaises(ValueError) as cm:
nightlight.check_nl_local_file_exists(check_path=check_path)
self.assertEqual(f'The given path does not exist: {check_path}',
str(cm.exception))

# test that files_exist is correct
files_exist = nightlight.check_nl_local_file_exists()
self.assertEqual(int(sum(files_exist)), 5)

def test_check_files_exist(self):
"""Test check_nightlight_local_file_exists"""
# If invalid directory is supplied it has to fail
try:
nightlight.check_nl_local_file_exists(
np.ones(np.count_nonzero(BM_FILENAMES)), 'Invalid/path')[0]
raise Exception("if the path is not valid, check_nl_local_file_exists should fail")
except ValueError:
pass
files_exist = nightlight.check_nl_local_file_exists(
np.ones(np.count_nonzero(BM_FILENAMES)), SYSTEM_DIR)
self.assertTrue(
files_exist.sum() > 0,
f'{files_exist} {BM_FILENAMES}'
)

# Execute Tests
if __name__ == "__main__":
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestNightlight)
Expand Down

0 comments on commit 4335e22

Please sign in to comment.