Skip to content

Commit

Permalink
Use model for ParameterItems
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcth committed Mar 24, 2024
1 parent f86614e commit 3437473
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
14 changes: 12 additions & 2 deletions src/smhi/metobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from requests.structures import CaseInsensitiveDict
from smhi.constants import METOBS_AVAILABLE_PERIODS, TYPE_MAP
from smhi.models.metobs_data import DataModel
from smhi.models.metobs_parameters import ParameterModel
from smhi.models.metobs_parameters import ParameterItem, ParameterModel
from smhi.models.metobs_periods import PeriodModel
from smhi.models.metobs_stations import StationModel
from smhi.models.metobs_versions import VersionModel
Expand Down Expand Up @@ -168,7 +168,17 @@ def __init__(
self.versions_object = versions_object
self.selected_version = version
self.resource = model.resource
self.data = tuple((x.key, x.title, x.summary) for x in self.resource)
self.data = tuple(
ParameterItem(
key=x.key,
title=x.title,
summary=x.summary,
unit=x.unit,
updated=x.updated,
geo_box=x.geo_box,
)
for x in self.resource
)


class Stations(BaseMetobs):
Expand Down
9 changes: 9 additions & 0 deletions src/smhi/models/metobs_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
from pydantic import BaseModel, Field, field_validator


class ParameterItem(BaseModel):
key: str
title: str
summary: str
unit: str
updated: int
geo_box: GeoBox


class LinkItem(BaseModel):
href: str
rel: str
Expand Down
12 changes: 2 additions & 10 deletions tests/fixtures/metobs/parameters_data.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
[
[
"1",
"Lufttemperatur",
"momentanv\u00e4rde, 1 g\u00e5ng/tim"
],
[
"2",
"Lufttemperatur",
"medelv\u00e4rde 1 dygn, 1 g\u00e5ng/dygn, kl 00"
]
"{\"key\":\"1\",\"title\":\"Lufttemperatur\",\"summary\":\"momentanv\u00e4rde, 1 g\u00e5ng/tim\",\"unit\":\"celsius\",\"updated\":1711270800000,\"geo_box\":{\"minLatitude\":55.0,\"minLongitude\":10.0,\"maxLatitude\":70.0,\"maxLongitude\":25.0}}",
"{\"key\":\"2\",\"title\":\"Lufttemperatur\",\"summary\":\"medelv\u00e4rde 1 dygn, 1 g\u00e5ng/dygn, kl 00\",\"unit\":\"celsius\",\"updated\":1711270800000,\"geo_box\":{\"minLatitude\":55.0,\"minLongitude\":10.0,\"maxLatitude\":70.0,\"maxLongitude\":25.0}}"
]
8 changes: 6 additions & 2 deletions tests/unit/test_unit_metobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Versions,
)
from smhi.models.metobs_data import DataModel
from smhi.models.metobs_parameters import ParameterModel
from smhi.models.metobs_parameters import ParameterItem, ParameterModel
from smhi.models.metobs_periods import PeriodModel
from smhi.models.metobs_stations import StationModel
from smhi.models.metobs_versions import VersionModel
Expand Down Expand Up @@ -111,6 +111,10 @@ def get_data(file, load_type=None):
with open(file) as f:
if load_type is None:
file_contents = tuple([tuple(x) for x in json.load(f)])
elif load_type == "parameters":
file_contents = tuple(
ParameterItem.model_validate_json(x) for x in json.load(f)
)
elif load_type == "data":
file_contents = MockedResponse(200, None, f.read())
else:
Expand Down Expand Up @@ -148,7 +152,7 @@ def setup_parameters(setup_versions):
mocked_model = get_model(
"tests/fixtures/metobs/parameters_model.json", ParameterModel
)
mocked_data = get_data("tests/fixtures/metobs/parameters_data.json")
mocked_data = get_data("tests/fixtures/metobs/parameters_data.json", "parameters")

return (
mocked_response,
Expand Down

0 comments on commit 3437473

Please sign in to comment.