forked from zimmerman-team/fulltext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
162 lines (123 loc) · 5.59 KB
/
tests.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
import os
import sys
import unittest
import fulltext
from functools import wraps
TEST = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ipsum augue, iaculis quis auctor eu, adipiscing non est. " \
"Nullam id sem diam, eget varius dui. Etiam sollicitudin sapien nec odio elementum sit amet luctus magna volutpat. Ut " \
"commodo nulla neque. Aliquam erat volutpat. Integer et nunc augue. Pellentesque habitant morbi tristique senectus et " \
"netus et malesuada fames ac turpis egestas. Quisque at enim nulla, vel tincidunt urna. Nam leo augue, elementum ut " \
"viverra eget, scelerisque in purus. In arcu orci, porta nec aliquet quis, pretium a sem. In fermentum nisl id diam " \
"luctus viverra. Nullam semper, metus at euismod vulputate, orci odio dignissim urna, quis iaculis neque lacus ut " \
"tortor. Ut a justo non dolor venenatis accumsan. Proin dolor eros, aliquam id condimentum et, aliquam quis metus. " \
"Vivamus eget purus diam."
def allow_missing_command(f):
wraps(f)
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except fulltext.MissingCommandException:
pass
return wrapper
class FullText(unittest.TestCase):
def test_missing_default(self):
"Ensures a missing file will return default value instead of exception."
self.assertEqual(fulltext.get('non-existent-file.pdf', 'canary'), 'canary')
def test_missing(self):
"Ensures a missing file without a default raises an exception."
self.assertRaises(fulltext.FullTextException, fulltext.get, 'non-existent-file.pdf')
def test_unknown_default(self):
"Ensures an unknown file type will return default value instead of exception."
self.assertEqual(fulltext.get('unknown-file.foobar', 'canary'), 'canary')
def test_unknown(self):
"Ensures an unknown file type without a default will raise an exception."
self.assertRaises(fulltext.FullTextException, fulltext.get, 'unknown-file.foobar')
def test_default_none(self):
"Ensures None is a valid value to pass as default."
self.assertEqual(fulltext.get('unknown-file.foobar', None), None)
def test_handler(self):
"Ensures that a handler registered for a given type is executed when that type is converted."
def test_handler(f, type):
return TEST
fulltext.add('application/test', '.test', test_handler)
self.assertEqual(fulltext.get('files/test.test'), TEST)
self.assertEqual(fulltext.get(file('files/test.test', 'r')), TEST)
def test_command(self):
"""Ensures that commands registered for a given type are executed by the `run_command` handler
when that type is converted."""
fulltext.add('application/test', '.test', fulltext.run_command, (('echo', TEST), ('echo', TEST), ))
self.assertEqual(fulltext.get('files/test.test'), TEST)
self.assertEqual(fulltext.get(file('files/test.test', 'r')), TEST)
class FullTextFiles(unittest.TestCase):
"Tests various file types using disk file method."
@allow_missing_command
def test_odt(self):
self.assertEqual(fulltext.get('files/test.odt'), TEST)
@allow_missing_command
def test_ods(self):
self.assertEqual(fulltext.get('files/test.ods'), TEST)
@allow_missing_command
def test_doc(self):
self.assertEqual(fulltext.get('files/test.doc'), TEST)
@allow_missing_command
def test_pdf(self):
self.assertEqual(fulltext.get('files/test.pdf'), TEST)
@allow_missing_command
def test_rtf(self):
self.assertEqual(fulltext.get('files/test.rtf'), TEST)
@allow_missing_command
def test_xls(self):
self.assertEqual(fulltext.get('files/test.xls'), TEST)
@allow_missing_command
def test_txt(self):
self.assertEqual(fulltext.get('files/test.txt'), TEST)
@allow_missing_command
def test_zip(self):
self.assertEqual(fulltext.get('files/test.zip'), TEST)
class FullTextFds(unittest.TestCase):
"Tests various file types using file-like object method."
@allow_missing_command
def test_odt(self):
self.assertEqual(fulltext.get(file('files/test.odt', 'r')), TEST)
@allow_missing_command
def test_ods(self):
self.assertEqual(fulltext.get(file('files/test.ods', 'r')), TEST)
@allow_missing_command
def test_doc(self):
self.assertEqual(fulltext.get(file('files/test.doc', 'r')), TEST)
@allow_missing_command
def test_pdf(self):
self.assertEqual(fulltext.get(file('files/test.pdf', 'r')), TEST)
@allow_missing_command
def test_rtf(self):
self.assertEqual(fulltext.get(file('files/test.rtf', 'r')), TEST)
@allow_missing_command
def test_xls(self):
self.assertEqual(fulltext.get(file('files/test.xls', 'r')), TEST)
@allow_missing_command
def test_txt(self):
self.assertEqual(fulltext.get(file('files/test.txt', 'r')), TEST)
@allow_missing_command
def test_zip(self):
self.assertEqual(fulltext.get(file('files/test.zip', 'r')), TEST)
class FullTextCheck(unittest.TestCase):
"Test the check function."
def test_success(self):
"At least verify the function executes without an error."
# The output can be ignored
stdout = sys.stdout
try:
sys.stdout = open(os.devnull, 'w')
except:
# We tried... not core to the test though.
pass
try:
fulltext.check()
except Exception, e:
self.fail(str(e))
finally:
sys.stdout = stdout
def main():
unittest.main()
if __name__ == '__main__':
main()