test_blueprints.py 4.9 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
2
import os
import logging
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
3

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
4
from senpy.extensions import Senpy
5
from senpy import models
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
6
from flask import Flask
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7
from unittest import TestCase
8
from itertools import product
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
9

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
10

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
11
def check_dict(indic, template):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
12
    return all(item in indic.items() for item in template.items())
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
13

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
14

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
15
def parse_resp(resp):
16
    return models.from_json(resp.data.decode('utf-8'))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
17

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
18
19

class BlueprintsTest(TestCase):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
20
    def setUp(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
21
        self.app = Flask("test_extensions")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
22
        self.app.debug = False
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
23
        self.client = self.app.test_client()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
24
25
26
27
        self.senpy = Senpy()
        self.senpy.init_app(self.app)
        self.dir = os.path.join(os.path.dirname(__file__), "..")
        self.senpy.add_folder(self.dir)
28
        self.senpy.activate_plugin("Dummy", sync=True)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
29
30
        self.senpy.activate_plugin("DummyRequired", sync=True)
        self.senpy.default_plugin = 'Dummy'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
31

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
32
33
    def assertCode(self, resp, code):
        self.assertEqual(resp.status_code, code)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
34

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
35
    def test_home(self):
36
37
38
        """
        Calling with no arguments should ask the user for more arguments
        """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
39
        resp = self.client.get("/api/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
40
        self.assertCode(resp, 400)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
41
42
        js = parse_resp(resp)
        logging.debug(js)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
43
        assert js["status"] == 400
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
44
        atleast = {
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
45
            "status": 400,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
46
47
            "message": "Missing or invalid parameters",
        }
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
48
        assert check_dict(js, atleast)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
49

50
    def test_analysis(self):
51
52
53
54
        """
        The dummy plugin returns an empty response,\
        it should contain the context
        """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
55
        resp = self.client.get("/api/?i=My aloha mohame")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
56
57
58
59
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug("Got response: %s", js)
        assert "@context" in js
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
60
        assert "entries" in js
61

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    def test_analysis_extra(self):
        """
        Extra params that have a default should
        """
        resp = self.client.get("/api/?i=My aloha mohame&algo=Dummy")
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug("Got response: %s", js)
        assert "@context" in js
        assert "entries" in js

    def test_analysis_extra_required(self):
        """
        Extra params that have a required argument that does not
        have a default should raise an error.
        """
        resp = self.client.get("/api/?i=My aloha mohame&algo=DummyRequired")
        self.assertCode(resp, 400)
        js = parse_resp(resp)
        logging.debug("Got response: %s", js)
        assert isinstance(js, models.Error)

84
85
86
87
88
89
90
91
92
93
94
    def test_error(self):
        """
        The dummy plugin returns an empty response,\
        it should contain the context
        """
        resp = self.client.get("/api/?i=My aloha mohame&algo=DOESNOTEXIST")
        self.assertCode(resp, 404)
        js = parse_resp(resp)
        logging.debug("Got response: %s", js)
        assert isinstance(js, models.Error)

95
96
    def test_list(self):
        """ List the plugins """
Ignacio Corcuera's avatar
Ignacio Corcuera committed
97
        resp = self.client.get("/api/plugins/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
98
99
100
101
102
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
        assert 'plugins' in js
        plugins = js['plugins']
103
104
        assert len(plugins) > 1
        assert list(p for p in plugins if p['name'] == "Dummy")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
105
        assert "@context" in js
106
107

    def test_headers(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
108
        for i, j in product(["/api/plugins/?nothing=", "/api/?i=test&"],
109
                            ["inHeaders"]):
Ignacio Corcuera's avatar
Ignacio Corcuera committed
110
            resp = self.client.get("%s" % (i))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
111
112
            js = parse_resp(resp)
            assert "@context" in js
Ignacio Corcuera's avatar
Ignacio Corcuera committed
113
            resp = self.client.get("%s&%s=0" % (i, j))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
114
            js = parse_resp(resp)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
115
            assert "@context" in js
Ignacio Corcuera's avatar
Ignacio Corcuera committed
116
            resp = self.client.get("%s&%s=1" % (i, j))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
117
            js = parse_resp(resp)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
118
            assert "@context" not in js
Ignacio Corcuera's avatar
Ignacio Corcuera committed
119
            resp = self.client.get("%s&%s=true" % (i, j))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
120
            js = parse_resp(resp)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
121
            assert "@context" not in js
122
123
124

    def test_detail(self):
        """ Show only one plugin"""
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
125
        resp = self.client.get("/api/plugins/Dummy/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
126
127
128
129
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
        assert "@id" in js
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
130
        assert js["@id"] == "plugins/Dummy_0.1"
131

132
133
    def test_default(self):
        """ Show only one plugin"""
134
        resp = self.client.get("/api/plugins/default/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
135
136
137
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
138
        assert "@id" in js
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
139
        assert js["@id"] == "plugins/Dummy_0.1"
140
141
142

    def test_context(self):
        resp = self.client.get("/api/contexts/context.jsonld")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
143
144
145
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        assert "@context" in js
146
        assert check_dict(
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
147
            js["@context"],
148
149
150
151
            {"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})

    def test_schema(self):
        resp = self.client.get("/api/schemas/definitions.json")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
152
153
        self.assertCode(resp, 200)
        js = parse_resp(resp)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
154
        assert "$schema" in js