Test Smart Brain implementation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

all.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import distro
  2. import getpass
  3. import inspect
  4. import jinja2
  5. import json
  6. import os
  7. import platform
  8. import report
  9. from tests import test_collection
  10. from tests.heating import testcase_heating
  11. from tests.light import testcase_light
  12. from tests.synchronisation import testcase_synchronisation
  13. from unittest import jsonlog
  14. try:
  15. from config import APP_NAME as ROOT_LOGGER_NAME
  16. except ImportError:
  17. ROOT_LOGGER_NAME = 'root'
  18. class test_smarthome(object):
  19. def __init__(self, rooms):
  20. self.__init__tcl__()
  21. # add testcases for switching devices
  22. for name in rooms.getmembers():
  23. obj = rooms.getobjbyname(name)
  24. if obj.__class__.__name__ == "videv_light":
  25. common_name = '.'.join(name.split('.')[:-1]) + '.' + name.split('.')[-1][6:]
  26. try:
  27. li_device = rooms.getobjbyname(common_name + '_zigbee') if obj.enable_brightness or obj.enable_color_temp else None
  28. except AttributeError:
  29. li_device = rooms.getobjbyname(common_name + '_zigbee_1') if obj.enable_brightness or obj.enable_color_temp else None
  30. try:
  31. sw_device = rooms.getobjbyname(common_name) if obj.enable_state else None
  32. except AttributeError:
  33. # must be a device without switching device
  34. sw_device = li_device
  35. setattr(self, common_name.replace('.', '_'), testcase_light(self.tcl, obj, sw_device, li_device))
  36. # add testcases for heating devices
  37. for name in rooms.getmembers():
  38. obj = rooms.getobjbyname(name)
  39. if obj.__class__.__name__ == "videv_heating":
  40. common_name = '.'.join(name.split('.')[:-1]) + '.' + name.split('.')[-1][6:]
  41. heat_device = rooms.getobjbyname(common_name + '_valve')
  42. setattr(self, common_name.replace('.', '_'), testcase_heating(self.tcl, obj, heat_device))
  43. # synchronisation
  44. # gfw.dirk.amplifier with cd_player
  45. self.gfw_dirk_cd_player_amplifier_sync = testcase_synchronisation(
  46. self.tcl,
  47. rooms.gfw.dirk.cd_player, None, None,
  48. rooms.gfw.dirk.amplifier)
  49. # gfw.floor.main_light_2 with gfw.floor.main_light_1
  50. self.gfw_floor_main_light_sync = testcase_synchronisation(
  51. self.tcl,
  52. rooms.gfw.floor.main_light, rooms.gfw.floor.videv_main_light, rooms.gfw.floor.videv_main_light,
  53. rooms.gfw.floor.main_light_zigbee_1, rooms.gfw.floor.main_light_zigbee_2
  54. )
  55. # ffe.diningroom.floorlamp with ffe.dinigroom.main_light
  56. self.ffe_diningroom_main_light_floor_lamp_sync = testcase_synchronisation(
  57. self.tcl,
  58. rooms.ffe.diningroom.main_light, None, None,
  59. rooms.ffe.diningroom.floor_lamp)
  60. # ffe.livingroom.floorlamp_[1-6] with ffe.livingroom.main_light
  61. self.ffe_livingroom_main_light_floor_lamp_sync = testcase_synchronisation(
  62. self.tcl,
  63. rooms.ffe.livingroom.main_light, rooms.ffe.livingroom.videv_floor_lamp, rooms.ffe.livingroom.videv_floor_lamp,
  64. *[getattr(rooms.ffe.livingroom, "floor_lamp_zigbee_%d" % i) for i in range(1, 7)]
  65. )
  66. # add test collection
  67. self.all = test_collection(self)
  68. def __init__tcl__(self):
  69. system_info = {}
  70. system_info[jsonlog.SYSI_ARCHITECTURE] = platform.architecture()[0]
  71. system_info[jsonlog.SYSI_MACHINE] = platform.machine()
  72. system_info[jsonlog.SYSI_HOSTNAME] = platform.node()
  73. system_info[jsonlog.SYSI_DISTRIBUTION] = distro.name() + " " + distro.version() + " (" + distro.codename() + ")"
  74. system_info[jsonlog.SYSI_SYSTEM] = platform.system()
  75. system_info[jsonlog.SYSI_KERNEL] = platform.release() + ' (%s)' % platform.version()
  76. system_info[jsonlog.SYSI_USERNAME] = getpass.getuser()
  77. system_info[jsonlog.SYSI_PATH] = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
  78. self.tcl = report.testSession(['__unittest__', 'unittest', ROOT_LOGGER_NAME])
  79. self.tcl[jsonlog.MAIN_KEY_SYSTEM_INFO] = system_info
  80. self.tcl["testcase_names"] = report.TCEL_NAMES
  81. def __eval_tcl__(self):
  82. path = os.path.abspath(os.path.join(os.path.basename(__file__), '..'))
  83. with open(os.path.join(path, "testresults", "testrun.json"), "w") as fh:
  84. fh.write(json.dumps(self.tcl, indent=4))
  85. template_path = os.path.join(path, 'templates')
  86. template_filename = 'unittest.tex'
  87. jenv = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path))
  88. template = jenv.get_template(template_filename)
  89. with open(os.path.join(path, "testresults", "testrun.tex"), "w") as fh:
  90. fh.write(template.render(data=self.tcl, details=False))
  91. with open(os.path.join(path, "testresults", "testrun_full.tex"), "w") as fh:
  92. fh.write(template.render(data=self.tcl, details=True))
  93. def __del__(self):
  94. try:
  95. self.__eval_tcl__()
  96. except:
  97. pass
  98. def getmembers(self, prefix=''):
  99. rv = []
  100. for name, obj in inspect.getmembers(self):
  101. if prefix:
  102. full_name = prefix + '.' + name
  103. else:
  104. full_name = name
  105. if not name.startswith('_'):
  106. try:
  107. if obj.__class__.__bases__[0].__name__ == "testcase" or obj.__class__.__name__ == "test_collection":
  108. rv.append(full_name)
  109. else:
  110. rv.extend(obj.getmembers(full_name))
  111. except AttributeError:
  112. pass
  113. return rv
  114. def getobjbyname(self, name):
  115. if name.startswith("test."):
  116. name = name[5:]
  117. obj = self
  118. for subname in name.split('.'):
  119. obj = getattr(obj, subname)
  120. return obj
  121. def command(self, full_command):
  122. try:
  123. parameter = " " + full_command.split(' ')[1]
  124. except IndexError:
  125. parameter = ""
  126. command = full_command.split(' ')[0].split('.')[-1] + parameter
  127. device_name = '.'.join(full_command.split(' ')[0].split('.')[:-1])
  128. self.getobjbyname(device_name).command(command)