Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for the Xiaomi Air Humidifier #134

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion miio/airhumidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def status(self) -> AirHumidifierStatus:
"""Retrieve properties."""

properties = ['power', 'mode', 'temp_dec', 'humidity', 'buzzer',
'led_b', ]
'led_b', 'led', ]

values = self.send(
"get_prop",
Expand Down
127 changes: 127 additions & 0 deletions miio/tests/test_airhumidifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from unittest import TestCase
from miio import AirHumidifier
from miio.airhumidifier import OperationMode, LedBrightness
from .dummies import DummyDevice
import pytest


class DummyAirHumidifier(DummyDevice, AirHumidifier):
def __init__(self, *args, **kwargs):
self.state = {
'power': 'on',
'mode': 'medium',
'temp_dec': 186,
'humidity': 62,
'buzzer': 'off',
'led': 'off',
'led_b': 2,
}
self.return_values = {
'get_prop': self._get_state,
'set_power': lambda x: self._set_state("power", x),
'set_mode': lambda x: self._set_state("mode", x),
'set_led': lambda x: self._set_state("led", x),
'set_led_b': lambda x: self._set_state("led_b", x),
'set_buzzer': lambda x: self._set_state("buzzer", x),
}
super().__init__(args, kwargs)


@pytest.fixture(scope="class")
def airhumidifier(request):
request.cls.device = DummyAirHumidifier()
# TODO add ability to test on a real device


@pytest.mark.usefixtures("airhumidifier")
class TestAirHumidifier(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

start_state = self.is_on()
assert start_state 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 self.is_on() is True
assert self.state().temperature == self.device.start_state["temp_dec"] / 10.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (85 > 79 characters)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (85 > 79 characters)

assert self.state().humidity == self.device.start_state["humidity"]
assert self.state().mode == OperationMode(self.device.start_state["mode"])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (82 > 79 characters)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (82 > 79 characters)

assert self.state().led == (self.device.start_state["led"] == 'on')
assert self.state().led_brightness == LedBrightness(self.device.start_state["led_b"])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (93 > 79 characters)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (93 > 79 characters)

assert self.state().buzzer == (self.device.start_state["buzzer"] == 'on')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (81 > 79 characters)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (81 > 79 characters)


def test_set_mode(self):
def mode():
return self.device.status().mode

self.device.set_mode(OperationMode.Silent)
assert mode() == OperationMode.Silent

self.device.set_mode(OperationMode.Medium)
assert mode() == OperationMode.Medium

self.device.set_mode(OperationMode.High)
assert mode() == OperationMode.High

def test_set_led_brightness(self):
def led_brightness():
return self.device.status().led_brightness

self.device.set_led_brightness(LedBrightness.Bright)
assert led_brightness() == LedBrightness.Bright

self.device.set_led_brightness(LedBrightness.Dim)
assert led_brightness() == LedBrightness.Dim

self.device.set_led_brightness(LedBrightness.Off)
assert led_brightness() == LedBrightness.Off

def test_set_led(self):
def led():
return self.device.status().led

self.device.set_led(True)
assert led() is True

self.device.set_led(False)
assert led() is False

def test_set_buzzer(self):
def buzzer():
return self.device.status().buzzer

self.device.set_buzzer(True)
assert buzzer() is True

self.device.set_buzzer(False)
assert buzzer() is False

def test_status_without_temperature(self):
self.device._reset_state()
self.device.state["temp_dec"] = None

assert self.state().temperature is None

def test_status_without_led_brightness(self):
self.device._reset_state()
self.device.state["led_b"] = None

assert self.state().led_brightness is None