Test Smart Brain implementation
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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