Case sensitive SQL queries checks (#62752)

This commit is contained in:
Diogo Gomes 2022-01-07 16:19:30 +00:00 committed by GitHub
parent 04796c4410
commit d5d8eefded
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View file

@ -97,7 +97,7 @@ def setup_platform(
value_template.hass = hass value_template.hass = hass
# MSSQL uses TOP and not LIMIT # MSSQL uses TOP and not LIMIT
if not ("LIMIT" in query_str or "SELECT TOP" in query_str): if not ("LIMIT" in query_str.upper() or "SELECT TOP" in query_str.upper()):
query_str = ( query_str = (
query_str.replace("SELECT", "SELECT TOP 1") query_str.replace("SELECT", "SELECT TOP 1")
if "mssql" in db_url if "mssql" in db_url

View file

@ -31,6 +31,30 @@ async def test_query(hass):
assert state.attributes["value"] == 5 assert state.attributes["value"] == 5
async def test_query_limit(hass):
"""Test the SQL sensor with a query containing 'LIMIT' in lowercase."""
config = {
"sensor": {
"platform": "sql",
"db_url": "sqlite://",
"queries": [
{
"name": "count_tables",
"query": "SELECT 5 as value limit 1",
"column": "value",
}
],
}
}
assert await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()
state = hass.states.get("sensor.count_tables")
assert state.state == "5"
assert state.attributes["value"] == 5
async def test_invalid_query(hass): async def test_invalid_query(hass):
"""Test the SQL sensor for invalid queries.""" """Test the SQL sensor for invalid queries."""
with pytest.raises(vol.Invalid): with pytest.raises(vol.Invalid):