test_brain_loader.py 10 KB

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