Skip to content

Commit 046462d

Browse files
committed
Extend test_services.test_query_service_rollback_on_error
Signed-off-by: David Rapan <[email protected]>
1 parent a6c3fb6 commit 046462d

File tree

3 files changed

+55
-34
lines changed

3 files changed

+55
-34
lines changed

homeassistant/components/sql/util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ async def _shutdown_db_engines(_: Event) -> None:
197197
"""Shutdown all database engines."""
198198
for sessmaker in session_makers_by_db_url.values():
199199
if isinstance(sessmaker, async_scoped_session):
200+
_LOGGER.error("Disposed async engine for shutdown 1")
200201
await (await sessmaker.connection()).engine.dispose()
202+
_LOGGER.error("Disposed async engine for shutdown 2")
203+
raise Exception("Disposed async engine for shutdown")
201204
else:
202205
sessmaker.connection().engine.dispose()
203206

tests/components/sql/test_sensor.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,6 @@ def make_test_db():
193193
@pytest.mark.parametrize(
194194
("patch_create", "url", "expected_patterns", "not_expected_patterns"),
195195
[
196-
(
197-
"homeassistant.components.sql.util.sqlalchemy.create_engine",
198-
"sqlite://homeassistant:[email protected]",
199-
["sqlite://****:****@homeassistant.local"],
200-
["sqlite://homeassistant:[email protected]"],
201-
),
202-
(
203-
"homeassistant.components.sql.util.sqlalchemy.create_engine",
204-
"sqlite://homeassistant.local",
205-
["sqlite://homeassistant.local"],
206-
[],
207-
),
208196
(
209197
"homeassistant.components.sql.util.create_async_engine",
210198
"sqlite+aiosqlite://homeassistant:[email protected]",
@@ -217,6 +205,18 @@ def make_test_db():
217205
["sqlite+aiosqlite://homeassistant.local"],
218206
[],
219207
),
208+
(
209+
"homeassistant.components.sql.util.sqlalchemy.create_engine",
210+
"sqlite://homeassistant:[email protected]",
211+
["sqlite://****:****@homeassistant.local"],
212+
["sqlite://homeassistant:[email protected]"],
213+
),
214+
(
215+
"homeassistant.components.sql.util.sqlalchemy.create_engine",
216+
"sqlite://homeassistant.local",
217+
["sqlite://homeassistant.local"],
218+
[],
219+
),
220220
],
221221
)
222222
async def test_invalid_url_setup(
@@ -301,7 +301,7 @@ def rollback(self) -> None:
301301
assert "sqlite://****:****@homeassistant.local" in caplog.text
302302

303303

304-
@pytest.mark.parametrize("async_driver", [False, True])
304+
@pytest.mark.parametrize("async_driver", [True, False])
305305
async def test_query_from_yaml(
306306
recorder_mock: Recorder, hass: HomeAssistant, async_driver: bool
307307
) -> None:
@@ -411,18 +411,6 @@ async def test_config_from_old_yaml(
411411
@pytest.mark.parametrize(
412412
("patch_create", "url", "expected_patterns", "not_expected_patterns"),
413413
[
414-
(
415-
"homeassistant.components.sql.util.sqlalchemy.create_engine",
416-
"sqlite://homeassistant:[email protected]",
417-
["sqlite://****:****@homeassistant.local"],
418-
["sqlite://homeassistant:[email protected]"],
419-
),
420-
(
421-
"homeassistant.components.sql.util.sqlalchemy.create_engine",
422-
"sqlite://homeassistant.local",
423-
["sqlite://homeassistant.local"],
424-
[],
425-
),
426414
(
427415
"homeassistant.components.sql.util.create_async_engine",
428416
"sqlite+aiosqlite://homeassistant:[email protected]",
@@ -435,6 +423,18 @@ async def test_config_from_old_yaml(
435423
["sqlite+aiosqlite://homeassistant.local"],
436424
[],
437425
),
426+
(
427+
"homeassistant.components.sql.util.sqlalchemy.create_engine",
428+
"sqlite://homeassistant:[email protected]",
429+
["sqlite://****:****@homeassistant.local"],
430+
["sqlite://homeassistant:[email protected]"],
431+
),
432+
(
433+
"homeassistant.components.sql.util.sqlalchemy.create_engine",
434+
"sqlite://homeassistant.local",
435+
["sqlite://homeassistant.local"],
436+
[],
437+
),
438438
],
439439
)
440440
async def test_invalid_url_setup_from_yaml(
@@ -728,17 +728,17 @@ async def test_attributes_from_entry_config(
728728
@pytest.mark.parametrize(
729729
("config", "patch_rollback"),
730730
[
731-
(
732-
{},
733-
"sqlalchemy.orm.session.Session.rollback",
734-
),
735731
(
736732
{CONF_DB_URL: "sqlite+aiosqlite:///"},
737733
"sqlalchemy.ext.asyncio.session.AsyncSession.rollback",
738734
),
735+
(
736+
{},
737+
"sqlalchemy.orm.session.Session.rollback",
738+
),
739739
],
740740
)
741-
async def test_session_rollback_on_error(
741+
async def test_query_rollback_on_error(
742742
recorder_mock: Recorder,
743743
hass: HomeAssistant,
744744
freezer: FrozenDateTimeFactory,

tests/components/sql/test_services.py

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

99
import pytest
10-
from sqlalchemy.exc import SQLAlchemyError
10+
from sqlalchemy import text
1111
import voluptuous as vol
1212
from voluptuous import MultipleInvalid
1313

@@ -87,14 +87,29 @@ async def test_query_service_external_db(hass: HomeAssistant, tmp_path: Path) ->
8787
}
8888

8989

90+
@pytest.mark.parametrize(
91+
("async_driver", "patch_rollback"),
92+
[
93+
(
94+
True,
95+
"sqlalchemy.ext.asyncio.session.AsyncSession.rollback",
96+
),
97+
(
98+
False,
99+
"sqlalchemy.orm.session.Session.rollback",
100+
),
101+
],
102+
)
90103
async def test_query_service_rollback_on_error(
91104
hass: HomeAssistant,
92105
tmp_path: Path,
93106
caplog: pytest.LogCaptureFixture,
107+
async_driver: bool,
108+
patch_rollback: str,
94109
) -> None:
95110
"""Test the query service."""
96111
db_path = tmp_path / "test.db"
97-
db_url = f"sqlite:///{db_path}"
112+
db_url = f"sqlite{'+aiosqlite' if async_driver else ''}:///{db_path}"
98113

99114
# Create and populate the external database
100115
conn = sqlite3.connect(db_path)
@@ -109,12 +124,12 @@ async def test_query_service_rollback_on_error(
109124
with (
110125
patch(
111126
"homeassistant.components.sql.services.generate_lambda_stmt",
112-
side_effect=SQLAlchemyError("Error executing query"),
127+
return_value=text("Faulty syntax create operational issue"),
113128
),
114129
pytest.raises(
115130
ServiceValidationError, match="An error occurred when executing the query"
116131
),
117-
patch("sqlalchemy.orm.session.Session.rollback") as mock_session_rollback,
132+
patch(patch_rollback) as mock_session_rollback,
118133
):
119134
await hass.services.async_call(
120135
DOMAIN,
@@ -124,8 +139,11 @@ async def test_query_service_rollback_on_error(
124139
return_response=True,
125140
)
126141

142+
assert "sqlite3.OperationalError" in caplog.text
127143
assert mock_session_rollback.call_count == 1
128144

145+
await hass.async_stop()
146+
129147

130148
async def test_query_service_data_conversion(
131149
hass: HomeAssistant, tmp_path: Path

0 commit comments

Comments
 (0)