|
| 1 | +""" |
| 2 | +This file is part of CLIMADA. |
| 3 | +
|
| 4 | +Copyright (C) 2017 ETH Zurich, CLIMADA contributors listed in AUTHORS. |
| 5 | +
|
| 6 | +CLIMADA is free software: you can redistribute it and/or modify it under the |
| 7 | +terms of the GNU General Public License as published by the Free |
| 8 | +Software Foundation, version 3. |
| 9 | +
|
| 10 | +CLIMADA is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 | +PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License along |
| 15 | +with CLIMADA. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +
|
| 17 | +--- |
| 18 | +
|
| 19 | +Test save module. |
| 20 | +""" |
| 21 | +from pathlib import Path |
| 22 | +import unittest |
| 23 | +from shutil import rmtree |
| 24 | + |
| 25 | +from climada import CONFIG |
| 26 | +from climada.util.api_client import Client, Download |
| 27 | + |
| 28 | +DATA_DIR = CONFIG.test_data.dir() |
| 29 | + |
| 30 | + |
| 31 | +class TestClient(unittest.TestCase): |
| 32 | + """Test data api client methods.""" |
| 33 | + |
| 34 | + def test_data_type(self): |
| 35 | + """""" |
| 36 | + lpdt = Client().get_data_type("litpop") |
| 37 | + self.assertEqual(lpdt.data_type, 'litpop') |
| 38 | + self.assertEqual(lpdt.data_type_group, 'exposures') |
| 39 | + |
| 40 | + def test_data_types(self): |
| 41 | + """""" |
| 42 | + exdts = Client().get_data_types("exposures") |
| 43 | + self.assertTrue('litpop' in [exdt.data_type for exdt in exdts]) |
| 44 | + |
| 45 | + def test_datasets(self): |
| 46 | + """""" |
| 47 | + datasets = Client().get_datasets(status=None, name='FAOSTAT_data_producer_prices') |
| 48 | + self.assertEqual(len(datasets), 1) |
| 49 | + |
| 50 | + def test_dataset(self): |
| 51 | + """""" |
| 52 | + client = Client() |
| 53 | + |
| 54 | + dataset = client.get_dataset(name='FAOSTAT_data_producer_prices') |
| 55 | + self.assertEqual(dataset.version, 'v1') |
| 56 | + self.assertEqual(len(dataset.files), 1) |
| 57 | + self.assertEqual(dataset.files[0].file_size, 26481) |
| 58 | + self.assertEqual(dataset.data_type.data_type, 'crop_production') |
| 59 | + |
| 60 | + dataset2 = client.get_dataset_by_uuid(dataset.uuid) |
| 61 | + self.assertEqual(dataset, dataset2) |
| 62 | + |
| 63 | + def test_download_file(self): |
| 64 | + """""" |
| 65 | + client = Client() |
| 66 | + client.MAX_WAITING_PERIOD = 0.1 |
| 67 | + dataset = client.get_dataset(name='FAOSTAT_data_producer_prices') |
| 68 | + |
| 69 | + # test failure |
| 70 | + def fail(x, y): |
| 71 | + raise Download.Failed("on purpose") |
| 72 | + self.assertRaises(Download.Failed, |
| 73 | + client.download_file, DATA_DIR, dataset.files[0], check=fail) |
| 74 | + self.assertFalse(DATA_DIR.joinpath(dataset.files[0].file_name).is_file()) |
| 75 | + |
| 76 | + # test success |
| 77 | + download = client.download_file(DATA_DIR, dataset.files[0]) |
| 78 | + self.assertEqual(download, DATA_DIR / dataset.files[0].file_name) |
| 79 | + self.assertTrue(download.is_file()) |
| 80 | + self.assertEqual(download.stat().st_size, dataset.files[0].file_size) |
| 81 | + download.unlink() |
| 82 | + self.assertFalse(download.is_file()) |
| 83 | + |
| 84 | + def test_download_dataset(self): |
| 85 | + """""" |
| 86 | + client = Client() |
| 87 | + client.MAX_WAITING_PERIOD = 0.1 |
| 88 | + |
| 89 | + dataset = client.get_dataset(name='test_write_raster') |
| 90 | + downloads = client.download_dataset(dataset, target_dir=DATA_DIR) |
| 91 | + self.assertEqual(len(downloads), 2) |
| 92 | + for download, dsfile in zip(downloads, dataset.files): |
| 93 | + self.assertTrue(download.is_file()) |
| 94 | + self.assertEqual(download.stat().st_size, dsfile.file_size) |
| 95 | + self.assertEqual(download.name, dsfile.file_name) |
| 96 | + self.assertEqual(download.parent.name, dataset.version) |
| 97 | + self.assertEqual(download.parent.parent.name, dataset.name) |
| 98 | + self.assertEqual(download.parent.parent.parent.name, dataset.data_type.data_type) |
| 99 | + download.unlink() |
| 100 | + rm_empty_dir(download.parent.parent.parent) |
| 101 | + |
| 102 | + |
| 103 | +def rm_empty_dir(folder): |
| 104 | + for subfolder in folder.iterdir(): |
| 105 | + if subfolder.is_dir(): |
| 106 | + rm_empty_dir(subfolder) |
| 107 | + try: |
| 108 | + folder.rmdir() |
| 109 | + except: |
| 110 | + pass |
| 111 | + |
| 112 | + |
| 113 | +# Execute Tests |
| 114 | +if __name__ == "__main__": |
| 115 | + TESTS = unittest.TestLoader().loadTestsFromTestCase(TestClient) |
| 116 | + unittest.TextTestRunner(verbosity=2).run(TESTS) |
0 commit comments