Просмотр исходного кода

Fill in more gaps in the unit tests.

Jason Rumney 4 лет назад
Родитель
Сommit
ab0fe9b817
3 измененных файлов с 85 добавлено и 3 удалено
  1. 1 0
      tests/devices/test_anko_fan.py
  2. 51 3
      tests/test_config_flow.py
  3. 33 0
      tests/test_device_config.py

+ 1 - 0
tests/devices/test_anko_fan.py

@@ -149,6 +149,7 @@ class TestAnkoFan(IsolatedAsyncioTestCase):
 
     def test_speed_step(self):
         self.assertEqual(self.subject.percentage_step, 12.5)
+        self.assertEqual(self.subject.speed_count, 8)
 
     async def test_set_speed_in_normal_mode(self):
         self.dps[PRESET_DPS] = "normal"

+ 51 - 3
tests/test_config_flow.py

@@ -7,10 +7,17 @@ from pytest_homeassistant_custom_component.common import MockConfigEntry
 
 import voluptuous as vol
 
-from custom_components.tuya_local import config_flow, async_migrate_entry
+from custom_components.tuya_local import (
+    config_flow,
+    async_migrate_entry,
+    async_setup_entry,
+)
 from custom_components.tuya_local.const import (
     CONF_CLIMATE,
     CONF_DEVICE_ID,
+    CONF_FAN,
+    CONF_HUMIDIFIER,
+    CONF_LIGHT,
     CONF_LOCAL_KEY,
     CONF_LOCK,
     CONF_SWITCH,
@@ -52,8 +59,8 @@ async def test_init_entry(hass):
     entry.add_to_hass(hass)
     await hass.config_entries.async_setup(entry.entry_id)
     await hass.async_block_till_done()
-    state = hass.states.get("climate.test")
-    assert state
+    assert hass.states.get("climate.test")
+    assert hass.states.get("lock.test")
 
 
 @patch("custom_components.tuya_local.setup_device")
@@ -463,3 +470,44 @@ async def test_options_flow_fails_when_config_is_missing(mock_test, hass):
     result = await hass.config_entries.options.async_init(config_entry.entry_id)
     assert result["type"] == "abort"
     assert result["reason"] == "not_supported"
+
+
+# More tests to exercise code branches that earlier tests missed.
+@patch("custom_components.tuya_local.setup_device")
+async def test_async_setup_entry_for_dehumidifier(mock_setup, hass):
+    """Test setting up based on a config entry.  Repeats test_init_entry."""
+    config_entry = MockConfigEntry(
+        domain=DOMAIN,
+        unique_id="uniqueid",
+        data={
+            CONF_CLIMATE: False,
+            CONF_DEVICE_ID: "deviceid",
+            CONF_FAN: True,
+            CONF_HOST: "hostname",
+            CONF_HUMIDIFIER: True,
+            CONF_LIGHT: True,
+            CONF_LOCK: False,
+            CONF_LOCAL_KEY: "localkey",
+            CONF_NAME: "test",
+            CONF_TYPE: "dehumidifier",
+        },
+    )
+    assert await async_setup_entry(hass, config_entry)
+
+
+@patch("custom_components.tuya_local.setup_device")
+async def test_async_setup_entry_for_switch(mock_device, hass):
+    """Test setting up based on a config entry.  Repeats test_init_entry."""
+    config_entry = MockConfigEntry(
+        domain=DOMAIN,
+        unique_id="uniqueid",
+        data={
+            CONF_DEVICE_ID: "deviceid",
+            CONF_HOST: "hostname",
+            CONF_LOCAL_KEY: "localkey",
+            CONF_NAME: "test",
+            CONF_SWITCH: True,
+            CONF_TYPE: "kogan_switch",
+        },
+    )
+    assert await async_setup_entry(hass, config_entry)

+ 33 - 0
tests/test_device_config.py

@@ -58,6 +58,39 @@ class TestDeviceConfig(unittest.TestCase):
             self.assertIsNotNone(parsed.legacy_type)
             self.assertIsNotNone(parsed.primary_entity)
 
+    # Most of the device_config functionality is exercised during testing of
+    # the various supported devices.  These tests concentrate only on the gaps.
+
+    def test_match_quality(self):
+        """Test the match_quality function."""
+        cfg = config_for_legacy_use("deta_fan")
+        q = cfg.match_quality({**KOGAN_HEATER_PAYLOAD, "updated_at": 0})
+        self.assertEqual(q, 0)
+        q = cfg.match_quality({**GPPH_HEATER_PAYLOAD})
+        self.assertEqual(q, 0)
+
+    def test_entity_find_unknown_dps_fails(self):
+        """Test that finding a dps that doesn't exist fails."""
+        cfg = config_for_legacy_use("kogan_switch")
+        non_existing = cfg.primary_entity.find_dps("missing")
+        self.assertIsNone(non_existing)
+
+    async def test_dps_async_set_readonly_value_fails(self):
+        """Test that setting a readonly dps fails."""
+        mock_device = MagicMock()
+        cfg = config_for_legacy_use("kogan_switch")
+        voltage = cfg.primary_entity.find_dps("voltage_v")
+        with self.assertRaises(TypeError):
+            await voltage.async_set_value(mock_device, 230)
+
+    async def test_dps_values_returns_none_with_no_mapping(self):
+        """Test that a dps with no mapping returns None as its possible values"""
+        cfg = config_for_legacy_use("kogan_switch")
+        voltage = cfg.primary_entity.find_dps("voltage_v")
+        self.assertIsNone(voltage.values)
+
+    # Test detection of all devices.
+
     def _test_detect(self, payload, legacy_type, legacy_class):
         """Test that payload is detected as the correct type and class."""
         matched = False