Skip to content

Commit

Permalink
Move fixtures and update strang integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcth committed Mar 29, 2024
1 parent 06138c2 commit ad17dd8
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 41 deletions.
12 changes: 6 additions & 6 deletions src/smhi/strang.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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,
Expand Down Expand Up @@ -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."
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/test_integration_metobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
72 changes: 43 additions & 29 deletions tests/integration/test_integration_strang.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -64,7 +67,7 @@ class TestIntegrationStrang:
None,
None,
None,
"date_time",
get_point(),
),
],
)
Expand All @@ -78,22 +81,28 @@ def test_integration_strang_point(
Args:
lat: longitude
lon: latitude
parameter: parameter
time_from: from
time_to: to
time_interval: interval
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.
Expand All @@ -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, :],
)

0 comments on commit ad17dd8

Please sign in to comment.