diff --git a/src/smhi/constants.py b/src/smhi/constants.py index d36e5add..2031478d 100644 --- a/src/smhi/constants.py +++ b/src/smhi/constants.py @@ -21,12 +21,12 @@ def get_now() -> datetime: return arrow.utcnow().datetime # .isoformat("T", "seconds") + "Z" -METOBS_AVAILABLE_PERIODS = [ - "latest-hour", - "latest-day", - "latest-months", - "corrected-archive", -] +METOBS_AVAILABLE_PERIODS = { + "latest-hour": 0, + "latest-day": 1, + "latest-months": 2, + "corrected-archive": 3, +} METFCTS_URL = ( "https://opendata-download-metfcst.smhi.se/" diff --git a/src/smhi/metobs.py b/src/smhi/metobs.py index f17aa0a5..141ca62a 100644 --- a/src/smhi/metobs.py +++ b/src/smhi/metobs.py @@ -298,7 +298,7 @@ def __init__( class Data(BaseMetobs): """Get data from period for version 1 of Metobs API.""" - _metobs_available_periods: Dict[str, str] = METOBS_AVAILABLE_PERIODS + _metobs_available_periods: Dict[str, int] = METOBS_AVAILABLE_PERIODS _metobs_parameter_tim: List[str] = ["Datum", "Tid (UTC)"] _metobs_parameter_dygn: List[str] = ["Representativt dygn"] _metobs_parameter_manad: List[str] = ["Representativ månad"] @@ -323,21 +323,13 @@ def __init__( """ super().__init__() - if period is None: - ordering = { - "latest-hour": 0, - "latest-day": 1, - "latest-months": 2, - "corrected-archive": 3, - } - available_periods = sorted( - periods_in_station.data, key=lambda x: ordering.__getitem__(x) - ) - period = available_periods[0] - if data_type != "json": raise TypeError("Only json supported.") + if period is None: + period = periods_in_station.data[0] + logger.info("No period selected, selecting: {period}.") + if self._check_available_periods(periods_in_station.data, period): logger.info( "Found only one period to download. " @@ -347,8 +339,8 @@ def __init__( if period not in self._metobs_available_periods: raise NotImplementedError( - "Select a supported periods: }" - + ", ".join([p for p in self._metobs_available_periods]) + "Select a supported period: " + + ", ".join([p for p in self._metobs_available_periods.keys()]) ) self.periods_in_station = periods_in_station diff --git a/src/smhi/models/metobs_model.py b/src/smhi/models/metobs_model.py index a482ebd7..4743e592 100644 --- a/src/smhi/models/metobs_model.py +++ b/src/smhi/models/metobs_model.py @@ -4,6 +4,7 @@ import pandas as pd from pydantic import BaseModel, ConfigDict, Field, field_validator +from smhi.constants import METOBS_AVAILABLE_PERIODS class MetobsLink(BaseModel): @@ -183,7 +184,7 @@ class MetobsStationModel(MetobsBaseModel): @field_validator("period") @classmethod def serialise_period_in_order(cls, period: List[MetobsLinks]): - return sorted(period, key=lambda x: x.key) + return sorted(period, key=lambda x: METOBS_AVAILABLE_PERIODS[x.key]) @property def data(self) -> Tuple[Optional[str], ...]: diff --git a/tests/unit/test_unit_smhi.py b/tests/unit/test_unit_smhi.py index 00b03178..23951b15 100644 --- a/tests/unit/test_unit_smhi.py +++ b/tests/unit/test_unit_smhi.py @@ -35,8 +35,8 @@ def __init__(self, data="str"): @pytest.fixture -def setup_test_x(): - """Pytest fixture""" +def setup_iterate_over_time(): + """Iterate over time fixture.""" df = pd.DataFrame( { "date": [ @@ -108,7 +108,7 @@ def test_unit_smhi_get_stations_from_title( @patch("smhi.metobs.Stations.__new__") @patch("smhi.metobs.Periods.__new__") @patch("smhi.metobs.Data.__new__") - @patch("smhi.smhi.SMHI._interpolate") + @patch("smhi.smhi.SMHI._interpolate", return_value="test") def test_unit_get_data( self, mock_interpolate, @@ -130,7 +130,6 @@ def test_unit_get_data( station, distance, """ - mock_interpolate.return_value = "test" client = SMHI() data = client.get_data(parameter, station, distance) assert data == "test" @@ -142,7 +141,7 @@ def test_unit_get_data( @patch("smhi.metobs.Periods.__new__") @patch("smhi.metobs.Data.__new__") @patch("smhi.smhi.SMHI._find_stations_by_city") - @patch("smhi.smhi.SMHI._interpolate") + @patch("smhi.smhi.SMHI._interpolate", return_value="test") def test_unit_get_data_by_city( self, mock_interpolate, @@ -166,7 +165,6 @@ def test_unit_get_data_by_city( city, distance """ - mock_interpolate.return_value = "test" client = SMHI() data = client.get_data_by_city(parameter, city, distance) assert data == "test" @@ -232,6 +230,7 @@ def test_find_stations_by_city( client = SMHI() _ = client._find_stations_by_city(parameter, city, distance) mock_nominatim.assert_called_once() + mock_find_from_gps.assert_called_once() @pytest.mark.parametrize("distance", [(0), (50)]) @patch("smhi.metobs.Stations.__new__") @@ -270,17 +269,15 @@ def test_interpolate( else: mock_find_from_gps.assert_called_once() - def test_iterate_over_time(self, setup_test_x): + def test_iterate_over_time(self, setup_iterate_over_time): """Unit test for SMHI _iterate_over_time method.""" - df, nearby_df, missing_df = setup_test_x + df, nearby_df, missing_df = setup_iterate_over_time client = SMHI() data = client._iterate_over_time(df, nearby_df, missing_df) assert data.tail(2).iloc[0]["Temperatur"] == nearby_df.iloc[0]["Temperatur"] - def test_find_missing_data( - self, - ): + def test_find_missing_data(self): """Unit test for SMHI _find_missing_data method.""" df = pd.DataFrame( {