-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_foldercompare.py
More file actions
162 lines (119 loc) · 5.57 KB
/
Copy pathtest_foldercompare.py
File metadata and controls
162 lines (119 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""Test the foldercompare.py module."""
import filecmp
import os
import shutil
import unittest
import foldercompare
class TestRecursiveDircmpReport(unittest.TestCase):
"""Test the _recursive_dircmp function."""
def setUp(self):
"""Create two folders for testing."""
self.folder1 = os.path.join('tests', 'results1')
self.folder2 = os.path.join('tests', 'results2')
os.mkdir(self.folder1)
os.mkdir(self.folder2)
@unittest.skip('Fails half the time -- enough to avoid same_files feature')
def test_dircmp_diff_files_accuracy(self):
"""Different files are identified as such using filecmp.dircmp()"""
file1 = os.path.join(self.folder1, 'hello_world.txt')
with open(file1, 'w') as file:
file.write('foo')
file2 = os.path.join(self.folder2, 'hello_world.txt')
with open(file2, 'w') as file:
file.write('bar')
comparison = filecmp.dircmp(self.folder1, self.folder2)
self.assertTrue(comparison.diff_files == ['hello_world.txt'])
self.assertTrue(comparison.same_files == [])
def test_file_in_both(self):
"""Classifies two identical files as the same."""
file1 = os.path.join(self.folder1, 'hello_world.txt')
with open(file1, 'w') as file:
file.write('hello world')
file2 = os.path.join(self.folder2, 'hello_world.txt')
with open(file2, 'w') as file:
file.write('hello world')
report = foldercompare._recursive_dircmp(self.folder1, self.folder2)
expected = {'both': ['./hello_world.txt'], 'right': [], 'left': []}
self.assertEqual(report, expected)
def test_file_only_in_left(self):
"""Classifies file only in one directory."""
file1 = os.path.join(self.folder1, 'hello_world.txt')
with open(file1, 'w') as file:
file.write('hello world')
report = foldercompare._recursive_dircmp(self.folder1, self.folder2)
expected = {'left': ['./hello_world.txt'], 'right': [], 'both': []}
self.assertEqual(report, expected)
def test_subdirectory_only_in_left(self):
"""Classifies subdirectory with file only in left folder."""
subdir1 = os.path.join(self.folder1, 'subdir')
os.mkdir(subdir1)
file1 = os.path.join(subdir1, 'hello_world.txt')
with open(file1, 'w') as file:
file.write('hello world')
report = foldercompare._recursive_dircmp(self.folder1, self.folder2)
expected = {'left': ['./subdir'], 'right': [], 'both': []}
self.assertEqual(report, expected)
def test_subdir_file_only_in_left(self):
"""Classifies file only in one subdirectory."""
subdir1 = os.path.join(self.folder1, 'subdir')
os.mkdir(subdir1)
subdir2 = os.path.join(self.folder2, 'subdir')
os.mkdir(subdir2)
file1 = os.path.join(subdir1, 'hello_world.txt')
with open(file1, 'w') as file:
file.write('hello world')
report = foldercompare._recursive_dircmp(self.folder1, self.folder2)
expected = {'left': ['./subdir/hello_world.txt'], 'right': [], 'both': []}
self.assertEqual(report, expected)
def tearDown(self):
"""Delete test folders after each run."""
shutil.rmtree(self.folder1)
shutil.rmtree(self.folder2)
class TestCompareRegression(unittest.TestCase):
"""Test the compare function with known inputs and expected outputs."""
def setUp(self):
"""Set test input data and expected control outputs."""
self.folder1 = os.path.join('.', 'tests', 'control_data_1', 'Data Folder')
self.folder2 = os.path.join('.', 'tests', 'control_data_2', 'Data Folder')
self.resultfile = os.path.join('tests', 'results')
self.controlresults = os.path.join('tests', 'control_results')
def test_create_txt(self):
"""Can create a single TXT file, identical to the control."""
foldercompare.compare(self.folder1, self.folder2,
self.resultfile, output_txt=True)
result = filecmp.cmp(self.resultfile + '.txt',
self.controlresults + '.txt',
shallow=False)
self.assertTrue(result)
def test_create_csv(self):
"""Can create a single CSV file, identical to the control."""
foldercompare.compare(self.folder1, self.folder2,
self.resultfile, output_csv=True)
result = filecmp.cmp(self.resultfile + '.csv',
self.controlresults + '.csv',
shallow=False)
self.assertTrue(result)
def test_create_both(self):
"""Can create both files at once, identical to the control."""
foldercompare.compare(self.folder1, self.folder2, self.resultfile,
output_txt=True, output_csv=True)
result = filecmp.cmp(self.resultfile + '.txt',
self.controlresults + '.txt',
shallow=False)
self.assertTrue(result)
result = filecmp.cmp(self.resultfile + '.csv',
self.controlresults + '.csv',
shallow=False)
self.assertTrue(result)
def tearDown(self):
"""Delete test files after each run."""
try:
os.remove(self.resultfile + '.txt')
except FileNotFoundError:
pass
try:
os.remove(self.resultfile + '.csv')
except FileNotFoundError:
pass
if __name__ == '__main__':
unittest.main()