浏览代码

[Tests] #203 Add a missing test + quick requested fixes

monf 8 年之前
父节点
当前提交
78a76bbddb
共有 5 个文件被更改,包括 101 次插入22 次删除
  1. 9 4
      Docs/settings.md
  2. 88 11
      Tests/test_order_analyser.py
  3. 0 4
      Tests/test_settings_loader.py
  4. 3 2
      kalliope/core/OrderAnalyser.py
  5. 1 1
      kalliope/settings.yml

+ 9 - 4
Docs/settings.md

@@ -295,27 +295,32 @@ resource_directory:
 The Global Variables paths list where to load the global variables.
 The Global Variables paths list where to load the global variables.
 Those variables can be reused in neuron parameters within double brackets.
 Those variables can be reused in neuron parameters within double brackets.
 
 
-E.g
+E.g 
 ```yml
 ```yml
 var_files:
 var_files:
   - variables.yml
   - variables.yml
   - variables2.yml
   - variables2.yml
 ```
 ```
+/!\ If a variable is defined in different files, the last file defines the value.
 
 
 In the files the variables are defined by key/value:
 In the files the variables are defined by key/value:
 ```yml
 ```yml
 variable: 60
 variable: 60
-variable2: "http://"
+baseURL: "http://blabla.com/"
+password: "secret"
 ```
 ```
 
 
 And use variables in your neurons:
 And use variables in your neurons:
+/!\ Because YAML format does no allow double braces not surrounded by quotes: you must use the variable between double quotes. 
 ```yml
 ```yml
   - name: "run-simple-sleep"
   - name: "run-simple-sleep"
     signals:
     signals:
       - order: "Wait for me "
       - order: "Wait for me "
     neurons:
     neurons:
-      - sleep:
-          seconds: {{variable}}
+      - uri:
+          url: "{{baseURL}}get/1"        
+          user: "admin"
+          password: "{{password}}"
 ```
 ```
 
 
 ## Next: configure the brain of Kalliope
 ## Next: configure the brain of Kalliope

+ 88 - 11
Tests/test_order_analyser.py

@@ -160,7 +160,6 @@ class TestOrderAnalyser(unittest.TestCase):
             mock_start_neuron_method.assert_not_called()
             mock_start_neuron_method.assert_not_called()
             mock_start_neuron_method.reset_mock()
             mock_start_neuron_method.reset_mock()
 
 
-
     def test_spelt_order_match_brain_order_via_table(self):
     def test_spelt_order_match_brain_order_via_table(self):
         order_to_test = "this is the order"
         order_to_test = "this is the order"
         sentence_to_test = "this is the order"
         sentence_to_test = "this is the order"
@@ -180,7 +179,6 @@ class TestOrderAnalyser(unittest.TestCase):
                         "Fail matching Upper/lower cases")
                         "Fail matching Upper/lower cases")
 
 
     def test_format_sentences_to_analyse(self):
     def test_format_sentences_to_analyse(self):
-
         # First capital in sentence
         # First capital in sentence
         order_to_test = "this is the order"
         order_to_test = "this is the order"
         sentence_to_test = "This is the order"
         sentence_to_test = "This is the order"
@@ -218,7 +216,6 @@ class TestOrderAnalyser(unittest.TestCase):
                          "Fails formatting the sentences with random in both order and sentence")
                          "Fails formatting the sentences with random in both order and sentence")
 
 
     def test_get_split_order_without_bracket(self):
     def test_get_split_order_without_bracket(self):
-
         # Success
         # Success
         order_to_test = "this is the order"
         order_to_test = "this is the order"
         expected_result = ["this", "is", "the", "order"]
         expected_result = ["this", "is", "the", "order"]
@@ -521,9 +518,9 @@ class TestOrderAnalyser(unittest.TestCase):
         """
         """
         Test to find the good synapse to run
         Test to find the good synapse to run
         Scenarii:
         Scenarii:
-            - Find the synapse
-            - No synpase found, no default synapse
-            - No synapse found, run the default synapse
+            - 1/ Find the synapse
+            - 2/ No synpase found, no default synapse
+            - 3/ No synapse found, run the default synapse
         """
         """
         # Init
         # Init
         neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
         neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
@@ -545,7 +542,7 @@ class TestOrderAnalyser(unittest.TestCase):
 
 
         br = Brain(synapses=all_synapse_list)
         br = Brain(synapses=all_synapse_list)
         st = Settings()
         st = Settings()
-        # Find synapse
+        # 1/ Find synapse
         order = "this is the sentence"
         order = "this is the sentence"
         expected_result = synapse1
         expected_result = synapse1
         oa_tuple_list = OrderAnalyser._find_synapse_to_run(brain=br,settings=st, order=order)
         oa_tuple_list = OrderAnalyser._find_synapse_to_run(brain=br,settings=st, order=order)
