浏览代码

[Tests] #203 Add tests for global variables

monf 8 年之前
父节点
当前提交
8170c384a9

+ 4 - 1
Tests/settings/settings_test.yml

@@ -112,5 +112,8 @@ resource_directory:
   trigger: "/tmp/kalliope/tests/kalliope_resources_dir/trigger"
 
 # ---------------------------
-# Variables TODO
+# Global files variables
+# /!\ If a variable is defined in different files, the last file defines the value.
 # ---------------------------
+var_files:
+  - "../Tests/settings/variables.yml"

+ 3 - 0
Tests/settings/variables.yml

@@ -0,0 +1,3 @@
+test: kalliope
+test_number: 60
+author: Lamonf

+ 0 - 44
Tests/test_order_analyser.py

@@ -161,50 +161,6 @@ class TestOrderAnalyser(unittest.TestCase):
             mock_start_neuron_method.reset_mock()
 
 
-    def test_is_containing_bracket(self):
-        #  Success
-        order_to_test = "This test contains {{ bracket }}"
-        self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
-                        "Fail returning True when order contains spaced brackets")
-
-        order_to_test = "This test contains {{bracket }}"
-        self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
-                        "Fail returning True when order contains right spaced bracket")
-
-        order_to_test = "This test contains {{ bracket}}"
-        self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
-                        "Fail returning True when order contains left spaced bracket")
-
-        order_to_test = "This test contains {{bracket}}"
-        self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
-                        "Fail returning True when order contains no spaced bracket")
-
-        #  Failure
-        order_to_test = "This test does not contain bracket"
-        self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test),
-                         "Fail returning False when order has no brackets")
-
-        #  Behaviour
-        order_to_test = ""
-        self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test),
-                         "Fail returning False when no order")
-
-    def test_get_next_value_list(self):
-        # Success
-        list_to_test = {1, 2, 3}
-        self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), 2,
-                         "Fail to match the expected next value from the list")
-
-        # Failure
-        list_to_test = {1}
-        self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), None,
-                         "Fail to ensure there is no next value from the list")
-
-        # Behaviour
-        list_to_test = {}
-        self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), None,
-                         "Fail to ensure the empty list return None value")
-
     def test_spelt_order_match_brain_order_via_table(self):
         order_to_test = "this is the order"
         sentence_to_test = "this is the order"

+ 21 - 5
Tests/test_settings_loader.py

@@ -51,19 +51,24 @@ class TestSettingLoader(unittest.TestCase):
             'text_to_speech': [
                 {'pico2wave': {'cache': True, 'language': 'fr-FR'}},
                 {'voxygen': {'voice': 'Agnes', 'cache': True}}
-            ]
+            ],
+            'var_files': ["../Tests/settings/variables.yml"]
         }
 
-
         # Init the folders, otherwise it raises an exceptions
         os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/neurons")
         os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/stt")
         os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/tts")
         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):
         # Cleanup
         shutil.rmtree('/tmp/kalliope/tests/kalliope_resources_dir')
+        shutil.rmtree('/tmp/kalliope/tests/variables/')
 
         Singleton._instances = {}
 
@@ -105,8 +110,12 @@ class TestSettingLoader(unittest.TestCase):
                               stt_folder="/tmp/kalliope/tests/kalliope_resources_dir/stt",
                               tts_folder="/tmp/kalliope/tests/kalliope_resources_dir/tts",
                               trigger_folder="/tmp/kalliope/tests/kalliope_resources_dir/trigger")
-
         settings_object.resources = resources
+        settings_object.variables = {
+            "author": "Lamonf",
+            "test_number": 60,
+            "test": "kalliope"
+        }
         settings_object.machine = platform.machine()
 
         sl = SettingLoader(file_path=self.settings_file_to_test)
@@ -190,8 +199,15 @@ class TestSettingLoader(unittest.TestCase):
         self.assertEquals(expected_resource, sl._get_resources(self.settings_dict))
 
     def test_get_variables(self):
-        # TODO
-        pass
+        expected_result = {
+            "author": "Lamonf",
+            "test_number": 60,
+            "test": "kalliope"
+        }
+        sl = SettingLoader(file_path=self.settings_file_to_test)
+        self.assertEqual(expected_result,
+                         sl._get_variables(self.settings_dict))
+
 
 if __name__ == '__main__':
     unittest.main()

+ 77 - 0
Tests/test_utils.py

