Skip to content

Commit

Permalink
Add tests for update_critical_repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Feb 5, 2022
1 parent 998ca45 commit 605a943
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 10 deletions.
7 changes: 2 additions & 5 deletions custom_components/hacs/tasks/handle_critical_notification.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
""""Hacs base setup task."""
from __future__ import annotations


from homeassistant.core import HomeAssistant

from ..utils.store import async_load_from_store

from ..enums import HacsStage

from ..base import HacsBase
from ..enums import HacsStage
from ..utils.store import async_load_from_store
from .base import HacsTask


Expand Down
8 changes: 5 additions & 3 deletions custom_components/hacs/tasks/update_critical_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from datetime import timedelta

from aiogithubapi import GitHubNotModifiedException

from homeassistant.core import HomeAssistant

from custom_components.hacs.utils.queue_manager import QueueManager
from custom_components.hacs.utils.store import async_load_from_store, async_save_to_store
from custom_components.hacs.utils.store import (
async_load_from_store,
async_save_to_store,
)

from ..base import HacsBase
from ..enums import HacsStage
Expand Down Expand Up @@ -64,7 +66,7 @@ async def async_execute(self) -> None:
"acknowledged": True,
}
if repository["repository"] not in instored:
if repo is not None and repo.installed:
if repo is not None and repo.data.installed:
self.hacs.log.critical(
"Removing repository %s, it is marked as critical",
repository["repository"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

from homeassistant.core import HomeAssistant

from ..enums import HacsGitHubRepo, HacsStage

from ..base import HacsBase
from ..enums import HacsGitHubRepo, HacsStage
from .base import HacsTask


Expand Down
152 changes: 152 additions & 0 deletions tests/tasks/test_update_critical_repositories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# pylint: disable=missing-function-docstring,missing-module-docstring, protected-access
from unittest.mock import AsyncMock, patch

from aiogithubapi import GitHubNotModifiedException
import pytest

from custom_components.hacs.base import HacsBase
from custom_components.hacs.const import VERSION_STORAGE
from custom_components.hacs.exceptions import HacsException
from custom_components.hacs.repositories.base import HacsRepository


@pytest.mark.asyncio
async def test_update_critical_repositories_no_critical(
hacs: HacsBase,
caplog: pytest.LogCaptureFixture,
):
await hacs.tasks.async_load()
task = hacs.tasks.get("update_critical_repositories")

assert task

with patch(
"custom_components.hacs.base.HacsBase.async_github_get_hacs_default_file",
return_value=[],
), patch("custom_components.hacs.utils.store.json_util.load_json") as load_json_mock:
await task.execute_task()
assert "No critical repositories" in caplog.text
load_json_mock.assert_not_called()


@pytest.mark.asyncio
async def test_update_critical_repositories_exception(
hacs: HacsBase,
caplog: pytest.LogCaptureFixture,
):
await hacs.tasks.async_load()
task = hacs.tasks.get("update_critical_repositories")

assert task

with patch(
"custom_components.hacs.base.HacsBase.async_github_get_hacs_default_file",
side_effect=GitHubNotModifiedException("err"),
), patch("custom_components.hacs.utils.store.json_util.load_json") as load_json_mock:
await task.execute_task()
load_json_mock.assert_not_called()
assert "No critical repositories" not in caplog.text

with patch(
"custom_components.hacs.base.HacsBase.async_github_get_hacs_default_file",
side_effect=HacsException("err"),
), patch("custom_components.hacs.utils.store.json_util.load_json") as load_json_mock:
await task.execute_task()
load_json_mock.assert_not_called()
assert "No critical repositories" in caplog.text


@pytest.mark.asyncio
async def test_update_critical_repositories_update_in_stored(
hacs: HacsBase,
caplog: pytest.LogCaptureFixture,
):
hacs.data.async_write = AsyncMock()
await hacs.tasks.async_load()
task = hacs.tasks.get("update_critical_repositories")

assert task

with patch(
"custom_components.hacs.base.HacsBase.async_github_get_hacs_default_file",
return_value=[{"repository": "test/test", "reason": "test", "link": "test"}],
), patch(
"custom_components.hacs.utils.store.json_util.load_json",
return_value={
"version": VERSION_STORAGE,
"data": [{"acknowledged": True, "repository": "test/test"}],
},
):
await task.execute_task()
assert "Resarting Home Assistant" not in caplog.text
assert "The queue is empty" in caplog.text


@pytest.mark.asyncio
async def test_update_critical_repositories_update_not_in_stored_not_installed(
hacs: HacsBase,
caplog: pytest.LogCaptureFixture,
repository: HacsRepository,
):
hacs.data.async_write = AsyncMock()
await hacs.tasks.async_load()
task = hacs.tasks.get("update_critical_repositories")

assert task

with patch(
"custom_components.hacs.base.HacsBase.async_github_get_hacs_default_file",
return_value=[{"repository": "test/test", "reason": "test", "link": "test"}],
), patch(
"custom_components.hacs.utils.store.json_util.load_json",
return_value={
"version": VERSION_STORAGE,
"data": [],
},
), patch(
"custom_components.hacs.base.HacsRepositories.get_by_full_name",
return_value=repository,
):
await task.execute_task()
assert "Resarting Home Assistant" not in caplog.text
assert "The queue is empty" in caplog.text


@pytest.mark.asyncio
async def test_update_critical_repositories_update_not_in_stored_installed(
hacs: HacsBase,
caplog: pytest.LogCaptureFixture,
repository: HacsRepository,
):
hacs.data.async_write = AsyncMock()
await hacs.tasks.async_load()
task = hacs.tasks.get("update_critical_repositories")

assert task

repository.data.installed = True

with patch(
"custom_components.hacs.base.HacsBase.async_github_get_hacs_default_file",
return_value=[{"repository": "test/test", "reason": "test", "link": "test"}],
), patch(
"custom_components.hacs.utils.store.json_util.load_json",
return_value={
"version": VERSION_STORAGE,
"data": [],
},
), patch(
"custom_components.hacs.base.HacsRepositories.get_by_full_name",
return_value=repository,
), patch(
"custom_components.hacs.repositories.base.HacsRepository.uninstall",
return_value=AsyncMock(),
) as uninstall_mock, patch(
"homeassistant.core.HomeAssistant.async_stop",
return_value=AsyncMock(),
) as async_stop_mock:
await task.execute_task()
uninstall_mock.assert_called()
async_stop_mock.assert_called()
assert "Resarting Home Assistant" in caplog.text
assert "Queue execution finished for 1 tasks finished" in caplog.text

0 comments on commit 605a943

Please sign in to comment.