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

Update Ruff to be used in tests #943

Merged
merged 2 commits into from
Mar 3, 2024
Merged
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
30 changes: 24 additions & 6 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# The contents of this file is based on https://github.com/home-assistant/core/blob/dev/pyproject.toml

target-version = "py310"

[lint]
select = ["ALL"]

# All the ones without a comment were the ones that are currently violated
Expand All @@ -23,18 +23,36 @@ ignore = [
"SLF001", # Private member accessed
]

[per-file-ignores]
"tests/*.py" = ["ALL"]
[lint.per-file-ignores]
"tests/*.py" = [
"ARG001", # Unused function argument: `call`
"D100", # Missing docstring in public module
"D103", # Missing docstring in public function
"D205", # 1 blank line required between summary line and description
"D400", # First line should end with a period
"D415", # First line should end with a period, question mark, or
"DTZ001", # The use of `datetime.datetime()` without `tzinfo`
"ERA001", # Found commented-out code
"FBT003", # Boolean positional value in function call
"FIX002", # Line contains TODO, consider resolving the issue
"G004", # Logging statement uses f-string
"PLR0915", # Too many statements (94 > 50)
"PT004", # Fixture `cleanup` does not return anything, add leading underscore
"PT007", # Wrong values type in `@pytest.mark.parametrize` expected `list` of
"S311", # Standard pseudo-random generators are not suitable for cryptographic
"TD002", # Missing author in TODO; try: `# TODO(<author_name>): ...` or `# TODO
"TD003", # Missing issue link on the line following this TODO
]
".github/*py" = ["INP001"]
"webapp/homeassistant_util_color.py" = ["ALL"]
"webapp/app.py" = ["INP001", "DTZ011", "A002"]
"custom_components/adaptive_lighting/homeassistant_util_color.py" = ["ALL"]

[flake8-pytest-style]
[lint.flake8-pytest-style]
fixture-parentheses = false

[pyupgrade]
[lint.pyupgrade]
keep-runtime-typing = true

[mccabe]
[lint.mccabe]
max-complexity = 25
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@

@pytest.fixture(autouse=True)
def auto_enable_custom_integrations(enable_custom_integrations):
yield
return
36 changes: 23 additions & 13 deletions tests/test_adaptation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

from unittest.mock import Mock

import pytest
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP_KELVIN,
ATTR_TRANSITION,
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_ON
from homeassistant.core import Context, State
import pytest

from custom_components.adaptive_lighting.adaptation_utils import (
ServiceData,
Expand All @@ -22,7 +22,7 @@


@pytest.mark.parametrize(
"input_data,expected_data_list",
("input_data", "expected_data_list"),
[
(
{"foo": 1},
Expand Down Expand Up @@ -72,7 +72,7 @@ async def test_split_service_call_data(input_data, expected_data_list):


@pytest.mark.parametrize(
"service_data,state,service_data_expected",
("service_data", "state", "service_data_expected"),
[
(
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 10, ATTR_TRANSITION: 2},
Expand All @@ -97,14 +97,16 @@ async def test_split_service_call_data(input_data, expected_data_list):
],
)
async def test_remove_redundant_attributes(
service_data: ServiceData, state: State | None, service_data_expected: ServiceData
service_data: ServiceData,
state: State | None,
service_data_expected: ServiceData,
):
"""Test filtering of service data."""
assert _remove_redundant_attributes(service_data, state) == service_data_expected


@pytest.mark.parametrize(
"service_data,expected_relevant",
("service_data", "expected_relevant"),
[
(
{ATTR_ENTITY_ID: "light.test"},
Expand All @@ -125,14 +127,15 @@ async def test_remove_redundant_attributes(
],
)
async def test_has_relevant_service_data_attributes(
service_data: ServiceData, expected_relevant: bool
service_data: ServiceData,
expected_relevant: bool,
):
"""Test the determination of relevancy of service data"""
assert _has_relevant_service_data_attributes(service_data) == expected_relevant


@pytest.mark.parametrize(
"service_datas,filter_by_state,service_datas_expected",
("service_datas", "filter_by_state", "service_datas_expected"),
[
(
[{ATTR_ENTITY_ID: "light.test"}],
Expand Down Expand Up @@ -198,11 +201,12 @@ async def test_create_service_call_data_iterator(
hass_states_mock,
):
"""Test the generator function for correct enumeration and filtering."""

generated_service_datas = [
data
async for data in _create_service_call_data_iterator(
hass_states_mock, service_datas, filter_by_state
hass_states_mock,
service_datas,
filter_by_state,
)
]

Expand All @@ -215,7 +219,13 @@ async def test_create_service_call_data_iterator(


@pytest.mark.parametrize(
"service_data,split,filter_by_state,service_datas_expected,sleep_time_expected",
(
"service_data",
"split",
"filter_by_state",
"service_datas_expected",
"sleep_time_expected",
),
[
(
{
Expand All @@ -230,7 +240,7 @@ async def test_create_service_call_data_iterator(
ATTR_ENTITY_ID: "light.test",
ATTR_BRIGHTNESS: 10,
ATTR_COLOR_TEMP_KELVIN: 4000,
}
},
],
1.2,
),
Expand Down Expand Up @@ -266,7 +276,7 @@ async def test_create_service_call_data_iterator(
{
ATTR_ENTITY_ID: "light.test",
ATTR_COLOR_TEMP_KELVIN: 4000,
}
},
],
1.2,
),
Expand All @@ -282,7 +292,7 @@ async def test_create_service_call_data_iterator(
{
ATTR_ENTITY_ID: "light.test",
ATTR_COLOR_TEMP_KELVIN: 4000,
}
},
],
0.7,
),
Expand Down
16 changes: 9 additions & 7 deletions tests/test_color_and_brightness.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import datetime as dt
import zoneinfo

import pytest
from astral import LocationInfo
from astral.location import Location

from custom_components.adaptive_lighting.color_and_brightness import (
SunEvents,
SUN_EVENT_SUNRISE,
SUN_EVENT_NOON,
SUN_EVENT_SUNRISE,
SunEvents,
)
import datetime as dt
from astral import LocationInfo
from astral.location import Location
import zoneinfo

# Create a mock astral_location object
location = Location(LocationInfo())
Expand All @@ -31,7 +33,7 @@ def tzinfo_and_location(request):
timezone=timezone,
latitude=lat,
longitude=long,
)
),
)
return tzinfo, location

Expand Down
6 changes: 4 additions & 2 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
async def test_flow_manual_configuration(hass):
"""Test that config flow works."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "user"}
DOMAIN,
context={"source": "user"},
)

assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["handler"] == "adaptive_lighting"

result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_NAME: "living room"}
result["flow_id"],
user_input={CONF_NAME: "living room"},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "living room"
Expand Down
3 changes: 1 addition & 2 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ async def test_setup_with_config(hass):
config = {
adaptive_lighting.DOMAIN: {
adaptive_lighting.CONF_NAME: DEFAULT_NAME,
}
},
}
assert await async_setup_component(hass, adaptive_lighting.DOMAIN, config)
assert adaptive_lighting.DOMAIN in hass.data


async def test_successful_config_entry(hass):
"""Test that Adaptive Lighting is configured successfully."""

entry = MockConfigEntry(
domain=adaptive_lighting.DOMAIN,
data={CONF_NAME: DEFAULT_NAME},
Expand Down
Loading
Loading