test_models.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import unittest
  2. import ast
  3. import mock
  4. from kalliope.core.Models.Player import Player
  5. from kalliope.core.Models.Signal import Signal
  6. from kalliope.core.Models.RecognitionOptions import RecognitionOptions
  7. from kalliope.core.Models.Tts import Tts
  8. from kalliope.core.Models.Trigger import Trigger
  9. from kalliope.core.Models.Stt import Stt
  10. from kalliope.core.Models.RestAPI import RestAPI
  11. from kalliope.core.Models.Dna import Dna
  12. from kalliope.core import LIFOBuffer
  13. from kalliope.core.Models.Settings import Settings
  14. from kalliope.core.Models import Neuron, Synapse, Brain, Resources, Singleton
  15. from kalliope.core.Models.APIResponse import APIResponse
  16. from kalliope.core.Models.MatchedSynapse import MatchedSynapse
  17. class TestModels(unittest.TestCase):
  18. def setUp(self):
  19. # Kill the singleton
  20. Singleton._instances = dict()
  21. # Init
  22. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  23. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  24. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  25. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  26. signal1 = Signal(name="order", parameters="this is the sentence")
  27. signal2 = Signal(name="order", parameters="this is the second sentence")
  28. signal3 = Signal(name="order", parameters="that is part of the third sentence")
  29. self.synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  30. self.synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  31. self.synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  32. self.all_synapse_list1 = [self.synapse1,
  33. self.synapse2,
  34. self.synapse3]
  35. self.all_synapse_list2 = [self.synapse2,
  36. self.synapse3]
  37. self.brain_test1 = Brain(synapses=self.all_synapse_list1)
  38. self.brain_test2 = Brain(synapses=self.all_synapse_list2)
  39. # this brain is the same as the first one
  40. self.brain_test3 = Brain(synapses=self.all_synapse_list1)
  41. self.settings_test = Settings()
  42. # clean the LiFO
  43. LIFOBuffer.lifo_list = list()
  44. def test_APIResponse(self):
  45. user_order = "user order"
  46. self.matched_synapse = MatchedSynapse(matched_synapse=self.synapse1, matched_order=user_order)
  47. api_response = APIResponse()
  48. api_response.user_order = user_order
  49. api_response.list_processed_matched_synapse = [self.matched_synapse]
  50. expected_result_serialize = {
  51. 'status': None,
  52. 'matched_synapses':
  53. [
  54. {
  55. 'matched_order': 'user order',
  56. 'neuron_module_list': [],
  57. 'synapse_name': 'Synapse1'
  58. }
  59. ],
  60. 'user_order': 'user order'
  61. }
  62. self.assertDictEqual(expected_result_serialize, api_response.serialize())
  63. def test_Brain(self):
  64. # test get synapse by name
  65. expect_result = self.synapse1
  66. synapse_name = "Synapse1"
  67. self.assertEqual(self.brain_test1.get_synapse_by_name(synapse_name), expect_result)
  68. # test equals
  69. self.assertTrue(self.brain_test1.__eq__(self.brain_test3))
  70. # test not equals
  71. self.assertFalse(self.brain_test1.__eq__(self.brain_test2))
  72. def test_Dna(self):
  73. # create DNA object
  74. dna1 = Dna(name="dna1", module_type="neuron", author="kalliope",
  75. kalliope_supported_version="0.4.4", tags="test")
  76. dna2 = Dna(name="dna2", module_type="neuron", author="community",
  77. kalliope_supported_version="0.4.2", tags="other")
  78. # this dna is exactly the same as the first one
  79. dna3 = Dna(name="dna1", module_type="neuron", author="kalliope",
  80. kalliope_supported_version="0.4.4", tags="test")
  81. expected_result_serialize = {
  82. 'kalliope_supported_version': '0.4.4',
  83. 'tags': 'test',
  84. 'type': 'neuron',
  85. 'name': 'dna1',
  86. 'author': 'kalliope'
  87. }
  88. self.assertDictEqual(expected_result_serialize, dna1.serialize())
  89. self.assertTrue(dna1.__eq__(dna3))
  90. self.assertFalse(dna1.__eq__(dna2))
  91. def test_MatchedSynapse(self):
  92. user_order = "user order"
  93. matched_synapse1 = MatchedSynapse(matched_synapse=self.synapse1, matched_order=user_order)
  94. matched_synapse2 = MatchedSynapse(matched_synapse=self.synapse2, matched_order=user_order)
  95. matched_synapse3 = MatchedSynapse(matched_synapse=self.synapse1, matched_order=user_order)
  96. expected_result_serialize = {
  97. 'matched_order': 'user order',
  98. 'neuron_module_list': [],
  99. 'synapse_name': 'Synapse1'
  100. }
  101. self.assertDictEqual(expected_result_serialize, matched_synapse1.serialize())
  102. self.assertTrue(matched_synapse1.__eq__(matched_synapse3))
  103. self.assertFalse(matched_synapse1.__eq__(matched_synapse2))
  104. # test neuron parameter loader is called
  105. with mock.patch("kalliope.core.NeuronParameterLoader.get_parameters") as mock_get_parameters:
  106. MatchedSynapse(matched_synapse=self.synapse1, matched_order=user_order, user_order=user_order)
  107. mock_get_parameters.assert_called_once_with(synapse_order=user_order,
  108. user_order=user_order)
  109. mock_get_parameters.reset_mock()
  110. def test_Neuron(self):
  111. neuron1 = Neuron(name="test", parameters={"key1": "val1", "key2": "val2"})
  112. neuron2 = Neuron(name="test", parameters={"key3": "val3", "key4": "val4"})
  113. neuron3 = Neuron(name="test", parameters={"key1": "val1", "key2": "val2"})
  114. expected_result_serialize = {'name': 'test', 'parameters': {'key2': 'val2', 'key1': 'val1'}}
  115. self.assertDictEqual(expected_result_serialize, neuron1.serialize())
  116. self.assertTrue(neuron1.__eq__(neuron3))
  117. self.assertFalse(neuron1.__eq__(neuron2))
  118. # test password
  119. neuron_name = "test"
  120. neuron_parameters = {
  121. "password": "my secret",
  122. "parameter": "test"
  123. }
  124. neuron = Neuron()
  125. neuron.name = neuron_name
  126. neuron.parameters = neuron_parameters
  127. expected_result_str = "{'name': 'test', 'parameters': {'password': '*****', 'parameter': 'test'}}"
  128. self.assertDictEqual(ast.literal_eval(neuron.__str__()), ast.literal_eval(expected_result_str))
  129. neuron_name = "test"
  130. neuron_parameters = {
  131. "password_parameter": "my secret",
  132. "parameter": "test"
  133. }
  134. neuron = Neuron()
  135. neuron.name = neuron_name
  136. neuron.parameters = neuron_parameters
  137. expected_result_str = "{'name': 'test', 'parameters': {'parameter': 'test', 'password_parameter': '*****'}}"
  138. self.assertDictEqual(ast.literal_eval(neuron.__str__()), ast.literal_eval(expected_result_str))
  139. def test_Resources(self):
  140. resource1 = Resources(neuron_folder="/path/neuron", stt_folder="/path/stt",
  141. tts_folder="/path/tts", trigger_folder="/path/trigger")
  142. resource2 = Resources(neuron_folder="/other_path/neuron", stt_folder="/other_path/stt",
  143. tts_folder="/other_path/tts", trigger_folder="/other_path/trigger")
  144. resource3 = Resources(neuron_folder="/path/neuron", stt_folder="/path/stt",
  145. tts_folder="/path/tts", trigger_folder="/path/trigger")
  146. expected_result_serialize = {
  147. 'tts_folder': '/path/tts',
  148. 'neuron_folder': '/path/neuron',
  149. 'stt_folder': '/path/stt',
  150. 'trigger_folder': '/path/trigger',
  151. 'signal_folder': None
  152. }
  153. self.assertDictEqual(expected_result_serialize, resource1.serialize())
  154. self.assertTrue(resource1.__eq__(resource3))
  155. self.assertFalse(resource1.__eq__(resource2))
  156. def test_RestAPI(self):
  157. rest_api1 = RestAPI(password_protected=True, login="admin", password="password", active=True,
  158. port=5000, allowed_cors_origin="*")
  159. rest_api2 = RestAPI(password_protected=False, active=False,
  160. port=5000, allowed_cors_origin=None)
  161. rest_api3 = RestAPI(password_protected=True, login="admin", password="password", active=True,
  162. port=5000, allowed_cors_origin="*")
  163. expected_result_serialize = {
  164. 'password_protected': True,
  165. 'port': 5000,
  166. 'active': True,
  167. 'allowed_cors_origin': '*',
  168. 'password': 'password',
  169. 'login': 'admin'
  170. }
  171. self.assertDictEqual(expected_result_serialize, rest_api1.serialize())
  172. self.assertTrue(rest_api1.__eq__(rest_api3))
  173. self.assertFalse(rest_api1.__eq__(rest_api2))
  174. def test_Settings(self):
  175. with mock.patch('platform.machine', return_value='pumpkins'):
  176. rest_api1 = RestAPI(password_protected=True,
  177. login="admin",
  178. password="password",
  179. active=True,
  180. port=5000, allowed_cors_origin="*")
  181. recognition_options = RecognitionOptions()
  182. setting1 = Settings(default_tts_name="pico2wav",
  183. default_stt_name="google",
  184. default_trigger_name="swoyboy",
  185. default_player_name="mplayer",
  186. ttss=["ttts"],
  187. stts=["stts"],
  188. triggers=["snowboy"],
  189. players=["mplayer"],
  190. rest_api=rest_api1,
  191. cache_path="/tmp/kalliope",
  192. resources=None,
  193. variables={"key1": "val1"},
  194. recognition_options=recognition_options,
  195. options={'deaf': False, 'mute': False})
  196. setting1.kalliope_version = "0.4.5"
  197. setting2 = Settings(default_tts_name="accapela",
  198. default_stt_name="bing",
  199. default_trigger_name="swoyboy",
  200. default_player_name="mplayer",
  201. ttss=["ttts"],
  202. stts=["stts"],
  203. triggers=["snowboy"],
  204. rest_api=rest_api1,
  205. cache_path="/tmp/kalliope_tmp",
  206. resources=None,
  207. variables={"key1": "val1"},
  208. recognition_options=recognition_options,
  209. options={'deaf': False, 'mute': False})
  210. setting2.kalliope_version = "0.4.5"
  211. setting3 = Settings(default_tts_name="pico2wav",
  212. default_stt_name="google",
  213. default_trigger_name="swoyboy",
  214. default_player_name="mplayer",
  215. ttss=["ttts"],
  216. stts=["stts"],
  217. triggers=["snowboy"],
  218. players=["mplayer"],
  219. rest_api=rest_api1,
  220. cache_path="/tmp/kalliope",
  221. resources=None,
  222. variables={"key1": "val1"},
  223. recognition_options=recognition_options,
  224. options={'deaf': False, 'mute': False})
  225. setting3.kalliope_version = "0.4.5"
  226. expected_result_serialize = {
  227. 'default_tts_name': 'pico2wav',
  228. 'hooks': None,
  229. 'rest_api':
  230. {
  231. 'password_protected': True,
  232. 'port': 5000,
  233. 'active': True,
  234. 'allowed_cors_origin': '*',
  235. 'password': 'password',
  236. 'login': 'admin'
  237. },
  238. 'default_stt_name': 'google',
  239. 'kalliope_version': '0.4.5',
  240. 'default_trigger_name': 'swoyboy',
  241. 'default_player_name': 'mplayer',
  242. 'cache_path': '/tmp/kalliope',
  243. 'stts': ['stts'],
  244. 'machine': 'pumpkins',
  245. 'ttss': ['ttts'],
  246. 'variables': {'key1': 'val1'},
  247. 'resources': None,
  248. 'triggers': ['snowboy'],
  249. 'players': ['mplayer'],
  250. 'recognition_options': {'energy_threshold': 4000, 'adjust_for_ambient_noise_second': 0},
  251. 'options': {'deaf': False, 'mute': False}
  252. }
  253. self.maxDiff = None
  254. self.assertDictEqual(expected_result_serialize, setting1.serialize())
  255. self.assertTrue(setting1.__eq__(setting3))
  256. self.assertFalse(setting1.__eq__(setting2))
  257. def test_Stt(self):
  258. stt1 = Stt(name="stt1", parameters={"key1": "val1"})
  259. stt2 = Stt(name="stt2", parameters={"key2": "val2"})
  260. stt3 = Stt(name="stt1", parameters={"key1": "val1"})
  261. expected_result_serialize = {'name': 'stt1', 'parameters': {'key1': 'val1'}}
  262. self.assertDictEqual(expected_result_serialize, stt1.serialize())
  263. self.assertTrue(stt1.__eq__(stt3))
  264. self.assertFalse(stt1.__eq__(stt2))
  265. def test_Synapse(self):
  266. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  267. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  268. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  269. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  270. signal1 = Signal(name="order", parameters="this is the sentence")
  271. signal2 = Signal(name="order", parameters="this is the second sentence")
  272. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  273. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  274. synapse3 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  275. expected_result_serialize = {
  276. 'signals': [
  277. {
  278. 'name': 'order',
  279. 'parameters': 'this is the sentence'
  280. }
  281. ],
  282. 'neurons': [
  283. {
  284. 'name': 'neurone1',
  285. 'parameters': {
  286. 'var1': 'val1'
  287. }
  288. },
  289. {
  290. 'name': 'neurone2',
  291. 'parameters':
  292. {
  293. 'var2': 'val2'
  294. }
  295. }
  296. ],
  297. 'name': 'Synapse1'
  298. }
  299. self.assertDictEqual(expected_result_serialize, synapse1.serialize())
  300. self.assertTrue(synapse1.__eq__(synapse3))
  301. self.assertFalse(synapse1.__eq__(synapse2))
  302. def test_Trigger(self):
  303. trigger1 = Trigger(name="trigger1", parameters={"key1": "val1"})
  304. trigger2 = Trigger(name="trigger2", parameters={"key2": "val2"})
  305. trigger3 = Trigger(name="trigger1", parameters={"key1": "val1"})
  306. expected_result_serialize = {'name': 'trigger1', 'parameters': {'key1': 'val1'}}
  307. self.assertDictEqual(expected_result_serialize, trigger1.serialize())
  308. self.assertTrue(trigger1.__eq__(trigger3))
  309. self.assertFalse(trigger1.__eq__(trigger2))
  310. def test_Player(self):
  311. player1 = Player(name="player1", parameters={"key1": "val1"})
  312. player2 = Player(name="player2", parameters={"key2": "val2"})
  313. player3 = Player(name="player1", parameters={"key1": "val1"})
  314. expected_result_serialize = {'name': 'player1', 'parameters': {'key1': 'val1'}}
  315. self.assertDictEqual(expected_result_serialize, player1.serialize())
  316. self.assertTrue(player1.__eq__(player3))
  317. self.assertFalse(player1.__eq__(player2))
  318. def test_Tts(self):
  319. tts1 = Tts(name="tts1", parameters={"key1": "val1"})
  320. tts2 = Tts(name="tts2", parameters={"key2": "val2"})
  321. tts3 = Tts(name="tts1", parameters={"key1": "val1"})
  322. expected_result_serialize = {'name': 'tts1', 'parameters': {'key1': 'val1'}}
  323. self.assertDictEqual(expected_result_serialize, tts1.serialize())
  324. self.assertTrue(tts1.__eq__(tts3))
  325. self.assertFalse(tts1.__eq__(tts2))
  326. if __name__ == '__main__':
  327. unittest.main()
  328. # suite = unittest.TestSuite()
  329. # suite.addTest(TestLIFOBuffer("test_process_neuron_list"))
  330. # runner = unittest.TextTestRunner()
  331. # runner.run(suite)