Skip to content

Commit

Permalink
test: Update tests to common standard
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Nov 25, 2024
1 parent 2b5c3c0 commit bcc907d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 64 deletions.
26 changes: 6 additions & 20 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,15 @@ def skip_notifications_fixture():
def base_config_entry(request, hass: HomeAssistant) -> MS365MockConfigEntry:
"""Create MS365 entry in Home Assistant."""
data = deepcopy(BASE_CONFIG_ENTRY)
options = None
if hasattr(request, "param"):
for key, value in request.param.items():
data[key] = value
if key == "options":
options = value
else:
data[key] = value
entry = MS365MockConfigEntry(
domain=DOMAIN,
title=TITLE,
unique_id=DOMAIN,
data=data,
)
entry.runtime_data = None
return entry


@pytest.fixture
def update_config_entry(hass: HomeAssistant) -> MS365MockConfigEntry:
"""Create MS365 entry in Home Assistant."""
data = deepcopy(BASE_CONFIG_ENTRY)
data["enable_update"] = True
entry = MS365MockConfigEntry(
domain=DOMAIN,
title=TITLE,
unique_id=DOMAIN,
data=data,
domain=DOMAIN, title=TITLE, unique_id=DOMAIN, data=data, options=options
)
entry.runtime_data = None
return entry
Expand Down
19 changes: 12 additions & 7 deletions tests/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,25 @@ def check_entity_state(
entity_state,
entity_attributes=None,
data_length=None,
subject=None,
attributes=None,
):
"""Check entity state."""
state = hass.states.get(entity_name)

# print(state)
assert state.state == entity_state
if entity_attributes:
# print("*************************** State Attributes")
# print(state.attributes)
assert state.attributes["data"] == entity_attributes
print("*************************** State Attributes")
print(state.attributes)
if "data" in state.attributes:
assert state.attributes["data"] == entity_attributes
else:
assert state.attributes == entity_attributes
if data_length is not None:
assert len(state.attributes["data"]) == data_length
if subject is not None:
assert state.attributes["message"] == subject

if attributes is not None:
for key, value in attributes.items():
assert state.attributes[key] == value


def utcnow():
Expand Down
13 changes: 10 additions & 3 deletions tests/integration/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ async def test_all_day_event(
base_config_entry: MS365MockConfigEntry,
) -> None:
"""Test all day event."""
check_entity_state(hass, "calendar.test_calendar1", "on", subject="Test all day")
check_entity_state(
hass, "calendar.test_calendar1", "on", attributes={"message": "Test all day"}
)


@pytest.mark.parametrize(
Expand All @@ -245,7 +247,9 @@ async def test_started_event(
) -> None:
"""Test started event."""

check_entity_state(hass, "calendar.test_calendar1", "on", subject="Test started")
check_entity_state(
hass, "calendar.test_calendar1", "on", attributes={"message": "Test started"}
)


@pytest.mark.parametrize(
Expand All @@ -261,7 +265,10 @@ async def test_not_started_event(
"""Test not started event."""

check_entity_state(
hass, "calendar.test_calendar1", "off", subject="Test not started"
hass,
"calendar.test_calendar1",
"off",
attributes={"message": "Test not started"},
)


Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_filemgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def test_base_filemgmt(
setup_base_integration,
base_config_entry: MS365MockConfigEntry,
) -> None:
"""Test basic file management."""
"""Test base file management."""

check_yaml_file_contents(tmp_path, "ms365_calendars_base")

Expand Down
32 changes: 1 addition & 31 deletions tests/integration/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from requests_mock import Mocker

from ..helpers.mock_config_entry import MS365MockConfigEntry
from ..helpers.utils import build_token_file, token_setup
from ..helpers.utils import build_token_file
from .helpers_integration.mocks import MS365MOCKS

### Note that permissions code also supports Presence/Send which are not present in the calendar integration
Expand Down Expand Up @@ -75,36 +75,6 @@ async def test_update_shared_permissions(
assert len(entities) == 2


async def test_no_token(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
base_config_entry: MS365MockConfigEntry,
) -> None:
"""Test no token."""
base_config_entry.add_to_hass(hass)

await hass.config_entries.async_setup(base_config_entry.entry_id)
await hass.async_block_till_done()

assert "Could not locate token" in caplog.text


async def test_corrupt_token(
tmp_path,
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
base_config_entry: MS365MockConfigEntry,
) -> None:
"""Fixture for setting up the component."""
token_setup(tmp_path, "corrupt")
base_config_entry.add_to_hass(hass)

await hass.config_entries.async_setup(base_config_entry.entry_id)
await hass.async_block_till_done()

assert "Token corrupted for integration" in caplog.text


async def test_missing_permissions(
tmp_path,
hass: HomeAssistant,
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
async def test_update_service_setup(
hass: HomeAssistant,
setup_update_integration,
update_config_entry: MS365MockConfigEntry,
base_config_entry: MS365MockConfigEntry,
) -> None:
"""Test the reconfigure flow."""
assert update_config_entry.data[CONF_ENABLE_UPDATE]
assert base_config_entry.data[CONF_ENABLE_UPDATE]
assert hass.services.has_service(DOMAIN, "create_calendar_event")
assert hass.services.has_service(DOMAIN, "modify_calendar_event")
assert hass.services.has_service(DOMAIN, "remove_calendar_event")
Expand Down
17 changes: 17 additions & 0 deletions tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from homeassistant.core import HomeAssistant

from .helpers.mock_config_entry import MS365MockConfigEntry
from .helpers.utils import token_setup


async def test_no_token(
Expand All @@ -19,3 +20,19 @@ async def test_no_token(
await hass.async_block_till_done()

assert "Could not locate token" in caplog.text


async def test_corrupt_token(
tmp_path,
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
base_config_entry: MS365MockConfigEntry,
) -> None:
"""Fixture for setting up the component."""
token_setup(tmp_path, "corrupt")
base_config_entry.add_to_hass(hass)

await hass.config_entries.async_setup(base_config_entry.entry_id)
await hass.async_block_till_done()

assert "Token corrupted for integration" in caplog.text

0 comments on commit bcc907d

Please sign in to comment.