test_brain_loader.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import unittest
  2. from kalliope.core.Models import Singleton
  3. from kalliope.core.ConfigurationManager import BrainLoader
  4. from kalliope.core.Models import Event
  5. from kalliope.core.Models import Neuron
  6. from kalliope.core.Models import Synapse
  7. from kalliope.core.Models import Order
  8. from kalliope.core.Models.Brain import Brain
  9. class TestBrainLoader(unittest.TestCase):
  10. def setUp(self):
  11. self.brain_to_test = "../Tests/brains/brain_test.yml"
  12. self.expected_result = [
  13. {'signals': [{'order': 'test_order'}],
  14. 'neurons': [{'say': {'message': ['test message']}}],
  15. 'name': 'test'},
  16. {'signals': [{'order': 'test_order_2'}],
  17. 'neurons': [{'say': {'message': ['test message']}}],
  18. 'name': 'test2'},
  19. {'includes': ['included_brain_test.yml']},
  20. {'signals': [{'order': 'test_order_3'}],
  21. 'neurons': [{'say': {'message': ['test message']}}],
  22. 'name': 'test3'}
  23. ]
  24. def tearDown(self):
  25. Singleton._instances = {}
  26. def test_get_yaml_config(self):
  27. """
  28. Test we can get a yaml config from the path
  29. """
  30. brain_loader = BrainLoader(file_path=self.brain_to_test)
  31. self.assertEqual(brain_loader.yaml_config, self.expected_result)
  32. del brain_loader
  33. def test_get_brain(self):
  34. """
  35. Test the class return a valid brain object
  36. """
  37. neuron = Neuron(name='say', parameters={'message': ['test message']})
  38. signal1 = Order(sentence="test_order")
  39. signal2 = Order(sentence="test_order_2")
  40. signal3 = Order(sentence="test_order_3")
  41. synapse1 = Synapse(name="test", neurons=[neuron], signals=[signal1])
  42. synapse2 = Synapse(name="test2", neurons=[neuron], signals=[signal2])
  43. synapse3 = Synapse(name="test3", neurons=[neuron], signals=[signal3])
  44. synapses = [synapse1, synapse2, synapse3]
  45. brain = Brain()
  46. brain.synapses = synapses
  47. brain.brain_file = self.brain_to_test
  48. brain.brain_yaml = self.expected_result
  49. brain_loader = BrainLoader(file_path=self.brain_to_test)
  50. self.assertEqual(brain, brain_loader.brain)
  51. del brain_loader
  52. def test_get_neurons(self):
  53. neuron_list = [{'say': {'message': ['test message']}}]
  54. neuron = Neuron(name='say', parameters={'message': ['test message']})
  55. bl = BrainLoader(file_path=self.brain_to_test)
  56. neurons_from_brain_loader = bl._get_neurons(neuron_list)
  57. self.assertEqual([neuron], neurons_from_brain_loader)
  58. del bl
  59. def test_get_signals(self):
  60. signals = [{'order': 'test_order'}]
  61. signal = Order(sentence='test_order')
  62. bl = BrainLoader(file_path=self.brain_to_test)
  63. signals_from_brain_loader = bl._get_signals(signals)
  64. self.assertEqual([signal], signals_from_brain_loader)
  65. del bl
  66. def test_get_event_or_order_from_dict(self):
  67. order_object = Order(sentence="test_order")
  68. event_object = Event(period="0 7 * * *")
  69. dict_order = {'order': 'test_order'}
  70. dict_event = {'event': '0 7 * * *'}
  71. bl = BrainLoader(file_path=self.brain_to_test)
  72. order_from_bl = bl._get_event_or_order_from_dict(dict_order)
  73. event_from_bl = bl._get_event_or_order_from_dict(dict_event)
  74. self.assertEqual(order_from_bl, order_object)
  75. self.assertEqual(event_from_bl, event_object)
  76. del bl
  77. def test_singleton(self):
  78. bl1 = BrainLoader(file_path=self.brain_to_test)
  79. bl2 = BrainLoader(file_path=self.brain_to_test)
  80. self.assertTrue(bl1.brain is bl2.brain)
  81. del bl1
  82. del bl2
  83. if __name__ == '__main__':
  84. unittest.main()