Browse Source

add tests for parameter loader and synapses launcher

nico 8 years ago
parent
commit
44a6b738c6

+ 175 - 0
Tests/test_neuron_parameter_loader.py

@@ -0,0 +1,175 @@
+import unittest
+
+from kalliope.core.NeuronParameterLoader import NeuronParameterLoader
+
+
+class TestNeuronParameterLoader(unittest.TestCase):
+
+    def test_get_parameters(self):
+
+        synapse_order = "this is the {{ sentence }}"
+        user_order = "this is the value"
+        expected_result = {'sentence': 'value'}
+
+        self.assertEquals(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
+                          expected_result,
+                          "Fail to retrieve 'the params' of the synapse_order from the order")
+
+        # Multiple match
+        synapse_order = "this is the {{ sentence }}"
+
+        user_order = "this is the value with multiple words"
+        expected_result = {'sentence': 'value with multiple words'}
+
+        self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
+                         expected_result,
+                         "Fail to retrieve the 'multiple words params' of the synapse_order from the order")
+
+        # Multiple params
+        synapse_order = "this is the {{ sentence }} with multiple {{ params }}"
+
+        user_order = "this is the value with multiple words"
+        expected_result = {'sentence': 'value',
+                            'params':'words'}
+
+        self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
+                         expected_result,
+                         "Fail to retrieve the 'multiple params' of the synapse_order from the order")
+
+        # Multiple params with multiple words
+        synapse_order = "this is the {{ sentence }} with multiple {{ params }}"
+
+        user_order = "this is the multiple values with multiple values as words"
+        expected_result = {'sentence': 'multiple values',
+                           'params': 'values as words'}
+
+        self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
+                         expected_result)
+
+        # params at the begining of the sentence
+        synapse_order = "{{ sentence }} this is the sentence"
+
+        user_order = "hello world this is the multiple values with multiple values as words"
+        expected_result = {'sentence': 'hello world'}
+
+        self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
+                         expected_result)
+
+        # all of the sentence is a variable
+        synapse_order = "{{ sentence }}"
+
+        user_order = "this is the all sentence is a variable"
+        expected_result = {'sentence': 'this is the all sentence is a variable'}
+
+        self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
+                         expected_result)
+
+    def test_associate_order_params_to_values(self):
+        ##
+        # Testing the brackets position behaviour
+        ##
+
+        # Success
+        order_brain = "This is the {{ variable }}"
+        order_user = "This is the value"
+        expected_result = {'variable': 'value'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        # Success
+        order_brain = "This is the {{variable }}"
+        order_user = "This is the value"
+        expected_result = {'variable': 'value'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        # Success
+        order_brain = "This is the {{ variable}}"
+        order_user = "This is the value"
+        expected_result = {'variable': 'value'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        # Success
+        order_brain = "This is the {{variable}}"
+        order_user = "This is the value"
+        expected_result = {'variable': 'value'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        # Fail
+        order_brain = "This is the {variable}"
+        order_user = "This is the value"
+        expected_result = {'variable': 'value'}
+        self.assertNotEquals(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                             expected_result)
+
+        # Fail
+        order_brain = "This is the { variable}}"
+        order_user = "This is the value"
+        expected_result = {'variable': 'value'}
+        self.assertNotEquals(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                             expected_result)
+
+        ##
+        # Testing the brackets position in the sentence
+        ##
+
+        # Success
+        order_brain = "{{ variable }} This is the"
+        order_user = "value This is the"
+        expected_result = {'variable': 'value'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        # Success
+        order_brain = "This is {{ variable }} the"
+        order_user = " This is value the"
+        expected_result = {'variable': 'value'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        ##
+        # Testing multi variables
+        ##
+
+        # Success
+        order_brain = "This is {{ variable }} the {{ variable2 }}"
+        order_user = "This is value the value2"
+        expected_result = {'variable': 'value',
+                           'variable2': 'value2'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        ##
+        # Testing multi words in variable
+        ##
+
+        # Success
+        order_brain = "This is the {{ variable }}"
+        order_user = "This is the value with multiple words"
+        expected_result = {'variable': 'value with multiple words'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        # Success
+        order_brain = "This is the {{ variable }} and  {{ variable2 }}"
+        order_user = "This is the value with multiple words and second value multiple"
+        expected_result = {'variable': 'value with multiple words',
+                           'variable2': 'second value multiple'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        ##
+        #  Specific Behaviour
+        ##
+
+        # Upper/Lower case
+        order_brain = "This Is The {{ variable }}"
+        order_user = "ThiS is tHe VAlue"
+        expected_result = {'variable': 'VAlue'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+if __name__ == '__main__':
+    unittest.main()

+ 8 - 1
Tests/test_order_analyser2.py

@@ -47,8 +47,15 @@ class TestOrderAnalyser2(unittest.TestCase):
         spoken_order = "this is the sentence"
         matched_synapses = OrderAnalyser2.get_matching_synapse(order=spoken_order, brain=br)
         self.assertEqual(len(matched_synapses), 1)
+        self.assertTrue(any(synapse1 in matched_synapse for matched_synapse in matched_synapses))
 
-        # TEST2: should empty
+        # TEST2: should return synapse1 and 2
+        spoken_order = "this is the second sentence"
+        matched_synapses = OrderAnalyser2.get_matching_synapse(order=spoken_order, brain=br)
+        self.assertEqual(len(matched_synapses), 2)
+        self.assertTrue(synapse1, synapse2 in matched_synapses)
+
+        # TEST3: should empty
         spoken_order = "not a valid order"
         matched_synapses = OrderAnalyser2.get_matching_synapse(order=spoken_order, brain=br)
         self.assertFalse(matched_synapses)

+ 79 - 0
Tests/test_synapse_launcher.py

@@ -0,0 +1,79 @@
+import unittest
+
+import mock
+
+from kalliope.core.Models import Brain
+from kalliope.core.Models.Settings import Settings
+from kalliope.core.SynapseLauncher import SynapseLauncher, SynapseNameNotFound
+
+from kalliope.core.Models import Neuron
+from kalliope.core.Models import Order
+from kalliope.core.Models import Synapse
+
+
+class TestSynapseLauncher(unittest.TestCase):
+    """
+    Test the class SynapseLauncher
+    """
+
+    def setUp(self):
+        # 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")
+
+        self.synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
+        self.synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
+        self.synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
+
+        all_synapse_list = [self.synapse1,
+                            self.synapse2,
+                            self.synapse3]
+
+        self.brain_test = Brain(synapses=all_synapse_list)
+        self.settings_test = Settings(default_synapse="Synapse3")
+
+    def test_match_synapse1(self):
+
+        with mock.patch("kalliope.core.NeuronLauncher.start_neuron_list"):
+            order_to_match = "this is the sentence"
+            expected_result = [self.synapse1]
+
+            self.assertEqual(expected_result,
+                             SynapseLauncher.run_matching_synapse_or_default(order_to_match,
+                                                                             brain=self.brain_test,
+                                                                             settings=self.settings_test))
+
+    def test_match_synapse1_and_2(self):
+        with mock.patch("kalliope.core.NeuronLauncher.start_neuron_list"):
+            order_to_match = "this is the second sentence"
+            expected_result = [self.synapse1, self.synapse2]
+
+            self.assertEqual(expected_result,
+                             SynapseLauncher.run_matching_synapse_or_default(order_to_match,
+                                                                             brain=self.brain_test,
+                                                                             settings=self.settings_test))
+
+    def test_match_default_synapse(self):
+        with mock.patch("kalliope.core.NeuronLauncher.start_neuron"):
+            order_to_match = "this is an invalid order"
+            expected_result = [self.synapse3]
+
+            self.assertEqual(expected_result,
+                             SynapseLauncher.run_matching_synapse_or_default(order_to_match,
+                                                                             brain=self.brain_test,
+                                                                             settings=self.settings_test))
+
+    def test_start_synapse(self):
+        with mock.patch("kalliope.core.NeuronLauncher.start_neuron"):
+            expected_result = self.synapse1
+            self.assertEqual(expected_result,
+                             SynapseLauncher.start_synapse("Synapse1", brain=self.brain_test))
+
+        with self.assertRaises(SynapseNameNotFound):
+            SynapseLauncher.start_synapse(name="no_do_exist", brain=self.brain_test)

+ 1 - 1
kalliope/core/NeuronModule.py

@@ -214,7 +214,7 @@ class NeuronModule(object):
             for signal in synapse_to_run.signals:
                 if isinstance(signal, Order):
                     parameters = NeuronParameterLoader.get_parameters(synapse_order=order_template,
-                                                                      user_order=order).next()
+                                                                      user_order=order)
                     logger.debug("[NeuronModule]-> parameter load from user answer: %s" % parameters)
                     if parameters is not None:
                         break

+ 3 - 3
kalliope/core/NeuronParameterLoader.py

@@ -17,10 +17,10 @@ class NeuronParameterLoader(object):
         if Utils.is_containing_bracket(synapse_order):
             params = cls._associate_order_params_to_values(user_order, synapse_order)
             logger.debug("Parameters for order: %s" % params)
-        yield params
+        return params
 
-    @staticmethod
-    def _associate_order_params_to_values(order, order_to_check):
+    @classmethod
+    def _associate_order_params_to_values(cls, order, order_to_check):
         """
         Associate the variables from the order to the incoming user order
         :param order_to_check: the order to check incoming from the brain

+ 2 - 1
kalliope/core/RestAPI/FlaskAPI.py

@@ -258,7 +258,8 @@ class FlaskAPI(threading.Thread):
         :return:
         """
         logger.debug("order to process %s" % order)
-        SynapseLauncher.run_matching_synapse_or_default(order, self.brain, self.settings)
+        list_launched_synapse = SynapseLauncher.run_matching_synapse_or_default(order, self.brain, self.settings)
+        self.launched_synapses = list_launched_synapse
 
         # this boolean will notify the main process that the order have been processed
         self.order_analyser_return = True

+ 20 - 5
kalliope/core/SynapseLauncher.py

@@ -38,6 +38,7 @@ class SynapseLauncher(object):
             raise SynapseNameNotFound("The synapse name \"%s\" does not exist in the brain file" % name)
         else:
             cls._run_synapse(synapse=synapse)
+            return synapse
 
     @classmethod
     def _run_synapse(cls, synapse):
@@ -52,26 +53,40 @@ class SynapseLauncher(object):
 
     @classmethod
     def run_matching_synapse_or_default(cls, order_to_process, brain, settings):
+        """
+        This method will run all synapse that match the given order "order_to_process"
+        :param order_to_process: The text order to process in the order analyser
+        :param brain: Brain instance
+        :param settings: Settings instance
+        :return: Return a list of launched synapse
+        """
         no_synapse_match = False
         if order_to_process is not None:  # maybe we have received a null audio from STT engine
-            oa2 = OrderAnalyser2.get_matching_synapse(order=order_to_process, brain=brain)
+            launched_synapses_tuple = OrderAnalyser2.get_matching_synapse(order=order_to_process, brain=brain)
 
             # oa2 contains the list Named tuple of synapse to run with the associated order that has matched
             # for each synapse, get neurons, et for each neuron, get parameters
-            if not oa2:
+            if not launched_synapses_tuple:
                 no_synapse_match = True
             else:
+                # create a list of launched synapse to return
+                launched_synapses = list()
                 # the order match one or more synapses
-                for tuple_el in oa2:
+                for tuple_el in launched_synapses_tuple:
+                    launched_synapses.append(tuple_el.synapse)
                     logger.debug("Get parameter for %s " % tuple_el.synapse.name)
                     parameters = NeuronParameterLoader.get_parameters(synapse_order=tuple_el.order,
-                                                                      user_order=order_to_process).next()
+                                                                      user_order=order_to_process)
                     # start the neuron list
                     NeuronLauncher.start_neuron_list(neuron_list=tuple_el.synapse.neurons,
                                                      parameters_dict=parameters)
+                # return the launched synapse list
+                return launched_synapses
         else:
             no_synapse_match = True
 
         if no_synapse_match:  # then run the default synapse
             if settings.default_synapse is not None:
-                SynapseLauncher.start_synapse(name=settings.default_synapse, brain=brain)
+                launched_synapses = SynapseLauncher.start_synapse(name=settings.default_synapse, brain=brain)
+
+                return [launched_synapses]

+ 3 - 0
kalliope/core/__init__.py

@@ -4,5 +4,8 @@ from kalliope.core.ShellGui import ShellGui
 from kalliope.core.Utils.Utils import Utils
 from kalliope.core.Utils import FileManager
 from kalliope.core.ResourcesManager import ResourcesManager
+from kalliope.core.NeuronLauncher import NeuronLauncher
+from kalliope.core.SynapseLauncher import SynapseLauncher
+