From 473afc4837d6e3c268bcccfa897bbf5005c7d885 Mon Sep 17 00:00:00 2001 From: Julian Engelhardt Date: Sat, 3 Oct 2020 03:42:50 +0200 Subject: [PATCH] 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 --- tests/components/sql/test_sensor.py | 94 ++++++++++++----------------- 1 file changed, 40 insertions(+), 54 deletions(-) diff --git a/tests/components/sql/test_sensor.py b/tests/components/sql/test_sensor.py index 8b8bca5e37c..ddab7b1ba36 100644 --- a/tests/components/sql/test_sensor.py +++ b/tests/components/sql/test_sensor.py @@ -1,71 +1,57 @@ """The test for the sql sensor platform.""" -import unittest - import pytest import voluptuous as vol from homeassistant.components.sql.sensor import validate_sql_select from homeassistant.const import STATE_UNKNOWN -from homeassistant.setup import setup_component - -from tests.common import get_test_home_assistant +from homeassistant.setup import async_setup_component -class TestSQLSensor(unittest.TestCase): +async def test_query(hass): """Test the SQL sensor.""" - - def setUp(self): - """Set up things to be run when tests are started.""" - self.hass = get_test_home_assistant() - - def teardown_method(self, method): - """Stop everything that was started.""" - self.hass.stop() - - 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", - } - ], - } + 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) - self.hass.block_till_done() + assert await async_setup_component(hass, "sensor", config) + await hass.async_block_till_done() - state = self.hass.states.get("sensor.count_tables") - assert state.state == "5" - assert state.attributes["value"] == 5 + state = hass.states.get("sensor.count_tables") + assert state.state == "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 = { - "sensor": { - "platform": "sql", - "db_url": "sqlite://", - "queries": [ - { - "name": "count_tables", - "query": "SELECT * value FROM sqlite_master;", - "column": "value", - } - ], - } +async def test_invalid_query(hass): + """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) - self.hass.block_till_done() + assert await async_setup_component(hass, "sensor", config) + await hass.async_block_till_done() - state = self.hass.states.get("sensor.count_tables") - assert state.state == STATE_UNKNOWN + state = hass.states.get("sensor.count_tables") + assert state.state == STATE_UNKNOWN