-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtest_rest.py
275 lines (214 loc) · 8.54 KB
/
test_rest.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
"""Unit tests for REST API backend code in Annif"""
import importlib
import annif.rest
def test_rest_list_projects(app):
with app.app_context():
result = annif.rest.list_projects()[0]
project_ids = [proj["project_id"] for proj in result["projects"]]
# public project should be returned
assert "dummy-fi" in project_ids
# hidden project should not be returned
assert "dummy-en" not in project_ids
# private project should not be returned
assert "dummy-private" not in project_ids
# project with no access level setting should be returned
assert "ensemble" in project_ids
def test_rest_show_info(app):
with app.app_context():
result = annif.rest.show_info()[0]
version = importlib.metadata.version("annif")
assert result == {"title": "Annif REST API", "version": version}
def test_rest_show_project_public(app):
# public projects should be accessible via REST
with app.app_context():
result = annif.rest.show_project("dummy-fi")[0]
assert result["project_id"] == "dummy-fi"
def test_rest_show_project_hidden(app):
# hidden projects should be accessible if you know the project id
with app.app_context():
result = annif.rest.show_project("dummy-en")[0]
assert result["project_id"] == "dummy-en"
def test_rest_show_project_private(app):
# private projects should not be accessible via REST
with app.app_context():
result = annif.rest.show_project("dummy-private")
assert result.status_code == 404
def test_rest_show_project_nonexistent(app):
with app.app_context():
result = annif.rest.show_project("nonexistent")
assert result.status_code == 404
def test_rest_detect_language_english(app):
# english text should be detected
with app.app_context():
result = annif.rest.detect_language(
{"text": "example text", "languages": ["en", "fi", "sv"]}
)[0]
assert result["results"][0] == {"language": "en", "score": 1}
def test_rest_detect_language_unknown(app):
# an unknown language should return None
with app.app_context():
result = annif.rest.detect_language(
{"text": "exampley texty", "languages": ["fi", "sv"]}
)[0]
assert result["results"][0] == {"language": None, "score": 1}
def test_rest_detect_language_no_text(app):
with app.app_context():
result = annif.rest.detect_language({"text": "", "languages": ["en"]})[0]
assert result["results"][0] == {"language": None, "score": 1}
def test_rest_detect_language_unsupported_candidates(app):
with app.app_context():
result = annif.rest.detect_language(
{"text": "example text", "languages": ["unk"]}
)
assert result.status_code == 400
def test_rest_suggest_public(app):
# public projects should be accessible via REST
with app.app_context():
result = annif.rest.suggest(
"dummy-fi", {"text": "example text", "limit": 10, "threshold": 0.0}
)[0]
assert "results" in result
def test_rest_suggest_hidden(app):
# hidden projects should be accessible if you know the project id
with app.app_context():
result = annif.rest.suggest(
"dummy-en", {"text": "example text", "limit": 10, "threshold": 0.0}
)[0]
assert "results" in result
def test_rest_suggest_private(app):
# private projects should not be accessible via REST
with app.app_context():
result = annif.rest.suggest(
"dummy-private", {"text": "example text", "limit": 10, "threshold": 0.0}
)
assert result.status_code == 404
def test_rest_suggest_nonexistent(app):
with app.app_context():
result = annif.rest.suggest(
"nonexistent", {"text": "example text", "limit": 10, "threshold": 0.0}
)
assert result.status_code == 404
def test_rest_suggest_novocab(app):
with app.app_context():
result = annif.rest.suggest(
"novocab", {"text": "example text", "limit": 10, "threshold": 0.0}
)
assert result.status_code == 503
def test_rest_suggest_with_language_override(app):
with app.app_context():
result = annif.rest.suggest(
"dummy-vocablang",
{"text": "example text", "limit": 10, "threshold": 0.0, "language": "en"},
)[0]
assert result["results"][0]["label"] == "dummy"
def test_rest_suggest_with_language_override_bad_value(app):
with app.app_context():
result = annif.rest.suggest(
"dummy-vocablang",
{"text": "example text", "limit": 10, "threshold": 0.0, "language": "xx"},
)
assert result.status_code == 400
def test_rest_suggest_with_different_vocab_language(app):
# project language is English - input should be in English
# vocab language is Finnish - subject labels should be in Finnish
with app.app_context():
result = annif.rest.suggest(
"dummy-vocablang", {"text": "example text", "limit": 10, "threshold": 0.0}
)[0]
assert result["results"][0]["label"] == "dummy-fi"
def test_rest_suggest_with_notations(app):
with app.app_context():
result = annif.rest.suggest(
"dummy-fi", {"text": "example text", "limit": 10, "threshold": 0.0}
)[0]
assert result["results"][0]["notation"] is None
def test_rest_suggest_batch_one_doc(app):
with app.app_context():
result = annif.rest.suggest_batch(
"dummy-fi", {"documents": [{"text": "example text"}]}
)[0]
assert len(result) == 1
assert result[0]["results"][0]["label"] == "dummy-fi"
assert result[0]["document_id"] is None
def test_rest_suggest_batch_one_doc_with_id(app):
with app.app_context():
result = annif.rest.suggest_batch(
"dummy-fi",
{"documents": [{"text": "example text", "document_id": "doc-0"}]},
)[0]
assert len(result) == 1
assert result[0]["results"][0]["label"] == "dummy-fi"
assert result[0]["document_id"] == "doc-0"
def test_rest_suggest_batch_two_docs(app):
with app.app_context():
result = annif.rest.suggest_batch(
"dummy-fi",
{
"documents": [
{"text": "example text"},
{"text": "another example text"},
]
},
)[0]
assert len(result) == 2
assert result[1]["results"][0]["label"] == "dummy-fi"
def test_rest_suggest_batch_with_language_override(app):
with app.app_context():
result = annif.rest.suggest_batch(
"dummy-vocablang",
{
"documents": [{"text": "example text"}],
},
language="en",
)[0]
assert result[0]["results"][0]["label"] == "dummy"
def test_rest_suggest_batch_with_limit_override(app):
with app.app_context():
result = annif.rest.suggest_batch(
"dummy-fi",
{
"documents": [{"text": "example text"}],
},
limit=0,
)[0]
assert len(result[0]["results"]) == 0
def test_rest_learn_empty(app):
with app.app_context():
response = annif.rest.learn("dummy-en", [])
assert response == (
None,
204,
{"Content-Type": "application/json"},
) # success, no output
def test_rest_learn(app):
documents = [
{
"text": "the quick brown fox",
"subjects": [{"uri": "http://example.org/none", "label": "none"}],
}
]
with app.app_context():
response = annif.rest.learn("dummy-en", documents)
assert response == (
None,
204,
{"Content-Type": "application/json"},
) # success, no output
result = annif.rest.suggest(
"dummy-en", {"text": "example text", "limit": 10, "threshold": 0.0}
)[0]
assert "results" in result
assert result["results"][0]["uri"] == "http://example.org/none"
assert result["results"][0]["label"] == "none"
def test_rest_learn_novocab(app):
with app.app_context():
result = annif.rest.learn("novocab", [])
assert result.status_code == 503
def test_rest_learn_nonexistent(app):
with app.app_context():
result = annif.rest.learn("nonexistent", [])
assert result.status_code == 404
def test_rest_learn_not_supported(app):
with app.app_context():
result = annif.rest.learn("tfidf-fi", [])
assert result.status_code == 503