浏览代码

[Tests] Add OrderAnalyser tests for default + some refacto

monf 8 年之前
父节点
当前提交
cddaf6bb59
共有 4 个文件被更改,包括 83 次插入4 次删除
  1. 80 1
      Tests/test_order_analyser.py
  2. 1 1
      kalliope/core/NeuroneLauncher.py
  3. 1 1
      kalliope/core/OrderAnalyser.py
  4. 1 1
      kalliope/core/SynapseLauncher.py

+ 80 - 1
Tests/test_order_analyser.py

@@ -1,9 +1,11 @@
 import unittest
-
+import mock
 
 from kalliope.core.OrderAnalyser import OrderAnalyser
 from kalliope.core.Models.Neuron import Neuron
 from kalliope.core.Models.Synapse import Synapse
+from kalliope.core.Models.Brain import Brain
+from kalliope.core.NeuroneLauncher import NeuroneLauncher
 from kalliope.core.Models.Order import Order
 
 
@@ -14,6 +16,83 @@ class TestOrderAnalyser(unittest.TestCase):
     def setUp(self):
         pass
 
+    def test_start(self):
+        """
+        Testing if the matches from the incoming messages and the signals/order sentences.
+        Scenarii :
+            - Order matchs a synapse and the synapse has been launched.
+            - Order does not match but have a default synapse.
+            - Order does not match and does not ahve default synapse.
+        """
+        # Init
+        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
+        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
+        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
+        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
+
+        signal1 = Order(sentence="this is the sentence")
+        signal2 = Order(sentence="this is the second sentence")
+        signal3 = Order(sentence="that is part of the third sentence")
+
+        synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
+        synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
+        synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
+
+        all_synapse_list = [synapse1,
+                            synapse2,
+                            synapse3]
+
+        br = Brain(synapses=all_synapse_list)
+
+        def _start_neuron_mock(cls, neuron, params):
+            pass
+
+        with mock.patch.object(OrderAnalyser, '_start_neuron', new=_start_neuron_mock) as mock_start_neuron_method:
+            # assert synapses have been launched
+            order_to_match = "this is the sentence"
+            oa = OrderAnalyser(order=order_to_match,
+                               brain=br)
+            expected_result = [synapse1]
+
+            self.assertEquals(oa.start(),
+                              expected_result,
+                              "Fail to run the expected Synapse matching the order")
+
+            # TODO investigate why this one fails
+            # calls = [mock.call(neuron1, {'var1':'val1'}), mock.call(neuron2, {'var2':'val2'})]
+            # mock_start_neuron_method.assert_has_calls(calls=calls)
+
+            # No order matching Default Synapse to run
+            order_to_match = "random sentence"
+            oa = OrderAnalyser(order=order_to_match,
+                               brain=br)
+            oa.settings = mock.MagicMock(default_synapse="Synapse3")
+            expected_result = [synapse3]
+            self.assertEquals(oa.start(),
+                              expected_result,
+                              "Fail to run the default Synapse because no other synapses match the order")
+
+            # No order matching no Default Synapse
+            order_to_match = "random sentence"
+            oa = OrderAnalyser(order=order_to_match,
+                               brain=br)
+            oa.settings = mock.MagicMock()
+            expected_result = []
+            self.assertEquals(oa.start(),
+                              expected_result,
+                              "Fail to no synapse because no synapse matchs and no default defined")
+
+    def test_start_neuron(self):
+        """
+        Testing params association and starting a Neuron
+        """
+        def start_neuron_mock(cls, neuron):
+            pass
+
+        with mock.patch.object(NeuroneLauncher, 'start_neuron', new=start_neuron_mock) as mock_start_neuron_method:
+            pass
+
+
     def test_is_containing_bracket(self):
         #  Success
         order_to_test = "This test contains {{ bracket }}"

+ 1 - 1
kalliope/core/NeuroneLauncher.py

@@ -12,7 +12,7 @@ class NeuroneLauncher:
         pass
 
     @classmethod
-    def start_neurone(cls, neuron):
+    def start_neuron(cls, neuron):
         """
         Start a neuron plugin
         :param neuron: neuron object

+ 1 - 1
kalliope/core/OrderAnalyser.py

@@ -133,7 +133,7 @@ class OrderAnalyser:
 
         # if no error detected, we run the neuron
         if not problem_in_neuron_found:
-            NeuroneLauncher.start_neurone(neuron)
+            NeuroneLauncher.start_neuron(neuron)
         else:
             Utils.print_danger("A problem has been found in the Synapse.")
 

+ 1 - 1
kalliope/core/SynapseLauncher.py

@@ -46,5 +46,5 @@ class SynapseLauncher(object):
         :return:
         """
         for neuron in synapse.neurons:
-            NeuroneLauncher.start_neurone(neuron)
+            NeuroneLauncher.start_neuron(neuron)
         return True