From ad17dd806ada5d747164f1371bb327da2fbd4399 Mon Sep 17 00:00:00 2001 From: Mladen Gibanica <11275336+mgcth@users.noreply.github.com> Date: Fri, 29 Mar 2024 10:09:06 +0100 Subject: [PATCH] Move fixtures and update strang integration tests --- src/smhi/strang.py | 12 ++-- .../unit_mesan_multipoint_format.json | 0 .../unit_mesan_multipoint_format_result.csv | 0 .../{ => mesan}/unit_mesan_point_format.json | 0 .../unit_mesan_point_format_result.csv | 0 .../{ => metobs}/metobs_integration_1.json | 0 .../{ => metobs}/metobs_integration_2.json | 0 .../{ => metobs}/metobs_integration_22.json | 0 tests/fixtures/{ => strang}/STRANG_NONE.csv | 2 +- ...ANG_RESULT_DAILY_2020_01_01_2020_01_02.csv | 2 +- ...NG_RESULT_HOURLY_2020_01_01_2020_01_02.csv | 2 +- ...G_RESULT_MONTHLY_2020_01_01_2020_02_01.csv | 2 +- ...ESULT_MULTIPOINT_2020_01_01_MONTHLY_10.csv | 2 +- tests/integration/test_integration_metobs.py | 4 +- tests/integration/test_integration_strang.py | 72 +++++++++++-------- 15 files changed, 57 insertions(+), 41 deletions(-) rename tests/fixtures/{ => mesan}/unit_mesan_multipoint_format.json (100%) rename tests/fixtures/{ => mesan}/unit_mesan_multipoint_format_result.csv (100%) rename tests/fixtures/{ => mesan}/unit_mesan_point_format.json (100%) rename tests/fixtures/{ => mesan}/unit_mesan_point_format_result.csv (100%) rename tests/fixtures/{ => metobs}/metobs_integration_1.json (100%) rename tests/fixtures/{ => metobs}/metobs_integration_2.json (100%) rename tests/fixtures/{ => metobs}/metobs_integration_22.json (100%) rename tests/fixtures/{ => strang}/STRANG_NONE.csv (61%) rename tests/fixtures/{ => strang}/STRANG_RESULT_DAILY_2020_01_01_2020_01_02.csv (59%) rename tests/fixtures/{ => strang}/STRANG_RESULT_HOURLY_2020_01_01_2020_01_02.csv (94%) rename tests/fixtures/{ => strang}/STRANG_RESULT_MONTHLY_2020_01_01_2020_02_01.csv (61%) rename tests/fixtures/{ => strang}/STRANG_RESULT_MULTIPOINT_2020_01_01_MONTHLY_10.csv (78%) diff --git a/src/smhi/strang.py b/src/smhi/strang.py index cc60fb35..e9dbfb49 100644 --- a/src/smhi/strang.py +++ b/src/smhi/strang.py @@ -91,7 +91,7 @@ def get_point( NotImplementedError: parameter not supported """ strang_parameter = self._available_parameters[parameter] - if strang_parameter.parameter is None: + if strang_parameter.key is None: raise NotImplementedError( "Parameter not implemented." + " Try client.parameters to list available parameters." @@ -109,7 +109,7 @@ def get_point( data, header, status = self._get_and_load_data(url, strang_parameter) return StrangPoint( - parameter_key=strang_parameter.parameter, + parameter_key=strang_parameter.key, parameter_meaning=strang_parameter.meaning, longitude=longitude, latitude=latitude, @@ -142,7 +142,7 @@ def get_multipoint( NotImplementedError: parameter not supported """ strang_parameter = self._available_parameters[parameter] - if strang_parameter.parameter is None: + if strang_parameter.key is None: raise NotImplementedError( "Parameter not implemented." + " Try client.parameters to list available parameters." @@ -159,7 +159,7 @@ def get_multipoint( data, header, status = self._get_and_load_data(url, strang_parameter) return StrangMultiPoint( - parameter_key=strang_parameter.parameter, + parameter_key=strang_parameter.key, parameter_meaning=strang_parameter.meaning, valid_time=valid_time, time_interval=time_interval, @@ -190,7 +190,7 @@ def _build_base_point_url( return url( lon=longitude, lat=latitude, - parameter=parameter.parameter, + parameter=parameter.key, ) def _build_base_multipoint_url( @@ -209,7 +209,7 @@ def _build_base_multipoint_url( """ return url( validtime=valid_time, - parameter=parameter.parameter, + parameter=parameter.key, ) def _build_time_point_url( diff --git a/tests/fixtures/unit_mesan_multipoint_format.json b/tests/fixtures/mesan/unit_mesan_multipoint_format.json similarity index 100% rename from tests/fixtures/unit_mesan_multipoint_format.json rename to tests/fixtures/mesan/unit_mesan_multipoint_format.json diff --git a/tests/fixtures/unit_mesan_multipoint_format_result.csv b/tests/fixtures/mesan/unit_mesan_multipoint_format_result.csv similarity index 100% rename from tests/fixtures/unit_mesan_multipoint_format_result.csv rename to tests/fixtures/mesan/unit_mesan_multipoint_format_result.csv diff --git a/tests/fixtures/unit_mesan_point_format.json b/tests/fixtures/mesan/unit_mesan_point_format.json similarity index 100% rename from tests/fixtures/unit_mesan_point_format.json rename to tests/fixtures/mesan/unit_mesan_point_format.json diff --git a/tests/fixtures/unit_mesan_point_format_result.csv b/tests/fixtures/mesan/unit_mesan_point_format_result.csv similarity index 100% rename from tests/fixtures/unit_mesan_point_format_result.csv rename to tests/fixtures/mesan/unit_mesan_point_format_result.csv diff --git a/tests/fixtures/metobs_integration_1.json b/tests/fixtures/metobs/metobs_integration_1.json similarity index 100% rename from tests/fixtures/metobs_integration_1.json rename to tests/fixtures/metobs/metobs_integration_1.json diff --git a/tests/fixtures/metobs_integration_2.json b/tests/fixtures/metobs/metobs_integration_2.json similarity index 100% rename from tests/fixtures/metobs_integration_2.json rename to tests/fixtures/metobs/metobs_integration_2.json diff --git a/tests/fixtures/metobs_integration_22.json b/tests/fixtures/metobs/metobs_integration_22.json similarity index 100% rename from tests/fixtures/metobs_integration_22.json rename to tests/fixtures/metobs/metobs_integration_22.json diff --git a/tests/fixtures/STRANG_NONE.csv b/tests/fixtures/strang/STRANG_NONE.csv similarity index 61% rename from tests/fixtures/STRANG_NONE.csv rename to tests/fixtures/strang/STRANG_NONE.csv index cbdffedd..96c87583 100644 --- a/tests/fixtures/STRANG_NONE.csv +++ b/tests/fixtures/strang/STRANG_NONE.csv @@ -1,3 +1,3 @@ -date_time,Direct normal irradiance [W/m²] +date_time,value 2020-01-01 00:00:00+00:00,23086.5 2020-02-01 00:00:00+00:00,51961.8 diff --git a/tests/fixtures/STRANG_RESULT_DAILY_2020_01_01_2020_01_02.csv b/tests/fixtures/strang/STRANG_RESULT_DAILY_2020_01_01_2020_01_02.csv similarity index 59% rename from tests/fixtures/STRANG_RESULT_DAILY_2020_01_01_2020_01_02.csv rename to tests/fixtures/strang/STRANG_RESULT_DAILY_2020_01_01_2020_01_02.csv index 8580d85e..c0d98f83 100644 --- a/tests/fixtures/STRANG_RESULT_DAILY_2020_01_01_2020_01_02.csv +++ b/tests/fixtures/strang/STRANG_RESULT_DAILY_2020_01_01_2020_01_02.csv @@ -1,3 +1,3 @@ -date_time,Direct normal irradiance [W/m²] +date_time,value 2020-01-01 00:00:00+00:00,608.0 2020-01-02 00:00:00+00:00,12.1 diff --git a/tests/fixtures/STRANG_RESULT_HOURLY_2020_01_01_2020_01_02.csv b/tests/fixtures/strang/STRANG_RESULT_HOURLY_2020_01_01_2020_01_02.csv similarity index 94% rename from tests/fixtures/STRANG_RESULT_HOURLY_2020_01_01_2020_01_02.csv rename to tests/fixtures/strang/STRANG_RESULT_HOURLY_2020_01_01_2020_01_02.csv index 4dd7a069..34dd7a45 100644 --- a/tests/fixtures/STRANG_RESULT_HOURLY_2020_01_01_2020_01_02.csv +++ b/tests/fixtures/strang/STRANG_RESULT_HOURLY_2020_01_01_2020_01_02.csv @@ -1,4 +1,4 @@ -date_time,Direct normal irradiance [W/m²] +date_time,value 2020-01-01 00:00:00+00:00,0.0 2020-01-01 01:00:00+00:00,0.0 2020-01-01 02:00:00+00:00,0.0 diff --git a/tests/fixtures/STRANG_RESULT_MONTHLY_2020_01_01_2020_02_01.csv b/tests/fixtures/strang/STRANG_RESULT_MONTHLY_2020_01_01_2020_02_01.csv similarity index 61% rename from tests/fixtures/STRANG_RESULT_MONTHLY_2020_01_01_2020_02_01.csv rename to tests/fixtures/strang/STRANG_RESULT_MONTHLY_2020_01_01_2020_02_01.csv index cbdffedd..96c87583 100644 --- a/tests/fixtures/STRANG_RESULT_MONTHLY_2020_01_01_2020_02_01.csv +++ b/tests/fixtures/strang/STRANG_RESULT_MONTHLY_2020_01_01_2020_02_01.csv @@ -1,3 +1,3 @@ -date_time,Direct normal irradiance [W/m²] +date_time,value 2020-01-01 00:00:00+00:00,23086.5 2020-02-01 00:00:00+00:00,51961.8 diff --git a/tests/fixtures/STRANG_RESULT_MULTIPOINT_2020_01_01_MONTHLY_10.csv b/tests/fixtures/strang/STRANG_RESULT_MULTIPOINT_2020_01_01_MONTHLY_10.csv similarity index 78% rename from tests/fixtures/STRANG_RESULT_MULTIPOINT_2020_01_01_MONTHLY_10.csv rename to tests/fixtures/strang/STRANG_RESULT_MULTIPOINT_2020_01_01_MONTHLY_10.csv index be09357c..f08747e3 100644 --- a/tests/fixtures/STRANG_RESULT_MULTIPOINT_2020_01_01_MONTHLY_10.csv +++ b/tests/fixtures/strang/STRANG_RESULT_MULTIPOINT_2020_01_01_MONTHLY_10.csv @@ -1,4 +1,4 @@ -,lat,lon,CIE UV irradiance [mW/m²] 2020-01-01T00:00:00+00:00 monthly +,lat,lon,value 0,71.139084,-9.659227,4.3 1,71.1481,-9.589094,4.3 2,71.157104,-9.5189,4.3 diff --git a/tests/integration/test_integration_metobs.py b/tests/integration/test_integration_metobs.py index ab6eaf5c..c1e038be 100644 --- a/tests/integration/test_integration_metobs.py +++ b/tests/integration/test_integration_metobs.py @@ -10,7 +10,9 @@ METOBS_INTEGRATION = {} for i in [1, 2, 22]: - with open(f"tests/fixtures/metobs_integration_{i}.json", encoding="utf-8") as f: + with open( + f"tests/fixtures/metobs/metobs_integration_{i}.json", encoding="utf-8" + ) as f: METOBS_INTEGRATION[i] = [] all_headers = json.load(f) for header in all_headers: diff --git a/tests/integration/test_integration_strang.py b/tests/integration/test_integration_strang.py index 1e3f10e3..4186d35c 100644 --- a/tests/integration/test_integration_strang.py +++ b/tests/integration/test_integration_strang.py @@ -4,24 +4,21 @@ import pytest from smhi.strang import Strang -RESULT_HOURLY_2020_01_01_2020_01_02 = pd.read_csv( - "tests/fixtures/STRANG_RESULT_HOURLY_2020_01_01_2020_01_02.csv", - parse_dates=[0], - index_col=0, -) -RESULT_DAILY_2020_01_01_2020_01_02 = pd.read_csv( - "tests/fixtures/STRANG_RESULT_DAILY_2020_01_01_2020_01_02.csv", - parse_dates=[0], - index_col=0, -) -RESULT_MONTHLY_2020_01_01_2020_02_01 = pd.read_csv( - "tests/fixtures/STRANG_RESULT_MONTHLY_2020_01_01_2020_02_01.csv", - parse_dates=[0], - index_col=0, -) -RESULT_MULTIPOINT_2020_01_01_MONTHLY_10 = pd.read_csv( - "tests/fixtures/STRANG_RESULT_MULTIPOINT_2020_01_01_MONTHLY_10.csv", index_col=0 -) + +def get_point(file=None): + return ( + "date_time" if file is None else pd.read_csv(file, parse_dates=[0], index_col=0) + ) + + +@pytest.fixture +def get_multipoint(): + df = pd.read_csv( + "tests/fixtures/strang/STRANG_RESULT_MULTIPOINT_2020_01_01_MONTHLY_10.csv", + index_col=0, + ) + + return df class TestIntegrationStrang: @@ -37,7 +34,9 @@ class TestIntegrationStrang: "2020-01-01", "2020-01-02", "hourly", - RESULT_HOURLY_2020_01_01_2020_01_02, + get_point( + "tests/fixtures/strang/STRANG_RESULT_HOURLY_2020_01_01_2020_01_02.csv" + ), ), ( 58, @@ -46,7 +45,9 @@ class TestIntegrationStrang: "2020-01-01", "2020-01-02", "daily", - RESULT_DAILY_2020_01_01_2020_01_02, + get_point( + "tests/fixtures/strang/STRANG_RESULT_DAILY_2020_01_01_2020_01_02.csv" + ), ), ( 58, @@ -55,7 +56,9 @@ class TestIntegrationStrang: "2020-01-01", "2020-02-01", "monthly", - RESULT_MONTHLY_2020_01_01_2020_02_01, + get_point( + "tests/fixtures/strang/STRANG_RESULT_MONTHLY_2020_01_01_2020_02_01.csv" + ), ), ( 58, @@ -64,7 +67,7 @@ class TestIntegrationStrang: None, None, None, - "date_time", + get_point(), ), ], ) @@ -78,7 +81,6 @@ def test_integration_strang_point( Args: lat: longitude lon: latitude - parameter: parameter time_from: from time_to: to @@ -86,14 +88,21 @@ def test_integration_strang_point( expected_result: expected result """ client = Strang() - data = client.get_point(lat, lon, parameter, time_from, time_to, time_interval) + point_model = client.get_point( + lat, lon, parameter, time_from, time_to, time_interval + ) + + assert point_model.parameter_key == parameter + assert point_model.latitude == lat + assert point_model.longitude == lon + assert point_model.time_interval == time_interval if time_from is not None: - pd.testing.assert_frame_equal(expected_result, data) + pd.testing.assert_frame_equal(expected_result, point_model.data) else: - assert expected_result == data.index.name + assert expected_result == point_model.data.index.name - def test_integration_strang_multipoint(self): + def test_integration_strang_multipoint(self, get_multipoint): """Strang MultiPoint integration tests. These tests require internet connectivity. @@ -102,7 +111,12 @@ def test_integration_strang_multipoint(self): parameter = 116 valid_time = "2020-01-01" time_interval = "monthly" - data = client.get_multipoint(parameter, valid_time, time_interval) + multipoint_model = client.get_multipoint(parameter, valid_time, time_interval) + + multipoint_model.parameter_key = parameter + multipoint_model.valid_time = valid_time + multipoint_model.time_interval = time_interval pd.testing.assert_frame_equal( - RESULT_MULTIPOINT_2020_01_01_MONTHLY_10, data.iloc[:10, :] + get_multipoint, + multipoint_model.data.iloc[:10, :], )