Improve tests for easyEnergy (#105989)

This commit is contained in:
Klaas Schoute 2023-12-19 19:02:40 +01:00 committed by GitHub
parent 91f8d3faef
commit 9275d35c0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 1864 additions and 34 deletions

View File

@ -31,9 +31,9 @@ ENERGY_USAGE_SERVICE_NAME: Final = "get_energy_usage_prices"
ENERGY_RETURN_SERVICE_NAME: Final = "get_energy_return_prices"
SERVICE_SCHEMA: Final = vol.Schema(
{
vol.Required(ATTR_INCL_VAT): bool,
vol.Optional(ATTR_START): str,
vol.Optional(ATTR_END): str,
vol.Required(ATTR_INCL_VAT, default=True): bool,
}
)
@ -55,6 +55,7 @@ def __get_date(date_input: str | None) -> date | datetime:
return value
raise ServiceValidationError(
"Invalid datetime provided.",
translation_domain=DOMAIN,
translation_key="invalid_date",
translation_placeholders={

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@
import pytest
from syrupy.assertion import SnapshotAssertion
import voluptuous as vol
from homeassistant.components.easyenergy.const import DOMAIN
from homeassistant.components.easyenergy.services import (
@ -25,15 +26,16 @@ async def test_has_services(
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize(
"service", [GAS_SERVICE_NAME, ENERGY_USAGE_SERVICE_NAME, ENERGY_RETURN_SERVICE_NAME]
)
@pytest.mark.parametrize("incl_vat", [{"incl_vat": False}, {"incl_vat": True}, {}])
@pytest.mark.parametrize(
"start", [{"start": "2023-01-01 00:00:00"}, {"start": "incorrect date"}, {}]
)
@pytest.mark.parametrize(
"end", [{"end": "2023-01-01 00:00:00"}, {"end": "incorrect date"}, {}]
"service",
[
GAS_SERVICE_NAME,
ENERGY_USAGE_SERVICE_NAME,
ENERGY_RETURN_SERVICE_NAME,
],
)
@pytest.mark.parametrize("incl_vat", [{"incl_vat": False}, {"incl_vat": True}])
@pytest.mark.parametrize("start", [{"start": "2023-01-01 00:00:00"}, {}])
@pytest.mark.parametrize("end", [{"end": "2023-01-01 00:00:00"}, {}])
async def test_service(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
@ -42,18 +44,63 @@ async def test_service(
start: dict[str, str],
end: dict[str, str],
) -> None:
"""Test the easyEnergy Service."""
"""Test the EnergyZero Service."""
data = incl_vat | start | end
try:
response = await hass.services.async_call(
assert snapshot == await hass.services.async_call(
DOMAIN,
service,
data,
blocking=True,
return_response=True,
)
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize(
"service",
[
GAS_SERVICE_NAME,
ENERGY_USAGE_SERVICE_NAME,
ENERGY_RETURN_SERVICE_NAME,
],
)
@pytest.mark.parametrize(
("service_data", "error", "error_message"),
[
({}, vol.er.Error, "required key not provided .+"),
(
{"incl_vat": "incorrect vat"},
vol.er.Error,
"expected bool for dictionary value .+",
),
(
{"incl_vat": True, "start": "incorrect date"},
ServiceValidationError,
"Invalid datetime provided.",
),
(
{"incl_vat": True, "end": "incorrect date"},
ServiceValidationError,
"Invalid datetime provided.",
),
],
)
async def test_service_validation(
hass: HomeAssistant,
service: str,
service_data: dict[str, str | bool],
error: type[Exception],
error_message: str,
) -> None:
"""Test the easyEnergy Service."""
with pytest.raises(error, match=error_message):
await hass.services.async_call(
DOMAIN,
service,
data,
service_data,
blocking=True,
return_response=True,
)
assert response == snapshot
except ServiceValidationError as e:
assert e == snapshot