test_client.py 1.67 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
from unittest import TestCase
2

3
from senpy.testing import patch_requests
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
4
from senpy.client import Client
5
from senpy.models import Results, Plugins, Error
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
6
from senpy.plugins import AnalysisPlugin
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7
8
9
10
11
12
13
14
15
16


class ModelsTest(TestCase):
    def setUp(self):
        self.host = '0.0.0.0'
        self.port = 5000

    def test_client(self):
        endpoint = 'http://dummy/'
        client = Client(endpoint)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
17
        with patch_requests(Results()) as (request, response):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
18
19
            resp = client.analyse('hello')
            assert isinstance(resp, Results)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
20
        request.assert_called_with(
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
21
            url=endpoint + '/', method='GET', params={'input': 'hello'})
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
22
        with patch_requests(Error('Nothing')) as (request, response):
23
24
25
26
27
            try:
                client.analyse(input='hello', algorithm='NONEXISTENT')
                raise Exception('Exceptions should be raised. This is not golang')
            except Error:
                pass
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
28
        request.assert_called_with(
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
29
30
31
32
            url=endpoint + '/',
            method='GET',
            params={'input': 'hello',
                    'algorithm': 'NONEXISTENT'})
33
34
35
36
37
38
39

    def test_plugins(self):
        endpoint = 'http://dummy/'
        client = Client(endpoint)
        plugins = Plugins()
        p1 = AnalysisPlugin({'name': 'AnalysisP1', 'version': 0, 'description': 'No'})
        plugins.plugins = [p1, ]
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
40
        with patch_requests(plugins) as (request, response):
41
42
43
44
            response = client.plugins()
            assert isinstance(response, dict)
            assert len(response) == 1
            assert "AnalysisP1" in response
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
45
        request.assert_called_with(
46
            url=endpoint + '/plugins', method='GET',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
47
            params={})