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 Chuang Mi Plug V1 #137

Merged
merged 4 commits 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
1 change: 1 addition & 0 deletions miio/plug_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class PlugV1Status:
"""Container for status reports from the plug."""

def __init__(self, data: Dict[str, Any]) -> None:
# { 'power': True, 'usb_on': True, 'temperature': 32 }
self.data = data

@property
Expand Down
89 changes: 89 additions & 0 deletions miio/tests/test_plug_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from unittest import TestCase
from miio import PlugV1
import pytest


class DummyPlugV1(PlugV1):
def __init__(self, *args, **kwargs):
self.state = {
'on': True,
'usb_on': True,
'temperature': 32,
}
self.return_values = {
'get_prop': self._get_state,
'set_on': lambda x: self._set_state("on", True),
'set_off': lambda x: self._set_state("on", False),
'set_usb_on': lambda x: self._set_state("usb_on", True),
'set_usb_off': lambda x: self._set_state("usb_on", False),
}
self.start_state = self.state.copy()

def send(self, command: str, parameters=None, retry_count=3):
"""Overridden send() to return values from `self.return_values`."""
return self.return_values[command](parameters)

def _reset_state(self):
"""Revert back to the original state."""
self.state = self.start_state.copy()

def _set_state(self, var, value):
"""Set a state of a variable"""
self.state[var] = value

def _get_state(self, props):
"""Return wanted properties"""
return [self.state[x] for x in props if x in self.state]


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


@pytest.mark.usefixtures("plugv1")
class TestPlugV1(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().usb_power is True
assert self.state().temperature == self.device.start_state[
"temperature"]

def test_usb_on(self):
self.device.usb_off() # ensure off
assert self.device.status().usb_power is False

self.device.usb_on()
assert self.device.status().usb_power is True

def test_usb_off(self):
self.device.usb_on() # ensure on
assert self.device.status().usb_power is True

self.device.usb_off()
assert self.device.status().usb_power is False