Skip to content

Commit

Permalink
Merge pull request #403 from dougiteixeira/tests-add-config_flow-p1
Browse files Browse the repository at this point in the history
Add basic tests for config flow (part 1)
  • Loading branch information
dougiteixeira authored Dec 16, 2024
2 parents 8546c8b + c1a581d commit 02c3f66
Show file tree
Hide file tree
Showing 13 changed files with 1,868 additions and 8 deletions.
14 changes: 8 additions & 6 deletions custom_components/proxmoxve/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,14 @@ async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
and CONF_NODES in import_config
and (import_nodes := import_config.get(CONF_NODES)) is not None
):
import_config[CONF_NODES] = []
config = import_config.copy()
config[CONF_NODES] = []
for node_data in import_nodes:
node = node_data[CONF_NODE]
if node in proxmox_nodes_host:
import_config[CONF_NODES].append(node)
import_config[CONF_QEMU] = node_data[CONF_VMS]
import_config[CONF_LXC] = node_data[CONF_CONTAINERS]
config[CONF_NODES].append(node)
config[CONF_QEMU] = node_data[CONF_VMS]
config[CONF_LXC] = node_data[CONF_CONTAINERS]
else:
ir.async_create_issue(
self.hass,
Expand All @@ -626,6 +627,7 @@ async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
"node": str(node),
},
)
return self.async_abort(reason="import_failed")

ir.async_create_issue(
self.hass,
Expand All @@ -644,8 +646,8 @@ async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
)

return self.async_create_entry(
title=(f"{import_config.get(CONF_HOST)}:{import_config.get(CONF_PORT)}"),
data=import_config,
title=(f"{config.get(CONF_HOST)}:{config.get(CONF_PORT)}"),
data=config,
)

async def async_step_reauth(self, data: Mapping[str, Any]) -> FlowResult:
Expand Down
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[tool.mypy]
exclude = ["venv/"]
platform = "linux"
show_error_codes = true
follow_imports = "normal"
local_partial_types = true
strict_equality = true
no_implicit_optional = true
warn_incomplete_stub = true
warn_redundant_casts = true
warn_unused_configs = true
warn_unused_ignores = true
disable_error_code = ["import-untyped"]
extra_checks = false
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
colorlog==6.9.0
homeassistant==2024.12.1
homeassistant==2024.12.3
pip>=21.3.1
proxmoxer==2.1.0
ruff==0.8.2
ruff==0.8.2
pre-commit
21 changes: 21 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-r requirements.txt
colorlog~=6.9
flake8~=7.1
flake8-docstrings~=1.7
fnv-hash-fast
mypy~=1.13
psutil-home-assistant==0.0.1
pylint~=3.3
pylint-strict-informational==0.1
pytest>=7.2
pytest-cov
pytest-homeassistant-custom-component
pytest-asyncio>=0.20
tzdata
ruff~=0.8
anyio
pytest-asyncio
pytest-tornasync
pytest-trio
proxmoxer
pre-commit
7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tool:pytest]
testpaths = tests
norecursedirs = .git
addopts =
--strict-markers
--cov=custom_components
asyncio_mode = auto
23 changes: 23 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Tests for Proxmox VE Custom Integration."""

from unittest.mock import patch

from homeassistant.core import HomeAssistant
from pytest_homeassistant_custom_component.common import MockConfigEntry


async def async_init_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> None:
"""Set up the La Marzocco integration for testing."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()


def patch_async_setup_entry(return_value=True):
"""Patch async setup entry to return True."""
return patch(
"custom_components.proxmoxve.async_setup_entry",
return_value=return_value,
)
44 changes: 44 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# pylint: disable=protected-access,redefined-outer-name
"""Global fixtures for integration."""

# Fixtures allow you to replace functions with a Mock object. You can perform
# many options via the Mock to reflect a particular behavior from the original
# function that you want to see without going through the function's actual logic.
# Fixtures can either be passed into tests as parameters, or if autouse=True, they
# will automatically be used across all tests.
#
# Fixtures that are defined in conftest.py are available across all tests. You can also
# define fixtures within a particular test file to scope them locally.
#
# pytest_homeassistant_custom_component provides some fixtures that are provided by
# Home Assistant core. You can find those fixture definitions here:
# https://github.com/MatthewFlamm/pytest-homeassistant-custom-component/blob/master/pytest_homeassistant_custom_component/common.py
#
# See here for more info: https://docs.pytest.org/en/latest/fixture.html (note that
# pytest includes fixtures OOB which you can use as defined on this page)

import pytest
from homeassistant.core import HomeAssistant
from pytest_homeassistant_custom_component.common import MockConfigEntry

from . import async_init_integration

pytest_plugins = "pytest_homeassistant_custom_component" # pylint: disable=invalid-name


# 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):
"""Automatically enable loading custom integrations in all tests."""
return


@pytest.fixture
async def init_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> MockConfigEntry:
"""Set up the Proxmox VE integration for testing."""
await async_init_integration(hass, mock_config_entry)

return mock_config_entry
3 changes: 3 additions & 0 deletions tests/fixtures/test_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test_key": "test_value"
}
11 changes: 11 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Tests changes to common module."""

import json

from pytest_homeassistant_custom_component.common import load_fixture


def test_load_fixture():
"""Test load fixture."""
data = json.loads(load_fixture("test_data.json"))
assert data == {"test_key": "test_value"}
Loading

0 comments on commit 02c3f66

Please sign in to comment.