From 9894f1c1834e3ac7bde8d5ddeee8142016bc9e5a Mon Sep 17 00:00:00 2001 From: Felipe Castillo Date: Wed, 24 Apr 2024 18:27:06 +0000 Subject: [PATCH] test: fix unit test --- pylintrc | 2 +- tests/bandit.yaml | 1 - tests/test_binary_sensor.py | 69 +++++++++++++++++++++---------------- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/pylintrc b/pylintrc index 850e50d..7b6263c 100644 --- a/pylintrc +++ b/pylintrc @@ -3,7 +3,7 @@ ignore=tests # Use a conservative default here; 2 should speed up most setups and not hurt # any too bad. Override on command line as appropriate. jobs=2 -load-plugins=pylint_strict_informational +fail-on=I persistent=no extension-pkg-whitelist=ciso8601 diff --git a/tests/bandit.yaml b/tests/bandit.yaml index ebd284e..dcacabd 100644 --- a/tests/bandit.yaml +++ b/tests/bandit.yaml @@ -12,6 +12,5 @@ tests: - B318 - B319 - B320 - - B325 - B602 - B604 diff --git a/tests/test_binary_sensor.py b/tests/test_binary_sensor.py index f3c9919..13e9603 100644 --- a/tests/test_binary_sensor.py +++ b/tests/test_binary_sensor.py @@ -6,6 +6,7 @@ import pytest from pytest import raises +from pytest_homeassistant_custom_component.common import async_mock_service from custom_components.snowtire.binary_sensor import ( SnowtireBinarySensor, @@ -18,19 +19,21 @@ ICON_WINTER, ) from homeassistant.components.weather import ( + ATTR_FORECAST_TEMP, + ATTR_FORECAST_TEMP_LOW, + ATTR_FORECAST_TIME, ATTR_WEATHER_TEMPERATURE, + SERVICE_GET_FORECASTS, WeatherEntityFeature, ) from homeassistant.const import ( ATTR_SUPPORTED_FEATURES, CONF_PLATFORM, - TEMP_CELSIUS, - TEMP_FAHRENHEIT, + UnitOfTemperature, ) from homeassistant.core import HomeAssistant, State from homeassistant.exceptions import HomeAssistantError - -# from homeassistant.util import dt as dt_util +from homeassistant.util import dt as dt_util MOCK_UNIQUE_ID: Final = "test_id" MOCK_NAME: Final = "test_name" @@ -92,12 +95,14 @@ async def test_async_added_to_hass(default_sensor): ) async def test__temp2c(temp1, temp2): """Test temperature conversions.""" - assert SnowtireBinarySensor._temp2c(temp1, TEMP_CELSIUS) == temp1 - assert round(SnowtireBinarySensor._temp2c(temp1, TEMP_FAHRENHEIT), 2) == temp2 - assert SnowtireBinarySensor._temp2c(None, TEMP_CELSIUS) is None + assert SnowtireBinarySensor._temp2c(temp1, UnitOfTemperature.CELSIUS) == temp1 + assert ( + round(SnowtireBinarySensor._temp2c(temp1, UnitOfTemperature.FAHRENHEIT), 2) + == temp2 + ) + assert SnowtireBinarySensor._temp2c(None, UnitOfTemperature.CELSIUS) is None -@pytest.mark.skip(reason="Can't mock a weather integration") async def test_async_update(hass: HomeAssistant, default_sensor): """Test sensor update.""" hass.states._states[MOCK_WEATHER_ENTITY] = State(MOCK_WEATHER_ENTITY, None) @@ -110,27 +115,33 @@ async def test_async_update(hass: HomeAssistant, default_sensor): with raises(HomeAssistantError): await default_sensor.async_update() - # today = dt_util.start_of_local_day() - # today_ts = int(today.timestamp() * 1000) - # day = days = 86400000 - - # forecast = [ - # { - # ATTR_FORECAST_TIME: today_ts - day, - # }, - # { - # ATTR_FORECAST_TIME: today, - # ATTR_FORECAST_TEMP: 9, - # }, - # { - # ATTR_FORECAST_TIME: today_ts + day, - # ATTR_FORECAST_TEMP_LOW: 1, - # ATTR_FORECAST_TEMP: 8, - # }, - # { - # ATTR_FORECAST_TIME: today_ts + (MOCK_DAYS + 1) * days, - # }, - # ] + today = dt_util.start_of_local_day() + today_ts = int(today.timestamp() * 1000) + day = days = 86400000 + + forecast = { + MOCK_WEATHER_ENTITY: { + "forecast": [ + { + ATTR_FORECAST_TIME: today_ts - day, + }, + { + ATTR_FORECAST_TIME: today, + ATTR_FORECAST_TEMP: 9, + }, + { + ATTR_FORECAST_TIME: today_ts + day, + ATTR_FORECAST_TEMP_LOW: 1, + ATTR_FORECAST_TEMP: 8, + }, + { + ATTR_FORECAST_TIME: today_ts + (MOCK_DAYS + 1) * days, + }, + ] + } + } + + async_mock_service(hass, CONF_WEATHER, SERVICE_GET_FORECASTS, response=forecast) hass.states.async_set( MOCK_WEATHER_ENTITY,