Skip to content

Commit

Permalink
refactor test_set_hotwater.py to use mock fixture; improve readabilit…
Browse files Browse the repository at this point in the history
…y and maintainability
  • Loading branch information
liudger committed Sep 30, 2024
1 parent d551393 commit faf5c16
Showing 1 changed file with 53 additions and 108 deletions.
161 changes: 53 additions & 108 deletions tests/test_set_hotwater.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,64 @@
# pylint: disable=protected-access
# file deepcode ignore W0212: this is a testfile

import json
from typing import Any

from unittest.mock import AsyncMock

import aiohttp
import pytest
from aresponses import ResponsesMockServer

from bsblan import BSBLAN, BSBLANConfig, BSBLANError
from bsblan.constants import API_V3, MULTI_PARAMETER_ERROR_MSG, NO_STATE_ERROR_MSG
from bsblan import BSBLAN, BSBLANError
from bsblan.constants import MULTI_PARAMETER_ERROR_MSG, NO_STATE_ERROR_MSG

request_mock: AsyncMock = AsyncMock(return_value={"status": "ok"})


@pytest.mark.asyncio
async def test_set_hot_water(aresponses: ResponsesMockServer, monkeypatch: Any) -> None:
async def test_set_hot_water(mock_bsblan: BSBLAN) -> None:
"""Test setting BSBLAN hot water state."""
# Set environment variable
monkeypatch.setenv("BSBLAN_PASS", "your_password")

aresponses.add(
"example.com",
"/JS",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=json.dumps({"status": "ok"}),
),
# Test setting operating_mode
await mock_bsblan.set_hot_water(operating_mode="3")
assert isinstance(mock_bsblan._request, AsyncMock) # Type check
mock_bsblan._request.assert_awaited_with(
base_path="/JS",
data={
"Parameter": "1600",
"EnumValue": "3",
"Type": "1",
},
)
async with aiohttp.ClientSession() as session:

config = BSBLANConfig(host="example.com")
bsblan = BSBLAN(config, session=session)

monkeypatch.setattr(bsblan, "_firmware_version", "1.0.38-20200730234859")
monkeypatch.setattr(bsblan, "_api_version", "v3")
monkeypatch.setattr(bsblan, "_api_data", API_V3)

# Mock _request method
request_mock = AsyncMock(return_value={"status": "ok"})
monkeypatch.setattr(bsblan, "_request", request_mock)

# Test setting operating_mode
await bsblan.set_hot_water(operating_mode="3")
request_mock.assert_called_with(
base_path="/JS",
data={
"Parameter": "1600",
"EnumValue": "3",
"Type": "1",
},
)

# Test setting nominal_setpoint
await bsblan.set_hot_water(nominal_setpoint=60.0)
request_mock.assert_called_with(
base_path="/JS",
data={
"Parameter": "1610",
"Value": "60.0",
"Type": "1",
},
)
# Test setting nominal_setpoint
await mock_bsblan.set_hot_water(nominal_setpoint=60.0)
mock_bsblan._request.assert_awaited_with(
base_path="/JS",
data={
"Parameter": "1610",
"Value": "60.0",
"Type": "1",
},
)

# Test setting reduced_setpoint
await bsblan.set_hot_water(reduced_setpoint=40.0)
request_mock.assert_called_with(
base_path="/JS",
data={
"Parameter": "1612",
"Value": "40.0",
"Type": "1",
},
)
# Test setting reduced_setpoint
await mock_bsblan.set_hot_water(reduced_setpoint=40.0)
mock_bsblan._request.assert_awaited_with(
base_path="/JS",
data={
"Parameter": "1612",
"Value": "40.0",
"Type": "1",
},
)

# Test setting multiple parameters (should raise an error)
with pytest.raises(BSBLANError, match=MULTI_PARAMETER_ERROR_MSG):
await bsblan.set_hot_water(operating_mode="3", nominal_setpoint=60.0)
# Test setting multiple parameters (should raise an error)
with pytest.raises(BSBLANError, match=MULTI_PARAMETER_ERROR_MSG):
await mock_bsblan.set_hot_water(operating_mode="3", nominal_setpoint=60.0)


@pytest.mark.asyncio
async def test_prepare_hot_water_state() -> None:
async def test_prepare_hot_water_state(mock_bsblan: BSBLAN) -> None:
"""Test preparing hot water state."""
bsblan = BSBLAN(BSBLANConfig(host="example.com"))

# Test preparing operating_mode
state = bsblan._prepare_hot_water_state(
state = mock_bsblan._prepare_hot_water_state(
operating_mode="3",
nominal_setpoint=None,
reduced_setpoint=None,
Expand All @@ -101,7 +73,7 @@ async def test_prepare_hot_water_state() -> None:
}

# Test preparing nominal_setpoint
state = bsblan._prepare_hot_water_state(
state = mock_bsblan._prepare_hot_water_state(
operating_mode=None,
nominal_setpoint=60.0,
reduced_setpoint=None,
Expand All @@ -113,7 +85,7 @@ async def test_prepare_hot_water_state() -> None:
}

# Test preparing reduced_setpoint
state = bsblan._prepare_hot_water_state(
state = mock_bsblan._prepare_hot_water_state(
operating_mode=None,
nominal_setpoint=None,
reduced_setpoint=40.0,
Expand All @@ -126,7 +98,7 @@ async def test_prepare_hot_water_state() -> None:

# Test preparing no parameters (should raise an error)
with pytest.raises(BSBLANError, match=NO_STATE_ERROR_MSG):
bsblan._prepare_hot_water_state(
mock_bsblan._prepare_hot_water_state(
operating_mode=None,
nominal_setpoint=None,
reduced_setpoint=None,
Expand All @@ -135,41 +107,14 @@ async def test_prepare_hot_water_state() -> None:

@pytest.mark.asyncio
async def test_set_hot_water_state(
aresponses: ResponsesMockServer,
monkeypatch: Any,
mock_bsblan: BSBLAN,
) -> None:
"""Test setting hot water state."""
# Set environment variable
monkeypatch.setenv("BSBLAN_PASS", "your_password")

aresponses.add(
"example.com",
"/JS",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=json.dumps({"status": "ok"}),
),
)
async with aiohttp.ClientSession() as session:

config = BSBLANConfig(host="example.com")
bsblan = BSBLAN(config, session=session)

monkeypatch.setattr(bsblan, "_firmware_version", "1.0.38-20200730234859")
monkeypatch.setattr(bsblan, "_api_version", "v3")
monkeypatch.setattr(bsblan, "_api_data", API_V3)

# Mock _request method
request_mock = AsyncMock(return_value={"status": "ok"})
monkeypatch.setattr(bsblan, "_request", request_mock)

# Test setting hot water state
state = {
"Parameter": "1600",
"EnumValue": "3",
"Type": "1",
}
await bsblan._set_hot_water_state(state)
request_mock.assert_called_with(base_path="/JS", data=state)
state = {
"Parameter": "1600",
"EnumValue": "3",
"Type": "1",
}
await mock_bsblan._set_hot_water_state(state)
assert isinstance(mock_bsblan._request, AsyncMock) # Type check
mock_bsblan._request.assert_awaited_with(base_path="/JS", data=state)

0 comments on commit faf5c16

Please sign in to comment.