test_order_analyser.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import unittest
  2. from kalliope.core.Models import Brain
  3. from kalliope.core.Models import Neuron
  4. from kalliope.core.Models import Order
  5. from kalliope.core.Models import Synapse
  6. from kalliope.core.OrderAnalyser import OrderAnalyser
  7. class TestOrderAnalyser(unittest.TestCase):
  8. """Test case for the OrderAnalyser Class"""
  9. def setUp(self):
  10. pass
  11. def test_get_matching_synapse(self):
  12. # Init
  13. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  14. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  15. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  16. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  17. signal1 = Order(sentence="this is the sentence")
  18. signal2 = Order(sentence="this is the second sentence")
  19. signal3 = Order(sentence="that is part of the third sentence")
  20. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  21. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  22. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  23. all_synapse_list = [synapse1,
  24. synapse2,
  25. synapse3]
  26. br = Brain(synapses=all_synapse_list)
  27. # TEST1: should return synapse1
  28. spoken_order = "this is the sentence"
  29. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  30. self.assertEqual(len(matched_synapses), 1)
  31. self.assertTrue(any(synapse1 in matched_synapse for matched_synapse in matched_synapses))
  32. # TEST2: should return synapse1 and 2
  33. spoken_order = "this is the second sentence"
  34. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  35. self.assertEqual(len(matched_synapses), 2)
  36. self.assertTrue(synapse1, synapse2 in matched_synapses)
  37. # TEST3: should empty
  38. spoken_order = "not a valid order"
  39. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  40. self.assertFalse(matched_synapses)
  41. def test_spelt_order_match_brain_order_via_table(self):
  42. order_to_test = "this is the order"
  43. sentence_to_test = "this is the order"
  44. # Success
  45. self.assertTrue(OrderAnalyser.spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test))
  46. # Failure
  47. sentence_to_test = "unexpected sentence"
  48. self.assertFalse(OrderAnalyser.spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test))
  49. # Upper/lower cases
  50. sentence_to_test = "THIS is THE order"
  51. self.assertTrue(OrderAnalyser.spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test))
  52. def test_get_split_order_without_bracket(self):
  53. # Success
  54. order_to_test = "this is the order"
  55. expected_result = ["this", "is", "the", "order"]
  56. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  57. "No brackets Fails to return the expected list")
  58. order_to_test = "this is the {{ order }}"
  59. expected_result = ["this", "is", "the"]
  60. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  61. "With spaced brackets Fails to return the expected list")
  62. order_to_test = "this is the {{order }}" # left bracket without space
  63. expected_result = ["this", "is", "the"]
  64. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  65. "Left brackets Fails to return the expected list")
  66. order_to_test = "this is the {{ order}}" # right bracket without space
  67. expected_result = ["this", "is", "the"]
  68. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  69. "Right brackets Fails to return the expected list")
  70. order_to_test = "this is the {{order}}" # bracket without space
  71. expected_result = ["this", "is", "the"]
  72. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  73. "No space brackets Fails to return the expected list")
  74. def test_counter_subset(self):
  75. list1 = ("word1", "word2")
  76. list2 = ("word3", "word4")
  77. list3 = ("word1", "word2", "word3", "word4")
  78. self.assertFalse(OrderAnalyser._counter_subset(list1, list2))
  79. self.assertTrue(OrderAnalyser._counter_subset(list1, list3))
  80. self.assertTrue(OrderAnalyser._counter_subset(list2, list3))
  81. if __name__ == '__main__':
  82. unittest.main()