-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
314 lines (281 loc) · 11.1 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""the module containing unit test code for the metadata storage api
"""
from base64 import b64decode
import os
import tempfile
import unittest
from xml.etree import ElementTree
from sys import stderr
from testlib.output import build_core, build_envelope, define_namespaces
import metadatastorageapi
class TestToSpecOnePointO(unittest.TestCase):
"""the api test suite
"""
def setUp(self):
self.db_fd, metadatastorageapi.APP.config['DATABASE'] = tempfile.mkstemp()
metadatastorageapi.APP.testing = True
self.app = metadatastorageapi.APP.test_client()
# with metadatastorageapi.APP.app_context():
# metadatastorageapi.init_db()
def tearDown(self):
os.close(self.db_fd)
os.unlink(metadatastorageapi.APP.config['DATABASE'])
# Start of private methods
def _convert_response_to_xml(self, response_data):
try:
return ElementTree.fromstring(response_data)
except ElementTree.ParseError:
return None
def _test_for_right_num(self, find_els, right_num):
if find_els:
return len(find_els) == right_num
def _comp_for_truthy(self, a_list):
out = True
for n in a_list:
out &= n
return out
def _universal_output_testing(self, resp):
resp_root = resp
req = resp_root.findall("{http://lib.uchicago.edu/ldr}request")
s_time = resp_root.findall("{http://lib.uchicago.edu/ldr}responseSentTimeStamp")
r_time = resp_root.findall("{http://lib.uchicago.edu/ldr}requestReceivedTimeStamp")
r_type = resp_root.findall("{http://lib.uchicago.edu/ldr}responseType")
resp = resp_root.findall("{http://lib.uchicago.edu/ldr}response")
mdata = resp[0].findall("{http://lib.uchicago.edu/ldr}metadata")
out = self._comp_for_truthy([self._test_for_right_num(x, 1)
for x in [req, s_time, r_time, r_type, mdata]])
return out
def _opening_check(self, resp):
if resp.get_data():
data = self._convert_response_to_xml(resp.get_data())
first_checks = self._universal_output_testing(data)
return (first_checks, data)
else:
return (False, None)
# End of private methods
# Start of posting data tests
# Start of aggregate endpoint tests
def test_get_root_collections(self):
"""test endpoint /collections/[collection identifier]
should return output that looks like:
<output>
<request/>
<requestReceivedTimeStamp/>
<responseSentTimeStamp/>
<responseType/>
<response>
<metadata>
<dc:relation xsi:type="dcterms:URI"/>
<dc:relation xsi:type="dcterms:URI"/>
<dc:relation xsi:type="dcterms:URI"/>
</metadata>
</response>
</output>
"""
response = self.app.get("/")
answer = self._opening_check(response)
if answer[1]:
root = answer[1]
rels = [x.tag for x in root.find(
"{http://lib.uchicago.edu/ldr}response" +
"/{http://lib.uchicago.edu/ldr}metadata"
)]
check = answer[0]
rels = ([x for x in rels if x == "{http://purl.org/dc/elements/1.1/}relation"])
check &= len(rels) == 1
else:
# this means th]at the response didn't get picked up for some reason
stderr.write("cannot open response for /.\n")
return self.assertTrue(check)
return self.assertTrue(check)
def test_get_root_list_collections(self):
"""test endpoint /collections/[collection identifier]
should return output that looks like:
<output>
<request/>
<requestReceivedTimeStamp/>
<responseSentTimeStamp/>
<responseType/>
<response>
<metadata>
<dc:relation xsi:type="dcterms:URI"/>
<dc:relation xsi:type="dcterms:URI"/>
<dc:relation xsi:type="dcterms:URI"/>
</metadata>
</response>
</output>
"""
response = self.app.get("/collections")
answer = self._opening_check(response)
if answer[1]:
root = answer[1]
parts = root.find(
"{http://lib.uchicago.edu/ldr}response/" +
"{http://lib.uchicago.edu/ldr}metadata"
)
parts = [x for x in parts if x.tag == "{http://purl.org/dc/elements/1.1/}hasPart"]
check = answer[0]
print(parts)
check &= len(parts) >= 1
print(check)
else:
check = answer[0]
return self.assertTrue(check)
def test_get_nested_collection_list(self):
"""test endpoint /collections/[collection identifier]/[sub-collection identifier]
should return output that looks like:
<output>
<request/>
<requestReceivedTimeStamp/>
<responseSentTimeStamp/>
<responseType/>
<response>
<metadata>
<dc:relation xsi:type="dcterms:URI"/>
<dc:relation xsi:type="dcterms:URI"/>
<dc:relation xsi:type="dcterms:URI"/>
</metadata>
</response>
</output>
"""
response = self.app.get("/collections/collection2")
answer = self._opening_check(response)
if answer[1]:
root = answer[1]
parts = root.find(
"{http://lib.uchicago.edu/ldr}response/" +
"{http://lib.uchicago.edu/ldr}metadata"
)
print(parts)
parts = [x for x in parts if x.tag == "{http://purl.org/dc/elements/1.1/}hasPart"]
print(parts)
check = answer[0]
check &= len(parts) >= 1
print(check)
else:
check = answer[0]
return self.assertTrue(check)
def test_get_double_nested_collection_list(self):
"""test endpoint /collections/[collection identifier]/[sub-collection identifier]
should return output that looks like:
<output>
<request/>
<requestReceivedTimeStamp/>
<responseSentTimeStamp/>
<responseType/>
<response>
<metadata>
<dc:relation xsi:type="dcterms:URI"/>
<dc:relation xsi:type="dcterms:URI"/>
<dc:relation xsi:type="dcterms:URI"/>
</metadata>
</response>
</output>
"""
response = self.app.get("/collections/collection2-subcollection1")
answer = self._opening_check(response)
if answer[1]:
root = answer[1]
parts = root.find(
"{http://lib.uchicago.edu/ldr}response/" +
"{http://lib.uchicago.edu/ldr}metadata"
)
print(parts)
print(ElementTree.tostring(root))
parts = [x for x in parts]
print(parts)
check = answer[0]
check &= len(parts) >= 1
print(check)
else:
check = answer[0]
return self.assertTrue(check)
def test_get_core_metadata(self):
"""test endpoint /collection/[collection identifier]/core
should return output that looks like:
<output>
<request/>
<requestReceivedTimeStamp/>
<responseSentTimeStamp/>
<responseType/>
<response>
<metadata>
<dc:title/>
<dc:identifier/>
<dc:creator/>
<dc:date/>
<dc:description/>
<dc:isPartOf/>
<dc:hasPartOf/>
</metadata>
</response>
</output>
"""
response = self.app.get("/collection/collection2-subcollection1/core")
answer = self._opening_check(response)
if answer[1]:
root = answer[1]
metadata = root.find(
"{http://lib.uchicago.edu/ldr}response/" +
"{http://lib.uchicago.edu/ldr}metadata"
)
identifier = [x for x in metadata if x.tag == "{http://purl.org/dc/elements/1.1/}identifier"]
title = [x for x in metadata if x.tag == "{http://purl.org/dc/elements/1.1/}title"]
check = answer[0]
check &= len(identifier) >= 1
check &= len(title) >= 1
else:
check = answer[1]
return self.assertTrue(check)
# identifier = True if root.findall(
# "{http://lib.uchicago.edu/ldr}response/{http://lib.uchicago.edu/ldr}metadata/{http://purl.org/dc/elements/1.1/}date"
# ).count() == 1 else False
# return self.assertTrue(first_checks) & self.assertTrue(title) & self.assertTrue(identifier)
# else:
# return self.assertTrue(False)
def test_get_extension_list(self):
"""test endpoint /collection/[collection identifier]/extensions
should return output that looks like:
<output>
<request/>
<requestReceivedTimeStamp/>
<responseSentTimeStamp/>
<responseType/>
<response>
<metadata>
<dc:relation xsi:type="dcterms:URI"/>
<dc:relation xsi:type="dcterms:URI"/>
<dc:relation xsi:type="dcterms:URI"/>
</metadata>
</response>
</output>
"""
response = self.app.get("/collection/collection2-subcollection1-item1/proxies")
answer = self._opening_check(response)
if answer[1]:
root = answer[1]
metadata = root.find(
"{http://lib.uchicago.edu/ldr}response/" +
"{http://lib.uchicago.edu/ldr}metadata"
)
rels = [x for x in metadata if x.tag == "{http://purl.org/dc/elements/1.1/}relation"]
check = answer[0]
check &= len(rels) >= 1
else:
check = answer[0]
return self.assertTrue(check)
def test_get_an_extension(self):
response = self.app.get("/collection/collection2-subcollection1-item1/proxies/collection2-subcollection1-item1-mods")
if response.status_code:
data = response.get_data()
check = True
try:
b64decode(data)
check &= True
except ValueError:
check &= False
else:
check = False
return self.assertTrue(check)
if __name__ == '__main__':
unittest.main()