Skip to content

Commit

Permalink
Improve test coverage (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Mar 26, 2018
1 parent e307486 commit f2d6de0
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 2 deletions.
19 changes: 19 additions & 0 deletions miio/tests/test_airpurifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
28 changes: 26 additions & 2 deletions miio/tests/test_ceil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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():
Expand Down
7 changes: 7 additions & 0 deletions miio/tests/test_chuangmi_ir.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@
"0000006C00220002015B00AD001600160016001600160016001600160016001600160016001600160016001600160041001600410016004100160041001600410016004100160041001600160016001600160041001600160016004100160016001600160016001600160016001600410016001600160041001600160016004100160041001600410016004100160622015B005700160E6C",
-1
]
},
{
"desc": "Invalid pronto command",
"in": [
"FFFFFFFFFFFF",
0
]
}
]
}
8 changes: 8 additions & 0 deletions miio/tests/test_chuangmi_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
70 changes: 70 additions & 0 deletions miio/tests/test_waterpurifier.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f2d6de0

Please sign in to comment.