-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
185 lines (162 loc) · 5.96 KB
/
test.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python3
import os
import shutil
import subprocess
import tempfile
import unittest
from os.path import join as j
from addext.addext import _puid_or_none, _check_file_extension
def is_non_zero_file(fpath):
return os.path.isfile(fpath) and os.path.getsize(fpath) > 0
class SelfCleaningTestCase(unittest.TestCase):
"""
TestCase subclass which cleans up self.tmpdir after each test
"""
def setUp(self):
super(SelfCleaningTestCase, self).setUp()
# tempdir for brunnhilde outputs
self.tmpdir = tempfile.mkdtemp()
if not os.path.isdir(self.tmpdir):
os.mkdirs(self.tmpdir)
def tearDown(self):
if os.path.isdir(self.tmpdir):
shutil.rmtree(self.tmpdir)
super(SelfCleaningTestCase, self).tearDown()
class TestIntegration(SelfCleaningTestCase):
"""
Integration tests. sf (Siegfried) must be installed on user's system.
"""
def test_dryrun_file(self):
"""
Test results of dryrun on file by checking
expected message against stdout
"""
cmd = [
'python3',
'addext/addext.py',
'-d',
'./test-data/animation',
'addext/pronom_v95.json'
]
stdout = subprocess.check_output(cmd).decode('utf-8')
statement = 'animation identified as Quicktime (x-fmt/384). Rename animation -> animation.mov'
self.assertIn(statement, stdout)
def test_dryrun_dir(self):
"""
Test results of dryrun on directory by checking
expected messages against stdout
"""
cmd = [
'python3',
'addext/addext.py',
'-d',
'./test-data/',
'addext/pronom_v95.json'
]
stdout = subprocess.check_output(cmd).decode('utf-8')
# Check file for presence of expected messages
checklist = [
'animation identified as Quicktime (x-fmt/384). Rename animation -> animation.mov',
'lorem-ipsum identified as Acrobat PDF 1.3 - Portable Document Format (fmt/17). Rename lorem-ipsum -> lorem-ipsum.pdf',
'PF identified as Lotus 1-2-3 Worksheet (x-fmt/114). Rename PF -> PF.wk1',
'TOPOREC identified as WordPerfect for MS-DOS/Windows Document (x-fmt/44). Rename TOPOREC -> TOPOREC.doc',
'valid identified as Microsoft Excel 97 Workbook (xls) (fmt/61). Rename valid -> valid.xls'
]
for statement in checklist:
self.assertIn(statement, stdout)
def test_auto_renaming_file(self):
"""
Test results of default auto-renaming mode for file
"""
# Copy test file to temporary directory
shutil.copy('./test-data/animation', self.tmpdir)
filepath = j(self.tmpdir, 'animation')
# Call addext in default (auto) mode
cmd = f'python3 addext/addext.py {filepath} addext/pronom_v95.json'
subprocess.call(cmd, shell=True)
# Check for presence of renamed file
renamed_filepath = j(self.tmpdir, 'animation.mov')
self.assertTrue(is_non_zero_file(renamed_filepath))
# Check for non-presence of original file
self.assertFalse(is_non_zero_file(filepath))
def test_auto_renaming_dir(self):
"""
Test results of default auto-renaming mode for directory
"""
# Copy test data to temporary directory
for f in os.listdir('./test-data'):
shutil.copy(j('./test-data', f), self.tmpdir)
# Call addext in default (auto) mode
cmd = f'python3 addext/addext.py {self.tmpdir} addext/pronom_v95.json'
subprocess.call(cmd, shell=True)
# Check that file with correct extension was not renamed
unchanged = ['lowercase_extension.pdf', 'uppercase_extension.PDF']
for f in unchanged:
self.assertTrue(is_non_zero_file(j(self.tmpdir, f)))
# Check for presence of renamed files
present = [
'animation.mov',
'lorem-ipsum.pdf',
'PF.wk1',
'TOPOREC.doc',
'valid.xls'
]
for f in present:
self.assertTrue(is_non_zero_file(j(self.tmpdir, f)))
# Check for non-presence of original files
not_present = [
'animation',
'lorem-ipsum',
'PF',
'TOPOREC',
'valid'
]
for f in not_present:
self.assertFalse(is_non_zero_file(j(self.tmpdir, f)))
class TestUnit(unittest.TestCase):
"""
Unit tests
"""
def test_puid_or_none(self):
"""
Unit tests for function _test_puid_or_none()
"""
# Test input with PUID
list_with_puid = [
{
"ns": "pronom",
"id": "x-fmt/384",
"format": "Quicktime",
"version": "",
"mime": "video/quicktime",
"basis": "byte match at 4, 8 (signature 4/8)",
"warning": "extension mismatch"
}
]
puid = _puid_or_none(list_with_puid)
self.assertTrue(puid == 'x-fmt/384')
# Test input with no PUID
list_without_puid = [
{
"ns": "something else",
"id": "abc123"
}
]
puid = _puid_or_none(list_without_puid)
self.assertTrue(puid is None)
def test_check_file_extension(self):
"""
Unit tests for function _check_file_extension()
"""
filepath = '/my/madeup/filepath.odt'
# Test positive case
pos_extensions = ['odt', 'ods', 'docx']
self.assertTrue(_check_file_extension(filepath, pos_extensions))
# Test case sensitivity
caps_extensions = ['ODT']
self.assertTrue(_check_file_extension(filepath, caps_extensions))
# Test negative case
neg_extensions = ['ods', 'docx', 'rtf']
self.assertFalse(_check_file_extension(filepath, neg_extensions))
if __name__ == '__main__':
unittest.main()