Skip to content

Commit ca148c0

Browse files
consequently use get_testfile (#822)
* deal with atl_prob_no_name test file * remove 'v' prefix before comparison * no message
1 parent 35a6c9d commit ca148c0

File tree

10 files changed

+22
-14
lines changed

10 files changed

+22
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Code freeze date: YYYY-MM-DD
2222
- The default tile layer in Exposures maps is not Stamen Terrain anymore, but [CartoDB Positron](https://github.com/CartoDB/basemap-styles). Affected methods are `climada.engine.Impact.plot_basemap_eai_exposure`,`climada.engine.Impact.plot_basemap_impact_exposure` and `climada.entity.Exposures.plot_basemap`. [#798](https://github.com/CLIMADA-project/climada_python/pull/798)
2323
- Recommend using Mamba instead of Conda for installing CLIMADA [#809](https://github.com/CLIMADA-project/climada_python/pull/809)
2424
- `Hazard.from_xarray_raster` now allows arbitrary values as 'event' coordinates [#837](https://github.com/CLIMADA-project/climada_python/pull/837)
25+
- `climada.test.get_test_file` now compares the version of the requested test dataset with the version of climada itself and selects the most appropriate dataset. In this way a test file can be updated without the need of changing the code of the unittest. [#822](https://github.com/CLIMADA-project/climada_python/pull/822)
2526

2627
### Fixed
2728

climada/engine/test/test_cost_benefit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from climada.test import get_test_file
3636

3737

38-
HAZ_TEST_MAT = get_test_file('atl_prob_no_name')
38+
HAZ_TEST_MAT = get_test_file('atl_prob_no_name', file_format='matlab')
3939
ENT_TEST_MAT = get_test_file('demo_today', file_format='MAT-file')
4040

4141

climada/entity/measures/test/test_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
from climada.entity.measures.measure_set import MeasureSet
3434
from climada.entity.measures.base import Measure, IMPF_ID_FACT
3535
from climada.util.constants import EXP_DEMO_H5, HAZ_DEMO_H5
36+
from climada.test import get_test_file
3637
import climada.util.coordinates as u_coord
37-
import climada.hazard.test as hazard_test
3838
import climada.entity.exposures.test as exposures_test
3939

4040
DATA_DIR = CONFIG.measures.test_data.dir()
4141

42-
HAZ_TEST_MAT = Path(hazard_test.__file__).parent / 'data' / 'atl_prob_no_name.mat'
42+
HAZ_TEST_MAT = get_test_file('atl_prob_no_name', file_format='matlab')
4343
ENT_TEST_MAT = Path(exposures_test.__file__).parent / 'data' / 'demo_today.mat'
4444

4545
class TestApply(unittest.TestCase):

climada/hazard/centroids/test/test_centr.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
import pandas as pd
2626
import geopandas as gpd
2727

28-
from climada import CONFIG
2928
from climada.hazard.centroids.centr import Centroids
3029
from climada.util.constants import GLB_CENTROIDS_MAT, HAZ_TEMPLATE_XLS
31-
import climada.hazard.test as hazard_test
30+
from climada.test import get_test_file
3231

33-
HAZ_TEST_MAT = Path(hazard_test.__file__).parent / 'data' / 'atl_prob_no_name.mat'
32+
HAZ_TEST_MAT = get_test_file('atl_prob_no_name', file_format='matlab')
3433

3534

3635
class TestCentroidsReader(unittest.TestCase):
-1.35 MB
Binary file not shown.

climada/hazard/test/test_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"""
4242
Directory for writing (and subsequent reading) of temporary files created during tests.
4343
"""
44-
HAZ_TEST_MAT :Path = Path(hazard_test.__file__).parent.joinpath('data', 'atl_prob_no_name.mat')
44+
HAZ_TEST_MAT :Path = get_test_file('atl_prob_no_name', file_format='matlab')
4545
"""
4646
Hazard test file from Git repository. Fraction is 1. Format: matlab.
4747
"""
@@ -1093,8 +1093,8 @@ class TestReaderMat(unittest.TestCase):
10931093
"""Test reader functionality of the ExposuresExcel class"""
10941094

10951095
def test_hazard_pass(self):
1096-
"""Read a hazard mat file correctly."""
1097-
# Read demo excel file
1096+
"""Read a hazard matlab file correctly."""
1097+
# Read demo matlab file
10981098
hazard = Hazard.from_mat(HAZ_TEST_MAT)
10991099

11001100
# Check results

climada/hazard/test/test_trop_cyclone.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import xarray as xr
3030

3131
from climada.util import ureg
32+
from climada.test import get_test_file
3233
from climada.hazard.tc_tracks import TCTracks
3334
from climada.hazard.trop_cyclone import (
3435
TropCyclone, get_close_centroids, _vtrans, _B_holland_1980, _bs_holland_2008,
@@ -43,7 +44,7 @@
4344
TEST_TRACK = DATA_DIR.joinpath("trac_brb_test.csv")
4445
TEST_TRACK_SHORT = DATA_DIR.joinpath("trac_short_test.csv")
4546

46-
CENTR_TEST_BRB = Centroids.from_mat(DATA_DIR.joinpath('centr_brb_test.mat'))
47+
CENTR_TEST_BRB = Centroids.from_hdf5(get_test_file('centr_test_brb', file_format='hdf5'))
4748

4849

4950
class TestReader(unittest.TestCase):

climada/test/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"""
2121

2222
from climada.util.api_client import Client
23+
from climada._version import __version__ as climada_version
2324

2425

2526
def get_test_file(ds_name, file_format=None):
@@ -42,7 +43,13 @@ def get_test_file(ds_name, file_format=None):
4243
the path to the downloaded file
4344
"""
4445
client = Client()
45-
test_ds = client.get_dataset_info(name=ds_name, status='test_dataset')
46+
# get the dataset with the highest version below (or equal to) the current climada version
47+
# in this way a test dataset can be updated without breaking tests on former versions
48+
# just make sure that the new dataset has a higher version than any previous version
49+
test_ds = [ds for ds in sorted(
50+
client.list_dataset_infos(name=ds_name, status='test_dataset', version='ANY'),
51+
key=lambda ds: ds.version
52+
) if ds.version.strip('v') <= climada_version.strip('v')][-1]
4653
_, files = client.download_dataset(test_ds)
4754
[test_file] = [fil for fil in files if fil.name in [
4855
dsf.file_name

climada/test/test_calibration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from climada.util.constants import ENT_DEMO_TODAY
3131
import climada.hazard.test as hazard_test
3232

33-
HAZ_TEST_MAT = Path(hazard_test.__file__).parent.joinpath('data', 'atl_prob_no_name.mat')
33+
HAZ_TEST_MAT = get_test_file('atl_prob_no_name', file_format='matlab')
3434

3535
DATA_FOLDER = CONFIG.test_data.dir()
3636

climada/test/test_hazard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
from climada.util.constants import (HAZ_DEMO_FL, WS_DEMO_NC)
3434
from climada.util.api_client import Client
3535
from climada.util import coordinates as u_coord
36-
import climada.hazard.test as hazard_test
36+
from climada.test import get_test_file
3737

3838
DATA_DIR = CONFIG.test_data.dir()
3939

4040
# Hazard test file from Git repository. Fraction is 1. Format: matlab.
41-
HAZ_TEST_MAT :Path = Path(hazard_test.__file__).parent.joinpath('data', 'atl_prob_no_name.mat')
41+
HAZ_TEST_MAT :Path = get_test_file('atl_prob_no_name', file_format='matlab')
4242

4343
class TestCentroids(unittest.TestCase):
4444
"""Test centroids functionalities"""

0 commit comments

Comments
 (0)