More tests

This commit is contained in:
G Johansson
2025-08-08 16:58:48 +00:00
parent dd202a228d
commit 1f550f3e22
4 changed files with 69 additions and 5 deletions

View File

@@ -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

View File

@@ -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",

View File

@@ -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",

View File

@@ -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 = {