mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fix flakey tibber tests (#121993)
This commit is contained in:
parent
8d290dd9d6
commit
89198b0512
@ -4,6 +4,7 @@ import asyncio
|
|||||||
import datetime as dt
|
import datetime as dt
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.tibber.const import DOMAIN
|
from homeassistant.components.tibber.const import DOMAIN
|
||||||
@ -11,11 +12,12 @@ from homeassistant.components.tibber.services import PRICE_SERVICE_NAME, __get_p
|
|||||||
from homeassistant.core import ServiceCall
|
from homeassistant.core import ServiceCall
|
||||||
from homeassistant.exceptions import ServiceValidationError
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
|
|
||||||
|
STARTTIME = dt.datetime.fromtimestamp(1615766400)
|
||||||
|
|
||||||
|
|
||||||
def generate_mock_home_data():
|
def generate_mock_home_data():
|
||||||
"""Create mock data from the tibber connection."""
|
"""Create mock data from the tibber connection."""
|
||||||
today = remove_microseconds(dt.datetime.now())
|
tomorrow = STARTTIME + dt.timedelta(days=1)
|
||||||
tomorrow = remove_microseconds(today + dt.timedelta(days=1))
|
|
||||||
mock_homes = [
|
mock_homes = [
|
||||||
MagicMock(
|
MagicMock(
|
||||||
name="first_home",
|
name="first_home",
|
||||||
@ -26,13 +28,13 @@ def generate_mock_home_data():
|
|||||||
"priceInfo": {
|
"priceInfo": {
|
||||||
"today": [
|
"today": [
|
||||||
{
|
{
|
||||||
"startsAt": today.isoformat(),
|
"startsAt": STARTTIME.isoformat(),
|
||||||
"total": 0.46914,
|
"total": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"startsAt": (
|
"startsAt": (
|
||||||
today + dt.timedelta(hours=1)
|
STARTTIME + dt.timedelta(hours=1)
|
||||||
).isoformat(),
|
).isoformat(),
|
||||||
"total": 0.46914,
|
"total": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
@ -67,13 +69,13 @@ def generate_mock_home_data():
|
|||||||
"priceInfo": {
|
"priceInfo": {
|
||||||
"today": [
|
"today": [
|
||||||
{
|
{
|
||||||
"startsAt": today.isoformat(),
|
"startsAt": STARTTIME.isoformat(),
|
||||||
"total": 0.46914,
|
"total": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"startsAt": (
|
"startsAt": (
|
||||||
today + dt.timedelta(hours=1)
|
STARTTIME + dt.timedelta(hours=1)
|
||||||
).isoformat(),
|
).isoformat(),
|
||||||
"total": 0.46914,
|
"total": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
@ -119,19 +121,16 @@ def create_mock_hass():
|
|||||||
return mock_hass
|
return mock_hass
|
||||||
|
|
||||||
|
|
||||||
def remove_microseconds(dt):
|
async def test_get_prices(
|
||||||
"""Remove microseconds from a datetime object."""
|
freezer: FrozenDateTimeFactory,
|
||||||
return dt.replace(microsecond=0)
|
) -> None:
|
||||||
|
|
||||||
|
|
||||||
async def test_get_prices():
|
|
||||||
"""Test __get_prices with mock data."""
|
"""Test __get_prices with mock data."""
|
||||||
today = remove_microseconds(dt.datetime.now())
|
freezer.move_to(STARTTIME)
|
||||||
tomorrow = remove_microseconds(dt.datetime.now() + dt.timedelta(days=1))
|
tomorrow = STARTTIME + dt.timedelta(days=1)
|
||||||
call = ServiceCall(
|
call = ServiceCall(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
PRICE_SERVICE_NAME,
|
PRICE_SERVICE_NAME,
|
||||||
{"start": today.date().isoformat(), "end": tomorrow.date().isoformat()},
|
{"start": STARTTIME.date().isoformat(), "end": tomorrow.date().isoformat()},
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await __get_prices(call, hass=create_mock_hass())
|
result = await __get_prices(call, hass=create_mock_hass())
|
||||||
@ -140,24 +139,24 @@ async def test_get_prices():
|
|||||||
"prices": {
|
"prices": {
|
||||||
"first_home": [
|
"first_home": [
|
||||||
{
|
{
|
||||||
"start_time": today,
|
"start_time": STARTTIME,
|
||||||
"price": 0.46914,
|
"price": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"start_time": today + dt.timedelta(hours=1),
|
"start_time": STARTTIME + dt.timedelta(hours=1),
|
||||||
"price": 0.46914,
|
"price": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
"second_home": [
|
"second_home": [
|
||||||
{
|
{
|
||||||
"start_time": today,
|
"start_time": STARTTIME,
|
||||||
"price": 0.46914,
|
"price": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"start_time": today + dt.timedelta(hours=1),
|
"start_time": STARTTIME + dt.timedelta(hours=1),
|
||||||
"price": 0.46914,
|
"price": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
},
|
},
|
||||||
@ -166,9 +165,11 @@ async def test_get_prices():
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_get_prices_no_input():
|
async def test_get_prices_no_input(
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
) -> None:
|
||||||
"""Test __get_prices with no input."""
|
"""Test __get_prices with no input."""
|
||||||
today = remove_microseconds(dt.datetime.now())
|
freezer.move_to(STARTTIME)
|
||||||
call = ServiceCall(DOMAIN, PRICE_SERVICE_NAME, {})
|
call = ServiceCall(DOMAIN, PRICE_SERVICE_NAME, {})
|
||||||
|
|
||||||
result = await __get_prices(call, hass=create_mock_hass())
|
result = await __get_prices(call, hass=create_mock_hass())
|
||||||
@ -177,24 +178,24 @@ async def test_get_prices_no_input():
|
|||||||
"prices": {
|
"prices": {
|
||||||
"first_home": [
|
"first_home": [
|
||||||
{
|
{
|
||||||
"start_time": today,
|
"start_time": STARTTIME,
|
||||||
"price": 0.46914,
|
"price": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"start_time": today + dt.timedelta(hours=1),
|
"start_time": STARTTIME + dt.timedelta(hours=1),
|
||||||
"price": 0.46914,
|
"price": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
"second_home": [
|
"second_home": [
|
||||||
{
|
{
|
||||||
"start_time": today,
|
"start_time": STARTTIME,
|
||||||
"price": 0.46914,
|
"price": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"start_time": today + dt.timedelta(hours=1),
|
"start_time": STARTTIME + dt.timedelta(hours=1),
|
||||||
"price": 0.46914,
|
"price": 0.46914,
|
||||||
"level": "VERY_EXPENSIVE",
|
"level": "VERY_EXPENSIVE",
|
||||||
},
|
},
|
||||||
@ -203,9 +204,12 @@ async def test_get_prices_no_input():
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_get_prices_start_tomorrow():
|
async def test_get_prices_start_tomorrow(
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
) -> None:
|
||||||
"""Test __get_prices with start date tomorrow."""
|
"""Test __get_prices with start date tomorrow."""
|
||||||
tomorrow = remove_microseconds(dt.datetime.now() + dt.timedelta(days=1))
|
freezer.move_to(STARTTIME)
|
||||||
|
tomorrow = STARTTIME + dt.timedelta(days=1)
|
||||||
call = ServiceCall(
|
call = ServiceCall(
|
||||||
DOMAIN, PRICE_SERVICE_NAME, {"start": tomorrow.date().isoformat()}
|
DOMAIN, PRICE_SERVICE_NAME, {"start": tomorrow.date().isoformat()}
|
||||||
)
|
)
|
||||||
@ -242,7 +246,7 @@ async def test_get_prices_start_tomorrow():
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_get_prices_invalid_input():
|
async def test_get_prices_invalid_input() -> None:
|
||||||
"""Test __get_prices with invalid input."""
|
"""Test __get_prices with invalid input."""
|
||||||
|
|
||||||
call = ServiceCall(DOMAIN, PRICE_SERVICE_NAME, {"start": "test"})
|
call = ServiceCall(DOMAIN, PRICE_SERVICE_NAME, {"start": "test"})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user