test_brain_loader.py 9.7 KB

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