test_brain_loader.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # coding: utf8
  2. import os
  3. import unittest
  4. from kalliope.core.Models import Singleton, Signal
  5. from kalliope.core.ConfigurationManager import BrainLoader
  6. from kalliope.core.Models import Neuron
  7. from kalliope.core.Models import Synapse
  8. from kalliope.core.Models.Brain import Brain
  9. from kalliope.core.Models.settings.Settings import Settings
  10. class TestBrainLoader(unittest.TestCase):
  11. def setUp(self):
  12. # be sure the brain haven't been instantiated before
  13. Singleton._instances = dict()
  14. if "/Tests" in os.getcwd():
  15. self.brain_to_test = os.getcwd() + os.sep + "brains/brain_test.yml"
  16. else:
  17. self.brain_to_test = os.getcwd() + os.sep + "Tests/brains/brain_test.yml"
  18. self.expected_result = [
  19. {'signals': [{'order': 'test_order'}],
  20. 'neurons': [{'say': {'message': ['test message']}}],
  21. 'name': 'test'},
  22. {'signals': [{'order': 'test_order_2'}],
  23. 'neurons': [{'say': {'message': ['test message']}}],
  24. 'name': 'test2'},
  25. {'signals': [{'order': 'order_for_int'}],
  26. 'neurons': [{'sleep': {'seconds': 60}}],
  27. 'name': 'testint'},
  28. {'includes': ['included_brain_test.yml']},
  29. {'signals': [{'order': 'test_order_3'}],
  30. 'neurons': [{'say': {'message': ['test message']}}],
  31. 'name': 'test3'}
  32. ]
  33. def tearDown(self):
  34. Singleton._instances = dict()
  35. def test_get_yaml_config(self):
  36. """
  37. Test we can get a yaml config from the path
  38. """
  39. brain_loader = BrainLoader(file_path=self.brain_to_test)
  40. self.assertEqual(brain_loader.yaml_config, self.expected_result)
  41. def test_load_brain(self):
  42. """
  43. Test the class return a valid brain object
  44. """
  45. neuron = Neuron(name='say', parameters={'message': ['test message']})
  46. neuron2 = Neuron(name='sleep', parameters={'seconds': 60})
  47. signal1 = Signal(name="order", parameters="test_order")
  48. signal2 = Signal(name="order", parameters="test_order_2")
  49. signal3 = Signal(name="order", parameters="test_order_3")
  50. signal4 = Signal(name="order", parameters="order_for_int")
  51. synapse1 = Synapse(name="test", neurons=[neuron], signals=[signal1])
  52. synapse2 = Synapse(name="test2", neurons=[neuron], signals=[signal2])
  53. synapse3 = Synapse(name="test3", neurons=[neuron], signals=[signal3])
  54. synapse4 = Synapse(name="testint", neurons=[neuron2], signals=[signal4])
  55. synapses = [synapse1, synapse2, synapse4, synapse3]
  56. brain = Brain()
  57. brain.synapses = synapses
  58. brain.brain_file = self.brain_to_test
  59. brain.brain_yaml = self.expected_result
  60. brain_loader = BrainLoader(file_path=self.brain_to_test)
  61. self.assertEqual(brain, brain_loader.brain)
  62. def test_get_neurons(self):
  63. """
  64. Test to get neurons from the brainLoader
  65. scenarii:
  66. - 1/ get a simple neuron from the brainloader
  67. - 2/ get a neuron with global variables as parameters
  68. - 3/ get a neuron with int as parameters
  69. """
  70. # 1/ get a simple neuron from the brainloader
  71. st = Settings()
  72. neuron_list = [{'say': {'message': ['test message']}}]
  73. neuron = Neuron(name='say', parameters={'message': ['test message']})
  74. bl = BrainLoader(file_path=self.brain_to_test)
  75. neurons_from_brain_loader = bl._get_neurons(neuron_list,
  76. settings=st)
  77. self.assertEqual([neuron], neurons_from_brain_loader)
  78. # 2/ get a neuron with global variables as parameters
  79. neuron_list = [{'say': {'message': ['bonjour {{name}}']}}]
  80. variables = {
  81. "author": "Lamonf",
  82. "test_number": 60,
  83. "name": "kalliope"
  84. }
  85. st = Settings(variables=variables)
  86. bl = BrainLoader(file_path=self.brain_to_test)
  87. neurons_from_brain_loader = bl._get_neurons(neuron_list,
  88. settings=st)
  89. neuron = Neuron(name='say', parameters={'message': ['bonjour kalliope']})
  90. self.assertEqual([neuron], neurons_from_brain_loader)
  91. # 3/ get a neuron with int as parameters
  92. st = Settings()
  93. neuron_list = [{'sleep': {'seconds': 60}}]
  94. neuron = Neuron(name='sleep', parameters={'seconds': 60})
  95. bl = BrainLoader(file_path=self.brain_to_test)
  96. neurons_from_brain_loader = bl._get_neurons(neuron_list,
  97. settings=st)
  98. self.assertEqual([neuron], neurons_from_brain_loader)
  99. def test_get_signals(self):
  100. signals = [{'order': 'test_order'}]
  101. signal = Signal(name="order", parameters="test_order")
  102. bl = BrainLoader(file_path=self.brain_to_test)
  103. signals_from_brain_loader = bl._get_signals(signals)
  104. self.assertEqual([signal], signals_from_brain_loader)
  105. def test_singleton(self):
  106. bl1 = BrainLoader(file_path=self.brain_to_test)
  107. bl2 = BrainLoader(file_path=self.brain_to_test)
  108. self.assertTrue(bl1.brain is bl2.brain)
  109. def test_replace_global_variables(self):
  110. """
  111. Testing the _replace_global_variables function from the NeuronLauncher.
  112. Scenarii:
  113. - 1/ only one global variable
  114. - 2/ global variable with string after
  115. - 3/ global variable with int after
  116. - 4/ multiple global variables
  117. - 5/ parameter value is a list
  118. - 6/ parameter is a dict
  119. """
  120. # 1/ only one global variable
  121. parameters = {
  122. 'var1': '{{hello}}'
  123. }
  124. variables = {
  125. "hello": "test",
  126. "hello2": "test2",
  127. }
  128. st = Settings(variables=variables)
  129. expected_parameters = {
  130. 'var1': 'test'
  131. }
  132. self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters,
  133. settings=st),
  134. expected_parameters,
  135. "Fail to assign a single global variable to parameters")
  136. # 2/ global variable with string after
  137. parameters = {
  138. 'var1': '{{hello}} Sispheor'
  139. }
  140. variables = {
  141. "hello": "test",
  142. "hello2": "test2",
  143. }
  144. st = Settings(variables=variables)
  145. expected_parameters = {
  146. 'var1': 'test Sispheor'
  147. }
  148. self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters,
  149. settings=st),
  150. expected_parameters,
  151. "Fail to assign a global variable with string after to parameters")
  152. # 3/ global variable with int after
  153. parameters = {
  154. 'var1': '{{hello}}0'
  155. }
  156. variables = {
  157. "hello": 60,
  158. "hello2": "test2",
  159. }
  160. st = Settings(variables=variables)
  161. expected_parameters = {
  162. 'var1': '600'
  163. }
  164. self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters,
  165. settings=st),
  166. expected_parameters,
  167. "Fail to assign global variable with int after to parameters")
  168. # 4/ multiple global variables
  169. parameters = {
  170. 'var1': '{{hello}} {{me}}'
  171. }
  172. variables = {
  173. "hello": "hello",
  174. "me": "LaMonf"
  175. }
  176. st = Settings(variables=variables)
  177. expected_parameters = {
  178. 'var1': 'hello LaMonf'
  179. }
  180. self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters,
  181. settings=st),
  182. expected_parameters,
  183. "Fail to assign multiple global variables to parameters")
  184. # 5/ parameter value is a list
  185. parameters = {
  186. 'var1': '[hello {{name}}, bonjour {{name}}]'
  187. }
  188. variables = {
  189. "name": "LaMonf",
  190. "hello2": "test2",
  191. }
  192. st = Settings(variables=variables)
  193. expected_parameters = {
  194. 'var1': '[hello LaMonf, bonjour LaMonf]'
  195. }
  196. self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters,
  197. settings=st),
  198. expected_parameters,
  199. "Fail to assign a single global when parameter value is a list to neuron")
  200. # 6/ parameter is a dict
  201. parameters = {'from_answer_link': [{'synapse': 'synapse2', 'answers': ['absolument', '{{ name }}']},
  202. {'synapse': 'synapse3', 'answers': ['{{ name }}']}], 'default': 'synapse4'}
  203. variables = {
  204. "name": "nico"
  205. }
  206. st = Settings(variables=variables)
  207. expected_parameters = {
  208. 'from_answer_link': [
  209. {'synapse': 'synapse2', 'answers': ['absolument', 'nico']},
  210. {'synapse': 'synapse3', 'answers': ['nico']}], 'default': 'synapse4'
  211. }
  212. self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters,
  213. settings=st),
  214. expected_parameters,
  215. "Fail to assign a single global when parameter value is a list to neuron")
  216. def test_get_global_variable(self):
  217. """
  218. Test the get_global_variable of the OrderAnalyser Class
  219. """
  220. sentence = "i am {{name2}}"
  221. variables = {
  222. "name": "LaMonf",
  223. "name2": "kalliope",
  224. "name3": u"kalliopé",
  225. "name4": 1
  226. }
  227. st = Settings(variables=variables)
  228. expected_result = "i am kalliope"
  229. self.assertEqual(BrainLoader._get_global_variable(sentence=sentence,
  230. settings=st),
  231. expected_result)
  232. # test with accent
  233. sentence = "i am {{name3}}"
  234. expected_result = u"i am kalliopé"
  235. self.assertEqual(BrainLoader._get_global_variable(sentence=sentence,
  236. settings=st),
  237. expected_result)
  238. # test with int
  239. sentence = "i am {{name4}}"
  240. expected_result = "i am 1"
  241. self.assertEqual(BrainLoader._get_global_variable(sentence=sentence,
  242. settings=st),
  243. expected_result)
  244. if __name__ == '__main__':
  245. unittest.main()