|
21 | 21 |
|
22 | 22 | import gzip |
23 | 23 | import io |
| 24 | +import shutil |
24 | 25 | import tarfile |
25 | 26 | import unittest |
26 | 27 | from pathlib import Path |
@@ -225,6 +226,10 @@ def test_load_nightlight_noaa(self): |
225 | 226 | pfile = f"{pattern}.p" |
226 | 227 | tiffile = f"{pattern}.tif" |
227 | 228 |
|
| 229 | + # Clean up potentially existing files |
| 230 | + SYSTEM_DIR.joinpath(pfile).unlink(missing_ok=True) |
| 231 | + SYSTEM_DIR.joinpath(gzfile).unlink(missing_ok=True) |
| 232 | + |
228 | 233 | # create an empty image |
229 | 234 | image = np.zeros((100, 100), dtype=np.uint8) |
230 | 235 | pilim = Image.fromarray(image) |
@@ -343,12 +348,18 @@ def test_check_nl_local_file_exists(self): |
343 | 348 | ) |
344 | 349 |
|
345 | 350 | # check logger message: not all files are available |
346 | | - with self.assertLogs( |
347 | | - "climada.entity.exposures.litpop.nightlight", level="DEBUG" |
348 | | - ) as cm: |
349 | | - nightlight.check_nl_local_file_exists() |
350 | | - self.assertIn("Not all satellite files available. Found ", cm.output[0]) |
351 | | - self.assertIn(f" out of 8 required files in {Path(SYSTEM_DIR)}", cm.output[0]) |
| 351 | + bm_filenames = [name % 2016 for name in BM_FILENAMES] |
| 352 | + with TemporaryDirectory() as tmpdir: |
| 353 | + # Copy two files |
| 354 | + shutil.copy(SYSTEM_DIR / bm_filenames[0], Path(tmpdir) / bm_filenames[0]) |
| 355 | + shutil.copy(SYSTEM_DIR / bm_filenames[2], Path(tmpdir) / bm_filenames[2]) |
| 356 | + |
| 357 | + with self.assertLogs( |
| 358 | + "climada.entity.exposures.litpop.nightlight", level="DEBUG" |
| 359 | + ) as cm: |
| 360 | + nightlight.check_nl_local_file_exists(check_path=tmpdir) |
| 361 | + self.assertIn("Not all satellite files available. Found 2", cm.output[0]) |
| 362 | + self.assertIn(f" out of 8 required files in {tmpdir}", cm.output[0]) |
352 | 363 |
|
353 | 364 | # check logger message: no files found in checkpath |
354 | 365 | check_path = Path("climada/entity/exposures") |
|
0 commit comments