/
test_backend_http.py
125 lines (106 loc) · 4.82 KB
/
test_backend_http.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
"""Unit tests for the HTTP backend in Annif"""
import requests.exceptions
import unittest.mock
import annif.backend.http
def test_http_suggest(app, project):
with unittest.mock.patch('requests.post') as mock_request:
# create a mock response whose .json() method returns the list that we
# define here
mock_response = unittest.mock.Mock()
mock_response.json.return_value = [
{'uri': 'http://example.org/http', 'label': 'http', 'score': 1.0}]
mock_request.return_value = mock_response
http_type = annif.backend.get_backend("http")
http = http_type(
backend_id='http',
config_params={
'endpoint': 'http://api.example.org/analyze',
'project': 'dummy'},
datadir=app.config['DATADIR'])
result = http.suggest('this is some text', project=project)
assert len(result) == 1
assert result[0].uri == 'http://example.org/http'
assert result[0].label == 'http'
assert result[0].score == 1.0
def test_http_suggest_with_results(app, project):
with unittest.mock.patch('requests.post') as mock_request:
# create a mock response whose .json() method returns the list that we
# define here
mock_response = unittest.mock.Mock()
mock_response.json.return_value = {'results': [
{'uri': 'http://example.org/http', 'label': 'http', 'score': 1.0}]}
mock_request.return_value = mock_response
http_type = annif.backend.get_backend("http")
http = http_type(
backend_id='http',
config_params={
'endpoint': 'http://api.example.org/dummy/analyze',
},
datadir=app.config['DATADIR'])
result = http.suggest('this is some text', project=project)
assert len(result) == 1
assert result[0].uri == 'http://example.org/http'
assert result[0].label == 'http'
assert result[0].score == 1.0
def test_http_suggest_zero_score(app, project):
with unittest.mock.patch('requests.post') as mock_request:
# create a mock response whose .json() method returns the list that we
# define here
mock_response = unittest.mock.Mock()
mock_response.json.return_value = [
{'uri': 'http://example.org/http', 'label': 'http', 'score': 0.0}]
mock_request.return_value = mock_response
http_type = annif.backend.get_backend("http")
http = http_type(
backend_id='http',
config_params={
'endpoint': 'http://api.example.org/analyze',
'project': 'dummy'},
datadir=app.config['DATADIR'])
result = http.suggest('this is some text', project=project)
assert len(result) == 0
def test_http_suggest_error(app, project):
with unittest.mock.patch('requests.post') as mock_request:
mock_request.side_effect = requests.exceptions.RequestException(
'failed')
http_type = annif.backend.get_backend("http")
http = http_type(
backend_id='http',
config_params={
'endpoint': 'http://api.example.org/analyze',
'project': 'dummy'},
datadir=app.config['DATADIR'])
result = http.suggest('this is some text', project=project)
assert len(result) == 0
def test_http_suggest_json_fails(app, project):
with unittest.mock.patch('requests.post') as mock_request:
# create a mock response whose .json() method returns the list that we
# define here
mock_response = unittest.mock.Mock()
mock_response.json.side_effect = ValueError("JSON decode failed")
mock_request.return_value = mock_response
http_type = annif.backend.get_backend("http")
http = http_type(
backend_id='http',
config_params={
'endpoint': 'http://api.example.org/analyze',
'project': 'dummy'},
datadir=app.config['DATADIR'])
result = http.suggest('this is some text', project=project)
assert len(result) == 0
def test_http_suggest_unexpected_json(app, project):
with unittest.mock.patch('requests.post') as mock_request:
# create a mock response whose .json() method returns the list that we
# define here
mock_response = unittest.mock.Mock()
mock_response.json.return_value = ["spanish inquisition"]
mock_request.return_value = mock_response
http_type = annif.backend.get_backend("http")
http = http_type(
backend_id='http',
config_params={
'endpoint': 'http://api.example.org/analyze',
'project': 'dummy'},
datadir=app.config['DATADIR'])
result = http.suggest('this is some text', project=project)
assert len(result) == 0