Default to recorder db for SQL integration (#85436)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
G Johansson 2023-03-14 04:41:32 +01:00 committed by GitHub
parent 2f4e9c8ef3
commit afa58b80bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 120 additions and 54 deletions

View file

@ -8,6 +8,7 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.util import get_instance
from homeassistant.components.sql import validate_sql_select
from homeassistant.components.sql.const import DOMAIN
from homeassistant.core import HomeAssistant
@ -56,3 +57,41 @@ async def test_invalid_query(hass: HomeAssistant) -> None:
"""Test invalid query."""
with pytest.raises(vol.Invalid):
validate_sql_select("DROP TABLE *")
async def test_remove_configured_db_url_if_not_needed_when_not_needed(
recorder_mock: Recorder,
hass: HomeAssistant,
) -> None:
"""Test configured db_url is replaced with None if matching the recorder db."""
recorder_db_url = get_instance(hass).db_url
config = {
"db_url": recorder_db_url,
"query": "SELECT 5 as value",
"column": "value",
"name": "count_tables",
}
config_entry = await init_integration(hass, config)
assert config_entry.options.get("db_url") is None
async def test_remove_configured_db_url_if_not_needed_when_needed(
recorder_mock: Recorder,
hass: HomeAssistant,
) -> None:
"""Test configured db_url is not replaced if it differs from the recorder db."""
db_url = "mssql://"
config = {
"db_url": db_url,
"query": "SELECT 5 as value",
"column": "value",
"name": "count_tables",
}
config_entry = await init_integration(hass, config)
assert config_entry.options.get("db_url") == db_url