Commit d52c56a07fdff9077480bf9ee9f8041b1faf8755

Authored by Odile@pc2050
1 parent be14c641
Exists in master

Add comments and complete tests coverage.

Showing 2 changed files with 30 additions and 20 deletions   Show diff stats
@@ -28,6 +28,9 @@ class PireneaFiles(object): @@ -28,6 +28,9 @@ class PireneaFiles(object):
28 def add_prefix(self, prefix="P0"): 28 def add_prefix(self, prefix="P0"):
29 """ 29 """
30 Search all files recursively and add a prefix to them. 30 Search all files recursively and add a prefix to them.
  31 + P0 : PIRENEA files produced with the Villa setup
  32 + P1 : PIRENEA files produced from the IRAP setup
  33 + P2 : PIRENEA files produced from the PILAB setup
31 """ 34 """
32 self.__check_prefix(prefix) 35 self.__check_prefix(prefix)
33 prefix += "_" 36 prefix += "_"
@@ -38,7 +41,7 @@ class PireneaFiles(object): @@ -38,7 +41,7 @@ class PireneaFiles(object):
38 41
39 def remove_prefix(self, prefix="P0"): 42 def remove_prefix(self, prefix="P0"):
40 """ 43 """
41 - Search all files recursively and remove their setup prefix. 44 + Search all files recursively and remove their prefix.
42 """ 45 """
43 self.__check_prefix(prefix) 46 self.__check_prefix(prefix)
44 prefix += "_" 47 prefix += "_"
@@ -51,9 +54,6 @@ class PireneaFiles(object): @@ -51,9 +54,6 @@ class PireneaFiles(object):
51 def __check_prefix(self, prefix="P0"): 54 def __check_prefix(self, prefix="P0"):
52 """ 55 """
53 Check if a prefix is valid. 56 Check if a prefix is valid.
54 - P0 : PIRENEA files produced with the Villa setup  
55 - P1 : PIRENEA files produced from the IRAP setup  
56 - P2 : PIRENEA files produced from the PILAB setup  
57 """ 57 """
58 prefix_list = {"P0", "P1", "P2"} 58 prefix_list = {"P0", "P1", "P2"}
59 if prefix not in prefix_list: 59 if prefix not in prefix_list:
tests/test_files.py
@@ -8,32 +8,42 @@ @@ -8,32 +8,42 @@
8 8
9 tests.test_files Created on 18 dec. 2017 9 tests.test_files Created on 18 dec. 2017
10 """ 10 """
  11 +import os
  12 +import tempfile
11 import unittest 13 import unittest
  14 +
12 from src.files import PireneaFiles 15 from src.files import PireneaFiles
13 16
14 17
15 class PireneaFilesTestCase(unittest.TestCase): 18 class PireneaFilesTestCase(unittest.TestCase):
16 19
17 def setUp(self): 20 def setUp(self):
18 - """Call before every test case."""  
19 - self.pirenea = PireneaFiles()  
20 - self.folder = "D:/PIRENEA/DATA_1"  
21 - self.bad_folder = "c:/documents"  
22 - self.empty = "" 21 + """Called before test case."""
  22 + self.bad_prefix = "P3"
  23 + self.nonexist = "PIRENEA_DATA"
23 24
24 - def test_add_prefix(self):  
25 - with self.assertRaises(ValueError):  
26 - self.pirenea.__init__(self.bad_folder) 25 + def test_check_input(self):
  26 + """Wrong directory."""
27 with self.assertRaises(ValueError): 27 with self.assertRaises(ValueError):
28 - self.pirenea.__init__(self.empty)  
29 - with self.assertRaises(ValueError):  
30 - self.pirenea.__init__(self.folder)  
31 - self.pirenea.add_prefix("PO")  
32 -  
33 - def test_remove_prefix(self): 28 + with tempfile.TemporaryDirectory() as bad_tmpdirname:
  29 + self.pirenea = PireneaFiles(bad_tmpdirname)
  30 + """Nonexistent directory."""
34 with self.assertRaises(ValueError): 31 with self.assertRaises(ValueError):
35 - self.pirenea.__init__(self.folder)  
36 - self.pirenea.remove_prefix(self.empty) 32 + self.pirenea = PireneaFiles(self.nonexist)
  33 + """Wrong prefix."""
  34 + with tempfile.TemporaryDirectory(prefix="PIRENEA_DATA") as tmpdirname:
  35 + self.pirenea = PireneaFiles(tmpdirname)
  36 + with self.assertRaises(ValueError):
  37 + self.pirenea.add_prefix(self.bad_prefix)
  38 +
  39 + def test_add_remove_prefix(self):
  40 + with tempfile.TemporaryDirectory(prefix="PIRENEA_DATA") as tmpdirname:
  41 + with tempfile.TemporaryFile(dir=tmpdirname) as tf:
  42 + self.pirenea = PireneaFiles(tmpdirname)
  43 + self.pirenea.add_prefix("P0")
  44 + self.pirenea.remove_prefix("P0")
  45 + """Check if temporary file ft is unchanged"""
  46 + self.assertTrue(os.path.isfile(tf.name))
37 47
38 48
39 if __name__ == "__main__": 49 if __name__ == "__main__":