mirror of
https://github.com/home-assistant/core.git
synced 2025-09-03 11:51:40 +02:00
More tests
This commit is contained in:
@@ -261,7 +261,7 @@ async def async_setup_sensor(
|
|||||||
|
|
||||||
# MSSQL uses TOP and not LIMIT
|
# MSSQL uses TOP and not LIMIT
|
||||||
mod_query_template = query_template
|
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:
|
if "mssql" in db_url:
|
||||||
mod_query_template = ValueTemplate(
|
mod_query_template = ValueTemplate(
|
||||||
f"SELECT TOP 1{query_template.template[6:]}", hass
|
f"SELECT TOP 1{query_template.template[6:]}", hass
|
||||||
|
@@ -38,6 +38,15 @@ ENTRY_CONFIG = {
|
|||||||
CONF_STATE_CLASS: SensorStateClass.TOTAL,
|
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 = {
|
ENTRY_CONFIG_WITH_VALUE_TEMPLATE = {
|
||||||
CONF_NAME: "Get Value",
|
CONF_NAME: "Get Value",
|
||||||
CONF_QUERY: "SELECT 5 as value",
|
CONF_QUERY: "SELECT 5 as value",
|
||||||
|
@@ -16,6 +16,7 @@ from homeassistant.data_entry_flow import FlowResultType
|
|||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
ENTRY_CONFIG,
|
ENTRY_CONFIG,
|
||||||
|
ENTRY_CONFIG_BLANK_QUERY,
|
||||||
ENTRY_CONFIG_INVALID_COLUMN_NAME,
|
ENTRY_CONFIG_INVALID_COLUMN_NAME,
|
||||||
ENTRY_CONFIG_INVALID_COLUMN_NAME_OPT,
|
ENTRY_CONFIG_INVALID_COLUMN_NAME_OPT,
|
||||||
ENTRY_CONFIG_INVALID_QUERY,
|
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["type"] is FlowResultType.FORM
|
||||||
assert result["errors"] == {}
|
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(
|
with patch(
|
||||||
"homeassistant.components.sql.async_setup_entry",
|
"homeassistant.components.sql.async_setup_entry",
|
||||||
return_value=True,
|
return_value=True,
|
||||||
) as mock_setup_entry:
|
) as mock_setup_entry:
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
ENTRY_CONFIG,
|
ENTRY_CONFIG,
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result2["title"] == "Get Value"
|
assert result["title"] == "Get Value"
|
||||||
assert result2["options"] == {
|
assert result["options"] == {
|
||||||
"name": "Get Value",
|
"name": "Get Value",
|
||||||
"query": "SELECT 5 as value",
|
"query": "SELECT 5 as value",
|
||||||
"column": "value",
|
"column": "value",
|
||||||
|
@@ -92,6 +92,33 @@ async def test_query_value_template(
|
|||||||
assert state.state == "5"
|
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(
|
async def test_query_value_template_invalid(
|
||||||
recorder_mock: Recorder, hass: HomeAssistant
|
recorder_mock: Recorder, hass: HomeAssistant
|
||||||
) -> None:
|
) -> None:
|
||||||
@@ -109,6 +136,25 @@ async def test_query_value_template_invalid(
|
|||||||
assert state.state == "5.01"
|
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:
|
async def test_query_limit(recorder_mock: Recorder, hass: HomeAssistant) -> None:
|
||||||
"""Test the SQL sensor with a query containing 'LIMIT' in lowercase."""
|
"""Test the SQL sensor with a query containing 'LIMIT' in lowercase."""
|
||||||
config = {
|
config = {
|
||||||
|
Reference in New Issue
Block a user