test_settings_loader.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. 'options': {'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 = {
  115. "deaf": True,
  116. "mute": False
  117. }
  118. settings_object.machine = platform.machine()
  119. settings_object.recognition_options = RecognitionOptions()
  120. settings_object.hooks = {'on_waiting_for_trigger': 'test',
  121. 'on_stop_listening': None,
  122. 'on_start_listening': None,
  123. 'on_order_found': None,
  124. 'on_start': ['on-start-synapse', 'bring-led-on'],
  125. 'on_undeaf': [],
  126. 'on_triggered': ['on-triggered-synapse'],
  127. 'on_deaf': [],
  128. 'on_mute': [],
  129. 'on_unmute': [],
  130. 'on_order_not_found': [
  131. 'order-not-found-synapse'],
  132. 'on_processed_synapses': None,
  133. 'on_start_speaking': None,
  134. 'on_stop_speaking': None,
  135. }
  136. sl = SettingLoader(file_path=self.settings_file_to_test)
  137. self.assertEqual(settings_object, sl.settings)
  138. def test_get_default_speech_to_text(self):
  139. expected_default_speech_to_text = "google"
  140. sl = SettingLoader(file_path=self.settings_file_to_test)
  141. self.assertEqual(expected_default_speech_to_text, sl._get_default_speech_to_text(self.settings_dict))
  142. def test_get_default_text_to_speech(self):
  143. expected_default_text_to_speech = "pico2wave"
  144. sl = SettingLoader(file_path=self.settings_file_to_test)
  145. self.assertEqual(expected_default_text_to_speech, sl._get_default_text_to_speech(self.settings_dict))
  146. def test_get_default_trigger(self):
  147. expected_default_trigger = "snowboy"
  148. sl = SettingLoader(file_path=self.settings_file_to_test)
  149. self.assertEqual(expected_default_trigger, sl._get_default_trigger(self.settings_dict))
  150. def test_get_stts(self):
  151. stt = Stt(name="google", parameters={'language': 'fr-FR'})
  152. sl = SettingLoader(file_path=self.settings_file_to_test)
  153. self.assertEqual([stt], sl._get_stts(self.settings_dict))
  154. def test_get_ttss(self):
  155. tts1 = Tts(name="pico2wave", parameters={'cache': True, 'language': 'fr-FR'})
  156. tts2 = Tts(name="voxygen", parameters={'voice': 'Agnes', 'cache': True})
  157. sl = SettingLoader(file_path=self.settings_file_to_test)
  158. self.assertEqual([tts1, tts2], sl._get_ttss(self.settings_dict))
  159. def test_get_triggers(self):
  160. trigger1 = Trigger(name="snowboy",
  161. parameters={'pmdl_file': 'trigger/snowboy/resources/kalliope-FR-6samples.pmdl'})
  162. sl = SettingLoader(file_path=self.settings_file_to_test)
  163. self.assertEqual([trigger1], sl._get_triggers(self.settings_dict))
  164. def test_get_players(self):
  165. player1 = Player(name="mplayer",
  166. parameters={})
  167. player2 = Player(name="pyalsaaudio",
  168. parameters={'device': 'default'})
  169. sl = SettingLoader(file_path=self.settings_file_to_test)
  170. self.assertEqual([player1, player2], sl._get_players(self.settings_dict))
  171. def test_get_rest_api(self):
  172. expected_rest_api = RestAPI(password_protected=True, active=True,
  173. login="admin", password="secret", port=5000,
  174. allowed_cors_origin=False)
  175. sl = SettingLoader(file_path=self.settings_file_to_test)
  176. self.assertEqual(expected_rest_api, sl._get_rest_api(self.settings_dict))
  177. def test_get_cache_path(self):
  178. expected_cache_path = '/tmp/kalliope_tts_cache'
  179. sl = SettingLoader(file_path=self.settings_file_to_test)
  180. self.assertEqual(expected_cache_path, sl._get_cache_path(self.settings_dict))
  181. def test_get_resources(self):
  182. resources = Resources(neuron_folder="/tmp/kalliope/tests/kalliope_resources_dir/neurons",
  183. stt_folder="/tmp/kalliope/tests/kalliope_resources_dir/stt",
  184. tts_folder="/tmp/kalliope/tests/kalliope_resources_dir/tts",
  185. trigger_folder="/tmp/kalliope/tests/kalliope_resources_dir/trigger")
  186. expected_resource = resources
  187. sl = SettingLoader(file_path=self.settings_file_to_test)
  188. self.assertEqual(expected_resource, sl._get_resources(self.settings_dict))
  189. def test_get_variables(self):
  190. expected_result = {
  191. "author": "Lamonf",
  192. "test_number": 60,
  193. "test": "kalliope"
  194. }
  195. sl = SettingLoader(file_path=self.settings_file_to_test)
  196. self.assertEqual(expected_result,
  197. sl._get_variables(self.settings_dict))
  198. def test_get_options(self):
  199. expected_result = {
  200. "deaf": True,
  201. "mute": False
  202. }
  203. sl = SettingLoader(file_path=self.settings_file_to_test)
  204. self.assertEqual(expected_result,
  205. sl._get_options(self.settings_dict))
  206. def test_get_hooks(self):
  207. # test with only one hook set
  208. settings = dict()
  209. settings["hooks"] = {
  210. "on_start": "test_synapse"
  211. }
  212. expected_dict = {
  213. "on_start": "test_synapse",
  214. "on_waiting_for_trigger": None,
  215. "on_triggered": None,
  216. "on_start_listening": None,
  217. "on_stop_listening": None,
  218. "on_order_found": None,
  219. "on_order_not_found": None,
  220. "on_deaf": None,
  221. "on_undeaf": None,
  222. "on_mute": None,
  223. "on_unmute": None,
  224. "on_start_speaking": None,
  225. "on_stop_speaking": None
  226. }
  227. returned_dict = SettingLoader._get_hooks(settings)
  228. self.assertEqual(returned_dict, expected_dict)
  229. # test with no hook set
  230. settings = dict()
  231. expected_dict = {
  232. "on_start": None,
  233. "on_waiting_for_trigger": None,
  234. "on_triggered": None,
  235. "on_start_listening": None,
  236. "on_stop_listening": None,
  237. "on_order_found": None,
  238. "on_order_not_found": None,
  239. "on_deaf": None,
  240. "on_undeaf": None,
  241. "on_mute": None,
  242. "on_unmute": None,
  243. "on_start_speaking": None,
  244. "on_stop_speaking": None
  245. }
  246. returned_dict = SettingLoader._get_hooks(settings)
  247. self.assertEqual(returned_dict, expected_dict)
  248. if __name__ == '__main__':
  249. unittest.main()
  250. # suite = unittest.TestSuite()
  251. # suite.addTest(TestSettingLoader("test_get_hooks"))
  252. # runner = unittest.TextTestRunner()
  253. # runner.run(suite)