Passed
Branch master (ea367e)
by Osma
02:12
created

test_http_analyze()   B

Complexity

Conditions 6

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
c 2
b 1
f 0
dl 0
loc 20
rs 8
1
"""Unit tests for the HTTP backend in Annif"""
2
3
import unittest.mock
4
import annif.backend.http
5
6
7
def test_http_analyze():
8
    with unittest.mock.patch('requests.post') as mock_request:
9
        # create a mock response whose .json() method returns the list that we
10
        # define here
11
        mock_response = unittest.mock.Mock()
12
        mock_response.json.return_value = [
13
            {'uri': 'http://example.org/http', 'label': 'http', 'score': 1.0}]
14
        mock_request.return_value = mock_response
15
16
        http_type = annif.backend.get_backend_type("http")
17
        http = http_type(
18
            backend_id='http',
19
            config={
20
                'endpoint': 'http://api.example.org/analyze',
21
                'project': 'dummy'})
22
        result = http.analyze('this is some text')
23
        assert len(result) == 1
24
        assert result[0].uri == 'http://example.org/http'
25
        assert result[0].label == 'http'
26
        assert result[0].score == 1.0
27