Rewrite SQL tests to pytest style (#41016)

* Remove unittests and common mock from SQL component tests

* Switch to async versions of setup_component and block_till_done
This commit is contained in:
Julian Engelhardt 2020-10-03 03:42:50 +02:00 committed by GitHub
parent 9611e5223a
commit 473afc4837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,71 +1,57 @@
"""The test for the sql sensor platform.""" """The test for the sql sensor platform."""
import unittest
import pytest import pytest
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sql.sensor import validate_sql_select from homeassistant.components.sql.sensor import validate_sql_select
from homeassistant.const import STATE_UNKNOWN from homeassistant.const import STATE_UNKNOWN
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant
class TestSQLSensor(unittest.TestCase): async def test_query(hass):
"""Test the SQL sensor.""" """Test the SQL sensor."""
config = {
def setUp(self): "sensor": {
"""Set up things to be run when tests are started.""" "platform": "sql",
self.hass = get_test_home_assistant() "db_url": "sqlite://",
"queries": [
def teardown_method(self, method): {
"""Stop everything that was started.""" "name": "count_tables",
self.hass.stop() "query": "SELECT 5 as value",
"column": "value",
def test_query(self): }
"""Test the SQL sensor.""" ],
config = {
"sensor": {
"platform": "sql",
"db_url": "sqlite://",
"queries": [
{
"name": "count_tables",
"query": "SELECT 5 as value",
"column": "value",
}
],
}
} }
}
assert setup_component(self.hass, "sensor", config) assert await async_setup_component(hass, "sensor", config)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("sensor.count_tables") state = hass.states.get("sensor.count_tables")
assert state.state == "5" assert state.state == "5"
assert state.attributes["value"] == 5 assert state.attributes["value"] == 5
def test_invalid_query(self):
"""Test the SQL sensor for invalid queries."""
with pytest.raises(vol.Invalid):
validate_sql_select("DROP TABLE *")
config = { async def test_invalid_query(hass):
"sensor": { """Test the SQL sensor for invalid queries."""
"platform": "sql", with pytest.raises(vol.Invalid):
"db_url": "sqlite://", validate_sql_select("DROP TABLE *")
"queries": [
{ config = {
"name": "count_tables", "sensor": {
"query": "SELECT * value FROM sqlite_master;", "platform": "sql",
"column": "value", "db_url": "sqlite://",
} "queries": [
], {
} "name": "count_tables",
"query": "SELECT * value FROM sqlite_master;",
"column": "value",
}
],
} }
}
assert setup_component(self.hass, "sensor", config) assert await async_setup_component(hass, "sensor", config)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("sensor.count_tables") state = hass.states.get("sensor.count_tables")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN