test_models.py 16 KB

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