@@ -555,16 +552,17 @@ class TestOrderAnalyser(unittest.TestCase):
 
 
         expected_result = signal1.sentence
         expected_result = signal1.sentence
         self.assertEquals(oa_tuple_list[0].order,
         self.assertEquals(oa_tuple_list[0].order,
-                        expected_result,
-                        "Fail to run the proper synapse matching the order")
-        # No Default synapse
+                          expected_result,
+                          "Fail to run the proper synapse matching the order")
+
+        # 2/ No Default synapse
         order = "No default synapse"
         order = "No default synapse"
         expected_result = []
         expected_result = []
         self.assertEquals(OrderAnalyser._find_synapse_to_run(brain=br,settings=st, order=order),
         self.assertEquals(OrderAnalyser._find_synapse_to_run(brain=br,settings=st, order=order),
                           expected_result,
                           expected_result,
                           "Fail to run no synapse, when no default is defined")
                           "Fail to run no synapse, when no default is defined")
 
 
-        # Default synapse
+        # 3/ Default synapse
         st = Settings(default_synapse="Synapse2")
         st = Settings(default_synapse="Synapse2")
         order = "default synapse"
         order = "default synapse"
         expected_result = synapse2
         expected_result = synapse2
@@ -574,5 +572,84 @@ class TestOrderAnalyser(unittest.TestCase):
                           "Fail to run the default synapse")
                           "Fail to run the default synapse")
 
 
 
 
+    def test_replace_global_variables(self):
+        """
+        Testing the _replace_global_variables function from the OrderAnalyser.
+        Scenarii:
+            - 1/ only one global variable
+            - 2/ global variable with string after
+            - 3/ global variable with int after
+            - 4/ multiple global variables
+
+        """
+
+        # 1/ only one global variable
+        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}}'})
+        variables = {
+            "hello": "test",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'test'})
+
+        # assign global variable to neuron1
+        OrderAnalyser._replace_global_variables(neuron=neuron1,
+                                                settings=st)
+        self.assertEquals(neuron1,
+                          expected_neuron_result,
+                          "Fail to assign a single global variable to neuron")
+
+        # 2/ global variable with string after
+        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}} Sispheor'})
+        variables = {
+            "hello": "test",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'test Sispheor'})
+
+        # assign global variable to neuron1
+        OrderAnalyser._replace_global_variables(neuron=neuron1,
+                                                settings=st)
+        self.assertEquals(neuron1,
+                          expected_neuron_result,
+                          "Fail to assign a global variable with string after to neuron")
+
+        # 3/ global variable with int after
+        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}}0'})
+        variables = {
+            "hello": 60,
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': '600'})
+
+        # assign global variable to neuron1
+        OrderAnalyser._replace_global_variables(neuron=neuron1,
+                                                settings=st)
+        self.assertEquals(neuron1,
+                          expected_neuron_result,
+                          "Fail to assign global variable with int after to neuron")
+
+        # 4/ multiple global variables
+        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}} {{me}}'})
+        variables = {
+            "hello": "hello",
+            "me": "LaMonf"
+        }
+        st = Settings(variables=variables)
+
+        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'hello LaMonf'})
+
+        # assign global variable to neuron1
+        OrderAnalyser._replace_global_variables(neuron=neuron1,
+                                                settings=st)
+        self.assertEquals(neuron1,
+                          expected_neuron_result,
+                          "Fail to assign multiple global variables to neuron")
+
 if __name__ == '__main__':
 if __name__ == '__main__':
     unittest.main()
     unittest.main()

+ 0 - 4
Tests/test_settings_loader.py

@@ -61,10 +61,6 @@ class TestSettingLoader(unittest.TestCase):
         os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/tts")
         os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/tts")
         os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/trigger")
         os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/trigger")
 
 
-        os.makedirs("/tmp/kalliope/tests/variables/")
-        open("/tmp/kalliope/tests/variables/variables.yml", 'a').close()
-        open("/tmp/kalliope/tests/variables/variables2.yml", 'a').close()
-
     def tearDown(self):
     def tearDown(self):
         # Cleanup
         # Cleanup
         shutil.rmtree('/tmp/kalliope/tests/kalliope_resources_dir')
         shutil.rmtree('/tmp/kalliope/tests/kalliope_resources_dir')

+ 3 - 2
kalliope/core/OrderAnalyser.py

@@ -168,8 +168,9 @@ class OrderAnalyser:
                     if param_no_brackets in settings.variables:
                     if param_no_brackets in settings.variables:
                         logger.debug("Replacing variable %s with  %s" % (param_with_bracket,
                         logger.debug("Replacing variable %s with  %s" % (param_with_bracket,
                                                                          settings.variables[param_no_brackets]))
                                                                          settings.variables[param_no_brackets]))
-                        neuron.parameters[param] = sentence_no_spaces.replace(param_with_bracket,
-                                                                                    str(settings.variables[param_no_brackets]))
+                        sentence_no_spaces = sentence_no_spaces.replace(param_with_bracket,
+                                                                        str(settings.variables[param_no_brackets]))
+                        neuron.parameters[param] = sentence_no_spaces
 
 
     @staticmethod
     @staticmethod
     def _start_neuron(neuron, params):
     def _start_neuron(neuron, params):

+ 1 - 1
kalliope/settings.yml

@@ -126,7 +126,7 @@ on_ready_sounds:
 # Rest API
 # Rest API
 # ---------------------------
 # ---------------------------
 rest_api:
 rest_api:
-  active: True
+  active: False
   port: 5000
   port: 5000
   password_protected: True
   password_protected: True
   login: admin
   login: admin