File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
homeassistant/components/sql Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 77from unittest .mock import patch
88
99import pytest
10+ from sqlalchemy .exc import SQLAlchemyError
1011import voluptuous as vol
1112from 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+
5884async 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"
You can’t perform that action at this time.
0 commit comments