Skip to content

Commit 4cc417b

Browse files
emanuel-schmidschmide
andauthored
Feature/data api client (#259)
* api_client: first draft * data_api: demo notebook added * explore parameter space * introduce data_api client matching to the current state of development of the data_api * api_client.Client: make use of the extended query options in rest/datasets * introduce data classes and cached download add tests * api_client.Client download methods implemented * api_client: rearrangements for cleaner organisation * tutorial is completely outdated. must be replaced by a guide that is up to date with the current api and client * include peewee dependency Co-authored-by: schmide <[email protected]>
1 parent 7923fe4 commit 4cc417b

File tree

5 files changed

+597
-2
lines changed

5 files changed

+597
-2
lines changed

climada/conf/climada.conf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,10 @@
7979
}
8080
},
8181
"log_level": "WARNING",
82-
"max_matrix_size": 1000000000
82+
"max_matrix_size": 1000000000,
83+
"data_api": {
84+
"host": "https://climada.ethz.ch",
85+
"chunk_size": 8192,
86+
"cache_db": "{local_data.system}/.downloads.db"
87+
}
8388
}

climada/test/test_api_client.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)