From 4a735e03dda96ce8cb71fb56f104d7ba5f266387 Mon Sep 17 00:00:00 2001 From: Daniel Raper Date: Sat, 30 Dec 2023 18:36:28 +0000 Subject: [PATCH] Add testing for utility functions --- custom_components/ohme/utils.py | 8 ++--- tests/test_utils.py | 59 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/test_utils.py diff --git a/custom_components/ohme/utils.py b/custom_components/ohme/utils.py index 06eed23..17159fc 100644 --- a/custom_components/ohme/utils.py +++ b/custom_components/ohme/utils.py @@ -10,10 +10,10 @@ def _format_charge_graph(charge_start, points): return [{"t": x["x"] + charge_start, "y": x["y"]} for x in points] -def charge_graph_next_slot(charge_start, points): +def charge_graph_next_slot(charge_start, points, skip_format=False): """Get the next charge slot start/end times from a list of graph points.""" now = int(time()) - data = _format_charge_graph(charge_start, points) + data = points if skip_format else _format_charge_graph(charge_start, points) # Filter to points from now onwards data = [x for x in data if x["t"] > now] @@ -47,10 +47,10 @@ def charge_graph_next_slot(charge_start, points): } -def charge_graph_in_slot(charge_start, points): +def charge_graph_in_slot(charge_start, points, skip_format=False): """Are we currently in a charge slot?""" now = int(time()) - data = _format_charge_graph(charge_start, points) + data = points if skip_format else _format_charge_graph(charge_start, points) # Loop through every value, skipping the last for idx in range(0, len(data) - 1): diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..d81a212 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,59 @@ +"""Tests for the utils.""" +from unittest import mock +import random +from time import time + +from custom_components.ohme import utils + + +async def test_format_charge_graph(hass): + """Test that the _test_format_charge_graph function adds given timestamp / 1000 to each x coordinate.""" + TEST_DATA = [{"x": 10, "y": 0}, {"x": 20, "y": 0}, + {"x": 30, "y": 0}, {"x": 40, "y": 0}] + + start_time = random.randint(1577836800, 1764547200) # 2020-2025 + start_time_ms = start_time * 1000 + + result = utils._format_charge_graph(start_time_ms, TEST_DATA) + expected = [{"t": TEST_DATA[0]['x'] + start_time, "y": mock.ANY}, + {"t": TEST_DATA[1]['x'] + start_time, "y": mock.ANY}, + {"t": TEST_DATA[2]['x'] + start_time, "y": mock.ANY}, + {"t": TEST_DATA[3]['x'] + start_time, "y": mock.ANY}] + + assert expected == result + + +async def test_charge_graph_next_slot(hass): + """Test that we correctly work out when the next slot starts and ends.""" + start_time = int(time()) + TEST_DATA = [{"t": start_time - 100, "y": 0}, + {"t": start_time + 1000, "y": 0}, + {"t": start_time + 1600, "y": 1000}, + {"t": start_time + 1800, "y": 1000}] + + result = utils.charge_graph_next_slot(0, TEST_DATA, skip_format=True) + result = { + "start": result['start'].timestamp(), + "end": result['end'].timestamp(), + } + + expected = { + "start": start_time + 1001, + "end": start_time + 1601, + } + + assert expected == result + + +async def test_charge_graph_in_slot(hass): + """Test that we correctly intepret outselves as in a slot.""" + start_time = int(time()) + TEST_DATA = [{"t": start_time - 100, "y": 0}, + {"t": start_time - 10, "y": 0}, + {"t": start_time + 200, "y": 1000}, + {"t": start_time + 300, "y": 1000}] + + result = utils.charge_graph_in_slot(0, TEST_DATA, skip_format=True) + expected = True + + assert expected == result