浏览代码

Merge pull request #106 from kalliope-project/tests_monf

Tests monf
Nicolas Marcq 8 年之前
父节点
当前提交
a04a8da59d

+ 0 - 0
kalliope/Tests/__init__.py → Tests/__init__.py


+ 0 - 0
kalliope/Tests/brains/brain_test.yml → Tests/brains/brain_test.yml


+ 0 - 0
kalliope/Tests/brains/included_brain_test.yml → Tests/brains/included_brain_test.yml


+ 0 - 0
kalliope/Tests/settings/settings_test.yml → Tests/settings/settings_test.yml


+ 1 - 1
kalliope/Tests/test_brain_loader.py → Tests/test_brain_loader.py

@@ -11,7 +11,7 @@ from kalliope.core.Models.Brain import Brain
 class TestBrainLoader(unittest.TestCase):
 
     def setUp(self):
-        self.brain_to_test = "Tests/brains/brain_test.yml"
+        self.brain_to_test = "../Tests/brains/brain_test.yml"
         self.expected_result = [
             {'signals': [{'order': 'test_order'}],
              'neurons': [{'say': {'message': ['test message']}}],

+ 1 - 1
kalliope/Tests/test_configuration_checker.py → Tests/test_configuration_checker.py

@@ -3,7 +3,7 @@ import unittest
 from kalliope.core.ConfigurationManager.ConfigurationChecker import ConfigurationChecker, NoSynapeName, NoSynapeNeurons, \
     NoSynapeSignals, NoValidSignal, NoEventPeriod, NoValidOrder, MultipleSameSynapseName
 from kalliope.core.Models import Synapse
-from kalliope.core.Utils import ModuleNotFoundError
+from kalliope.core.Utils.Utils import ModuleNotFoundError
 
 
 class TestConfigurationChecker(unittest.TestCase):

+ 4 - 4
kalliope/Tests/test_dynamic_loading.py → Tests/test_dynamic_loading.py

@@ -21,16 +21,16 @@ class TestDynamicLoading(unittest.TestCase):
         root_dir = os.path.normpath(cur_script_directory + os.sep + os.pardir)
 
         # get the neuron dir
-        self.neurons_dir = os.path.normpath(root_dir + os.sep + "neurons")
+        self.neurons_dir = os.path.normpath(root_dir + os.sep + "kalliope/neurons")
 
         # get stt dir
-        self.stt_dir = os.path.normpath(root_dir + os.sep + "stt")
+        self.stt_dir = os.path.normpath(root_dir + os.sep + "kalliope/stt")
 
         # get tts dir
-        self.tts_dir = os.path.normpath(root_dir + os.sep + "tts")
+        self.tts_dir = os.path.normpath(root_dir + os.sep + "kalliope/tts")
 
         # get trigger dir
-        self.trigger_dir = os.path.normpath(root_dir + os.sep + "trigger")
+        self.trigger_dir = os.path.normpath(root_dir + os.sep + "kalliope/trigger")
 
     def test_packages_present(self):
         """

+ 171 - 0
Tests/test_file_manager.py

@@ -0,0 +1,171 @@
+import unittest
+import os
+
+from kalliope.core.Utils.FileManager import FileManager
+
+
+class TestFileManager(unittest.TestCase):
+    """
+    Class to test FileManager
+    """
+
+    def setUp(self):
+        pass
+
+    def test_create_directory(self):
+        """
+        Test to create a new directory.
+        """
+        # set up
+        cache_path = "/tmp/kalliope/tests/testDirectory"
+        if os.path.exists(cache_path):
+            os.removedirs(cache_path)
+
+        # Test FileManager.create_directory
+        FileManager.create_directory(cache_path)
+        self.assertTrue(os.path.exists(cache_path),
+                        "Fail creating a directory to the path ")
+
+        # Remove the directory
+        os.removedirs(cache_path)
+
+    def test_write_in_file(self):
+        """
+        Test to write in file.
+        """
+
+        # set up the context
+        dir_path = "/tmp/kalliope/tests/"
+        file_name = "test_FileManager_writeInFile"
+        file_path = os.path.join(dir_path,file_name)
+        in_file_text = "[Kalliope] Testing the write_in_file method from Utils.FileManager"
+        if os.path.exists(file_path):
+            os.remove(file_path)
+        if not os.path.exists(dir_path):
+            os.makedirs(dir_path)
+
+        # Test FileManager.write_in_file
+        FileManager.write_in_file(file_path=file_path, content=in_file_text)
+        with open(file_path, 'r') as content_file:
+            content = content_file.read()
+            self.assertEqual(content, in_file_text,
+                             "Fail writing in the file ")
+
+        # Clean up
+        if os.path.exists(file_path):
+            os.remove(file_path)
+
+    def test_file_is_empty(self):
+        """
+        Test that the file is empty
+        """
+
+        # set up the context
+        dir_path = "/tmp/kalliope/tests/"
+        file_name = "test_FileManager_fileIsEmpty"
+        file_path = os.path.join(dir_path, file_name)
+        if os.path.exists(file_path):
+            os.remove(file_path)
+        if not os.path.exists(dir_path):
+            os.makedirs(dir_path)
+
+        # Test FileManager.file_is_empty
+        with open(file_path, "wb") as file_open:
+            file_open.write("")
+            file_open.close()
+        self.assertTrue(FileManager.file_is_empty(file_path=file_path),
+                        "Fail matching to verify that file is empty ")
+
+        # Clean up
+        if os.path.exists(file_path):
+            os.remove(file_path)
+
+    def test_remove_file(self):
+        """
+        Test to remove a file
+        """
+
+        # set up the context
+        dir_path = "/tmp/kalliope/tests/"
+        file_name = "test_FileManager_fileRemove"
+        file_path = os.path.join(dir_path, file_name)
+        if os.path.exists(file_path):
+            os.remove(file_path)
+        if not os.path.exists(dir_path):
+            os.makedirs(dir_path)
+
+        # Test to remove the file
+        # FileManager.remove_file
+        with open(file_path, "wb") as file_open:
+            file_open.write("")
+            file_open.close()
+        FileManager.remove_file(file_path=file_path)
+        self.assertFalse(os.path.exists(file_path),
+                         "Fail removing the file")
+
+    def test_is_path_creatable(self):
+        """
+        Test if the path is creatable for the user
+        Does the user has the permission to use this path ?
+        """
+
+        # set up the context
+        dir_path = "/tmp/kalliope/tests/"
+        file_name = "test_FileManager_filePathCreatable"
+        file_path = os.path.join(dir_path, file_name)
+        if os.path.exists(file_path):
+            os.remove(file_path)
+        if not os.path.exists(dir_path):
+            os.makedirs(dir_path)
+
+        # test not allowed : return False
+        not_allowed_root_path = "/root/"
+        not_allowed_path = os.path.join(not_allowed_root_path, file_name)
+        self.assertFalse(FileManager.is_path_creatable(not_allowed_path),
+                         "Fail to assert not accessing this path ")
+        # test allowed : return True
+        self.assertTrue(FileManager.is_path_creatable(file_path))
+
+    def test_is_path_exists_or_creatable(self):
+        """
+        Test the _is_path_exists_or_creatable
+        4 scenarii :
+            - the file exists and is creatable : return True
+            - the file does not exist but is creatable : return True
+            - the file exists but is not allowed : return True --> need a review !
+            - the file does not exist and is not allowed : return False
+        """
+
+        # set up the context
+        dir_path = "/tmp/kalliope/tests/"
+        file_name = "test_FileManager_fileIsPathExistsOrCreatable"
+        file_path = os.path.join(dir_path, file_name)
+        if os.path.exists(file_path):
+            os.remove(file_path)
+        if not os.path.exists(dir_path):
+            os.makedirs(dir_path)
+
+        # Test the file exist and creatable : return True
+        with open(file_path, "wb") as file_open:
+            file_open.write("[Kalliope] Test Running the test_is_path_exists_or_creatable method")
+            file_open.close()
+        self.assertTrue(FileManager.is_path_exists_or_creatable(file_path),
+                        "Fail to assert the file exist ")
+
+        # test the file not exist but creatable : return True
+        os.remove(file_path)
+        self.assertTrue(FileManager.is_path_exists_or_creatable(file_path),
+                         "Fail asserting the file does not exist ")
+
+        # test the file exist but not creatable : return True
+        # file_exist_not_allowed = "/root/.ssh/known_hosts"
+        # self.assertTrue(FileManager.is_path_creatable(file_exist_not_allowed))
+
+        # test the file not exist and not allowed : return False
+        not_allowed_root_path = "/root/"
+        not_allowed_path = os.path.join(not_allowed_root_path, file_name)
+        self.assertFalse(FileManager.is_path_creatable(not_allowed_path),
+                         "Fail to assert not accessing this path ")
+
+if __name__ == '__main__':
+    unittest.main()

+ 0 - 0
kalliope/Tests/test_order_analyser.py → Tests/test_order_analyser.py


+ 1 - 1
kalliope/Tests/test_rest_api.py → Tests/test_rest_api.py

@@ -23,7 +23,7 @@ class TestRestAPI(unittest.TestCase):
         sl.settings.active = True
         sl.settings.port = 5000
         # prepare a test brain
-        brain_to_test = "Tests/brains/brain_test.yml"
+        brain_to_test = "../Tests/brains/brain_test.yml"
         brain_loader = BrainLoader.Instance(file_path=brain_to_test)
         brain = brain_loader.brain
 

+ 1 - 1
kalliope/Tests/test_settings_loader.py → Tests/test_settings_loader.py

@@ -11,7 +11,7 @@ class TestSettingLoader(unittest.TestCase):
 
     def setUp(self):
 
-        self.settings_file_to_test = "Tests/settings/settings_test.yml"
+        self.settings_file_to_test = "../Tests/settings/settings_test.yml"
 
         self.settings_dict = {
             'rest_api':

+ 11 - 4
kalliope/Tests/test_tts_module.py → Tests/test_tts_module.py

@@ -1,10 +1,11 @@
+import os
 import unittest
+
 import mock
-import os
 
-from kalliope.core.TTS.TTSModule import TTSModule, TtsGenerateAudioFunctionNotFound
 from kalliope.core.Models.Settings import Settings
-from kalliope.core.FileManager import FileManager
+from kalliope.core.TTS.TTSModule import TTSModule, TtsGenerateAudioFunctionNotFound
+from kalliope.core.Utils.FileManager import FileManager
 
 
 class TestTTSModule(unittest.TestCase):
@@ -79,6 +80,9 @@ class TestTTSModule(unittest.TestCase):
             # create tmp file
             tmp_base_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
             file_path = os.path.join(tmp_base_path, "5c186d1e123be2667fb5fd54640e4fd0.tts")
+            if os.path.isfile(file_path):
+                # Remove the file
+                FileManager.remove_file(file_path)
             if not os.path.exists(tmp_base_path):
                 os.makedirs(tmp_base_path)
             FileManager.write_in_file(file_path, "[kalliope-test] test_generate_and_play")
@@ -97,8 +101,11 @@ class TestTTSModule(unittest.TestCase):
 
         base_cache_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
         md5_word = "5c186d1e123be2667fb5fd54640e4fd0"
-        file_path = "/tmp/kalliope/tests/TTSModule/tests/default/5c186d1e123be2667fb5fd54640e4fd0.tts"
+        file_path = os.path.join(base_cache_path, "5c186d1e123be2667fb5fd54640e4fd0.tts")
 
+        if os.path.isfile(file_path):
+            # Remove the file
+            FileManager.remove_file(file_path)
         # Create a tmp file
         if not os.path.exists(base_cache_path):
             os.makedirs(base_cache_path)

+ 1 - 1
kalliope/Tests/test_yaml_loader.py → Tests/test_yaml_loader.py

@@ -13,7 +13,7 @@ class TestYAMLLoader(unittest.TestCase):
 
     def test_get_config(self):
 
-        valid_file_path_to_test = "Tests/brains/brain_test.yml"
+        valid_file_path_to_test = "../Tests/brains/brain_test.yml"
         invalid_file_path = "brains/non_existing_brain.yml"
         expected_result = [
             {'signals': [{'order': 'test_order'}],

+ 1 - 1
kalliope/core/ConfigurationManager/ConfigurationChecker.py

@@ -1,6 +1,6 @@
 import re
 
-from kalliope.core.Utils import ModuleNotFoundError
+from kalliope.core.Utils.Utils import ModuleNotFoundError
 
 
 class InvalidSynapeName(Exception):

+ 1 - 1
kalliope/core/ConfigurationManager/SettingLoader.py

@@ -1,13 +1,13 @@
 import logging
 
 from YAMLLoader import YAMLLoader
-from kalliope.core.FileManager import FileManager
 from kalliope.core.Models import Singleton
 from kalliope.core.Models.RestAPI import RestAPI
 from kalliope.core.Models.Settings import Settings
 from kalliope.core.Models.Stt import Stt
 from kalliope.core.Models.Trigger import Trigger
 from kalliope.core.Models.Tts import Tts
+from kalliope.core.Utils.FileManager import FileManager
 
 FILE_NAME = "settings.yml"
 

+ 1 - 1
kalliope/core/NeuronModule.py

@@ -8,7 +8,7 @@ from jinja2 import Template
 
 from kalliope.core import OrderListener
 from kalliope.core.SynapseLauncher import SynapseLauncher
-from kalliope.core.Utils import Utils
+from kalliope.core.Utils.Utils import Utils
 from kalliope.core.ConfigurationManager import SettingLoader, BrainLoader
 
 logging.basicConfig()

+ 1 - 1
kalliope/core/NeuroneLauncher.py

@@ -1,6 +1,6 @@
 import logging
 
-from kalliope.core.Utils import Utils
+from kalliope.core.Utils.Utils import Utils
 
 logging.basicConfig()
 logger = logging.getLogger("kalliope")

+ 1 - 1
kalliope/core/OrderAnalyser.py

@@ -2,7 +2,7 @@
 import re
 from collections import Counter
 
-from kalliope.core.Utils import Utils
+from kalliope.core.Utils.Utils import Utils
 from kalliope.core.Models import Order
 from kalliope.core.NeuroneLauncher import NeuroneLauncher
 

+ 1 - 1
kalliope/core/OrderListener.py

@@ -4,7 +4,7 @@ from threading import Thread
 
 from cffi import FFI as _FFI
 
-from kalliope.core.Utils import Utils
+from kalliope.core.Utils.Utils import Utils
 from kalliope.core.ConfigurationManager import SettingLoader
 
 logging.basicConfig()

+ 1 - 1
kalliope/core/ShellGui.py

@@ -10,7 +10,7 @@ from dialog import Dialog
 from kalliope.core import OrderListener
 from kalliope.core.ConfigurationManager import SettingLoader
 from kalliope.core.SynapseLauncher import SynapseLauncher
-from kalliope.core.Utils import Utils
+from kalliope.core.Utils.Utils import Utils
 from kalliope.neurons.say.say import Say
 
 logging.basicConfig()

+ 1 - 1
kalliope/core/TTS/TTSModule.py

@@ -3,9 +3,9 @@ import hashlib
 import logging
 import os
 
-from kalliope.core.FileManager import FileManager
 from kalliope.core.ConfigurationManager import SettingLoader
 from kalliope.core.Players import Mplayer
+from kalliope.core.Utils.FileManager import FileManager
 
 logging.basicConfig()
 logger = logging.getLogger("kalliope")

+ 0 - 0
kalliope/core/FileManager.py → kalliope/core/Utils/FileManager.py


+ 0 - 0
kalliope/core/Utils.py → kalliope/core/Utils/Utils.py


+ 2 - 0
kalliope/core/Utils/__init__.py

@@ -0,0 +1,2 @@
+from kalliope.core.Utils import Utils
+from kalliope.core.Utils import FileManager

+ 2 - 1
kalliope/core/__init__.py

@@ -1,6 +1,7 @@
 from kalliope.core.OrderAnalyser import OrderAnalyser
 from kalliope.core.OrderListener import OrderListener
 from kalliope.core.ShellGui import ShellGui
-from kalliope.core.FileManager import FileManager
 from kalliope.core.Utils import Utils
+from kalliope.core.Utils import FileManager
+