From 4335e2250b7ae0f628928c05b4753a0389105888 Mon Sep 17 00:00:00 2001 From: Lukas Riedel <34276446+peanutfun@users.noreply.github.com> Date: Thu, 17 Aug 2023 13:53:35 +0200 Subject: [PATCH] Fix Nightlight and LitPop tests * Move file checks to integration tests. They require file downloads. * Fix assertion for almost equal arrays. --- climada/entity/exposures/test/test_litpop.py | 2 +- .../entity/exposures/test/test_nightlight.py | 52 +------------------ climada/test/test_nightlight.py | 51 ++++++++++++++++++ 3 files changed, 53 insertions(+), 52 deletions(-) diff --git a/climada/entity/exposures/test/test_litpop.py b/climada/entity/exposures/test/test_litpop.py index 851c910fb..d8ec001cd 100644 --- a/climada/entity/exposures/test/test_litpop.py +++ b/climada/entity/exposures/test/test_litpop.py @@ -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""" diff --git a/climada/entity/exposures/test/test_nightlight.py b/climada/entity/exposures/test/test_nightlight.py index beaca1a84..b2ba19b0f 100644 --- a/climada/entity/exposures/test/test_nightlight.py +++ b/climada/entity/exposures/test/test_nightlight.py @@ -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 @@ -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__": diff --git a/climada/test/test_nightlight.py b/climada/test/test_nightlight.py index ce571cef2..636679eab 100644 --- a/climada/test/test_nightlight.py +++ b/climada/test/test_nightlight.py @@ -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)