Skip to content

Commit 4335e22

Browse files
committed
Fix Nightlight and LitPop tests
* Move file checks to integration tests. They require file downloads. * Fix assertion for almost equal arrays.
1 parent 36f2ed4 commit 4335e22

File tree

3 files changed

+53
-52
lines changed

3 files changed

+53
-52
lines changed

climada/entity/exposures/test/test_litpop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def test_gridpoints_core_calc_offsets_exp_rescale(self):
317317
self.assertEqual(result_array.shape, results_check.shape)
318318
self.assertAlmostEqual(result_array.sum(), tot)
319319
self.assertEqual(result_array[1,2], results_check[1,2])
320-
np.testing.assert_array_almost_equal_nulp(result_array, results_check)
320+
np.testing.assert_allclose(result_array, results_check)
321321

322322
def test_grp_read_pass(self):
323323
"""test _grp_read() to pass and return either dict with admin1 values or None"""

climada/entity/exposures/test/test_nightlight.py

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,6 @@ def test_required_files(self):
5656
self.assertRaises(ValueError, nightlight.get_required_nl_files,
5757
(-90, 90))
5858

59-
def test_check_files_exist(self):
60-
"""Test check_nightlight_local_file_exists"""
61-
# If invalid directory is supplied it has to fail
62-
try:
63-
nightlight.check_nl_local_file_exists(
64-
np.ones(np.count_nonzero(BM_FILENAMES)), 'Invalid/path')[0]
65-
raise Exception("if the path is not valid, check_nl_local_file_exists should fail")
66-
except ValueError:
67-
pass
68-
files_exist = nightlight.check_nl_local_file_exists(
69-
np.ones(np.count_nonzero(BM_FILENAMES)), SYSTEM_DIR)
70-
self.assertTrue(
71-
files_exist.sum() > 0,
72-
f'{files_exist} {BM_FILENAMES}'
73-
)
74-
7559
def test_download_nightlight_files(self):
7660
"""Test check_nightlight_local_file_exists"""
7761
# Not the same length of arguments
@@ -117,41 +101,7 @@ def test_get_required_nl_files(self):
117101
req_files = nightlight.get_required_nl_files(bounds = bounds_c3)
118102
bool = np.array_equal(np.array([0, 0, 0, 0, 0, 0, 1, 0]), req_files)
119103
self.assertTrue(bool)
120-
121-
def test_check_nl_local_file_exists(self):
122-
""" Test that an array with the correct number of already existing files
123-
is produced, the LOGGER messages logged and the ValueError raised. """
124-
125-
# check logger messages by giving a to short req_file
126-
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='WARNING') as cm:
127-
nightlight.check_nl_local_file_exists(required_files = np.array([0, 0, 1, 1]))
128-
self.assertIn('The parameter \'required_files\' was too short and is ignored',
129-
cm.output[0])
130-
131-
# check logger message: not all files are available
132-
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='DEBUG') as cm:
133-
nightlight.check_nl_local_file_exists()
134-
self.assertIn('Not all satellite files available. '
135-
f'Found 5 out of 8 required files in {Path(SYSTEM_DIR)}', cm.output[0])
136-
137-
# check logger message: no files found in checkpath
138-
check_path = Path('climada/entity/exposures')
139-
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='INFO') as cm:
140-
# using a random path where no files are stored
141-
nightlight.check_nl_local_file_exists(check_path=check_path)
142-
self.assertIn(f'No satellite files found locally in {check_path}',
143-
cm.output[0])
144-
145-
# test raises with wrong path
146-
check_path = Path('/random/wrong/path')
147-
with self.assertRaises(ValueError) as cm:
148-
nightlight.check_nl_local_file_exists(check_path=check_path)
149-
self.assertEqual(f'The given path does not exist: {check_path}',
150-
str(cm.exception))
151-
152-
# test that files_exist is correct
153-
files_exist = nightlight.check_nl_local_file_exists()
154-
self.assertEqual(int(sum(files_exist)), 5)
104+
155105

156106
# Execute Tests
157107
if __name__ == "__main__":

climada/test/test_nightlight.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,57 @@ def test_untar_noaa_stable_nighlight(self):
254254
self.assertIn('found more than one potential intensity file in', cm.output[0])
255255
path_tar.unlink()
256256

257+
def test_check_nl_local_file_exists(self):
258+
""" Test that an array with the correct number of already existing files
259+
is produced, the LOGGER messages logged and the ValueError raised. """
260+
261+
# check logger messages by giving a to short req_file
262+
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='WARNING') as cm:
263+
nightlight.check_nl_local_file_exists(required_files = np.array([0, 0, 1, 1]))
264+
self.assertIn('The parameter \'required_files\' was too short and is ignored',
265+
cm.output[0])
266+
267+
# check logger message: not all files are available
268+
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='DEBUG') as cm:
269+
nightlight.check_nl_local_file_exists()
270+
self.assertIn("Not all satellite files available.", cm.output[0])
271+
self.assertIn(f"files in {Path(SYSTEM_DIR)}", cm.output[0])
272+
273+
# check logger message: no files found in checkpath
274+
check_path = Path('climada/entity/exposures')
275+
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='INFO') as cm:
276+
# using a random path where no files are stored
277+
nightlight.check_nl_local_file_exists(check_path=check_path)
278+
self.assertIn(f'No satellite files found locally in {check_path}',
279+
cm.output[0])
280+
281+
# test raises with wrong path
282+
check_path = Path('/random/wrong/path')
283+
with self.assertRaises(ValueError) as cm:
284+
nightlight.check_nl_local_file_exists(check_path=check_path)
285+
self.assertEqual(f'The given path does not exist: {check_path}',
286+
str(cm.exception))
287+
288+
# test that files_exist is correct
289+
files_exist = nightlight.check_nl_local_file_exists()
290+
self.assertEqual(int(sum(files_exist)), 5)
291+
292+
def test_check_files_exist(self):
293+
"""Test check_nightlight_local_file_exists"""
294+
# If invalid directory is supplied it has to fail
295+
try:
296+
nightlight.check_nl_local_file_exists(
297+
np.ones(np.count_nonzero(BM_FILENAMES)), 'Invalid/path')[0]
298+
raise Exception("if the path is not valid, check_nl_local_file_exists should fail")
299+
except ValueError:
300+
pass
301+
files_exist = nightlight.check_nl_local_file_exists(
302+
np.ones(np.count_nonzero(BM_FILENAMES)), SYSTEM_DIR)
303+
self.assertTrue(
304+
files_exist.sum() > 0,
305+
f'{files_exist} {BM_FILENAMES}'
306+
)
307+
257308
# Execute Tests
258309
if __name__ == "__main__":
259310
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestNightlight)

0 commit comments

Comments
 (0)