From 1f550f3e22ccdc13a8320f6f3b7e18efa80c79de Mon Sep 17 00:00:00 2001 From: G Johansson Date: Fri, 8 Aug 2025 16:58:48 +0000 Subject: [PATCH] More tests --- homeassistant/components/sql/sensor.py | 2 +- tests/components/sql/__init__.py | 9 +++++ tests/components/sql/test_config_flow.py | 17 ++++++--- tests/components/sql/test_sensor.py | 46 ++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/sql/sensor.py b/homeassistant/components/sql/sensor.py index 7f76c4f29ad..8609e2c17c2 100644 --- a/homeassistant/components/sql/sensor.py +++ b/homeassistant/components/sql/sensor.py @@ -261,7 +261,7 @@ async def async_setup_sensor( # MSSQL uses TOP and not LIMIT mod_query_template = query_template - if not ("LIMIT" in upper_query or upper_query.startswith("SELECT TOP")): + if "LIMIT" not in upper_query and not upper_query.startswith("SELECT TOP"): if "mssql" in db_url: mod_query_template = ValueTemplate( f"SELECT TOP 1{query_template.template[6:]}", hass diff --git a/tests/components/sql/__init__.py b/tests/components/sql/__init__.py index 2f1ade2d00a..c26e0626e26 100644 --- a/tests/components/sql/__init__.py +++ b/tests/components/sql/__init__.py @@ -38,6 +38,15 @@ ENTRY_CONFIG = { CONF_STATE_CLASS: SensorStateClass.TOTAL, } +ENTRY_CONFIG_BLANK_QUERY = { + CONF_NAME: "Get Value", + CONF_QUERY: " ", + CONF_COLUMN_NAME: "value", + CONF_UNIT_OF_MEASUREMENT: "MiB", + CONF_DEVICE_CLASS: SensorDeviceClass.DATA_SIZE, + CONF_STATE_CLASS: SensorStateClass.TOTAL, +} + ENTRY_CONFIG_WITH_VALUE_TEMPLATE = { CONF_NAME: "Get Value", CONF_QUERY: "SELECT 5 as value", diff --git a/tests/components/sql/test_config_flow.py b/tests/components/sql/test_config_flow.py index 9d4e957db0b..8891e59d8a3 100644 --- a/tests/components/sql/test_config_flow.py +++ b/tests/components/sql/test_config_flow.py @@ -16,6 +16,7 @@ from homeassistant.data_entry_flow import FlowResultType from . import ( ENTRY_CONFIG, + ENTRY_CONFIG_BLANK_QUERY, ENTRY_CONFIG_INVALID_COLUMN_NAME, ENTRY_CONFIG_INVALID_COLUMN_NAME_OPT, ENTRY_CONFIG_INVALID_QUERY, @@ -49,19 +50,27 @@ async def test_form(recorder_mock: Recorder, hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.FORM assert result["errors"] == {} + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + ENTRY_CONFIG_BLANK_QUERY, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"query": "query_invalid"} + with patch( "homeassistant.components.sql.async_setup_entry", return_value=True, ) as mock_setup_entry: - result2 = await hass.config_entries.flow.async_configure( + result = await hass.config_entries.flow.async_configure( result["flow_id"], ENTRY_CONFIG, ) await hass.async_block_till_done() - assert result2["type"] is FlowResultType.CREATE_ENTRY - assert result2["title"] == "Get Value" - assert result2["options"] == { + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "Get Value" + assert result["options"] == { "name": "Get Value", "query": "SELECT 5 as value", "column": "value", diff --git a/tests/components/sql/test_sensor.py b/tests/components/sql/test_sensor.py index 6800fbd58d3..ffb9cee967b 100644 --- a/tests/components/sql/test_sensor.py +++ b/tests/components/sql/test_sensor.py @@ -92,6 +92,33 @@ async def test_query_value_template( assert state.state == "5" +async def test_template_query( + recorder_mock: Recorder, + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, +) -> None: + """Test the SQL sensor with a query template.""" + config = { + "db_url": "sqlite://", + "query": "SELECT {% if states('sensor.input1')=='on' %} 5 {% else %} 6 {% endif %} as value", + "column": "value", + "name": "count_tables", + "value_template": "{{ value | int }}", + } + await init_integration(hass, config) + + state = hass.states.get("sensor.count_tables") + assert state.state == "6" + + hass.states.async_set("sensor.input1", "on") + freezer.tick(timedelta(minutes=1)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + state = hass.states.get("sensor.count_tables") + assert state.state == "5" + + async def test_query_value_template_invalid( recorder_mock: Recorder, hass: HomeAssistant ) -> None: @@ -109,6 +136,25 @@ async def test_query_value_template_invalid( assert state.state == "5.01" +async def test_broken_template_query( + recorder_mock: Recorder, + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, +) -> None: + """Test the SQL sensor with a query template which is broken.""" + config = { + "db_url": "sqlite://", + "query": "SELECT {{ 5 as value", + "column": "value", + "name": "count_tables", + "value_template": "{{ value | int }}", + } + await init_integration(hass, config) + + state = hass.states.get("sensor.count_tables") + assert not state + + async def test_query_limit(recorder_mock: Recorder, hass: HomeAssistant) -> None: """Test the SQL sensor with a query containing 'LIMIT' in lowercase.""" config = {