2 Test suite for storage. Uses unittest module.
4 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro.
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"))
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"))
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"))
35 def create_numeric_files(path):
36 f = open(os.path.join(path, "33"), 'w')
38 f = open(os.path.join(path, "6"), 'w')
40 f = open(os.path.join(path, "424"), 'w')
42 f = open(os.path.join(path, "1402"), 'w')
44 f = open(os.path.join(path, "93"), 'w')
47 class StorageTest(unittest.TestCase):
49 Test suite for stand alone functions in storage.py.
52 # Class specific variables. Initialized in setUp, used throughout tests.
53 path = "/tmp/ttfa-test"
60 """Remove base folder."""
61 shutil.rmtree(self.path)
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)
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)
72 id = storage.find_last_numeric_subfolder(self.path)
73 self.assertEqual(id, None)
75 def test_find_last_numeric_subfolder_only_numeric_subfolders_sequential(self):
77 Test return of correct id when sequential numeric named
78 subfolders only are present (1, 2, 3, ...).
80 create_numeric_subfolders_sequential(self.path)
82 id = storage.find_last_numeric_subfolder(self.path)
83 self.assertEqual(id, 5)
85 def test_find_last_numeric_subfolder_only_numeric_subfolders_nonsequential(self):
87 Test return of correct id when nonsequential numeric named
88 subfolders only are present (32, 5, 423, ...).
90 create_numeric_subfolders_non_sequential(self.path)
92 id = storage.find_last_numeric_subfolder(self.path)
93 self.assertEqual(id, 1401)
95 def test_find_last_numeric_subfolder_mixed_subfolders_sequential(self):
97 Test return of None when mixed named subfolders (numeric and
98 others) are present. Numeric folders are sequential.
100 create_non_numeric_subfolders(self.path)
101 create_numeric_subfolders_sequential(self.path)
103 id = storage.find_last_numeric_subfolder(self.path)
104 self.assertEqual(id, 5)
106 def test_find_last_numeric_subfolder_mixed_subfolders_nonsequential(self):
108 Test return of None when mixed named subfolders (numeric and
109 others) are present. Numeric folders are non-sequential.
111 create_non_numeric_subfolders(self.path)
112 create_numeric_subfolders_non_sequential(self.path)
114 id = storage.find_last_numeric_subfolder(self.path)
115 self.assertEqual(id, 1401)
117 def test_find_last_numeric_subfolder_mixed_subfolders_files(self):
119 Test return of None when mixed named subfolders (numeric and
120 others) together with files are present. Numeric folders are
123 create_non_numeric_subfolders(self.path)
124 create_numeric_subfolders_non_sequential(self.path)
125 create_numeric_files(self.path)
127 id = storage.find_last_numeric_subfolder(self.path)
128 self.assertEqual(id, 1401)
131 class TreeTextFileAccessTest(unittest.TestCase):
133 Test suite for TreeTextFileAccess class in storage.py.
136 # Class specific variables. Initialized in setUp, used throughout tests.
137 path = "/tmp/ttfa-test"
140 """Create folder. Initialize object. """
144 """Remove base folder."""
145 shutil.rmtree(self.path)
147 def test_add_swarm_folder_and_file_are_created(self):
148 create_numeric_subfolders_sequential(self.path)
149 expected_swarm_subfolder = 6
151 # Create instance of class and add swarm.
152 a = storage.TreeTextFileAccess(self.path)
153 s = storage.Swarm(torrent_filename = "fedora.torrent",
157 expected_swarm_path = os.path.join(self.path,
158 str(expected_swarm_subfolder))
159 expected_swarm_config = os.path.join(expected_swarm_path, "swarm.conf")
161 expected_true = os.path.isdir(expected_swarm_path) and \
162 os.path.isfile(expected_swarm_config)
163 self.assertEqual(expected_true, True)
165 if __name__ == "__main__":