Skip to content

Commit 3da83ee

Browse files
committed
Add test_services.test_async_error_executing_query
Signed-off-by: David Rapan <[email protected]>
1 parent cc11b85 commit 3da83ee

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

homeassistant/components/sql/util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ async def _async_validate_and_get_session_maker_for_db_url(
215215
"""Validate the db_url and return a async session maker."""
216216
try:
217217
maker = async_scoped_session(
218-
async_sessionmaker(bind=create_async_engine(db_url)),
218+
async_sessionmaker(
219+
bind=create_async_engine(db_url, future=True), future=True
220+
),
219221
scopefunc=asyncio.current_task,
220222
)
221223
# Run a dummy query just to test the db_url

tests/components/sql/test_services.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from unittest.mock import patch
88

99
import pytest
10+
from sqlalchemy.exc import SQLAlchemyError
1011
import voluptuous as vol
1112
from voluptuous import MultipleInvalid
1213

@@ -55,6 +56,31 @@ async def test_query_service_recorder_db(
5556
}
5657

5758

59+
async def test_async_error_executing_query(
60+
hass: HomeAssistant,
61+
caplog: pytest.LogCaptureFixture,
62+
) -> None:
63+
"""Test the query service."""
64+
db_url = "sqlite+aiosqlite:///"
65+
66+
await async_setup_component(hass, DOMAIN, {})
67+
await hass.async_block_till_done()
68+
69+
with patch(
70+
"sqlalchemy.ext.asyncio.session.AsyncSession.execute",
71+
side_effect=SQLAlchemyError("Error executing query"),
72+
):
73+
await hass.services.async_call(
74+
DOMAIN,
75+
SERVICE_QUERY,
76+
{"query": "SELECT name, age FROM users ORDER BY age", "db_url": db_url},
77+
blocking=True,
78+
return_response=True,
79+
)
80+
81+
assert "Error executing query" in caplog.text
82+
83+
5884
async def test_query_service_external_db(hass: HomeAssistant, tmp_path: Path) -> None:
5985
"""Test the query service with an external database via db_url."""
6086
db_path = tmp_path / "test.db"

0 commit comments

Comments
 (0)