Skip to content

Commit

Permalink
consequently use get_testfile (#822)
Browse files Browse the repository at this point in the history
* deal with atl_prob_no_name test file

* remove 'v' prefix before comparison

* no message
  • Loading branch information
emanuel-schmid authored Jan 18, 2024
1 parent 35a6c9d commit ca148c0
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Code freeze date: YYYY-MM-DD
- 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)
- Recommend using Mamba instead of Conda for installing CLIMADA [#809](https://github.com/CLIMADA-project/climada_python/pull/809)
- `Hazard.from_xarray_raster` now allows arbitrary values as 'event' coordinates [#837](https://github.com/CLIMADA-project/climada_python/pull/837)
- `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)

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion climada/engine/test/test_cost_benefit.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from climada.test import get_test_file


HAZ_TEST_MAT = get_test_file('atl_prob_no_name')
HAZ_TEST_MAT = get_test_file('atl_prob_no_name', file_format='matlab')
ENT_TEST_MAT = get_test_file('demo_today', file_format='MAT-file')


Expand Down
4 changes: 2 additions & 2 deletions climada/entity/measures/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
from climada.entity.measures.measure_set import MeasureSet
from climada.entity.measures.base import Measure, IMPF_ID_FACT
from climada.util.constants import EXP_DEMO_H5, HAZ_DEMO_H5
from climada.test import get_test_file
import climada.util.coordinates as u_coord
import climada.hazard.test as hazard_test
import climada.entity.exposures.test as exposures_test

DATA_DIR = CONFIG.measures.test_data.dir()

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

class TestApply(unittest.TestCase):
Expand Down
5 changes: 2 additions & 3 deletions climada/hazard/centroids/test/test_centr.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
import pandas as pd
import geopandas as gpd

from climada import CONFIG
from climada.hazard.centroids.centr import Centroids
from climada.util.constants import GLB_CENTROIDS_MAT, HAZ_TEMPLATE_XLS
import climada.hazard.test as hazard_test
from climada.test import get_test_file

HAZ_TEST_MAT = Path(hazard_test.__file__).parent / 'data' / 'atl_prob_no_name.mat'
HAZ_TEST_MAT = get_test_file('atl_prob_no_name', file_format='matlab')


class TestCentroidsReader(unittest.TestCase):
Expand Down
Binary file removed climada/hazard/test/data/atl_prob_no_name.mat
Binary file not shown.
6 changes: 3 additions & 3 deletions climada/hazard/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"""
Directory for writing (and subsequent reading) of temporary files created during tests.
"""
HAZ_TEST_MAT :Path = Path(hazard_test.__file__).parent.joinpath('data', 'atl_prob_no_name.mat')
HAZ_TEST_MAT :Path = get_test_file('atl_prob_no_name', file_format='matlab')
"""
Hazard test file from Git repository. Fraction is 1. Format: matlab.
"""
Expand Down Expand Up @@ -1093,8 +1093,8 @@ class TestReaderMat(unittest.TestCase):
"""Test reader functionality of the ExposuresExcel class"""

def test_hazard_pass(self):
"""Read a hazard mat file correctly."""
# Read demo excel file
"""Read a hazard matlab file correctly."""
# Read demo matlab file
hazard = Hazard.from_mat(HAZ_TEST_MAT)

# Check results
Expand Down
3 changes: 2 additions & 1 deletion climada/hazard/test/test_trop_cyclone.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import xarray as xr

from climada.util import ureg
from climada.test import get_test_file
from climada.hazard.tc_tracks import TCTracks
from climada.hazard.trop_cyclone import (
TropCyclone, get_close_centroids, _vtrans, _B_holland_1980, _bs_holland_2008,
Expand All @@ -43,7 +44,7 @@
TEST_TRACK = DATA_DIR.joinpath("trac_brb_test.csv")
TEST_TRACK_SHORT = DATA_DIR.joinpath("trac_short_test.csv")

CENTR_TEST_BRB = Centroids.from_mat(DATA_DIR.joinpath('centr_brb_test.mat'))
CENTR_TEST_BRB = Centroids.from_hdf5(get_test_file('centr_test_brb', file_format='hdf5'))


class TestReader(unittest.TestCase):
Expand Down
9 changes: 8 additions & 1 deletion climada/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""

from climada.util.api_client import Client
from climada._version import __version__ as climada_version


def get_test_file(ds_name, file_format=None):
Expand All @@ -42,7 +43,13 @@ def get_test_file(ds_name, file_format=None):
the path to the downloaded file
"""
client = Client()
test_ds = client.get_dataset_info(name=ds_name, status='test_dataset')
# get the dataset with the highest version below (or equal to) the current climada version
# in this way a test dataset can be updated without breaking tests on former versions
# just make sure that the new dataset has a higher version than any previous version
test_ds = [ds for ds in sorted(
client.list_dataset_infos(name=ds_name, status='test_dataset', version='ANY'),
key=lambda ds: ds.version
) if ds.version.strip('v') <= climada_version.strip('v')][-1]
_, files = client.download_dataset(test_ds)
[test_file] = [fil for fil in files if fil.name in [
dsf.file_name
Expand Down
2 changes: 1 addition & 1 deletion climada/test/test_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from climada.util.constants import ENT_DEMO_TODAY
import climada.hazard.test as hazard_test

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

DATA_FOLDER = CONFIG.test_data.dir()

Expand Down
4 changes: 2 additions & 2 deletions climada/test/test_hazard.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
from climada.util.constants import (HAZ_DEMO_FL, WS_DEMO_NC)
from climada.util.api_client import Client
from climada.util import coordinates as u_coord
import climada.hazard.test as hazard_test
from climada.test import get_test_file

DATA_DIR = CONFIG.test_data.dir()

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

class TestCentroids(unittest.TestCase):
"""Test centroids functionalities"""
Expand Down

0 comments on commit ca148c0

Please sign in to comment.