make tests to test against all known device variants

This commit is contained in:
Teemu Rytilahti 2017-08-05 17:22:36 +02:00
parent 1d7e5fa73c
commit 675f618777
2 changed files with 66 additions and 32 deletions

View File

@ -3,7 +3,7 @@ from voluptuous import Schema, Invalid, All, Range
from functools import partial from functools import partial
from .. import SmartBulb, SmartDeviceException from .. import SmartBulb, SmartDeviceException
from .fakes import FakeTransportProtocol, sysinfo_lb130 from .fakes import FakeTransportProtocol, sysinfo_lb130, sysinfo_lb110
BULB_IP = '192.168.250.186' BULB_IP = '192.168.250.186'
SKIP_STATE_TESTS = False SKIP_STATE_TESTS = False
@ -23,6 +23,7 @@ def check_mode(x):
class TestSmartBulb(TestCase): class TestSmartBulb(TestCase):
SYSINFO = sysinfo_lb130
# these schemas should go to the mainlib as # these schemas should go to the mainlib as
# they can be useful when adding support for new features/devices # they can be useful when adding support for new features/devices
# as well as to check that faked devices are operating properly. # as well as to check that faked devices are operating properly.
@ -80,7 +81,7 @@ class TestSmartBulb(TestCase):
def setUp(self): def setUp(self):
self.bulb = SmartBulb(BULB_IP, self.bulb = SmartBulb(BULB_IP,
protocol=FakeTransportProtocol(sysinfo_lb130)) protocol=FakeTransportProtocol(self.SYSINFO))
def tearDown(self): def tearDown(self):
self.bulb = None self.bulb = None
@ -91,7 +92,7 @@ class TestSmartBulb(TestCase):
def test_initialize_invalid_connection(self): def test_initialize_invalid_connection(self):
bulb = SmartBulb('127.0.0.1', bulb = SmartBulb('127.0.0.1',
protocol=FakeTransportProtocol(sysinfo_lb130, protocol=FakeTransportProtocol(self.SYSINFO,
invalid=True)) invalid=True))
with self.assertRaises(SmartDeviceException): with self.assertRaises(SmartDeviceException):
bulb.sys_info['model'] bulb.sys_info['model']
@ -187,3 +188,7 @@ class TestSmartBulb(TestCase):
def test_rssi(self): def test_rssi(self):
self.sysinfo_schema({'rssi': self.bulb.rssi}) # wrapping for vol self.sysinfo_schema({'rssi': self.bulb.rssi}) # wrapping for vol
class TestSmartBulbLB110(TestSmartBulb):
SYSINFO = sysinfo_lb110

View File

