diff --git a/src/files.py b/src/files.py index a78d394..6aa53d1 100644 --- a/src/files.py +++ b/src/files.py @@ -28,6 +28,9 @@ class PireneaFiles(object): def add_prefix(self, prefix="P0"): """ Search all files recursively and add a prefix to them. + P0 : PIRENEA files produced with the Villa setup + P1 : PIRENEA files produced from the IRAP setup + P2 : PIRENEA files produced from the PILAB setup """ self.__check_prefix(prefix) prefix += "_" @@ -38,7 +41,7 @@ class PireneaFiles(object): def remove_prefix(self, prefix="P0"): """ - Search all files recursively and remove their setup prefix. + Search all files recursively and remove their prefix. """ self.__check_prefix(prefix) prefix += "_" @@ -51,9 +54,6 @@ class PireneaFiles(object): def __check_prefix(self, prefix="P0"): """ Check if a prefix is valid. - P0 : PIRENEA files produced with the Villa setup - P1 : PIRENEA files produced from the IRAP setup - P2 : PIRENEA files produced from the PILAB setup """ prefix_list = {"P0", "P1", "P2"} if prefix not in prefix_list: diff --git a/tests/test_files.py b/tests/test_files.py index 1ef65eb..55eef23 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -8,32 +8,42 @@ tests.test_files Created on 18 dec. 2017 """ +import os +import tempfile import unittest + from src.files import PireneaFiles class PireneaFilesTestCase(unittest.TestCase): def setUp(self): - """Call before every test case.""" - self.pirenea = PireneaFiles() - self.folder = "D:/PIRENEA/DATA_1" - self.bad_folder = "c:/documents" - self.empty = "" + """Called before test case.""" + self.bad_prefix = "P3" + self.nonexist = "PIRENEA_DATA" - def test_add_prefix(self): - with self.assertRaises(ValueError): - self.pirenea.__init__(self.bad_folder) + def test_check_input(self): + """Wrong directory.""" with self.assertRaises(ValueError): - self.pirenea.__init__(self.empty) - with self.assertRaises(ValueError): - self.pirenea.__init__(self.folder) - self.pirenea.add_prefix("PO") - - def test_remove_prefix(self): + with tempfile.TemporaryDirectory() as bad_tmpdirname: + self.pirenea = PireneaFiles(bad_tmpdirname) + """Nonexistent directory.""" with self.assertRaises(ValueError): - self.pirenea.__init__(self.folder) - self.pirenea.remove_prefix(self.empty) + self.pirenea = PireneaFiles(self.nonexist) + """Wrong prefix.""" + with tempfile.TemporaryDirectory(prefix="PIRENEA_DATA") as tmpdirname: + self.pirenea = PireneaFiles(tmpdirname) + with self.assertRaises(ValueError): + self.pirenea.add_prefix(self.bad_prefix) + + def test_add_remove_prefix(self): + with tempfile.TemporaryDirectory(prefix="PIRENEA_DATA") as tmpdirname: + with tempfile.TemporaryFile(dir=tmpdirname) as tf: + self.pirenea = PireneaFiles(tmpdirname) + self.pirenea.add_prefix("P0") + self.pirenea.remove_prefix("P0") + """Check if temporary file ft is unchanged""" + self.assertTrue(os.path.isfile(tf.name)) if __name__ == "__main__": -- libgit2 0.21.2