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 Quality Monitor #135

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/airqualitymonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class AirQualityMonitorStatus:
"""Container of air quality monitor status."""
def __init__(self, data):
# ['power': 'on', 'aqi': 34, 'battery': 0, 'usb_state': 'on']
# {'power': 'on', 'aqi': 34, 'battery': 100, 'usb_state': 'off'}
self.data = data

@property
Expand Down
58 changes: 58 additions & 0 deletions miio/tests/test_airqualitymonitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from unittest import TestCase
from miio import AirQualityMonitor
from .dummies import DummyDevice
import pytest


class DummyAirQualityMonitor(DummyDevice, AirQualityMonitor):
def __init__(self, *args, **kwargs):
self.state = {
'power': 'on',
'aqi': 34,
'battery': 100,
'usb_state': 'off'
}
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 airqualitymonitor(request):
request.cls.device = DummyAirQualityMonitor()
# TODO add ability to test on a real device


@pytest.mark.usefixtures("airqualitymonitor")
class TestAirQualityMonitor(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().aqi == self.device.start_state["aqi"]
assert self.state().battery == self.device.start_state["battery"]
assert self.state().usb_power == (self.device.start_state["usb_state"] == 'on')

Choose a reason for hiding this comment

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

line too long (87 > 79 characters)