浏览代码

[Tests] Add tests for global variables now loading from the brainloader

monf 8 年之前
父节点
当前提交
d32b0ec374
共有 2 个文件被更改,包括 153 次插入114 次删除
  1. 153 1
      Tests/test_brain_loader.py
  2. 0 113
      Tests/test_launchers.py

+ 153 - 1
Tests/test_brain_loader.py

@@ -9,6 +9,7 @@ from kalliope.core.Models import Neuron
 from kalliope.core.Models import Synapse
 from kalliope.core.Models import Order
 from kalliope.core.Models.Brain import Brain
+from kalliope.core.Models.Settings import Settings
 
 
 class TestBrainLoader(unittest.TestCase):
@@ -63,12 +64,37 @@ class TestBrainLoader(unittest.TestCase):
         self.assertEqual(brain, brain_loader.brain)
 
     def test_get_neurons(self):
+        """
+        Test to get neurons from the brainLoader
+        scenarii:
+            - 1/ get a simple neuron from the brainloader
+            - 2/ get a neuron with global variables as parameters
+        """
+        # 1/ get a simple neuron from the brainloader
+        st = Settings()
         neuron_list = [{'say': {'message': ['test message']}}]
 
         neuron = Neuron(name='say', parameters={'message': ['test message']})
 
         bl = BrainLoader(file_path=self.brain_to_test)
-        neurons_from_brain_loader = bl._get_neurons(neuron_list)
+        neurons_from_brain_loader = bl._get_neurons(neuron_list,
+                                                    settings=st)
+
+        self.assertEqual([neuron], neurons_from_brain_loader)
+
+        # 2/ get a neuron with global variables as parameters
+        neuron_list = [{'say': {'message': ['bonjour {{name}}']}}]
+        variables = {
+            "author": "Lamonf",
+            "test_number": 60,
+            "name": "kalliope"
+        }
+        st = Settings(variables=variables)
+        bl = BrainLoader(file_path=self.brain_to_test)
+        neurons_from_brain_loader = bl._get_neurons(neuron_list,
+                                                    settings=st)
+
+        neuron = Neuron(name='say', parameters={'message': ['bonjour kalliope']})
 
         self.assertEqual([neuron], neurons_from_brain_loader)
 
@@ -103,5 +129,131 @@ class TestBrainLoader(unittest.TestCase):
 
         self.assertTrue(bl1.brain is bl2.brain)
 
+    def test_replace_global_variables(self):
+        """
+        Testing the _replace_global_variables function from the NeuronLauncher.
+        Scenarii:
+            - 1/ only one global variable
+            - 2/ global variable with string after
+            - 3/ global variable with int after
+            - 4/ multiple global variables
+            - 5/ parameter value is a list
+
+        """
+
+        # 1/ only one global variable
+        parameters={
+            'var1': '{{hello}}'
+        }
+
+        variables = {
+            "hello": "test",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_parameters={
+            'var1': 'test'
+        }
+
+        self.assertEquals(BrainLoader._replace_global_variables(parameters=parameters,
+                                                                settings=st),
+                          expected_parameters,
+                          "Fail to assign a single global variable to parameters")
+
+        # 2/ global variable with string after
+        parameters={
+            'var1': '{{hello}} Sispheor'
+        }
+        variables = {
+            "hello": "test",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_parameters = {
+            'var1': 'test Sispheor'
+        }
+
+        self.assertEquals(BrainLoader._replace_global_variables(parameters=parameters,
+                                                                settings=st),
+                          expected_parameters,
+                          "Fail to assign a global variable with string after to parameters")
+
+        # 3/ global variable with int after
+        parameters={
+            'var1': '{{hello}}0'
+        }
+        variables = {
+            "hello": 60,
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_parameters={
+            'var1': '600'
+        }
+
+        self.assertEquals(BrainLoader._replace_global_variables(parameters=parameters,
+                                                                settings=st),
+                          expected_parameters,
+                          "Fail to assign global variable with int after to parameters")
+
+        # 4/ multiple global variables
+        parameters={
+            'var1': '{{hello}} {{me}}'
+        }
+        variables = {
+            "hello": "hello",
+            "me": "LaMonf"
+        }
+        st = Settings(variables=variables)
+
+        expected_parameters={
+            'var1': 'hello LaMonf'
+        }
+
+        self.assertEquals(BrainLoader._replace_global_variables(parameters=parameters,
+                                                                settings=st),
+                          expected_parameters,
+                          "Fail to assign multiple global variables to parameters")
+
+        # 5/ parameter value is a list
+        parameters={
+            'var1': '[hello {{name}}, bonjour {{name}}]'
+        }
+        variables = {
+            "name": "LaMonf",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_parameters={
+            'var1': '[hello LaMonf, bonjour LaMonf]'
+        }
+
+        self.assertEquals(BrainLoader._replace_global_variables(parameters=parameters,
+                                                                settings=st),
+                          expected_parameters,
+                          "Fail to assign a single global when parameter value is a list to neuron")
+
+    def test_get_global_variable(self):
+        """
+        Test the get_global_variable of the OrderAnalyser Class
+        """
+        sentence = "i am {{name2}}"
+        variables = {
+            "name": "LaMonf",
+            "name2": "kalliope",
+        }
+        st = Settings(variables=variables)
+
+        expected_result = "i am kalliope"
+
+        self.assertEquals(BrainLoader._get_global_variable(sentence=sentence,
+                                                           settings=st),
+                          expected_result,
+                          "Fail to get the global variable from the sentence")
+
 if __name__ == '__main__':
     unittest.main()

+ 0 - 113
Tests/test_launchers.py

@@ -137,117 +137,4 @@ class TestLaunchers(unittest.TestCase):
                                                                  resources_dir=sl.settings.resources.neuron_folder)
             mock_get_class_instantiation.reset_mock()
 
-    def test_replace_global_variables(self):
-        """
-        Testing the _replace_global_variables function from the NeuronLauncher.
-        Scenarii:
-            - 1/ only one global variable
-            - 2/ global variable with string after
-            - 3/ global variable with int after
-            - 4/ multiple global variables
-            - 5/ parameter value is a list
-
-        """
 
-        # 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
-        NeuronLauncher._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
-        NeuronLauncher._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
-        NeuronLauncher._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
-        NeuronLauncher._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign multiple global variables to neuron")
-
-        # 5/ parameter value is a list
-        neuron1 = Neuron(name='neuron1', parameters={'var1': '[hello {{name}}, bonjour {{name}}]'})
-        variables = {
-            "name": "LaMonf",
-            "hello2": "test2",
-        }
-        st = Settings(variables=variables)
-
-        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': '[hello LaMonf, bonjour LaMonf]'})
-
-        # assign global variable to neuron1
-        NeuronLauncher._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign a single global when parameter value is a list to neuron")
-
-    def test_get_global_variable(self):
-        """
-        Test the get_global_variable of the OrderAnalyser Class
-        """
-        sentence = "i am {{name2}}"
-        variables = {
-            "name": "LaMonf",
-            "name2": "kalliope",
-        }
-        st = Settings(variables=variables)
-
-        expected_result = "i am kalliope"
-
-        self.assertEquals(NeuronLauncher._get_global_variable(sentence=sentence,
-                                                             settings=st),
-                          expected_result,
-                          "Fail to get the global variable from the sentence")