@ -1,11 +1,14 @@
from unittest import TestCase, skip, skipIf from unittest import TestCase, skip, skipIf
from voluptuous import Schema, Invalid, All, Range from voluptuous import Schema, Invalid, All, Any, Range
from functools import partial from functools import partial
import datetime import datetime
import re import re
from .. import SmartPlug, SmartDeviceException from .. import SmartPlug, SmartDeviceException
from .fakes import FakeTransportProtocol, sysinfo_hs110, sysinfo_hs105 from .fakes import (FakeTransportProtocol,
sysinfo_hs110,
sysinfo_hs105,
sysinfo_hs200)
PLUG_IP = '192.168.250.186' PLUG_IP = '192.168.250.186'
SKIP_STATE_TESTS = False SKIP_STATE_TESTS = False
@ -24,13 +27,14 @@ def check_mac(x):
def check_mode(x): def check_mode(x):
if x in ['schedule']: if x in ['schedule', 'none']:
return x return x
raise Invalid("invalid mode {}".format(x)) raise Invalid("invalid mode {}".format(x))
class TestSmartPlug(TestCase): class TestSmartPlugHS110(TestCase):
SYSINFO = sysinfo_hs110
# these schemas should go to the mainlib as # these schemas should go to the mainlib as
# they can be useful when adding support for new features/devices # they can be useful when adding support for new features/devices
# as well as to check that faked devices are operating properly. # as well as to check that faked devices are operating properly.
@ -44,26 +48,29 @@ class TestSmartPlug(TestCase):
'hwId': str, 'hwId': str,
'hw_ver': str, 'hw_ver': str,
'icon_hash': str, 'icon_hash': str,
'latitude': All(float, Range(min=-90, max=90)),
'led_off': check_int_bool, 'led_off': check_int_bool,
'longitude': All(float, Range(min=-180, max=180)), 'latitude': Any(All(float, Range(min=-90, max=90)), None),
'latitude_i': Any(All(float, Range(min=-90, max=90)), None),
'longitude': Any(All(float, Range(min=-180, max=180)), None),
'longitude_i': Any(All(float, Range(min=-180, max=180)), None),
'mac': check_mac, 'mac': check_mac,
'model': str, 'model': str,
'oemId': str, 'oemId': str,
'on_time': int, 'on_time': int,
'relay_state': int, 'relay_state': int,
'rssi': All(int, Range(max=0)), 'rssi': int, # apparently rssi can also be positive, see #54
'sw_ver': str, 'sw_ver': str,
'type': str, 'type': str,
'mic_type': str,
'updating': check_int_bool, 'updating': check_int_bool,
}) })
current_consumption_schema = Schema({ current_consumption_schema = Schema(Any({
'voltage': All(float, Range(min=0, max=300)), 'voltage': All(float, Range(min=0, max=300)),
'power': All(float, Range(min=0)), 'power': All(float, Range(min=0)),
'total': All(float, Range(min=0)), 'total': All(float, Range(min=0)),
'current': All(float, Range(min=0)), 'current': All(float, Range(min=0)),
}) }, None))
tz_schema = Schema({ tz_schema = Schema({
'zone_str': str, 'zone_str': str,
@ -74,7 +81,7 @@ class TestSmartPlug(TestCase):
def setUp(self): def setUp(self):
self.plug = SmartPlug(PLUG_IP, self.plug = SmartPlug(PLUG_IP,
protocol=FakeTransportProtocol(sysinfo_hs110)) protocol=FakeTransportProtocol(self.SYSINFO))
def tearDown(self): def tearDown(self):
self.plug = None self.plug = None
@ -85,7 +92,7 @@ class TestSmartPlug(TestCase):
def test_initialize_invalid_connection(self): def test_initialize_invalid_connection(self):
plug = SmartPlug('127.0.0.1', plug = SmartPlug('127.0.0.1',
protocol=FakeTransportProtocol(sysinfo_hs110, protocol=FakeTransportProtocol(self.SYSINFO,
invalid=True)) invalid=True))
with self.assertRaises(SmartDeviceException): with self.assertRaises(SmartDeviceException):
plug.sys_info['model'] plug.sys_info['model']
@ -152,31 +159,45 @@ class TestSmartPlug(TestCase):
self.assertFalse(self.plug.has_emeter) self.assertFalse(self.plug.has_emeter)
def test_get_emeter_realtime(self): def test_get_emeter_realtime(self):
self.current_consumption_schema((self.plug.get_emeter_realtime())) if self.plug.has_emeter:
self.current_consumption_schema((self.plug.get_emeter_realtime()))
else:
self.assertEqual(self.plug.get_emeter_realtime(), None)
def test_get_emeter_daily(self): def test_get_emeter_daily(self):
self.assertEqual(self.plug.get_emeter_daily(year=1900, month=1), {}) if self.plug.has_emeter:
self.assertEqual(self.plug.get_emeter_daily(year=1900, month=1),
{})
k, v = self.plug.get_emeter_daily().popitem() k, v = self.plug.get_emeter_daily().popitem()
self.assertTrue(isinstance(k, int)) self.assertTrue(isinstance(k, int))
self.assertTrue(isinstance(v, float)) self.assertTrue(isinstance(v, float))
else:
self.assertEqual(self.plug.get_emeter_daily(year=1900, month=1),
None)
def test_get_emeter_monthly(self): def test_get_emeter_monthly(self):
self.assertEqual(self.plug.get_emeter_monthly(year=1900), {}) if self.plug.has_emeter:
self.assertEqual(self.plug.get_emeter_monthly(year=1900), {})
d = self.plug.get_emeter_monthly() d = self.plug.get_emeter_monthly()
k, v = d.popitem() k, v = d.popitem()
self.assertTrue(isinstance(k, int)) self.assertTrue(isinstance(k, int))
self.assertTrue(isinstance(v, float)) self.assertTrue(isinstance(v, float))
else:
self.assertEqual(self.plug.get_emeter_monthly(year=1900), None)
@skip("not clearing your stats..") @skip("not clearing your stats..")
def test_erase_emeter_stats(self): def test_erase_emeter_stats(self):
self.fail() self.fail()
def test_current_consumption(self): def test_current_consumption(self):
x = self.plug.current_consumption() if self.plug.has_emeter:
self.assertTrue(isinstance(x, float)) x = self.plug.current_consumption()
self.assertTrue(x >= 0.0) self.assertTrue(isinstance(x, float))
self.assertTrue(x >= 0.0)
else:
self.assertEqual(self.plug.current_consumption(), None)
def test_identify(self): def test_identify(self):
ident = self.plug.identify() ident = self.plug.identify()
@ -221,14 +242,22 @@ class TestSmartPlug(TestCase):
def test_location(self): def test_location(self):
self.sysinfo_schema(self.plug.location) self.sysinfo_schema(self.plug.location)
def test_location_i(self):
plug_i = SmartPlug(PLUG_IP,
protocol=FakeTransportProtocol(sysinfo_hs105))
self.sysinfo_schema(plug_i.location)
def test_rssi(self): def test_rssi(self):
self.sysinfo_schema({'rssi': self.plug.rssi}) # wrapping for vol self.sysinfo_schema({'rssi': self.plug.rssi}) # wrapping for vol
def test_mac(self): def test_mac(self):
self.sysinfo_schema({'mac': self.plug.mac}) # wrapping for val self.sysinfo_schema({'mac': self.plug.mac}) # wrapping for val
# TODO check setting? # TODO check setting?
class TestSmartPlugHS200(TestSmartPlugHS110):
SYSINFO = sysinfo_hs200
class TestSmartPlugHS105(TestSmartPlugHS110):
SYSINFO = sysinfo_hs105
def test_location_i(self):
plug_i = SmartPlug(PLUG_IP,
protocol=FakeTransportProtocol(self.SYSINFO))
self.sysinfo_schema(plug_i.location)