Skip to content

Commit

Permalink
Unit tests for the Chuang Mi Plug V1. First draft.
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi committed Nov 27, 2017
1 parent 5ba379e commit 5310663
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
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
80 changes: 80 additions & 0 deletions miio/tests/test_plug_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from unittest import TestCase
from miio import PlugV1
import pytest


class DummyPlugV1(PlugV1):
def __init__(self, *args, **kwargs):
self.state = {
'power': True,
'usb_on': True,
'temperature': 32,
}
self.return_values = {
'get_prop': self._get_state,
'set_on': lambda: self._set_state("power", True),
'set_off': lambda: self._set_state("power", False),
'set_usb_on': lambda: self._set_state("usb_on", True),
'set_usb_off': lambda: 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`."""
if parameters is None:
return self.return_values[command]()

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,
the value is expected to be an array with length of 1."""
# print("setting %s = %s" % (var, value))
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"]

0 comments on commit 5310663

Please sign in to comment.