Skip to content

Commit

Permalink
Add more tests (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
raman325 committed Mar 6, 2024
1 parent ca7dcba commit e5f137f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
6 changes: 4 additions & 2 deletions custom_components/lock_code_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
CONF_LOCKS: {},
COORDINATORS: {},
}:
resources: ResourceStorageCollection
resources: ResourceStorageCollection | ResourceYAMLCollection
if resources := hass.data.get(LL_DOMAIN, {}).get("resources"):
if hass_data["resources"]:
try:
Expand All @@ -274,7 +274,9 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
if data[CONF_URL] == STRATEGY_PATH
)
except StopIteration:
pass
_LOGGER.debug(
"Strategy module not found so there is nothing to remove"
)
else:
await resources.async_delete_item(resource_id)
_LOGGER.debug(
Expand Down
13 changes: 13 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,16 @@ async def test_config_flow_slots_already_configured(
{CONF_SLOTS: {2: {CONF_ENABLED: False, CONF_PIN: "0123"}}},
)
assert result["errors"] == {"base": "slots_already_configured"}


async def test_config_flow_two_entries_same_locks(
hass: HomeAssistant, mock_lock_config_entry, lock_code_manager_config_entry
):
"""Test two entries that use same locks but different slots set up successfully."""
flow_id = await _start_yaml_config_flow(hass)

result = await hass.config_entries.flow.async_configure(
flow_id,
{CONF_SLOTS: {3: {CONF_ENABLED: False, CONF_PIN: "0123"}}},
)
assert result["type"] == "create_entry"
63 changes: 61 additions & 2 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ async def test_resource_already_loaded_ui(

@pytest.mark.parametrize(
"config",
[{"mode": "yaml", "resources": [{"type": "module", "url": STRATEGY_PATH}]}],
[{"mode": "yaml", "resources": [{"type": "module", CONF_URL: STRATEGY_PATH}]}],
)
async def test_resource_already_loaded_yaml(
hass: HomeAssistant,
Expand Down Expand Up @@ -268,7 +268,7 @@ async def test_resource_already_loaded_yaml(

@pytest.mark.parametrize(
"config",
[{"mode": "yaml", "resources": [{"type": "module", "url": "fake_module.js"}]}],
[{"mode": "yaml", "resources": [{"type": "module", CONF_URL: "fake_module.js"}]}],
)
async def test_resource_not_loaded_yaml(
hass: HomeAssistant,
Expand All @@ -293,3 +293,62 @@ async def test_resource_not_loaded_yaml(
assert "module can't automatically be registered" in caplog.text

await hass.config_entries.async_unload(config_entry.entry_id)


async def test_two_entries_same_locks(
hass: HomeAssistant, mock_lock_config_entry, lock_code_manager_config_entry
):
"""Test two entries that use same locks but different slots set up successfully."""
new_config = copy.deepcopy(BASE_CONFIG)
new_config[CONF_SLOTS] = {3: {CONF_ENABLED: False, CONF_PIN: "0123"}}
new_entry = MockConfigEntry(
domain=DOMAIN, data=new_config, unique_id="Mock Title 2", title="Mock Title 2"
)
new_entry.add_to_hass(hass)
await hass.config_entries.async_setup(new_entry.entry_id)
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids(Platform.BINARY_SENSOR)) == 3
assert len(hass.states.async_entity_ids(Platform.EVENT)) == 3
assert len(hass.states.async_entity_ids(Platform.SENSOR)) == 6
assert len(hass.states.async_entity_ids(Platform.SWITCH)) == 3
assert len(hass.states.async_entity_ids(Platform.TEXT)) == 6


@pytest.mark.parametrize("config", [{}])
async def test_resource_not_loaded_on_unload(
hass: HomeAssistant,
setup_lovelace_ui,
mock_lock_config_entry,
caplog: pytest.LogCaptureFixture,
monkeypatch: pytest.MonkeyPatch,
):
"""Test when strategy resource is not loaded when unloading config entry."""
resources = hass.data[LL_DOMAIN].get("resources")
assert resources
await resources.async_load()

monkeypatch.setattr(
"custom_components.lock_code_manager.helpers.INTEGRATIONS_CLASS_MAP",
{"test": MockLCMLock},
)

config_entry = MockConfigEntry(
domain=DOMAIN, data=BASE_CONFIG, unique_id="Mock Title"
)
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

assert "Registered strategy module" in caplog.text

await resources.async_delete_item(
next(
item["id"]
for item in resources.async_items()
if item[CONF_URL] == STRATEGY_PATH
)
)

await hass.config_entries.async_unload(config_entry.entry_id)

assert "Strategy module not found so there is nothing to remove" in caplog.text

0 comments on commit e5f137f

Please sign in to comment.