@@ -138,3 +138,80 @@ class TestUtils(unittest.TestCase):
                                    Say),
                         "Fail instantiate a class")
 
+
+    def test_is_containing_bracket(self):
+        #  Success
+        order_to_test = "This test contains {{ bracket }}"
+        self.assertTrue(Utils.is_containing_bracket(order_to_test),
+                        "Fail returning True when order contains spaced brackets")
+
+        order_to_test = "This test contains {{bracket }}"
+        self.assertTrue(Utils.is_containing_bracket(order_to_test),
+                        "Fail returning True when order contains right spaced bracket")
+
+        order_to_test = "This test contains {{ bracket}}"
+        self.assertTrue(Utils.is_containing_bracket(order_to_test),
+                        "Fail returning True when order contains left spaced bracket")
+
+        order_to_test = "This test contains {{bracket}}"
+        self.assertTrue(Utils.is_containing_bracket(order_to_test),
+                        "Fail returning True when order contains no spaced bracket")
+
+        #  Failure
+        order_to_test = "This test does not contain bracket"
+        self.assertFalse(Utils.is_containing_bracket(order_to_test),
+                         "Fail returning False when order has no brackets")
+
+        #  Behaviour
+        order_to_test = ""
+        self.assertFalse(Utils.is_containing_bracket(order_to_test),
+                         "Fail returning False when no order")
+
+    def test_get_next_value_list(self):
+        # Success
+        list_to_test = {1, 2, 3}
+        self.assertEqual(Utils.get_next_value_list(list_to_test), 2,
+                         "Fail to match the expected next value from the list")
+
+        # Failure
+        list_to_test = {1}
+        self.assertEqual(Utils.get_next_value_list(list_to_test), None,
+                         "Fail to ensure there is no next value from the list")
+
+        # Behaviour
+        list_to_test = {}
+        self.assertEqual(Utils.get_next_value_list(list_to_test), None,
+                         "Fail to ensure the empty list return None value")
+
+    def test_find_all_matching_brackets(self):
+        """
+        Test the Utils find all matching brackets
+        """
+        sentence = "This is the {{bracket}}"
+        expected_result = ["{{bracket}}"]
+        self.assertEqual(Utils.find_all_matching_brackets(sentence=sentence),
+                         expected_result,
+                         "Fail to match one bracket")
+
+        sentence = "This is the {{bracket}} {{second}}"
+        expected_result = ["{{bracket}}", "{{second}}"]
+        self.assertEqual(Utils.find_all_matching_brackets(sentence=sentence),
+                         expected_result,
+                         "Fail to match two brackets")
+
+    def test_remove_spaces_in_brackets(self):
+        """
+        Test the Utils remove_spaces_in_brackets
+        """
+
+        sentence = "This is the {{ bracket   }}"
+        expected_result = "This is the {{bracket}}"
+        self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
+                         expected_result,
+                         "Fail to remove spaces in one bracket")
+
+        sentence = "This is the {{ bracket   }} {{  second     }}"
+        expected_result = "This is the {{bracket}} {{second}}"
+        self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
+                         expected_result,
+                         "Fail to remove spaces in two brackets")

+ 1 - 1
kalliope/core/OrderAnalyser.py

@@ -240,7 +240,7 @@ class OrderAnalyser:
             if Utils.is_containing_bracket(ow):
                 # remove bracket and grab the next value / stop value
                 var_name = ow.replace("{{", "").replace("}}", "")
-                stop_value = Utils._get_next_value_list(list_word_in_order[idx:])
+                stop_value = Utils.get_next_value_list(list_word_in_order[idx:])
                 if stop_value is None:
                     dict_var[var_name] = " ".join(truncate_list_word_said)
                     break

+ 1 - 1
kalliope/core/Utils/Utils.py

@@ -267,7 +267,7 @@ class Utils(object):
     #
     #########
     @staticmethod
-    def _get_next_value_list(list_to_check):
+    def get_next_value_list(list_to_check):
         ite = list_to_check.__iter__()
         next(ite, None)
         return next(ite, None)

+ 3 - 3
kalliope/settings.yml

@@ -159,6 +159,6 @@ default_synapse: "default-synapse"
 # Global files variables
 # /!\ If a variable is defined in different files, the last file defines the value.
 # ---------------------------
-var_files:
-  - variables.yml
-  - variables2.yml
+#var_files:
+#  - variables.yml
+#  - variables2.yml