test_settings_loader.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import os
  2. import inspect
  3. import platform
  4. import shutil
  5. import unittest
  6. from kalliope.core.ConfigurationManager import SettingLoader
  7. from kalliope.core.Models import Singleton
  8. from kalliope.core.Models import Resources
  9. from kalliope.core.Models.settings.Options import Options
  10. from kalliope.core.Models.settings.Player import Player
  11. from kalliope.core.Models.settings.RestAPI import RestAPI
  12. from kalliope.core.Models.settings.Settings import Settings
  13. from kalliope.core.Models.settings.Stt import Stt
  14. from kalliope.core.Models.settings.Trigger import Trigger
  15. from kalliope.core.Models.settings.Tts import Tts
  16. class TestSettingLoader(unittest.TestCase):
  17. def setUp(self):
  18. # get current script directory path. We are in /an/unknown/path/kalliope/core/tests
  19. cur_script_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  20. # get parent dir. Now we are in /an/unknown/path/kalliope
  21. root_dir = os.path.normpath(cur_script_directory + os.sep + os.pardir)
  22. self.settings_file_to_test = root_dir + os.sep + "Tests/settings/settings_test.yml"
  23. self.settings_dict = {
  24. 'rest_api':
  25. {'allowed_cors_origin': False,
  26. 'active': True,
  27. 'login': 'admin',
  28. 'password_protected': True,
  29. 'password': 'secret', 'port': 5000},
  30. 'default_trigger': 'snowboy',
  31. 'triggers': [{'snowboy': {'pmdl_file': 'trigger/snowboy/resources/kalliope-FR-6samples.pmdl'}}],
  32. 'default_player': 'mplayer',
  33. 'players': [{'mplayer': {}}, {'pyalsaaudio': {"device": "default"}}],
  34. 'speech_to_text': [{'google': {'language': 'fr-FR'}}],
  35. 'cache_path': '/tmp/kalliope_tts_cache',
  36. 'resource_directory': {
  37. 'stt': '/tmp/kalliope/tests/kalliope_resources_dir/stt',
  38. 'tts': '/tmp/kalliope/tests/kalliope_resources_dir/tts',
  39. 'neuron': '/tmp/kalliope/tests/kalliope_resources_dir/neurons',
  40. 'trigger': '/tmp/kalliope/tests/kalliope_resources_dir/trigger'},
  41. 'default_text_to_speech': 'pico2wave',
  42. 'default_speech_to_text': 'google',
  43. 'text_to_speech': [
  44. {'pico2wave': {'cache': True, 'language': 'fr-FR'}},
  45. {'voxygen': {'voice': 'Agnes', 'cache': True}}
  46. ],
  47. 'var_files': ["../Tests/settings/variables.yml"],
  48. 'options': {'energy_threshold': 3000, 'deaf': True, 'mute': False},
  49. 'hooks': {'on_waiting_for_trigger': 'test',
  50. 'on_stop_listening': None,
  51. 'on_start_listening': None,
  52. 'on_order_found': None,
  53. 'on_start': ['on-start-synapse', 'bring-led-on'],
  54. 'on_undeaf': [],
  55. 'on_triggered': ['on-triggered-synapse'],
  56. 'on_deaf': [],
  57. 'on_mute': [],
  58. 'on_unmute': [],
  59. 'on_order_not_found': [
  60. 'order-not-found-synapse'],
  61. 'on_processed_synapses': None,
  62. 'on_start_speaking': None,
  63. 'on_stop_speaking': None
  64. }
  65. }
  66. # Init the folders, otherwise it raises an exceptions
  67. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/neurons")
  68. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/stt")
  69. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/tts")
  70. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/trigger")
  71. def tearDown(self):
  72. # Cleanup
  73. shutil.rmtree('/tmp/kalliope/tests/kalliope_resources_dir')
  74. Singleton._instances = {}
  75. def test_singleton(self):
  76. s1 = SettingLoader(file_path=self.settings_file_to_test)
  77. s2 = SettingLoader(file_path=self.settings_file_to_test)
  78. self.assertTrue(s1.settings is s2.settings)
  79. def test_get_yaml_config(self):
  80. sl = SettingLoader(file_path=self.settings_file_to_test)
  81. self.maxDiff = None
  82. self.assertDictEqual(sl.yaml_config, self.settings_dict)
  83. def test_get_settings(self):
  84. settings_object = Settings()
  85. settings_object.default_tts_name = "pico2wave"
  86. settings_object.default_stt_name = "google"
  87. settings_object.default_trigger_name = "snowboy"
  88. settings_object.default_player_name = "mplayer"
  89. tts1 = Tts(name="pico2wave", parameters={'cache': True, 'language': 'fr-FR'})
  90. tts2 = Tts(name="voxygen", parameters={'voice': 'Agnes', 'cache': True})
  91. settings_object.ttss = [tts1, tts2]
  92. stt = Stt(name="google", parameters={'language': 'fr-FR'})
  93. settings_object.stts = [stt]
  94. trigger1 = Trigger(name="snowboy",
  95. parameters={'pmdl_file': 'trigger/snowboy/resources/kalliope-FR-6samples.pmdl'})
  96. settings_object.triggers = [trigger1]
  97. player1 = Player(name="mplayer", parameters={})
  98. player2 = Player(name="pyalsaaudio", parameters={"device": "default"})
  99. settings_object.players = [player1, player2]
  100. settings_object.rest_api = RestAPI(password_protected=True, active=True,
  101. login="admin", password="secret", port=5000,
  102. allowed_cors_origin=False)
  103. settings_object.cache_path = '/tmp/kalliope_tts_cache'
  104. resources = Resources(neuron_folder="/tmp/kalliope/tests/kalliope_resources_dir/neurons",
  105. stt_folder="/tmp/kalliope/tests/kalliope_resources_dir/stt",
  106. tts_folder="/tmp/kalliope/tests/kalliope_resources_dir/tts",
  107. trigger_folder="/tmp/kalliope/tests/kalliope_resources_dir/trigger")
  108. settings_object.resources = resources
  109. settings_object.variables = {
  110. "author": "Lamonf",
  111. "test_number": 60,
  112. "test": "kalliope"
  113. }
  114. settings_object.options = Options(energy_threshold=3000, deaf=True, mute=False)
  115. settings_object.machine = platform.machine()
  116. settings_object.hooks = {'on_waiting_for_trigger': 'test',
  117. 'on_stop_listening': None,
  118. 'on_start_listening': None,
  119. 'on_order_found': None,
  120. 'on_start': ['on-start-synapse', 'bring-led-on'],
  121. 'on_undeaf': [],
  122. 'on_triggered': ['on-triggered-synapse'],
  123. 'on_deaf': [],
  124. 'on_mute': [],
  125. 'on_unmute': [],
  126. 'on_order_not_found': [
  127. 'order-not-found-synapse'],
  128. 'on_processed_synapses': None,
  129. 'on_start_speaking': None,
  130. 'on_stop_speaking': None,
  131. }
  132. sl = SettingLoader(file_path=self.settings_file_to_test)
  133. self.assertEqual(settings_object, sl.settings)
  134. def test_get_default_speech_to_text(self):
  135. expected_default_speech_to_text = "google"
  136. sl = SettingLoader(file_path=self.settings_file_to_test)
  137. self.assertEqual(expected_default_speech_to_text, sl._get_default_speech_to_text(self.settings_dict))
  138. def test_get_default_text_to_speech(self):
  139. expected_default_text_to_speech = "pico2wave"
  140. sl = SettingLoader(file_path=self.settings_file_to_test)
  141. self.assertEqual(expected_default_text_to_speech, sl._get_default_text_to_speech(self.settings_dict))
  142. def test_get_default_trigger(self):
  143. expected_default_trigger = "snowboy"
  144. sl = SettingLoader(file_path=self.settings_file_to_test)
  145. self.assertEqual(expected_default_trigger, sl._get_default_trigger(self.settings_dict))
  146. def test_get_stts(self):
  147. stt = Stt(name="google", parameters={'language': 'fr-FR'})
  148. sl = SettingLoader(file_path=self.settings_file_to_test)
  149. self.assertEqual([stt], sl._get_stts(self.settings_dict))
  150. def test_get_ttss(self):
  151. tts1 = Tts(name="pico2wave", parameters={'cache': True, 'language': 'fr-FR'})
  152. tts2 = Tts(name="voxygen", parameters={'voice': 'Agnes', 'cache': True})
  153. sl = SettingLoader(file_path=self.settings_file_to_test)
  154. self.assertEqual([tts1, tts2], sl._get_ttss(self.settings_dict))
  155. def test_get_triggers(self):
  156. trigger1 = Trigger(name="snowboy",
  157. parameters={'pmdl_file': 'trigger/snowboy/resources/kalliope-FR-6samples.pmdl'})
  158. sl = SettingLoader(file_path=self.settings_file_to_test)
  159. self.assertEqual([trigger1], sl._get_triggers(self.settings_dict))
  160. def test_get_players(self):
  161. player1 = Player(name="mplayer",
  162. parameters={})
  163. player2 = Player(name="pyalsaaudio",
  164. parameters={'device': 'default'})
  165. sl = SettingLoader(file_path=self.settings_file_to_test)
  166. self.assertEqual([player1, player2], sl._get_players(self.settings_dict))
  167. def test_get_rest_api(self):
  168. expected_rest_api = RestAPI(password_protected=True, active=True,
  169. login="admin", password="secret", port=5000,
  170. allowed_cors_origin=False)
  171. sl = SettingLoader(file_path=self.settings_file_to_test)
  172. self.assertEqual(expected_rest_api, sl._get_rest_api(self.settings_dict))
  173. def test_get_cache_path(self):
  174. expected_cache_path = '/tmp/kalliope_tts_cache'
  175. sl = SettingLoader(file_path=self.settings_file_to_test)
  176. self.assertEqual(expected_cache_path, sl._get_cache_path(self.settings_dict))
  177. def test_get_resources(self):
  178. resources = Resources(neuron_folder="/tmp/kalliope/tests/kalliope_resources_dir/neurons",
  179. stt_folder="/tmp/kalliope/tests/kalliope_resources_dir/stt",
  180. tts_folder="/tmp/kalliope/tests/kalliope_resources_dir/tts",
  181. trigger_folder="/tmp/kalliope/tests/kalliope_resources_dir/trigger")
  182. expected_resource = resources
  183. sl = SettingLoader(file_path=self.settings_file_to_test)
  184. self.assertEqual(expected_resource, sl._get_resources(self.settings_dict))
  185. def test_get_variables(self):
  186. expected_result = {
  187. "author": "Lamonf",
  188. "test_number": 60,
  189. "test": "kalliope"
  190. }
  191. sl = SettingLoader(file_path=self.settings_file_to_test)
  192. self.assertEqual(expected_result,
  193. sl._get_variables(self.settings_dict))
  194. def test_get_options(self):
  195. expected_result = Options(energy_threshold=3000, deaf=True, mute=False)
  196. sl = SettingLoader(file_path=self.settings_file_to_test)
  197. self.assertEqual(expected_result,
  198. sl._get_options(self.settings_dict))
  199. def test_get_hooks(self):
  200. # test with only one hook set
  201. settings = dict()
  202. settings["hooks"] = {
  203. "on_start": "test_synapse"
  204. }
  205. expected_dict = {
  206. "on_start": "test_synapse",
  207. "on_waiting_for_trigger": None,
  208. "on_triggered": None,
  209. "on_start_listening": None,
  210. "on_stop_listening": None,
  211. "on_order_found": None,
  212. "on_order_not_found": None,
  213. "on_deaf": None,
  214. "on_undeaf": None,
  215. "on_mute": None,
  216. "on_unmute": None,
  217. "on_start_speaking": None,
  218. "on_stop_speaking": None
  219. }
  220. returned_dict = SettingLoader._get_hooks(settings)
  221. self.assertEqual(returned_dict, expected_dict)
  222. # test with no hook set
  223. settings = dict()
  224. expected_dict = {
  225. "on_start": None,
  226. "on_waiting_for_trigger": None,
  227. "on_triggered": None,
  228. "on_start_listening": None,
  229. "on_stop_listening": None,
  230. "on_order_found": None,
  231. "on_order_not_found": None,
  232. "on_deaf": None,
  233. "on_undeaf": None,
  234. "on_mute": None,
  235. "on_unmute": None,
  236. "on_start_speaking": None,
  237. "on_stop_speaking": None
  238. }
  239. returned_dict = SettingLoader._get_hooks(settings)
  240. self.assertEqual(returned_dict, expected_dict)
  241. if __name__ == '__main__':
  242. unittest.main()
  243. # suite = unittest.TestSuite()
  244. # suite.addTest(TestSettingLoader("test_get_hooks"))
  245. # runner = unittest.TextTestRunner()
  246. # runner.run(suite)