Support for queries with no results (fix for #12856) (#12888)

* Addresses issue #12856

* error -> warning

* added edge case and test

* uff uff

* Added SELECT validation

* Improved tests
This commit is contained in:
Diogo Gomes 2018-03-06 03:44:04 +00:00 committed by Paulus Schoutsen
parent 38af04c6ce
commit 5063464d5e
2 changed files with 44 additions and 8 deletions

View file

@ -1,7 +1,11 @@
"""The test for the sql sensor platform."""
import unittest
import pytest
import voluptuous as vol
from homeassistant.components.sensor.sql import validate_sql_select
from homeassistant.setup import setup_component
from homeassistant.const import STATE_UNKNOWN
from tests.common import get_test_home_assistant
@ -35,3 +39,25 @@ class TestSQLSensor(unittest.TestCase):
state = self.hass.states.get('sensor.count_tables')
self.assertEqual(state.state, '0')
def test_invalid_query(self):
"""Test the SQL sensor for invalid queries."""
with pytest.raises(vol.Invalid):
validate_sql_select("DROP TABLE *")
config = {
'sensor': {
'platform': 'sql',
'db_url': 'sqlite://',
'queries': [{
'name': 'count_tables',
'query': 'SELECT * value FROM sqlite_master;',
'column': 'value',
}]
}
}
assert setup_component(self.hass, 'sensor', config)
state = self.hass.states.get('sensor.count_tables')
self.assertEqual(state.state, STATE_UNKNOWN)