Use library classes instead of namedtuple in ipma tests (#115372)

This commit is contained in:
Sid 2024-04-12 00:03:10 +02:00 committed by GitHub
parent 137514edb7
commit a093f943d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 83 additions and 131 deletions

View file

@ -1,8 +1,12 @@
"""Tests for the IPMA component.""" """Tests for the IPMA component."""
from collections import namedtuple
from datetime import UTC, datetime from datetime import UTC, datetime
from pyipma.forecast import Forecast, Forecast_Location, Weather_Type
from pyipma.observation import Observation
from pyipma.rcm import RCM
from pyipma.uv import UV
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_MODE, CONF_NAME from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_MODE, CONF_NAME
ENTRY_CONFIG = { ENTRY_CONFIG = {
@ -18,109 +22,90 @@ class MockLocation:
async def fire_risk(self, api): async def fire_risk(self, api):
"""Mock Fire Risk.""" """Mock Fire Risk."""
RCM = namedtuple(
"RCM",
[
"dico",
"rcm",
"coordinates",
],
)
return RCM("some place", 3, (0, 0)) return RCM("some place", 3, (0, 0))
async def uv_risk(self, api): async def uv_risk(self, api):
"""Mock UV Index.""" """Mock UV Index."""
UV = namedtuple( return UV(0, "0", datetime(2020, 1, 16, 0, 0, 0), 0, 5.7)
"UV",
["idPeriodo", "intervaloHora", "data", "globalIdLocal", "iUv"],
)
return UV(0, "0", datetime.now(), 0, 5.7)
async def observation(self, api): async def observation(self, api):
"""Mock Observation.""" """Mock Observation."""
Observation = namedtuple( return Observation(
"Observation", precAcumulada=0.0,
[ humidade=71.0,
"accumulated_precipitation", pressao=1000.0,
"humidity", radiacao=0.0,
"pressure", temperatura=18.0,
"radiation", idDireccVento=8,
"temperature", intensidadeVentoKM=3.94,
"wind_direction", intensidadeVento=1.0944,
"wind_intensity_km", timestamp=datetime(2020, 1, 16, 0, 0, 0),
], idEstacao=0,
) )
return Observation(0.0, 71.0, 1000.0, 0.0, 18.0, "NW", 3.94)
async def forecast(self, api, period): async def forecast(self, api, period):
"""Mock Forecast.""" """Mock Forecast."""
Forecast = namedtuple(
"Forecast",
[
"feels_like_temperature",
"forecast_date",
"forecasted_hours",
"humidity",
"max_temperature",
"min_temperature",
"precipitation_probability",
"temperature",
"update_date",
"weather_type",
"wind_direction",
"wind_strength",
],
)
WeatherType = namedtuple("WeatherType", ["id", "en", "pt"])
if period == 24: if period == 24:
return [ return [
Forecast( Forecast(
None, utci=None,
datetime(2020, 1, 16, 0, 0, 0), dataPrev=datetime(2020, 1, 16, 0, 0, 0),
24, idPeriodo=24,
None, hR=None,
16.2, tMax=16.2,
10.6, tMin=10.6,
"100.0", probabilidadePrecipita=100.0,
13.4, tMed=13.4,
"2020-01-15T07:51:00", dataUpdate=datetime(2020, 1, 15, 7, 51, 0),
WeatherType(9, "Rain/showers", "Chuva/aguaceiros"), idTipoTempo=Weather_Type(9, "Rain/showers", "Chuva/aguaceiros"),
"S", ddVento="S",
"10", ffVento=10,
idFfxVento=0,
iUv=0,
intervaloHora="",
location=Forecast_Location(0, "", 0, 0, 0, "", (0, 0)),
), ),
] ]
if period == 1: if period == 1:
return [ return [
Forecast( Forecast(
"7.7", utci=7.7,
datetime(2020, 1, 15, 1, 0, 0, tzinfo=UTC), dataPrev=datetime(2020, 1, 15, 1, 0, 0, tzinfo=UTC),
1, idPeriodo=1,
"86.9", hR=86.9,
12.0, tMax=12.0,
None, tMin=None,
80.0, probabilidadePrecipita=80.0,
10.6, tMed=10.6,
"2020-01-15T02:51:00", dataUpdate=datetime(2020, 1, 15, 2, 51, 0),
WeatherType(10, "Light rain", "Chuva fraca ou chuvisco"), idTipoTempo=Weather_Type(
"S", 10, "Light rain", "Chuva fraca ou chuvisco"
"32.7", ),
ddVento="S",
ffVento=32.7,
idFfxVento=0,
iUv=0,
intervaloHora="",
location=Forecast_Location(0, "", 0, 0, 0, "", (0, 0)),
), ),
Forecast( Forecast(
"5.7", utci=5.7,
datetime(2020, 1, 15, 2, 0, 0, tzinfo=UTC), dataPrev=datetime(2020, 1, 15, 2, 0, 0, tzinfo=UTC),
1, idPeriodo=1,
"86.9", hR=86.9,
12.0, tMax=12.0,
None, tMin=None,
80.0, probabilidadePrecipita=80.0,
10.6, tMed=10.6,
"2020-01-15T02:51:00", dataUpdate=datetime(2020, 1, 15, 2, 51, 0),
WeatherType(1, "Clear sky", "C\u00e9u limpo"), idTipoTempo=Weather_Type(1, "Clear sky", "C\u00e9u limpo"),
"S", ddVento="S",
"32.7", ffVento=32.7,
idFfxVento=0,
iUv=0,
intervaloHora="",
location=Forecast_Location(0, "", 0, 0, 0, "", (0, 0)),
), ),
] ]

View file

@ -1,15 +1,10 @@
# serializer version: 1 # serializer version: 1
# name: test_diagnostics # name: test_diagnostics
dict({ dict({
'current_weather': list([ 'current_weather': dict({
0.0, '__type': "<class 'pyipma.observation.Observation'>",
71.0, 'repr': 'Observation(intensidadeVentoKM=3.94, temperatura=18.0, radiacao=0.0, idDireccVento=8, precAcumulada=0.0, intensidadeVento=1.0944, humidade=71.0, pressao=1000.0, timestamp=datetime.datetime(2020, 1, 16, 0, 0), idEstacao=0)',
1000.0, }),
0.0,
18.0,
'NW',
3.94,
]),
'location_information': dict({ 'location_information': dict({
'global_id_local': 1130600, 'global_id_local': 1130600,
'id_station': 1200545, 'id_station': 1200545,
@ -19,42 +14,14 @@
'station': 'HomeTown Station', 'station': 'HomeTown Station',
}), }),
'weather_forecast': list([ 'weather_forecast': list([
list([ dict({
'7.7', '__type': "<class 'pyipma.forecast.Forecast'>",
'2020-01-15T01:00:00+00:00', 'repr': "Forecast(tMed=10.6, tMin=None, ffVento=32.7, idFfxVento=0, dataUpdate=datetime.datetime(2020, 1, 15, 2, 51), tMax=12.0, iUv=0, intervaloHora='', idTipoTempo=Weather_Type(id=10, en='Light rain', pt='Chuva fraca ou chuvisco'), hR=86.9, location=Forecast_Location(globalIdLocal=0, local='', idRegiao=0, idDistrito=0, idConcelho=0, idAreaAviso='', coordinates=(0, 0)), probabilidadePrecipita=80.0, idPeriodo=1, dataPrev=datetime.datetime(2020, 1, 15, 1, 0, tzinfo=datetime.timezone.utc), ddVento='S', utci=7.7)",
1, }),
'86.9', dict({
12.0, '__type': "<class 'pyipma.forecast.Forecast'>",
None, 'repr': "Forecast(tMed=10.6, tMin=None, ffVento=32.7, idFfxVento=0, dataUpdate=datetime.datetime(2020, 1, 15, 2, 51), tMax=12.0, iUv=0, intervaloHora='', idTipoTempo=Weather_Type(id=1, en='Clear sky', pt='Céu limpo'), hR=86.9, location=Forecast_Location(globalIdLocal=0, local='', idRegiao=0, idDistrito=0, idConcelho=0, idAreaAviso='', coordinates=(0, 0)), probabilidadePrecipita=80.0, idPeriodo=1, dataPrev=datetime.datetime(2020, 1, 15, 2, 0, tzinfo=datetime.timezone.utc), ddVento='S', utci=5.7)",
80.0, }),
10.6,
'2020-01-15T02:51:00',
list([
10,
'Light rain',
'Chuva fraca ou chuvisco',
]),
'S',
'32.7',
]),
list([
'5.7',
'2020-01-15T02:00:00+00:00',
1,
'86.9',
12.0,
None,
80.0,
10.6,
'2020-01-15T02:51:00',
list([
1,
'Clear sky',
'Céu limpo',
]),
'S',
'32.7',
]),
]), ]),
}) })
# --- # ---

View file

@ -83,7 +83,7 @@
dict({ dict({
'condition': 'rainy', 'condition': 'rainy',
'datetime': datetime.datetime(2020, 1, 16, 0, 0), 'datetime': datetime.datetime(2020, 1, 16, 0, 0),
'precipitation_probability': '100.0', 'precipitation_probability': 100.0,
'temperature': 16.2, 'temperature': 16.2,
'templow': 10.6, 'templow': 10.6,
'wind_bearing': 'S', 'wind_bearing': 'S',
@ -121,7 +121,7 @@
dict({ dict({
'condition': 'rainy', 'condition': 'rainy',
'datetime': datetime.datetime(2020, 1, 16, 0, 0), 'datetime': datetime.datetime(2020, 1, 16, 0, 0),
'precipitation_probability': '100.0', 'precipitation_probability': 100.0,
'temperature': 16.2, 'temperature': 16.2,
'templow': 10.6, 'templow': 10.6,
'wind_bearing': 'S', 'wind_bearing': 'S',
@ -160,7 +160,7 @@
dict({ dict({
'condition': 'rainy', 'condition': 'rainy',
'datetime': '2020-01-16T00:00:00', 'datetime': '2020-01-16T00:00:00',
'precipitation_probability': '100.0', 'precipitation_probability': 100.0,
'temperature': 16.2, 'temperature': 16.2,
'templow': 10.6, 'templow': 10.6,
'wind_bearing': 'S', 'wind_bearing': 'S',
@ -173,7 +173,7 @@
dict({ dict({
'condition': 'rainy', 'condition': 'rainy',
'datetime': '2020-01-16T00:00:00', 'datetime': '2020-01-16T00:00:00',
'precipitation_probability': '100.0', 'precipitation_probability': 100.0,
'temperature': 16.2, 'temperature': 16.2,
'templow': 10.6, 'templow': 10.6,
'wind_bearing': 'S', 'wind_bearing': 'S',