Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bruxy70 committed Nov 6, 2023
1 parent f6c5755 commit d86bbbf
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 81 deletions.
3 changes: 1 addition & 2 deletions Holidays.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
{
"path": "."
}
],
"settings": {}
]
}
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
18 changes: 5 additions & 13 deletions test/test_calendar.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test calendar for simple integration."""
from datetime import date

import pytest_asyncio
from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from pytest_homeassistant_custom_component.common import MockConfigEntry
Expand All @@ -14,7 +13,6 @@
ERROR_LENGTH = "Holidays should have {} items, not {}."


@pytest_asyncio.fixture(scope="session")
async def test_uk(hass: HomeAssistant) -> None:
"""Test UK Holidays."""

Expand All @@ -34,15 +32,14 @@ async def test_uk(hass: HomeAssistant) -> None:
next_holiday = uk_holidays.attributes["next_holiday"]
holidays = uk_holidays.attributes["holidays"]
len_holidays = len(holidays)
assert state == "40", ERROR_STATE.format(9, state)
assert state == "40", ERROR_STATE.format(40, state)
assert next_holiday == "Good Friday", ERROR_NAME.format("Good Friday", next_holiday)
assert next_date.date() == date(2020, 4, 10), ERROR_DATE.format(
"April 10", next_date.date()
)
assert len_holidays == 27, ERROR_LENGTH.format(27, len_holidays)


@pytest_asyncio.fixture(scope="session")
async def test_cz(hass: HomeAssistant) -> None:
"""Test CZ Holidays."""

Expand All @@ -62,14 +59,13 @@ async def test_cz(hass: HomeAssistant) -> None:
holidays = cz_holidays.attributes["holidays"]
len_holidays = len(holidays)
assert state == "40", ERROR_STATE.format(9, state)
assert next_holiday == "Velký pátek", ERROR_NAME.format("Velký pátek", next_holiday)
assert next_holiday == "Good Friday", ERROR_NAME.format("Good Friday", next_holiday)
assert next_date.date() == date(2020, 4, 10), ERROR_DATE.format(
"April 10", next_date.date()
)
assert len_holidays == 39, ERROR_LENGTH.format(39, len_holidays)


@pytest_asyncio.fixture(scope="session")
async def test_se(hass: HomeAssistant) -> None:
"""Test SE Holidays."""

Expand All @@ -89,16 +85,13 @@ async def test_se(hass: HomeAssistant) -> None:
holidays = se_holidays.attributes["holidays"]
len_holidays = len(holidays)
assert state == "40", ERROR_STATE.format(9, state)
assert next_holiday == "Långfredagen", ERROR_NAME.format(
"Långfredagen", next_holiday
)
assert next_holiday == "Good Friday", ERROR_NAME.format("Good Friday", next_holiday)
assert next_date.date() == date(2020, 4, 10), ERROR_DATE.format(
"April 10", next_date.date()
)
assert len_holidays == 48, ERROR_LENGTH.format(48, len_holidays)


@pytest_asyncio.fixture(scope="session")
async def test_pop_uk(hass: HomeAssistant) -> None:
"""Test Pop Holidays."""

Expand Down Expand Up @@ -129,10 +122,9 @@ async def test_pop_uk(hass: HomeAssistant) -> None:
assert next_date.date() == date(2020, 4, 10), ERROR_DATE.format(
"April 10", next_date.date()
)
assert len_holidays == 40, ERROR_LENGTH.format(40, len_holidays)
assert len_holidays == 21, ERROR_LENGTH.format(21, len_holidays)


@pytest_asyncio.fixture(scope="session")
async def test_pop_texas(hass: HomeAssistant) -> None:
"""Test Pop Holidays."""

Expand Down Expand Up @@ -165,4 +157,4 @@ async def test_pop_texas(hass: HomeAssistant) -> None:
assert next_date.date() == date(2020, 3, 2), ERROR_DATE.format(
"April 10", next_date.date()
)
assert len_holidays == 65, ERROR_LENGTH.format(65, len_holidays)
assert len_holidays == 61, ERROR_LENGTH.format(61, len_holidays)
63 changes: 4 additions & 59 deletions test/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test the Simple Integration config flow."""
from unittest.mock import patch

