-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
357 lines (267 loc) · 11.2 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import unittest
import requests
import json
class TestBasicAPI(unittest.TestCase):
def setUp(self):
self.data = {"text" : "Example sentences, which can be tokenized. We can also split the sentences. We can extract scores like an agatston score of 432."}
self.headers = {'Content-Type': 'application/json'}
self.base_url = "http://127.0.0.1:5000/api/"
def test_accepted_method(self):
tasks = ('simple/tokenize','simple/sentence_split','extract/health_scores')
for task in tasks:
url = self.base_url + task
r = requests.get(url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# check status code (405)
self.assertEquals(result['status'], 405)
def test_endpoint_not_found(self):
url = self.base_url + "non_existing"
r = requests.post(url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# check status code (404)
self.assertEquals(result['status'], 404)
def test_content_type(self):
tasks = ('simple/tokenize','simple/sentence_split','extract/health_scores')
self.headers['Content-Type'] = "application/xml"
for task in tasks:
url = self.base_url + task
r = requests.post(url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# check status code (415)
self.assertEquals(result['status'], 415)
def test_missing_text(self):
tasks = ('simple/tokenize','simple/sentence_split','extract/health_scores')
self.data = {"incorrect_label" : "Example sentences, which can be tokenized. We can also split the sentences. We can extract scores like an agatston score of 432."}
for task in tasks:
url = self.base_url + task
r = requests.post(url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# check status code (415)
self.assertEquals(result['status'], 400)
class TestSimpleFunctions(unittest.TestCase):
def setUp(self):
self.data = {"text" : "Example sentences, which can be tokenized. We can also split the sentences. We can extract scores like an agatston score of 432."}
self.headers = {'Content-Type': 'application/json'}
self.base_url = "http://127.0.0.1:5000/api/simple/"
def test_tokenize(self):
url = self.base_url + "tokenize"
r = requests.post(url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# check status code (200)
self.assertEquals(result['status'], 200)
# check if result exists in json
self.assertTrue('result' in result)
# check if length of the list matches number of tokens
self.assertTrue(len(result['result']), 26)
def test_sentence_split(self):
url = self.base_url + "sentence_split"
r = requests.post(url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# check status code (200)
self.assertEquals(result['status'], 200)
# check if result exists in json
self.assertTrue('result' in result)
# check if length of the list matches number of tokens
self.assertTrue(len(result['result']), 3)
class TestExtractionFunctions(unittest.TestCase):
def setUp(self):
self.data = {"text" : "Example sentences, which can be tokenized met pizza brood voor unknownValue test. The first agatston score is 432 432 (mesa percentiel 22). We can also split the sentences. We can extract scores like an agatston score of 612 which is the 23e MESA percentiel. Other agatston scores are -9 which is too low. Also, for agatston 8,6 is incorrect. Finally, an agatston score of 40000 is too high to be possible. The following agatston is too far away to be considered useful 430."}
self.health_scores = {
'agatston' : {
'synonyms' : [
'agatston',
'agatston-score',
'agatstonscore',
'kalkscore',
'calciumscore'
],
'values' : {
'type' : "int",
'min' : 0,
'max' : 9999,
'format' : "(\-*\d+)((,|\.)\d+)?",
'group' : 0,
'position' : {
'before' : 0,
'after' : 40
}
}
},
'unknownValue' : {
'synonyms' : ["pizza", "brood"],
'values' : {}
},
'mesa' : {
'synonyms' : [
'MESA'
],
'values' : {
'type' : "int",
'min' : 0,
'max' : 100,
'format' : "\D(\d+)\D",
'group' : 1,
'position' : {
'before' : -10,
'after' : 35
}
}
}
}
self.headers = {'Content-Type': 'application/json'}
self.base_url = "http://127.0.0.1:5000/api/extract/health_scores"
def test_missing_health_scores(self):
r = requests.post(self.base_url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# check status code (415)
self.assertEquals(result['status'], 400)
def test_multiple_values_strict(self):
self.data['health_scores'] = self.health_scores
r = requests.post(self.base_url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# assert status
self.assertEquals(result['status'], 200)
#assert existence of json result
#print result
self.assertTrue('result' in result)
# assert existence of agatston score in result
self.assertTrue('agatston' in result['result']['findings'])
self.assertTrue('mesa' in result['result']['findings'])
# assert correct value of agatston
self.assertEquals(len(result['result']['findings']['agatston']), 6)
# First 2 are 'good' findings
self.assertEquals(432, result['result']['findings']['agatston'][0]['value'])
self.assertEquals(612, result['result']['findings']['agatston'][1]['value'])
# Last 4 should be warnings (but still included for highlight / 'incorrect' value display)
self.assertTrue(-9 in [x['value'] for x in result['result']['findings']['agatston'][2]['optional'] ])
# assert correct value of mesa
self.assertEquals(len(result['result']['findings']['mesa']), 2)
self.assertEquals(22, result['result']['findings']['mesa'][0]['value'])
self.assertEquals(23, result['result']['findings']['mesa'][1]['value'])
def test_multiple_values_fuzzy(self):
self.data['health_scores'] = {
'agatston' : {
'synonyms' : [
'agatston',
'agatston-score',
'agatstonscore',
'kalkscore',
'calciumscore'
],
'values' : {}
}
}
r = requests.post(self.base_url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# assert status
self.assertEquals(result['status'], 200)
#assert existence of json result
self.assertTrue('result' in result)
# assert existence of agatston score in result
self.assertTrue('agatston' in result['result']['findings'])
# assert correct value of agatston
self.assertEquals(432, result['result']['findings']['agatston'][0]['value'])
self.assertEquals(612, result['result']['findings']['agatston'][1]['value'])
## Following values are not in the results, since 'hack' default values
# Too low
self.assertTrue(-9 in [x['value'] for x in result['result']['findings']['agatston'][2]['optional']])
# It is a float, but agatston accepts int, so give warning
self.assertTrue("8.6" in [x['value'] for x in result['result']['findings']['agatston'][3]['optional']])
# Too high
self.assertTrue(40000 in [x['value'] for x in result['result']['findings']['agatston'][4]['optional']])
def test_nonexistent_type(self):
self.data['health_scores'] = self.health_scores
self.data['health_scores']['unknownValue']['values']['type'] = "string"
r = requests.post(self.base_url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# assert status
self.assertEquals(result['status'], 400)
def test_types(self):
types = ('float','int')
self.data['health_scores'] = self.health_scores
for t in types:
self.data['health_scores']['agatston']['values']['type'] = t
r = requests.post(self.base_url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# assert status
self.assertEquals(result['status'], 200)
class TestResultFormatting(unittest.TestCase):
def setUp(self):
self.data = {"text" : "RA (rheumatoid antigen) can be positive in several different conditions beyond rheumatoid arthritis. It can also be incidentally positive without disease. If it is specifically your muscles hurting and not your joints then rheumatoid arthritis seems a bit less likely for you."}
self.health_scores = {
'rheuma' : {
'synonyms' : [
"rheumatoid disease",
"atrofische artritis",
"rheumatoid arthritis"
]
}
}
self.headers = {'Content-Type': 'application/json'}
self.base_url = "http://127.0.0.1:5000/api/extract/health_scores"
def test_without_valueDescription(self):
self.data['health_scores'] = self.health_scores
r = requests.post(self.base_url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
# Check if keys are properly returned
self.assertTrue('sentences' in result['result'])
self.assertTrue('findings' in result['result'])
self.assertTrue(isinstance(result['result']['findings']['rheuma'], list))
self.assertEquals(result['status'], 200)
def test_empty_valueDescription(self):
self.data['health_scores'] = self.health_scores
self.data['health_scores']['rheuma']['values'] = {}
r = requests.post(self.base_url, data=json.dumps(self.data), headers=self.headers)
result = r.json()
self.assertTrue('sentences' in result['result'])
self.assertTrue('findings' in result['result'])
self.assertTrue(isinstance(result['result']['findings']['rheuma'], list))
self.assertEquals(result['status'], 200)
# If nothing is found it should return something
def test_empty_synonyms(self):
import copy
tempData = copy.copy(self.data)
tempData['health_scores'] = {
"rheuma" : {
"synonyms" : []
}
}
r = requests.post(self.base_url, data=json.dumps(tempData), headers=self.headers)
result = r.json()
self.assertTrue('sentences' in result['result'])
self.assertTrue('findings' in result['result'])
self.assertTrue(isinstance(result['result']['findings']['rheuma'], list))
self.assertEquals(len(result['result']['findings']['rheuma']), 0)
self.assertEquals(result['status'], 200)
def test_unfound_synonyms(self):
import copy
tempData = copy.copy(self.data)
tempData['health_scores'] = {
"rheuma" : {
"synonyms" : ["924989ufhusadfhjehrewrwer9ae"]
}
}
r = requests.post(self.base_url, data=json.dumps(tempData), headers=self.headers)
result = r.json()
self.assertTrue('sentences' in result['result'])
self.assertTrue('findings' in result['result'])
self.assertTrue(isinstance(result['result']['findings']['rheuma'], list))
self.assertEquals(len(result['result']['findings']['rheuma']), 0)
self.assertEquals(result['status'], 200)
if __name__ == '__main__':
print "Testing basic API functionality\n"
suite = unittest.TestLoader().loadTestsFromTestCase(TestBasicAPI)
unittest.TextTestRunner(verbosity=2).run(suite)
print "\n\n"
print "Testing simple text analysis functionality\n"
suite = unittest.TestLoader().loadTestsFromTestCase(TestSimpleFunctions)
unittest.TextTestRunner(verbosity=2).run(suite)
print "\n\n"
print "Testing result format\n"
suite = unittest.TestLoader().loadTestsFromTestCase(TestResultFormatting)
unittest.TextTestRunner(verbosity=2).run(suite)
print "\n\n"
print "Testing extraction functionality\n"
suite = unittest.TestLoader().loadTestsFromTestCase(TestExtractionFunctions)
unittest.TextTestRunner(verbosity=2).run(suite)
print "\n\n"