Skip to content

Commit

Permalink
Add testing for utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Dec 30, 2023
1 parent 3a2861e commit 4a735e0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
8 changes: 4 additions & 4 deletions custom_components/ohme/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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):
Expand Down
59 changes: 59 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4a735e0

Please sign in to comment.