Skip to content

Commit cc11b85

Browse files
committed
Add test_sensor.test_async_invalid_url_setup
Signed-off-by: David Rapan <[email protected]>
1 parent 56b7d17 commit cc11b85

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tests/components/sql/test_sensor.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,59 @@ async def test_invalid_url_setup(
243243
assert pattern in caplog.text
244244

245245

246+
@pytest.mark.parametrize(
247+
("url", "expected_patterns", "not_expected_patterns"),
248+
[
249+
(
250+
"sqlite+aiosqlite://homeassistant:[email protected]",
251+
["sqlite+aiosqlite://****:****@homeassistant.local"],
252+
["sqlite+aiosqlite://homeassistant:[email protected]"],
253+
),
254+
(
255+
"sqlite+aiosqlite://homeassistant.local",
256+
["sqlite+aiosqlite://homeassistant.local"],
257+
[],
258+
),
259+
],
260+
)
261+
async def test_async_invalid_url_setup(
262+
recorder_mock: Recorder,
263+
hass: HomeAssistant,
264+
caplog: pytest.LogCaptureFixture,
265+
url: str,
266+
expected_patterns: str,
267+
not_expected_patterns: str,
268+
) -> None:
269+
"""Test invalid db url with redacted credentials."""
270+
config = {
271+
CONF_QUERY: "SELECT 5 as value",
272+
CONF_COLUMN_NAME: "value",
273+
}
274+
entry = MockConfigEntry(
275+
title="count_tables",
276+
domain=DOMAIN,
277+
source=SOURCE_USER,
278+
data={CONF_DB_URL: url},
279+
options=config,
280+
entry_id="1",
281+
version=2,
282+
)
283+
284+
entry.add_to_hass(hass)
285+
286+
with patch(
287+
"homeassistant.components.sql.util.create_async_engine",
288+
side_effect=SQLAlchemyError(url),
289+
):
290+
await hass.config_entries.async_setup(entry.entry_id)
291+
await hass.async_block_till_done()
292+
293+
for pattern in not_expected_patterns:
294+
assert pattern not in caplog.text
295+
for pattern in expected_patterns:
296+
assert pattern in caplog.text
297+
298+
246299
async def test_invalid_url_on_update(
247300
recorder_mock: Recorder,
248301
hass: HomeAssistant,

0 commit comments

Comments
 (0)