250 řádky
9.9 KiB
Python
250 řádky
9.9 KiB
Python
import colored
|
|
import inspect
|
|
from __simulation__ import devices
|
|
import time
|
|
|
|
|
|
DT_TOGGLE = 0.3
|
|
|
|
|
|
TEST_FULL = 'full'
|
|
TEST_SMOKE = 'smoke'
|
|
#
|
|
COLOR_SUCCESS = colored.fg("light_green")
|
|
COLOR_FAIL = colored.fg("light_red")
|
|
|
|
|
|
class test_smarthome(object):
|
|
def __init__(self, rooms):
|
|
# add testcases by room objects
|
|
for name in rooms.getmembers():
|
|
obj = rooms.getobjbyname(name)
|
|
if obj.__class__.__name__ == "videv_light":
|
|
common_name = '.'.join(name.split('.')[:-1]) + '.' + name.split('.')[-1][6:]
|
|
li_device = rooms.getobjbyname(common_name + '_zigbee') if obj.enable_brightness or obj.enable_color_temp else None
|
|
try:
|
|
sw_device = rooms.getobjbyname(common_name) if obj.enable_state else None
|
|
except AttributeError:
|
|
# must be a device without switching device
|
|
sw_device = li_device
|
|
setattr(self, common_name.replace('.', '_'), testcase_light(obj, sw_device, li_device))
|
|
# add test collection
|
|
self.all = test_collection(self)
|
|
|
|
def getmembers(self, prefix=''):
|
|
rv = []
|
|
for name, obj in inspect.getmembers(self):
|
|
if prefix:
|
|
full_name = prefix + '.' + name
|
|
else:
|
|
full_name = name
|
|
if not name.startswith('_'):
|
|
try:
|
|
if obj.__class__.__bases__[0].__name__ == "testcase" or obj.__class__.__name__ == "test_collection":
|
|
rv.append(full_name)
|
|
else:
|
|
rv.extend(obj.getmembers(full_name))
|
|
except AttributeError:
|
|
pass
|
|
return rv
|
|
|
|
def getobjbyname(self, name):
|
|
if name.startswith("test."):
|
|
name = name[5:]
|
|
obj = self
|
|
for subname in name.split('.'):
|
|
obj = getattr(obj, subname)
|
|
return obj
|
|
|
|
def command(self, full_command):
|
|
try:
|
|
parameter = " " + full_command.split(' ')[1]
|
|
except IndexError:
|
|
parameter = ""
|
|
command = full_command.split(' ')[0].split('.')[-1] + parameter
|
|
device_name = '.'.join(full_command.split(' ')[0].split('.')[:-1])
|
|
self.getobjbyname(device_name).command(command)
|
|
|
|
|
|
class test_result_base(object):
|
|
def __init__(self):
|
|
self.__init_test_counters__()
|
|
|
|
def __init_test_counters__(self):
|
|
self.test_counter = 0
|
|
self.success_tests = 0
|
|
self.failed_tests = 0
|
|
|
|
def statistic(self):
|
|
return (self.test_counter, self.success_tests, self.failed_tests)
|
|
|
|
def print_statistic(self):
|
|
color = COLOR_SUCCESS if self.test_counter == self.success_tests else COLOR_FAIL
|
|
print(color + "*** SUCCESS: (%4d/%4d) FAIL: (%4d/%4d) ***\n" % (self.success_tests,
|
|
self.test_counter, self.failed_tests, self.test_counter) + colored.attr("reset"))
|
|
|
|
|
|
class test_collection(test_result_base):
|
|
def __init__(self, test_instance):
|
|
super().__init__()
|
|
self.test_instance = test_instance
|
|
|
|
def capabilities(self):
|
|
return [TEST_FULL, TEST_SMOKE]
|
|
|
|
def command(self, command):
|
|
self.__init_test_counters__()
|
|
for member in self.test_instance.getmembers():
|
|
obj = self.test_instance.getobjbyname(member)
|
|
if id(obj) != id(self):
|
|
obj.test_all(command)
|
|
num, suc, fail = obj.statistic()
|
|
self.test_counter += num
|
|
self.success_tests += suc
|
|
self.failed_tests += fail
|
|
self.print_statistic()
|
|
|
|
|
|
class testcase(test_result_base):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.__test_list__ = []
|
|
|
|
def capabilities(self):
|
|
if len(self.__test_list__) > 0 and not 'test_all' in self.__test_list__:
|
|
self.__test_list__.append('test_all')
|
|
self.__test_list__.sort()
|
|
return self.__test_list__
|
|
|
|
def test_all(self, test=TEST_FULL):
|
|
test_counter = 0
|
|
success_tests = 0
|
|
failed_tests = 0
|
|
for tc_name in self.capabilities():
|
|
if tc_name != "test_all":
|
|
self.command(tc_name, test)
|
|
test_counter += self.test_counter
|
|
success_tests += self.success_tests
|
|
failed_tests += self.failed_tests
|
|
self.test_counter = test_counter
|
|
self.success_tests = success_tests
|
|
self.failed_tests = failed_tests
|
|
|
|
def command(self, command, test=TEST_FULL):
|
|
self.__init_test_counters__()
|
|
tc = getattr(self, command)
|
|
self.__init_test_counters__()
|
|
rv = tc(test)
|
|
self.print_statistic()
|
|
|
|
def heading(self, desciption):
|
|
print(desciption)
|
|
|
|
def sub_heading(self, desciption):
|
|
print(2 * " " + desciption)
|
|
|
|
def result(self, desciption, success):
|
|
self.test_counter += 1
|
|
if success:
|
|
self.success_tests += 1
|
|
else:
|
|
self.failed_tests += 1
|
|
print(4 * " " + ("SUCCESS - " if success else "FAIL - ") + desciption)
|
|
|
|
|
|
class testcase_light(testcase):
|
|
def __init__(self, videv, sw_device, li_device):
|
|
self.videv = videv
|
|
self.sw_device = sw_device
|
|
self.li_device = li_device
|
|
self.__test_list__ = []
|
|
if self.videv.enable_state:
|
|
self.__test_list__.append('test_power_on_off')
|
|
if self.videv.enable_brightness:
|
|
self.__test_list__.append('test_brightness')
|
|
if self.videv.enable_color_temp:
|
|
self.__test_list__.append('test_color_temp')
|
|
|
|
def test_power_on_off(self, test=TEST_FULL):
|
|
self.heading("Power On/ Off test (%s)" % self.videv.topic)
|
|
#
|
|
sw_state = self.sw_device.get_data(self.sw_device.KEY_OUTPUT_0)
|
|
#
|
|
for i in range(0, 2):
|
|
self.sub_heading("State change of switching device")
|
|
#
|
|
self.sw_device.store_data(**{self.sw_device.KEY_OUTPUT_0: not self.sw_device.data.get(self.sw_device.KEY_OUTPUT_0)})
|
|
time.sleep(DT_TOGGLE)
|
|
self.result("Virtual device state after Switch on by switching device", sw_state != self.videv.get_data(self.videv.KEY_STATE))
|
|
self.result("Switching device state after Switch on by switching device",
|
|
sw_state != self.sw_device.get_data(self.sw_device.KEY_OUTPUT_0))
|
|
|
|
self.sub_heading("State change of virtual device")
|
|
#
|
|
self.videv.send(self.videv.KEY_STATE, not self.videv.data.get(self.videv.KEY_STATE))
|
|
time.sleep(DT_TOGGLE)
|
|
self.result("Virtual device state after Switch off by virtual device", sw_state == self.videv.get_data(self.videv.KEY_STATE))
|
|
self.result("Switching device state after Switch on by switching device",
|
|
sw_state == self.sw_device.get_data(self.sw_device.KEY_OUTPUT_0))
|
|
|
|
def test_brightness(self, test=TEST_FULL):
|
|
self.heading("Brightness test (%s)" % self.videv.topic)
|
|
#
|
|
br_state = self.li_device.get_data(self.li_device.KEY_BRIGHTNESS)
|
|
delta = -15 if br_state > 50 else 15
|
|
|
|
self.sw_device.store_data(**{self.sw_device.KEY_OUTPUT_0: True})
|
|
time.sleep(DT_TOGGLE)
|
|
|
|
for i in range(0, 2):
|
|
self.sub_heading("Brightness change by light device")
|
|
#
|
|
self.li_device.store_data(**{self.li_device.KEY_BRIGHTNESS: br_state + delta})
|
|
time.sleep(DT_TOGGLE)
|
|
self.result("Virtual device state after setting brightness by light device",
|
|
br_state + delta == self.videv.get_data(self.videv.KEY_BRIGHTNESS))
|
|
self.result("Light device state after setting brightness by light device", br_state +
|
|
delta == self.li_device.get_data(self.li_device.KEY_BRIGHTNESS))
|
|
|
|
self.sub_heading("Brightness change by virtual device")
|
|
#
|
|
self.videv.send(self.videv.KEY_BRIGHTNESS, br_state)
|
|
time.sleep(DT_TOGGLE)
|
|
self.result("Virtual device state after setting brightness by light device", br_state == self.videv.get_data(self.videv.KEY_BRIGHTNESS))
|
|
self.result("Light device state after setting brightness by light device",
|
|
br_state == self.li_device.get_data(self.li_device.KEY_BRIGHTNESS))
|
|
|
|
self.sw_device.store_data(**{self.sw_device.KEY_OUTPUT_0: False})
|
|
time.sleep(DT_TOGGLE)
|
|
|
|
def test_color_temp(self, test=TEST_FULL):
|
|
self.heading("Color temperature test (%s)" % self.videv.topic)
|
|
#
|
|
ct_state = self.li_device.get_data(self.li_device.KEY_COLOR_TEMP)
|
|
delta = -3 if ct_state > 5 else 3
|
|
|
|
self.sw_device.store_data(**{self.sw_device.KEY_OUTPUT_0: True})
|
|
time.sleep(DT_TOGGLE)
|
|
|
|
for i in range(0, 2):
|
|
self.sub_heading("Color temperature change by light device")
|
|
#
|
|
self.li_device.store_data(**{self.li_device.KEY_COLOR_TEMP: ct_state + delta})
|
|
time.sleep(DT_TOGGLE)
|
|
self.result("Virtual device state after setting color temperature by light device",
|
|
ct_state + delta == self.videv.get_data(self.videv.KEY_COLOR_TEMP))
|
|
self.result("Light device state after setting color temperature by light device", ct_state +
|
|
delta == self.li_device.get_data(self.li_device.KEY_COLOR_TEMP))
|
|
|
|
self.sub_heading("Color temperature change by virtual device")
|
|
#
|
|
self.videv.send(self.videv.KEY_COLOR_TEMP, ct_state)
|
|
time.sleep(DT_TOGGLE)
|
|
self.result("Virtual device state after setting color temperature by light device",
|
|
ct_state == self.videv.get_data(self.videv.KEY_COLOR_TEMP))
|
|
self.result("Light device state after setting color temperature by light device",
|
|
ct_state == self.li_device.get_data(self.li_device.KEY_COLOR_TEMP))
|
|
|
|
self.sw_device.store_data(**{self.sw_device.KEY_OUTPUT_0: False})
|
|
time.sleep(DT_TOGGLE)
|