test_settings_loader.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.Player import Player
  10. from kalliope.core.Models.RestAPI import RestAPI
  11. from kalliope.core.Models.Settings import Settings
  12. from kalliope.core.Models.Stt import Stt
  13. from kalliope.core.Models.RecognitionOptions import RecognitionOptions
  14. from kalliope.core.Models.Trigger import Trigger
  15. from kalliope.core.Models.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. 'start_options': {'muted': True},
  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_unmute': [],
  55. 'on_triggered': ['on-triggered-synapse'],
  56. 'on_mute': [],
  57. 'on_order_not_found': [
  58. 'order-not-found-synapse'],
  59. 'on_start_speaking': None,
  60. 'on_stop_speaking': None
  61. }
  62. }
  63. # Init the folders, otherwise it raises an exceptions
  64. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/neurons")
  65. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/stt")
  66. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/tts")
  67. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/trigger")
  68. def tearDown(self):
  69. # Cleanup
  70. shutil.rmtree('/tmp/kalliope/tests/kalliope_resources_dir')
  71. Singleton._instances = {}
  72. def test_singleton(self):
  73. s1 = SettingLoader(file_path=self.settings_file_to_test)
  74. s2 = SettingLoader(file_path=self.settings_file_to_test)
  75. self.assertTrue(s1.settings is s2.settings)
  76. def test_get_yaml_config(self):
  77. sl = SettingLoader(file_path=self.settings_file_to_test)
  78. self.maxDiff = None
  79. self.assertDictEqual(sl.yaml_config, self.settings_dict)
  80. def test_get_settings(self):
  81. settings_object = Settings()
  82. settings_object.default_tts_name = "pico2wave"
  83. settings_object.default_stt_name = "google"
  84. settings_object.default_trigger_name = "snowboy"
  85. settings_object.default_player_name = "mplayer"
  86. tts1 = Tts(name="pico2wave", parameters={'cache': True, 'language': 'fr-FR'})
  87. tts2 = Tts(name="voxygen", parameters={'voice': 'Agnes', 'cache': True})
  88. settings_object.ttss = [tts1, tts2]
  89. stt = Stt(name="google", parameters={'language': 'fr-FR'})
  90. settings_object.stts = [stt]
  91. trigger1 = Trigger(name="snowboy",
  92. parameters={'pmdl_file': 'trigger/snowboy/resources/kalliope-FR-6samples.pmdl'})
  93. settings_object.triggers = [trigger1]
  94. player1 = Player(name="mplayer", parameters={})
  95. player2 = Player(name="pyalsaaudio", parameters={"device": "default"})
  96. settings_object.players = [player1, player2]
  97. settings_object.rest_api = RestAPI(password_protected=True, active=True,
  98. login="admin", password="secret", port=5000,
  99. allowed_cors_origin=False)
  100. settings_object.cache_path = '/tmp/kalliope_tts_cache'
  101. resources = Resources(neuron_folder="/tmp/kalliope/tests/kalliope_resources_dir/neurons",
  102. stt_folder="/tmp/kalliope/tests/kalliope_resources_dir/stt",
  103. tts_folder="/tmp/kalliope/tests/kalliope_resources_dir/tts",
  104. trigger_folder="/tmp/kalliope/tests/kalliope_resources_dir/trigger")
  105. settings_object.resources = resources
  106. settings_object.variables = {
  107. "author": "Lamonf",
  108. "test_number": 60,
  109. "test": "kalliope"
  110. }
  111. settings_object.start_options = {
  112. "muted": True
  113. }
  114. settings_object.machine = platform.machine()
  115. settings_object.recognition_options = RecognitionOptions()
  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_unmute': [],
  122. 'on_triggered': ['on-triggered-synapse'],
  123. 'on_mute': [],
  124. 'on_order_not_found': [
  125. 'order-not-found-synapse'],
  126. 'on_start_speaking': None,
  127. 'on_stop_speaking': None,
  128. }
  129. sl = SettingLoader(file_path=self.settings_file_to_test)
  130. self.assertEqual(settings_object, sl.settings)
  131. def test_get_default_speech_to_text(self):
  132. expected_default_speech_to_text = "google"
  133. sl = SettingLoader(file_path=self.settings_file_to_test)
  134. self.assertEqual(expected_default_speech_to_text, sl._get_default_speech_to_text(self.settings_dict))
  135. def test_get_default_text_to_speech(self):
  136. expected_default_text_to_speech = "pico2wave"
  137. sl = SettingLoader(file_path=self.settings_file_to_test)
  138. self.assertEqual(expected_default_text_to_speech, sl._get_default_text_to_speech(self.settings_dict))
  139. def test_get_default_trigger(self):
  140. expected_default_trigger = "snowboy"
  141. sl = SettingLoader(file_path=self.settings_file_to_test)
  142. self.assertEqual(expected_default_trigger, sl._get_default_trigger(self.settings_dict))
  143. def test_get_stts(self):
  144. stt = Stt(name="google", parameters={'language': 'fr-FR'})
  145. sl = SettingLoader(file_path=self.settings_file_to_test)
  146. self.assertEqual([stt], sl._get_stts(self.settings_dict))
  147. def test_get_ttss(self):
  148. tts1 = Tts(name="pico2wave", parameters={'cache': True, 'language': 'fr-FR'})
  149. tts2 = Tts(name="voxygen", parameters={'voice': 'Agnes', 'cache': True})
  150. sl = SettingLoader(file_path=self.settings_file_to_test)
  151. self.assertEqual([tts1, tts2], sl._get_ttss(self.settings_dict))
  152. def test_get_triggers(self):
  153. trigger1 = Trigger(name="snowboy",
  154. parameters={'pmdl_file': 'trigger/snowboy/resources/kalliope-FR-6samples.pmdl'})
  155. sl = SettingLoader(file_path=self.settings_file_to_test)
  156. self.assertEqual([trigger1], sl._get_triggers(self.settings_dict))
  157. def test_get_players(self):
  158. player1 = Player(name="mplayer",
  159. parameters={})
  160. player2 = Player(name="pyalsaaudio",
  161. parameters={'device': 'default'})
  162. sl = SettingLoader(file_path=self.settings_file_to_test)
  163. self.assertEqual([player1, player2], sl._get_players(self.settings_dict))
  164. def test_get_rest_api(self):
  165. expected_rest_api = RestAPI(password_protected=True, active=True,
  166. login="admin", password="secret", port=5000,
  167. allowed_cors_origin=False)
  168. sl = SettingLoader(file_path=self.settings_file_to_test)
  169. self.assertEqual(expected_rest_api, sl._get_rest_api(self.settings_dict))
  170. def test_get_cache_path(self):
  171. expected_cache_path = '/tmp/kalliope_tts_cache'
  172. sl = SettingLoader(file_path=self.settings_file_to_test)
  173. self.assertEqual(expected_cache_path, sl._get_cache_path(self.settings_dict))
  174. def test_get_resources(self):
  175. resources = Resources(neuron_folder="/tmp/kalliope/tests/kalliope_resources_dir/neurons",
  176. stt_folder="/tmp/kalliope/tests/kalliope_resources_dir/stt",
  177. tts_folder="/tmp/kalliope/tests/kalliope_resources_dir/tts",
  178. trigger_folder="/tmp/kalliope/tests/kalliope_resources_dir/trigger")
  179. expected_resource = resources
  180. sl = SettingLoader(file_path=self.settings_file_to_test)
  181. self.assertEqual(expected_resource, sl._get_resources(self.settings_dict))
  182. def test_get_variables(self):
  183. expected_result = {
  184. "author": "Lamonf",
  185. "test_number": 60,
  186. "test": "kalliope"
  187. }
  188. sl = SettingLoader(file_path=self.settings_file_to_test)
  189. self.assertEqual(expected_result,
  190. sl._get_variables(self.settings_dict))
  191. def test_get_start_options(self):
  192. expected_result = {
  193. "muted": True
  194. }
  195. sl = SettingLoader(file_path=self.settings_file_to_test)
  196. self.assertEqual(expected_result,
  197. sl._get_start_options(self.settings_dict))
  198. def test_get_hooks(self):
  199. # test with only one hook set
  200. settings = dict()
  201. settings["hooks"] = {
  202. "on_start": "test_synapse"
  203. }
  204. expected_dict = {
  205. "on_start": "test_synapse",
  206. "on_waiting_for_trigger": None,
  207. "on_triggered": None,
  208. "on_start_listening": None,
  209. "on_stop_listening": None,
  210. "on_order_found": None,
  211. "on_order_not_found": None,
  212. "on_mute": None,
  213. "on_unmute": None,
  214. "on_start_speaking": None,
  215. "on_stop_speaking": None
  216. }
  217. returned_dict = SettingLoader._get_hooks(settings)
  218. self.assertEqual(returned_dict, expected_dict)
  219. # test with no hook set
  220. settings = dict()
  221. expected_dict = {
  222. "on_start": None,
  223. "on_waiting_for_trigger": None,
  224. "on_triggered": None,
  225. "on_start_listening": None,
  226. "on_stop_listening": None,
  227. "on_order_found": None,
  228. "on_order_not_found": None,
  229. "on_mute": None,
  230. "on_unmute": None,
  231. "on_start_speaking": None,
  232. "on_stop_speaking": None
  233. }
  234. returned_dict = SettingLoader._get_hooks(settings)
  235. self.assertEqual(returned_dict, expected_dict)
  236. if __name__ == '__main__':
  237. unittest.main()
  238. # suite = unittest.TestSuite()
  239. # suite.addTest(TestSettingLoader("test_get_hooks"))
  240. # runner = unittest.TextTestRunner()
  241. # runner.run(suite)