mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 17:57:55 +00:00
Improve tests for easyEnergy (#105989)
This commit is contained in:
parent
91f8d3faef
commit
9275d35c0a
@ -31,9 +31,9 @@ ENERGY_USAGE_SERVICE_NAME: Final = "get_energy_usage_prices"
|
|||||||
ENERGY_RETURN_SERVICE_NAME: Final = "get_energy_return_prices"
|
ENERGY_RETURN_SERVICE_NAME: Final = "get_energy_return_prices"
|
||||||
SERVICE_SCHEMA: Final = vol.Schema(
|
SERVICE_SCHEMA: Final = vol.Schema(
|
||||||
{
|
{
|
||||||
|
vol.Required(ATTR_INCL_VAT): bool,
|
||||||
vol.Optional(ATTR_START): str,
|
vol.Optional(ATTR_START): str,
|
||||||
vol.Optional(ATTR_END): 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
|
return value
|
||||||
|
|
||||||
raise ServiceValidationError(
|
raise ServiceValidationError(
|
||||||
|
"Invalid datetime provided.",
|
||||||
translation_domain=DOMAIN,
|
translation_domain=DOMAIN,
|
||||||
translation_key="invalid_date",
|
translation_key="invalid_date",
|
||||||
translation_placeholders={
|
translation_placeholders={
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy.assertion import SnapshotAssertion
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.easyenergy.const import DOMAIN
|
from homeassistant.components.easyenergy.const import DOMAIN
|
||||||
from homeassistant.components.easyenergy.services import (
|
from homeassistant.components.easyenergy.services import (
|
||||||
@ -25,15 +26,16 @@ async def test_has_services(
|
|||||||
|
|
||||||
@pytest.mark.usefixtures("init_integration")
|
@pytest.mark.usefixtures("init_integration")
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"service", [GAS_SERVICE_NAME, ENERGY_USAGE_SERVICE_NAME, ENERGY_RETURN_SERVICE_NAME]
|
"service",
|
||||||
)
|
[
|
||||||
@pytest.mark.parametrize("incl_vat", [{"incl_vat": False}, {"incl_vat": True}, {}])
|
GAS_SERVICE_NAME,
|
||||||
@pytest.mark.parametrize(
|
ENERGY_USAGE_SERVICE_NAME,
|
||||||
"start", [{"start": "2023-01-01 00:00:00"}, {"start": "incorrect date"}, {}]
|
ENERGY_RETURN_SERVICE_NAME,
|
||||||
)
|
],
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"end", [{"end": "2023-01-01 00:00:00"}, {"end": "incorrect date"}, {}]
|
|
||||||
)
|
)
|
||||||
|
@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(
|
async def test_service(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
@ -42,18 +44,63 @@ async def test_service(
|
|||||||
start: dict[str, str],
|
start: dict[str, str],
|
||||||
end: dict[str, str],
|
end: dict[str, str],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test the easyEnergy Service."""
|
"""Test the EnergyZero Service."""
|
||||||
|
|
||||||
data = incl_vat | start | end
|
data = incl_vat | start | end
|
||||||
|
|
||||||
try:
|
assert snapshot == await hass.services.async_call(
|
||||||
response = await hass.services.async_call(
|
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
service,
|
service,
|
||||||
data,
|
data,
|
||||||
blocking=True,
|
blocking=True,
|
||||||
return_response=True,
|
return_response=True,
|
||||||
)
|
)
|
||||||
assert response == snapshot
|
|
||||||
except ServiceValidationError as e:
|
|
||||||
assert e == snapshot
|
@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,
|
||||||
|
service_data,
|
||||||
|
blocking=True,
|
||||||
|
return_response=True,
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user