Skip to content

Commit

Permalink
Add some myuplink tests (#110521)
Browse files Browse the repository at this point in the history
* Add some myuplink tests

* Update fixtures for api endpoints

* Adjust according to review

* Update snapshot file

* Remove unneded fixtures and improve typing

* More cleanup

* One last session scope removed

* Fix typing

---------

Co-authored-by: Martin Hjelmare <[email protected]>
  • Loading branch information
astrandb and MartinHjelmare authored Feb 15, 2024
1 parent 57d3f3f commit fd0f093
Show file tree
Hide file tree
Showing 4 changed files with 1,177 additions and 1,107 deletions.
103 changes: 80 additions & 23 deletions tests/components/myuplink/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Test helpers for myuplink."""
from collections.abc import Generator
import json
import time
from typing import Any
from unittest.mock import MagicMock, patch

from myuplink import Device, System
from myuplink import Device, DevicePoint, System
import pytest

from homeassistant.components.application_credentials import (
Expand All @@ -15,10 +14,11 @@
from homeassistant.components.myuplink.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util.json import json_loads

from .const import CLIENT_ID, CLIENT_SECRET

from tests.common import MockConfigEntry, load_fixture, load_json_value_fixture
from tests.common import MockConfigEntry, load_fixture


@pytest.fixture(name="expires_at")
Expand Down Expand Up @@ -64,34 +64,91 @@ async def setup_credentials(hass: HomeAssistant) -> None:
)


# Fixture group for device API endpoint.


@pytest.fixture(scope="session")
def load_device_file() -> str:
"""Fixture for loading device file."""
return load_fixture("device.json", DOMAIN)


@pytest.fixture
def mock_myuplink_client() -> Generator[MagicMock, None, None]:
"""Mock a myuplink client."""
def device_fixture(load_device_file: str) -> Device:
"""Fixture for device."""
return Device(json_loads(load_device_file))


# Fixture group for systems API endpoint.


@pytest.fixture
def load_systems_jv_file(load_systems_file: str) -> dict[str, Any]:
"""Load fixture file for systems endpoint."""
return json_loads(load_systems_file)


@pytest.fixture(scope="session")
def load_systems_file() -> str:
"""Load fixture file for systems."""
return load_fixture("systems.json", DOMAIN)


@pytest.fixture
def system_fixture(load_systems_file: str) -> list[System]:
"""Fixture for systems."""
data = json_loads(load_systems_file)
return [System(system_data) for system_data in data["systems"]]


def process_json_system(data: dict[str, Any]) -> System:
array = json.loads(data)
return [System(system_data) for system_data in array["systems"]]
# Fixture group for device points API endpoint.


@pytest.fixture(scope="session")
def load_device_points_file() -> str:
"""Load fixture file for device-points endpoint."""
return load_fixture("device_points_nibe_f730.json", DOMAIN)


@pytest.fixture
def load_device_points_jv_file():
"""Load fixture file for device_points."""
return json_loads(load_device_points_file)


@pytest.fixture
def device_points_fixture(load_device_points_file: str) -> list[DevicePoint]:
"""Fixture for devce_points."""
data = json_loads(load_device_points_file)
return [DevicePoint(point_data) for point_data in data]


@pytest.fixture
def mock_myuplink_client(
load_device_file,
device_fixture,
load_device_points_file,
device_points_fixture,
system_fixture,
load_systems_jv_file,
) -> Generator[MagicMock, None, None]:
"""Mock a myuplink client."""

with patch(
"homeassistant.components.myuplink.MyUplinkAPI",
autospec=True,
) as mock_client:
client = mock_client.return_value
client.async_get_device_points_json.return_value = load_json_value_fixture(
"device_points_nibe_f730.json", DOMAIN
)
client.async_get_systems.return_value = process_json_system(
load_fixture("systems.json", DOMAIN)
)
client.async_get_device.return_value = Device(
load_json_value_fixture("device.json", DOMAIN)
)
client.async_get_device_json.return_value = load_json_value_fixture(
"device.json", DOMAIN
)
client.async_get_systems_json.return_value = load_json_value_fixture(
"systems.json", DOMAIN
)

client.async_get_systems.return_value = system_fixture
client.async_get_systems_json.return_value = load_systems_jv_file

client.async_get_device.return_value = device_fixture
client.async_get_device_json.return_value = load_device_file

client.async_get_device_points.return_value = device_points_fixture
client.async_get_device_points_json.return_value = load_device_points_file

yield client


Expand Down
Loading

0 comments on commit fd0f093

Please sign in to comment.