test_cortex.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import unittest
  2. from kalliope.core.Cortex import Cortex
  3. class TestCortex(unittest.TestCase):
  4. def setUp(self):
  5. # cleanup the cortex memory
  6. Cortex.memory = dict()
  7. Cortex.temp = dict()
  8. def test_get_memory(self):
  9. test_memory = {
  10. "key1": "value1",
  11. "key2": "value2"
  12. }
  13. Cortex.memory = test_memory
  14. self.assertDictEqual(test_memory, Cortex.get_memory())
  15. def test_save(self):
  16. key_to_save = "key1"
  17. value_to_save = "value1"
  18. expected_memory = {
  19. "key1": "value1"
  20. }
  21. Cortex.save(key=key_to_save, value=value_to_save)
  22. self.assertDictEqual(expected_memory, Cortex.memory)
  23. def test_get_from_key(self):
  24. test_memory = {
  25. "key1": "value1",
  26. "key2": "value2"
  27. }
  28. Cortex.memory = test_memory
  29. expected_value = "value2"
  30. self.assertEqual(expected_value, Cortex.get_from_key("key2"))
  31. def test_add_parameters_from_order(self):
  32. order_parameters = {
  33. "key1": "value1",
  34. "key2": "value2"
  35. }
  36. expected_temp_dict = {
  37. "key1": "value1",
  38. "key2": "value2"
  39. }
  40. Cortex.add_parameters_from_order(order_parameters)
  41. self.assertDictEqual(Cortex.temp, expected_temp_dict)
  42. def test_clean_parameter_from_order(self):
  43. Cortex.temp = {
  44. "key1": "value1",
  45. "key2": "value2"
  46. }
  47. Cortex.clean_parameter_from_order()
  48. expected_temp_dict = dict()
  49. self.assertDictEqual(expected_temp_dict, Cortex.memory)
  50. def test_save_neuron_parameter_in_memory(self):
  51. # test with a list of parameter with bracket
  52. neuron1_parameters = {
  53. "key1": "value1",
  54. "key2": "value2"
  55. }
  56. dict_val_to_save = {"my_key_in_memory": "{{key1}}"}
  57. expected_dict = {"my_key_in_memory": "value1"}
  58. Cortex.save_neuron_parameter_in_memory(kalliope_memory_dict=dict_val_to_save,
  59. neuron_parameters=neuron1_parameters)
  60. self.assertDictEqual(expected_dict, Cortex.memory)
  61. # test with a list of parameter with brackets and string
  62. self.setUp() # clean
  63. neuron1_parameters = {
  64. "key1": "value1",
  65. "key2": "value2"
  66. }
  67. dict_val_to_save = {"my_key_in_memory": "string {{key1}}"}
  68. expected_dict = {"my_key_in_memory": "string value1"}
  69. Cortex.save_neuron_parameter_in_memory(kalliope_memory_dict=dict_val_to_save,
  70. neuron_parameters=neuron1_parameters)
  71. self.assertDictEqual(expected_dict, Cortex.memory)
  72. # test with a list of parameter with only a string. Neuron parameters are not used
  73. self.setUp() # clean
  74. neuron1_parameters = {
  75. "key1": "value1",
  76. "key2": "value2"
  77. }
  78. dict_val_to_save = {"my_key_in_memory": "string"}
  79. expected_dict = {"my_key_in_memory": "string"}
  80. Cortex.save_neuron_parameter_in_memory(kalliope_memory_dict=dict_val_to_save,
  81. neuron_parameters=neuron1_parameters)
  82. self.assertDictEqual(expected_dict, Cortex.memory)
  83. # test with an empty list of parameter to save (no kalliope_memory set)
  84. self.setUp() # clean
  85. neuron1_parameters = {
  86. "key1": "value1",
  87. "key2": "value2"
  88. }
  89. dict_val_to_save = None
  90. Cortex.save_neuron_parameter_in_memory(kalliope_memory_dict=dict_val_to_save,
  91. neuron_parameters=neuron1_parameters)
  92. self.assertDictEqual(dict(), Cortex.memory)
  93. def test_save_parameter_from_order_in_memory(self):
  94. # Test with a value that exist in the temp memory
  95. order_parameters = {
  96. "key1": "value1",
  97. "key2": "value2"
  98. }
  99. Cortex.temp = order_parameters
  100. dict_val_to_save = {"my_key_in_memory": "{{key1}}"}
  101. expected_dict = {"my_key_in_memory": "value1"}
  102. Cortex.save_parameter_from_order_in_memory(dict_val_to_save)
  103. self.assertDictEqual(expected_dict, Cortex.memory)
  104. # test with a value that does not exsit
  105. order_parameters = {
  106. "key1": "value1",
  107. "key2": "value2"
  108. }
  109. Cortex.temp = order_parameters
  110. dict_val_to_save = {"my_key_in_memory": "{{key3}}"}
  111. self.assertFalse(Cortex.save_parameter_from_order_in_memory(dict_val_to_save))
  112. if __name__ == '__main__':
  113. unittest.main()