import pytest_asyncio
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.core import HomeAssistant
from pytest_homeassistant_custom_component.common import MockConfigEntry
Expand All @@ -10,7 +9,6 @@
from custom_components.holidays.const import DOMAIN


@pytest_asyncio.fixture(scope="session")
async def test_gb_config_flow(hass: HomeAssistant) -> None:
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
Expand Down Expand Up @@ -46,7 +44,7 @@ async def test_gb_config_flow(hass: HomeAssistant) -> None:
# ...add England for subdiv
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={"subdiv": "England"},
user_input={"subdiv": "ENG"},
)
assert (
"type" in result
Expand All @@ -73,65 +71,12 @@ async def test_gb_config_flow(hass: HomeAssistant) -> None:
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["options"] == {
"country": "GB",
"subdiv": "England",
"subdiv": "ENG",
"name": "English calendar",
}
assert len(mock_setup_entry.mock_calls) == 1


@pytest_asyncio.fixture(scope="session")
async def test_pl_config_flow(hass: HomeAssistant) -> None:
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})

# Initialise Config Flow
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert "type" in result and "step_id" in result and "flow_id" in result

# Check that the config flow shows the user form as the first step
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"

# If a user were to enter `PL` for country,
# it would result in this function call
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={"name": "Polish calendar", "country": "PL"},
)
assert (
"type" in result
and "step_id" in result
and "flow_id" in result
and "errors" in result
)

# Should pass to the pop step
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "pop"
assert not result["errors"]

# ... wil leave pop enpty
with patch(
"custom_components.holidays.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={},
)
assert "type" in result and "options" in result
# Should create entry
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["options"] == {
"country": "PL",
"name": "Polish calendar",
}
assert len(mock_setup_entry.mock_calls) == 1


@pytest_asyncio.fixture(scope="session")
async def test_options_flow(hass: HomeAssistant) -> None:
"""Test we get the form."""

Expand Down Expand Up @@ -174,7 +119,7 @@ async def test_options_flow(hass: HomeAssistant) -> None:
# ...add England for subdiv
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={"subdiv": "England"},
user_input={"subdiv": "ENG"},
)
assert (
"type" in result
Expand All @@ -197,5 +142,5 @@ async def test_options_flow(hass: HomeAssistant) -> None:
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["data"] == {
"country": "GB",
"subdiv": "England",
"subdiv": "ENG",
}
4 changes: 0 additions & 4 deletions test/test_device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Test all frequencies (except blank)."""
import pytest_asyncio
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import entity_registry as er
Expand All @@ -8,7 +7,6 @@
from custom_components.holidays import const


@pytest_asyncio.fixture(scope="session")
async def test_device(hass: HomeAssistant) -> None:
"""Test device registry."""

Expand All @@ -29,7 +27,6 @@ async def test_device(hass: HomeAssistant) -> None:
assert device.name == "CZ Holidays"


@pytest_asyncio.fixture(scope="session")
async def test_device_info(hass: HomeAssistant) -> None:
"""Test device info."""

Expand All @@ -49,7 +46,6 @@ async def test_device_info(hass: HomeAssistant) -> None:
}


@pytest_asyncio.fixture(scope="session")
async def test_entity(hass: HomeAssistant) -> None:
"""Test entity registry."""

Expand Down
3 changes: 0 additions & 3 deletions test/test_migration.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Test migration from older version."""
import pytest_asyncio
from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.holidays import const


@pytest_asyncio.fixture(scope="session")
async def test_version1(hass: HomeAssistant) -> None:
"""Migration from version 1."""

Expand Down Expand Up @@ -35,7 +33,6 @@ async def test_version1(hass: HomeAssistant) -> None:
assert config_entry2.state == config_entries.ConfigEntryState.LOADED


@pytest_asyncio.fixture(scope="session")
async def test_version2(hass: HomeAssistant) -> None:
"""Migration from version 2."""

Expand Down

0 comments on commit d86bbbf

Please sign in to comment.