Skip to content

Commit

Permalink
Tibber: cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
functionpointer committed Oct 7, 2024
1 parent 68af540 commit e360606
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions tests/components/tibber/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError

STARTTIME = dt.datetime.fromtimestamp(1615766400).replace(tzinfo=dt.UTC)
START_TIME = dt.datetime.fromtimestamp(1615766400).replace(tzinfo=dt.UTC)


def generate_mock_home_data():
"""Create mock data from the tibber connection."""
tomorrow = STARTTIME + dt.timedelta(days=1)
tomorrow = START_TIME + dt.timedelta(days=1)
mock_homes = [
MagicMock(
name="first_home",
Expand All @@ -27,13 +27,13 @@ def generate_mock_home_data():
"priceInfo": {
"today": [
{
"startsAt": STARTTIME.isoformat(),
"startsAt": START_TIME.isoformat(),
"total": 0.36914,
"level": "VERY_EXPENSIVE",
},
{
"startsAt": (
STARTTIME + dt.timedelta(hours=1)
START_TIME + dt.timedelta(hours=1)
).isoformat(),
"total": 0.36914,
"level": "VERY_EXPENSIVE",
Expand Down Expand Up @@ -68,13 +68,13 @@ def generate_mock_home_data():
"priceInfo": {
"today": [
{
"startsAt": STARTTIME.isoformat(),
"startsAt": START_TIME.isoformat(),
"total": 0.36914,
"level": "VERY_EXPENSIVE",
},
{
"startsAt": (
STARTTIME + dt.timedelta(hours=1)
START_TIME + dt.timedelta(hours=1)
).isoformat(),
"total": 0.36914,
"level": "VERY_EXPENSIVE",
Expand All @@ -101,6 +101,8 @@ def generate_mock_home_data():
},
),
]
# set name again, as the name is special in mock objects
# see documentation: https://docs.python.org/3/library/unittest.mock.html#attaching-mocks-as-attributes
mock_homes[0].name = "first_home"
mock_homes[1].name = "second_home"
return mock_homes
Expand All @@ -110,10 +112,10 @@ def generate_mock_home_data():
"data",
[
{},
{"start": STARTTIME.isoformat()},
{"start": START_TIME.isoformat()},
{
"start": STARTTIME.isoformat(),
"end": (STARTTIME + dt.timedelta(days=1)).isoformat(),
"start": START_TIME.isoformat(),
"end": (START_TIME + dt.timedelta(days=1)).isoformat(),
},
],
)
Expand All @@ -123,8 +125,8 @@ async def test_get_prices(
freezer: FrozenDateTimeFactory,
data,
) -> None:
"""Test __get_prices with mock data."""
freezer.move_to(STARTTIME)
"""Test get_prices with mock data."""
freezer.move_to(START_TIME)
mock_tibber_setup.get_homes.return_value = generate_mock_home_data()

result = await hass.services.async_call(
Expand All @@ -136,28 +138,28 @@ async def test_get_prices(
"prices": {
"first_home": [
{
"start_time": dt.datetime.fromisoformat(STARTTIME.isoformat()),
"start_time": dt.datetime.fromisoformat(START_TIME.isoformat()),
# back and forth conversion to deal with HAFakeDatetime vs real datetime being different types
"price": 0.36914,
"level": "VERY_EXPENSIVE",
},
{
"start_time": dt.datetime.fromisoformat(
(STARTTIME + dt.timedelta(hours=1)).isoformat()
(START_TIME + dt.timedelta(hours=1)).isoformat()
),
"price": 0.36914,
"level": "VERY_EXPENSIVE",
},
],
"second_home": [
{
"start_time": dt.datetime.fromisoformat(STARTTIME.isoformat()),
"start_time": dt.datetime.fromisoformat(START_TIME.isoformat()),
"price": 0.36914,
"level": "VERY_EXPENSIVE",
},
{
"start_time": dt.datetime.fromisoformat(
(STARTTIME + dt.timedelta(hours=1)).isoformat()
(START_TIME + dt.timedelta(hours=1)).isoformat()
),
"price": 0.36914,
"level": "VERY_EXPENSIVE",
Expand All @@ -172,9 +174,9 @@ async def test_get_prices_start_tomorrow(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test __get_prices with start date tomorrow."""
freezer.move_to(STARTTIME)
tomorrow = STARTTIME + dt.timedelta(days=1)
"""Test get_prices with start date tomorrow."""
freezer.move_to(START_TIME)
tomorrow = START_TIME + dt.timedelta(days=1)

mock_tibber_setup.get_homes.return_value = generate_mock_home_data()

Expand Down Expand Up @@ -220,8 +222,8 @@ async def test_get_prices_start_tomorrow(
@pytest.mark.parametrize(
"start_time",
[
STARTTIME.isoformat(),
(STARTTIME + dt.timedelta(hours=4))
START_TIME.isoformat(),
(START_TIME + dt.timedelta(hours=4))
.replace(tzinfo=dt.timezone(dt.timedelta(hours=4)))
.isoformat(),
],
Expand All @@ -232,8 +234,8 @@ async def test_get_prices_with_timezones(
freezer: FrozenDateTimeFactory,
start_time: str,
) -> None:
"""Test __get_prices with timezone and without."""
freezer.move_to(STARTTIME)
"""Test get_prices with timezone and without."""
freezer.move_to(START_TIME)

mock_tibber_setup.get_homes.return_value = generate_mock_home_data()

Expand All @@ -250,24 +252,24 @@ async def test_get_prices_with_timezones(
"prices": {
"first_home": [
{
"start_time": STARTTIME,
"start_time": START_TIME,
"price": 0.36914,
"level": "VERY_EXPENSIVE",
},
{
"start_time": STARTTIME + dt.timedelta(hours=1),
"start_time": START_TIME + dt.timedelta(hours=1),
"price": 0.36914,
"level": "VERY_EXPENSIVE",
},
],
"second_home": [
{
"start_time": STARTTIME,
"start_time": START_TIME,
"price": 0.36914,
"level": "VERY_EXPENSIVE",
},
{
"start_time": STARTTIME + dt.timedelta(hours=1),
"start_time": START_TIME + dt.timedelta(hours=1),
"price": 0.36914,
"level": "VERY_EXPENSIVE",
},
Expand All @@ -279,14 +281,14 @@ async def test_get_prices_with_timezones(
@pytest.mark.parametrize(
"start_time",
[
(STARTTIME + dt.timedelta(hours=2)).isoformat(),
(STARTTIME + dt.timedelta(hours=2))
(START_TIME + dt.timedelta(hours=2)).isoformat(),
(START_TIME + dt.timedelta(hours=2))
.astimezone(tz=dt.timezone(dt.timedelta(hours=5)))
.isoformat(),
(STARTTIME + dt.timedelta(hours=2))
(START_TIME + dt.timedelta(hours=2))
.astimezone(tz=dt.timezone(dt.timedelta(hours=8)))
.isoformat(),
(STARTTIME + dt.timedelta(hours=2))
(START_TIME + dt.timedelta(hours=2))
.astimezone(tz=dt.timezone(dt.timedelta(hours=-8)))
.isoformat(),
],
Expand All @@ -297,9 +299,9 @@ async def test_get_prices_with_wrong_timezones(
freezer: FrozenDateTimeFactory,
start_time: str,
) -> None:
"""Test __get_prices with timezone and without, while expecting it to fail."""
freezer.move_to(STARTTIME)
tomorrow = STARTTIME + dt.timedelta(days=1)
"""Test get_prices with incorrect time and/or timezone. We expect an empty list."""
freezer.move_to(START_TIME)
tomorrow = START_TIME + dt.timedelta(days=1)

mock_tibber_setup.get_homes.return_value = generate_mock_home_data()

Expand All @@ -319,7 +321,7 @@ async def test_get_prices_invalid_input(
mock_tibber_setup: MagicMock,
hass: HomeAssistant,
) -> None:
"""Test __get_prices with invalid input."""
"""Test get_prices with invalid input."""

with pytest.raises(ServiceValidationError):
await hass.services.async_call(
Expand Down

0 comments on commit e360606

Please sign in to comment.