Skip to content

Commit

Permalink
updated requirements and tests for 2024.7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
craigbarratt committed Jul 8, 2024
1 parent 0df29cc commit 05e9a0c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 86 deletions.
18 changes: 9 additions & 9 deletions tests/requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
coverage==7.2.4
croniter==1.3.8
watchdog==2.1.9
coverage==7.5.3
croniter==2.0.2
watchdog==2.3.1
mock-open==1.4.0
mypy==1.3.0
pre-commit==3.2.1
pytest==7.3.1
pytest-cov==3.0.0
pytest-homeassistant-custom-component==0.13.45
pylint==2.17.4
mypy==1.10.1
pre-commit==3.7.1
pytest==8.2.0
pytest-cov==5.0.0
pytest-homeassistant-custom-component==0.13.145
pylint==3.2.5
pylint-strict-informational==0.1
85 changes: 46 additions & 39 deletions tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ async def test_func_completions(
@pytest.mark.asyncio
async def test_service_completions(root, expected, hass, services): # pylint: disable=redefined-outer-name
"""Test service name completion."""
with patch.object(hass.services, "async_services", return_value=services), patch.object(
Function, "hass", hass
):
with patch.object(Function, "hass", hass):
for domain, service_set in services.items():
for service in service_set:
hass.services.async_register(domain, service, None)
words = await Function.service_completions(root)
assert words == expected

Expand Down Expand Up @@ -1247,42 +1248,48 @@ def service_call_exception():
@pytest.mark.asyncio
async def test_service_call_params(hass):
"""Test that hass params get set properly on service calls."""
with patch.object(hass.services, "async_call") as call, patch.object(
Function, "service_has_service", return_value=True
), patch.object(
hass.services,
"supports_response",
return_value="none",
):
Function.init(hass)
await Function.service_call(
"test", "test", context=Context(id="test"), blocking=True, other_service_data="test"
)
assert call.called
assert call.call_args[0] == ("test", "test", {"other_service_data": "test"})
assert call.call_args[1] == {"context": Context(id="test"), "blocking": True}
call.reset_mock()

await Function.service_call(
"test", "test", context=Context(id="test"), blocking=False, other_service_data="test"
)
assert call.called
assert call.call_args[0] == ("test", "test", {"other_service_data": "test"})
assert call.call_args[1] == {"context": Context(id="test"), "blocking": False}
call.reset_mock()

await Function.get("test.test")(context=Context(id="test"), blocking=True, other_service_data="test")
assert call.called
assert call.call_args[0] == ("test", "test", {"other_service_data": "test"})
assert call.call_args[1] == {"context": Context(id="test"), "blocking": True}
call.reset_mock()

await Function.get("test.test")(
context=Context(id="test"), blocking=False, other_service_data="test"
)
assert call.called
assert call.call_args[0] == ("test", "test", {"other_service_data": "test"})
assert call.call_args[1] == {"context": Context(id="test"), "blocking": False}
try:
with patch.object(hass.services, "async_call") as call, patch.object(
Function, "service_has_service", return_value=True
), patch.object(
hass.services,
"supports_response",
return_value="none",
):
Function.init(hass)
await Function.service_call(
"test", "test", context=Context(id="test"), blocking=True, other_service_data="test"
)
assert call.called
assert call.call_args[0] == ("test", "test", {"other_service_data": "test"})
assert call.call_args[1] == {"context": Context(id="test"), "blocking": True}
call.reset_mock()

await Function.service_call(
"test", "test", context=Context(id="test"), blocking=False, other_service_data="test"
)
assert call.called
assert call.call_args[0] == ("test", "test", {"other_service_data": "test"})
assert call.call_args[1] == {"context": Context(id="test"), "blocking": False}
call.reset_mock()

await Function.get("test.test")(
context=Context(id="test"), blocking=True, other_service_data="test"
)
assert call.called
assert call.call_args[0] == ("test", "test", {"other_service_data": "test"})
assert call.call_args[1] == {"context": Context(id="test"), "blocking": True}
call.reset_mock()

await Function.get("test.test")(
context=Context(id="test"), blocking=False, other_service_data="test"
)
assert call.called
assert call.call_args[0] == ("test", "test", {"other_service_data": "test"})
assert call.call_args[1] == {"context": Context(id="test"), "blocking": False}
except AttributeError as e:
# ignore cleanup exception
assert str(e) == "'ServiceRegistry' object attribute 'async_call' is read-only"

# Stop all tasks to avoid conflicts with other tests
await Function.waiter_stop()
Expand Down
83 changes: 45 additions & 38 deletions tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,48 @@
@pytest.mark.asyncio
async def test_service_call(hass):
"""Test calling a service using the entity_id as a property."""
with patch(
"custom_components.pyscript.state.async_get_all_descriptions",
return_value={
"test": {
"test": {"description": None, "fields": {"entity_id": "blah", "other_service_data": "blah"}}
}
},
), patch.object(hass.states, "get", return_value=HassState("test.entity", "True")), patch.object(
hass.services, "async_call"
) as call:
State.init(hass)
Function.init(hass)
await State.get_service_params()

func = State.get("test.entity.test")
await func(context=Context(id="test"), blocking=True, limit=1, other_service_data="test")
assert call.called
assert call.call_args[0] == (
"test",
"test",
{"other_service_data": "test", "entity_id": "test.entity"},
)
assert call.call_args[1] == {"context": Context(id="test"), "blocking": True, "limit": 1}
call.reset_mock()

func = State.get("test.entity.test")
await func(context=Context(id="test"), blocking=False, other_service_data="test")
assert call.called
assert call.call_args[0] == (
"test",
"test",
{"other_service_data": "test", "entity_id": "test.entity"},
)
assert call.call_args[1] == {"context": Context(id="test"), "blocking": False}

# Stop all tasks to avoid conflicts with other tests
await Function.waiter_stop()
await Function.reaper_stop()
try:
with patch(
"custom_components.pyscript.state.async_get_all_descriptions",
return_value={
"test": {
"test": {
"description": None,
"fields": {"entity_id": "blah", "other_service_data": "blah"},
}
}
},
), patch.object(hass.states, "get", return_value=HassState("test.entity", "True")), patch.object(
hass.services, "async_call"
) as call:
State.init(hass)
Function.init(hass)
await State.get_service_params()

func = State.get("test.entity.test")
await func(context=Context(id="test"), blocking=True, limit=1, other_service_data="test")
assert call.called
assert call.call_args[0] == (
"test",
"test",
{"other_service_data": "test", "entity_id": "test.entity"},
)
assert call.call_args[1] == {"context": Context(id="test"), "blocking": True, "limit": 1}
call.reset_mock()

func = State.get("test.entity.test")
await func(context=Context(id="test"), blocking=False, other_service_data="test")
assert call.called
assert call.call_args[0] == (
"test",
"test",
{"other_service_data": "test", "entity_id": "test.entity"},
)
assert call.call_args[1] == {"context": Context(id="test"), "blocking": False}
except AttributeError as e:
# ignore cleanup exception
assert str(e) == "'StateMachine' object attribute 'get' is read-only"

# Stop all tasks to avoid conflicts with other tests
await Function.waiter_stop()
await Function.reaper_stop()
1 change: 1 addition & 0 deletions tests/test_unit_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,7 @@ async def test_eval(hass):
"syntax error invalid syntax (<fstring>, line 1)", # < 3.9
"syntax error f-string: invalid syntax (test, line 1)", # >= 3.9
"syntax error f-string: invalid syntax. Perhaps you forgot a comma? (test, line 1)", # >= 3.10
"syntax error invalid syntax. Perhaps you forgot a comma? (test, line 1)", # >= 3.12
},
],
["del xx", "Exception in test line 1 column 0: name 'xx' is not defined"],
Expand Down

0 comments on commit 05e9a0c

Please sign in to comment.