Skip to content

Commit

Permalink
Initial improvements to test coverage 40.48%
Browse files Browse the repository at this point in the history
- started at 12% or so.
  • Loading branch information
agittins committed Apr 6, 2024
1 parent f379d28 commit 683732a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 51 deletions.
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
asyncio_mode = auto
26 changes: 17 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@

import pytest

# from custom_components.bermuda import BermudaDataUpdateCoordinator

pytest_plugins = "pytest_homeassistant_custom_component"


# This fixture enables loading custom integrations in all tests.
# Remove to enable selective use of this fixture
@pytest.fixture(autouse=True)
def auto_enable_custom_integrations(enable_custom_integrations):
"""Enable loading custom integrations."""
yield


# This fixture is used to prevent HomeAssistant from
# attempting to create and dismiss persistent
# notifications. These calls would fail without this
Expand All @@ -30,9 +40,8 @@ def skip_notifications_fixture():
@pytest.fixture(name="bypass_get_data")
def bypass_get_data_fixture():
"""Skip calls to get data from API."""
# AJG: We don't implement this, so nothing to monkeypatch.
# with patch("custom_components.bermuda.BermudaApiClient.async_get_data"):
# yield
with patch("custom_components.bermuda.BermudaDataUpdateCoordinator.async_refresh"):
yield


# In this fixture, we are forcing calls to async_get_data to raise
Expand All @@ -41,9 +50,8 @@ def bypass_get_data_fixture():
@pytest.fixture(name="error_on_get_data")
def error_get_data_fixture():
"""Simulate error when retrieving data from API."""
# AJG: again we don't implement async_get_data...
# with patch(
# "custom_components.bermuda.BermudaApiClient.async_get_data",
# side_effect=Exception,
# ):
# yield
with patch(
"custom_components.bermuda.BermudaDataUpdateCoordinator.async_refresh",
side_effect=Exception,
):
yield
25 changes: 17 additions & 8 deletions tests/const.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
"""Constants for Bermuda BLE Trilateration tests."""

# AJG: We don't use these vars in our flow. TODO: Add some we _do_ use!
# from custom_components.bermuda.const import (
# CONF_PASSWORD,
# )
# from custom_components.bermuda.const import (
# CONF_USERNAME,
# )
# MOCK_CONFIG = {CONF_USERNAME: "test_username", CONF_PASSWORD: "test_password"}
from __future__ import annotations

import custom_components.bermuda.const

# from custom_components.bermuda.const import CONF_DEVICES
# from custom_components.bermuda.const import CONF_MAX_RADIUS


MOCK_CONFIG = {
custom_components.bermuda.const.CONF_MAX_RADIUS: 20.0,
custom_components.bermuda.const.CONF_MAX_VELOCITY: 3.0,
custom_components.bermuda.const.CONF_DEVTRACK_TIMEOUT: 30,
custom_components.bermuda.const.CONF_UPDATE_INTERVAL: 10.0,
custom_components.bermuda.const.CONF_SMOOTHING_SAMPLES: 20,
custom_components.bermuda.const.CONF_ATTENUATION: 3.0,
custom_components.bermuda.const.CONF_REF_POWER: -55.0,
custom_components.bermuda.const.CONF_DEVICES: ["EE:E8:37:9F:6B:54"],
}

MOCK_CONFIG = {}
13 changes: 9 additions & 4 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ async def test_setup_unload_and_reload_entry(hass, bypass_get_data):
# call, no code from custom_components/bermuda/api.py actually runs.
assert await async_setup_entry(hass, config_entry)
assert DOMAIN in hass.data and config_entry.entry_id in hass.data[DOMAIN]
assert type(hass.data[DOMAIN][config_entry.entry_id]).isinstance(
BermudaDataUpdateCoordinator
assert isinstance(
hass.data[DOMAIN][config_entry.entry_id], BermudaDataUpdateCoordinator
)

# Reload the entry and assert that the data from above is still there
assert await async_reload_entry(hass, config_entry) is None
assert DOMAIN in hass.data and config_entry.entry_id in hass.data[DOMAIN]
assert type(hass.data[DOMAIN][config_entry.entry_id]).isinstance(
BermudaDataUpdateCoordinator
assert isinstance(
hass.data[DOMAIN][config_entry.entry_id], BermudaDataUpdateCoordinator
)

# Unload the entry and verify that the data has been removed
Expand All @@ -57,5 +57,10 @@ async def test_setup_entry_exception(hass, error_on_get_data):
# In this case we are testing the condition where async_setup_entry raises
# ConfigEntryNotReady using the `error_on_get_data` fixture which simulates
# an error.

# Hmmm... this doesn't seem to be how this works. The super's _async_refresh might
# handle exceptions, in which it then sets self.last_update_status, which is what
# async_setup_entry checks in order to raise ConfigEntryNotReady, but I don't think
# anything will "catch" our over-ridded async_refresh's exception.
with pytest.raises(ConfigEntryNotReady):
assert await async_setup_entry(hass, config_entry)
63 changes: 33 additions & 30 deletions tests/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

from __future__ import annotations

from unittest.mock import call
from unittest.mock import patch

from homeassistant.components.switch import SERVICE_TURN_OFF
from homeassistant.components.switch import SERVICE_TURN_ON
from homeassistant.const import ATTR_ENTITY_ID
# from homeassistant.components.switch import SERVICE_TURN_OFF
# from homeassistant.components.switch import SERVICE_TURN_ON
# from homeassistant.const import ATTR_ENTITY_ID
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.bermuda import async_setup_entry
from custom_components.bermuda.const import DEFAULT_NAME

# from custom_components.bermuda.const import DEFAULT_NAME
from custom_components.bermuda.const import DOMAIN
from custom_components.bermuda.const import SWITCH

from .const import MOCK_CONFIG

# from unittest.mock import call
# from unittest.mock import patch


# from custom_components.bermuda.const import SWITCH


async def test_switch_services(hass):
"""Test switch services."""
Expand All @@ -29,25 +32,25 @@ async def test_switch_services(hass):
# in test code as well and can be used to test
# additional things, like whether a function
# was called or what arguments it was called with
with patch(
"custom_components.bermuda.BermudaApiClient.async_set_title"
) as title_func:
await hass.services.async_call(
SWITCH,
SERVICE_TURN_OFF,
service_data={ATTR_ENTITY_ID: f"{SWITCH}.{DEFAULT_NAME}_{SWITCH}"},
blocking=True,
)
assert title_func.called
assert title_func.call_args == call("foo")

title_func.reset_mock()

await hass.services.async_call(
SWITCH,
SERVICE_TURN_ON,
service_data={ATTR_ENTITY_ID: f"{SWITCH}.{DEFAULT_NAME}_{SWITCH}"},
blocking=True,
)
assert title_func.called
assert title_func.call_args == call("bar")
# with patch(
# "custom_components.bermuda.BermudaDataUpdateCoordinator.async_set_title"
# ) as title_func:
# await hass.services.async_call(
# SWITCH,
# SERVICE_TURN_OFF,
# service_data={ATTR_ENTITY_ID: f"{SWITCH}.{DEFAULT_NAME}_{SWITCH}"},
# blocking=True,
# )
# assert title_func.called
# assert title_func.call_args == call("foo")

# title_func.reset_mock()

# await hass.services.async_call(
# SWITCH,
# SERVICE_TURN_ON,
# service_data={ATTR_ENTITY_ID: f"{SWITCH}.{DEFAULT_NAME}_{SWITCH}"},
# blocking=True,
# )
# assert title_func.called
# assert title_func.call_args == call("bar")

0 comments on commit 683732a

Please sign in to comment.