From f2d6de00baa312c508b517c59acb23211663023f Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Mon, 26 Mar 2018 22:49:52 +0200 Subject: [PATCH] Improve test coverage (#269) --- miio/tests/test_airpurifier.py | 19 +++++++++ miio/tests/test_ceil.py | 28 ++++++++++++- miio/tests/test_chuangmi_ir.json | 7 ++++ miio/tests/test_chuangmi_ir.py | 8 ++++ miio/tests/test_waterpurifier.py | 70 ++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 miio/tests/test_waterpurifier.py diff --git a/miio/tests/test_airpurifier.py b/miio/tests/test_airpurifier.py index 13fb879ba..c3d04e7d2 100644 --- a/miio/tests/test_airpurifier.py +++ b/miio/tests/test_airpurifier.py @@ -246,6 +246,9 @@ def extra_features(): self.device.set_extra_features(2) assert extra_features() == 2 + with pytest.raises(AirPurifierException): + self.device.set_extra_features(-1) + def test_reset_filter(self): def filter_hours_used(): return self.device.status().filter_hours_used @@ -328,3 +331,19 @@ def test_status_filter_rfid_product_ids(self): assert self.state().filter_type is FilterType.Regular self.device.state["rfid_product_id"] = '0:0:41:30' assert self.state().filter_type is FilterType.AntiBacterial + + def test_status_without_sleep_mode(self): + self.device._reset_state() + self.device.state["sleep_mode"] = None + assert self.state().sleep_mode is None + + def test_status_without_app_extra(self): + self.device._reset_state() + self.device.state["app_extra"] = None + assert self.state().extra_features is None + assert self.state().turbo_mode_supported is None + + def test_status_without_auto_detect(self): + self.device._reset_state() + self.device.state["act_det"] = None + assert self.state().auto_detect is None diff --git a/miio/tests/test_ceil.py b/miio/tests/test_ceil.py index e8b66dea4..d8f6de894 100644 --- a/miio/tests/test_ceil.py +++ b/miio/tests/test_ceil.py @@ -85,6 +85,12 @@ def brightness(): self.device.set_brightness(20) assert brightness() == 20 + with pytest.raises(CeilException): + self.device.set_brightness(-1) + + with pytest.raises(CeilException): + self.device.set_brightness(101) + def test_set_color_temperature(self): def color_temperature(): return self.device.status().color_temperature @@ -94,6 +100,12 @@ def color_temperature(): self.device.set_color_temperature(20) assert color_temperature() == 20 + with pytest.raises(CeilException): + self.device.set_color_temperature(-1) + + with pytest.raises(CeilException): + self.device.set_color_temperature(101) + def test_set_brightness_and_color_temperature(self): def color_temperature(): return self.device.status().color_temperature @@ -138,14 +150,26 @@ def delay_off_countdown(): self.device.delay_off(200) assert delay_off_countdown() == 200 + with pytest.raises(CeilException): + self.device.delay_off(0) + + with pytest.raises(CeilException): + self.device.delay_off(-1) + def test_set_scene(self): def scene(): return self.device.status().scene self.device.set_scene(1) assert scene() == 1 - self.device.set_scene(2) - assert scene() == 2 + self.device.set_scene(4) + assert scene() == 4 + + with pytest.raises(CeilException): + self.device.set_scene(0) + + with pytest.raises(CeilException): + self.device.set_scene(5) def test_smart_night_light_on(self): def smart_night_light(): diff --git a/miio/tests/test_chuangmi_ir.json b/miio/tests/test_chuangmi_ir.json index cd4995533..3b85acf57 100644 --- a/miio/tests/test_chuangmi_ir.json +++ b/miio/tests/test_chuangmi_ir.json @@ -103,6 +103,13 @@ "0000006C00220002015B00AD001600160016001600160016001600160016001600160016001600160016001600160041001600410016004100160041001600410016004100160041001600160016001600160041001600160016004100160016001600160016001600160016001600410016001600160041001600160016004100160041001600410016004100160622015B005700160E6C", -1 ] + }, + { + "desc": "Invalid pronto command", + "in": [ + "FFFFFFFFFFFF", + 0 + ] } ] } \ No newline at end of file diff --git a/miio/tests/test_chuangmi_ir.py b/miio/tests/test_chuangmi_ir.py index e3e0498eb..6b6105685 100644 --- a/miio/tests/test_chuangmi_ir.py +++ b/miio/tests/test_chuangmi_ir.py @@ -125,3 +125,11 @@ def test_play_with_type(self): self.device.state['last_ir_played'], args['out'] ) + with pytest.raises(ChuangmiIrException): + self.device.play('invalid:command') + + with pytest.raises(ChuangmiIrException): + self.device.play('pronto:command:invalid:argument:count') + + with pytest.raises(ChuangmiIrException): + self.device.play('pronto:command:invalidargument') diff --git a/miio/tests/test_waterpurifier.py b/miio/tests/test_waterpurifier.py new file mode 100644 index 000000000..3467859a5 --- /dev/null +++ b/miio/tests/test_waterpurifier.py @@ -0,0 +1,70 @@ +from unittest import TestCase +from miio import WaterPurifier +from miio.waterpurifier import WaterPurifierStatus +from .dummies import DummyDevice +import pytest + + +class DummyWaterPurifier(DummyDevice, WaterPurifier): + def __init__(self, *args, **kwargs): + self.state = { + 'power': 'on', + 'mode': 'unknown', + 'tds': 'unknown', + 'filter1_life': -1, + 'filter1_state': -1, + 'filter_life': -1, + 'filter_state': -1, + 'life': -1, + 'state': -1, + 'level': 'unknown', + 'volume': 'unknown', + 'filter': 'unknown', + 'usage': 'unknown', + 'temperature': 'unknown', + 'uv_life': -1, + 'uv_state': -1, + 'elecval_state': 'unknown' + + } + self.return_values = { + 'get_prop': self._get_state, + 'set_power': lambda x: self._set_state("power", x), + } + super().__init__(args, kwargs) + + +@pytest.fixture(scope="class") +def waterpurifier(request): + request.cls.device = DummyWaterPurifier() + # TODO add ability to test on a real device + + +@pytest.mark.usefixtures("waterpurifier") +class TestWaterPurifier(TestCase): + def is_on(self): + return self.device.status().is_on + + def state(self): + return self.device.status() + + def test_on(self): + self.device.off() # ensure off + assert self.is_on() is False + + self.device.on() + assert self.is_on() is True + + def test_off(self): + self.device.on() # ensure on + assert self.is_on() is True + + self.device.off() + assert self.is_on() is False + + def test_status(self): + self.device._reset_state() + + assert repr(self.state()) == repr(WaterPurifierStatus(self.device.start_state)) + + assert self.is_on() is True