Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GHA tests check and initial tests #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Python tests

on:
push:
branches:
- master

pull_request:
branches: ["*"]

jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11", "3.12"]

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements_test.txt

- name: Run pytest
# run: |
# pytest --cov-report xml:coverage.xml
run: |
pytest \
-qq \
--timeout=9 \
--durations=10 \
-n auto \
-o console_output_style=count \
-p no:sugar \
tests
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea/
**/__pycache__/

.env
poetry.lock
3 changes: 3 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Strictly for tests
pytest==7.4.4
pytest-homeassistant-custom-component==0.13.103
101 changes: 101 additions & 0 deletions tests/fan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import pytest
from unittest.mock import AsyncMock, patch
from custom_components.comfoconnect import ComfoConnectFan
from custom_components.comfoconnect.const import DOMAIN, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED
from aiocomfoconnect.const import VentilationMode, VentilationSpeed
from custom_components.comfoconnect.sensors import SENSOR_FAN_SPEED_MODE, SENSOR_OPERATING_MODE

# Fixtures and test setup

@pytest.fixture
def comfoconnect_bridge():
"""Generate a mock ComfoConnectBridge."""
ccb = AsyncMock()
ccb.uuid = 'mock-uuid'
return ccb

@pytest.fixture
def fan(comfoconnect_bridge, hass):
"""Create a mock ComfoConnectFan entity."""
fan = ComfoConnectFan(ccb=comfoconnect_bridge, config_entry=AsyncMock())
fan.hass = hass
return fan

# Test cases

@pytest.mark.asyncio
async def test_initialization(fan):
"""Test initialization of the fan entity."""
assert fan.unique_id == 'mock-uuid'
assert isinstance(fan, ComfoConnectFan)

@pytest.mark.asyncio
async def test_fan_turn_on_defaults(hass, fan):
"""Test turning on the fan with default values."""
with patch.object(fan, 'async_set_percentage') as mock_set_percentage:
await fan.async_turn_on()
mock_set_percentage.assert_called_once_with(1) # Default turn on to low

@pytest.mark.asyncio
async def test_fan_turn_on_with_percentage(hass, fan):
"""Test turning on the fan with a specific percentage."""
test_percentage = 50
with patch.object(fan, 'async_set_percentage') as mock_set_percentage:
await fan.async_turn_on(percentage=test_percentage)
mock_set_percentage.assert_called_once_with(test_percentage)

@pytest.mark.asyncio
async def test_fan_turn_off(hass, fan):
"""Test turning off the fan."""
with patch.object(fan, 'async_set_percentage') as mock_set_percentage:
await fan.async_turn_off()
mock_set_percentage.assert_called_once_with(0) # Off should set to away

@pytest.mark.asyncio
async def test_preset_mode(hass, fan):
"""Test setting preset modes on the fan."""
with patch.object(fan, 'async_set_preset_mode') as mock_set_preset_mode:
await fan.async_turn_on(preset_mode=VentilationMode.AUTO)
mock_set_preset_mode.assert_called_once_with(VentilationMode.AUTO)

@pytest.mark.asyncio
async def test_invalid_preset_mode(hass, fan):
"""Test setting invalid preset mode on the fan."""
with pytest.raises(ValueError):
await fan.async_set_preset_mode("InvalidPresetMode")

@pytest.mark.asyncio
async def test_set_valid_percentage(hass, fan):
"""Test setting a valid percentage for the fan speed."""
valid_percentage = 50 # Assuming 50% is a valid speed
with patch.object(fan._ccb, 'set_speed') as mock_set_speed:
await fan.async_set_percentage(valid_percentage)
mock_set_speed.assert_called_once()

@pytest.mark.asyncio
async def test_set_invalid_percentage(hass, fan):
"""Test setting an invalid percentage for the fan speed."""
invalid_percentage = 999 # Assuming 999% is invalid
with pytest.raises(ValueError):
await fan.async_set_percentage(invalid_percentage)

@pytest.mark.asyncio
async def test_handle_speed_update(hass, fan):
"""Test handling updates for the fan speed."""
# Simulate a speed update signal for medium speed
fan._handle_speed_update(FAN_SPEED_MAPPING[2]) # Medium speed
await hass.async_block_till_done()
assert fan.percentage == 67 # Or the concrete percentage for medium speed (2 out of 3)

@pytest.mark.asyncio
async def test_handle_mode_update(hass, fan):
"""Test handling updates for the operating mode."""
# Test updating to AUTO mode
fan._handle_mode_update(-1) # AUTO mode
await hass.async_block_till_done()
assert fan.preset_mode == VentilationMode.AUTO

# Test updating to MANUAL mode
fan._handle_mode_update(0) # MANUAL mode
await hass.async_block_till_done()
assert fan.preset_mode == VentilationMode.MANUAL
Loading