From 34374731c4071ad424e6d623ace09d8e877576c0 Mon Sep 17 00:00:00 2001 From: Mladen Gibanica <11275336+mgcth@users.noreply.github.com> Date: Sun, 24 Mar 2024 19:48:54 +0100 Subject: [PATCH] Use model for ParameterItems --- src/smhi/metobs.py | 14 ++++++++++++-- src/smhi/models/metobs_parameters.py | 9 +++++++++ tests/fixtures/metobs/parameters_data.json | 12 ++---------- tests/unit/test_unit_metobs.py | 8 ++++++-- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/smhi/metobs.py b/src/smhi/metobs.py index f204c971..be5a0e38 100644 --- a/src/smhi/metobs.py +++ b/src/smhi/metobs.py @@ -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 @@ -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): diff --git a/src/smhi/models/metobs_parameters.py b/src/smhi/models/metobs_parameters.py index 70ccf77f..79955483 100644 --- a/src/smhi/models/metobs_parameters.py +++ b/src/smhi/models/metobs_parameters.py @@ -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 diff --git a/tests/fixtures/metobs/parameters_data.json b/tests/fixtures/metobs/parameters_data.json index 9a7165de..0158be2d 100644 --- a/tests/fixtures/metobs/parameters_data.json +++ b/tests/fixtures/metobs/parameters_data.json @@ -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}}" ] diff --git a/tests/unit/test_unit_metobs.py b/tests/unit/test_unit_metobs.py index 69c6dde4..72cceada 100644 --- a/tests/unit/test_unit_metobs.py +++ b/tests/unit/test_unit_metobs.py @@ -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 @@ -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: @@ -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,