ppf: Add tests for storage.py functions.
[cs-p2p-next.git] / ppf / new / tests / test_storage.py
1 """
2 Test suite for storage. Uses unittest module.
3
4 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro.
5 """
6
7 import unittest
8 import os
9 import os.path
10 import shutil
11
12 import storage
13
14 def create_non_numeric_subfolders(path):
15     os.mkdir(os.path.join(path, "ana"))
16     os.mkdir(os.path.join(path, "are"))
17     os.mkdir(os.path.join(path, "mere"))
18     os.mkdir(os.path.join(path, "si"))
19     os.mkdir(os.path.join(path, "pere"))
20
21 def create_numeric_subfolders_sequential(path):
22     os.mkdir(os.path.join(path, "1"))
23     os.mkdir(os.path.join(path, "2"))
24     os.mkdir(os.path.join(path, "3"))
25     os.mkdir(os.path.join(path, "4"))
26     os.mkdir(os.path.join(path, "5"))
27
28 def create_numeric_subfolders_non_sequential(path):
29     os.mkdir(os.path.join(path, "32"))
30     os.mkdir(os.path.join(path, "5"))
31     os.mkdir(os.path.join(path, "423"))
32     os.mkdir(os.path.join(path, "1401"))
33     os.mkdir(os.path.join(path, "92"))
34
35 def create_numeric_files(path):
36     f = open(os.path.join(path, "33"), 'w')
37     f.close()
38     f = open(os.path.join(path, "6"), 'w')
39     f.close()
40     f = open(os.path.join(path, "424"), 'w')
41     f.close()
42     f = open(os.path.join(path, "1402"), 'w')
43     f.close()
44     f = open(os.path.join(path, "93"), 'w')
45     f.close()
46
47 class StorageTest(unittest.TestCase):
48     """
49     Test suite for stand alone functions in storage.py.
50     """
51
52     # Class specific variables. Initialized in setUp, used throughout tests.
53     path = "/tmp/ttfa-test"
54
55     def setUp(self):
56         """Create folder."""
57         os.mkdir(self.path)
58
59     def tearDown(self):
60         """Remove base folder."""
61         shutil.rmtree(self.path)
62
63     def test_find_last_numeric_subfolder_no_subfolders(self):
64         """Test return of None when no subfolders are present."""
65         id = storage.find_last_numeric_subfolder(self.path)
66         self.assertEqual(id, None)
67
68     def test_find_last_numeric_subfolder_no_numeric_subfolders(self):
69         """Test return of None when no numeric subfolders are present."""
70         create_non_numeric_subfolders(self.path)
71
72         id = storage.find_last_numeric_subfolder(self.path)
73         self.assertEqual(id, None)
74
75     def test_find_last_numeric_subfolder_only_numeric_subfolders_sequential(self):
76         """
77         Test return of correct id when sequential numeric named
78         subfolders only are present (1, 2, 3, ...).
79         """
80         create_numeric_subfolders_sequential(self.path)
81
82         id = storage.find_last_numeric_subfolder(self.path)
83         self.assertEqual(id, 5)
84
85     def test_find_last_numeric_subfolder_only_numeric_subfolders_nonsequential(self):
86         """
87         Test return of correct id when nonsequential numeric named
88         subfolders only are present (32, 5, 423, ...).
89         """
90         create_numeric_subfolders_non_sequential(self.path)
91
92         id = storage.find_last_numeric_subfolder(self.path)
93         self.assertEqual(id, 1401)
94
95     def test_find_last_numeric_subfolder_mixed_subfolders_sequential(self):
96         """
97         Test return of None when mixed named subfolders (numeric and
98         others) are present. Numeric folders are sequential.
99         """
100         create_non_numeric_subfolders(self.path)
101         create_numeric_subfolders_sequential(self.path)
102
103         id = storage.find_last_numeric_subfolder(self.path)
104         self.assertEqual(id, 5)
105
106     def test_find_last_numeric_subfolder_mixed_subfolders_nonsequential(self):
107         """
108         Test return of None when mixed named subfolders (numeric and
109         others) are present. Numeric folders are non-sequential.
110         """
111         create_non_numeric_subfolders(self.path)
112         create_numeric_subfolders_non_sequential(self.path)
113
114         id = storage.find_last_numeric_subfolder(self.path)
115         self.assertEqual(id, 1401)
116
117     def test_find_last_numeric_subfolder_mixed_subfolders_files(self):
118         """
119         Test return of None when mixed named subfolders (numeric and
120         others) together with files are present. Numeric folders are
121         non-sequential.
122         """
123         create_non_numeric_subfolders(self.path)
124         create_numeric_subfolders_non_sequential(self.path)
125         create_numeric_files(self.path)
126
127         id = storage.find_last_numeric_subfolder(self.path)
128         self.assertEqual(id, 1401)
129
130 if __name__ == "__main__":
131     unittest.main()