test_blueprints.py 4.08 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
5
6
from senpy.extensions import Senpy
from flask import Flask
from flask.ext.testing import TestCase
7
from gevent import sleep
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
J. Fernando Sánchez committed
14
15

class BlueprintsTest(TestCase):
16

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
17
18
19
20
21
22
    def create_app(self):
        self.app = Flask("test_extensions")
        self.senpy = Senpy()
        self.senpy.init_app(self.app)
        self.dir = os.path.join(os.path.dirname(__file__), "..")
        self.senpy.add_folder(self.dir)
23
        self.senpy.activate_plugin("Dummy", sync=True)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
24
25
26
        return self.app

    def test_home(self):
27
28
29
        """
        Calling with no arguments should ask the user for more arguments
        """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
30
        resp = self.client.get("/api/")
31
        self.assert404(resp)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
32
        logging.debug(resp.json)
33
        assert resp.json["status"] == 404
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
34
        atleast = {
35
            "status": 404,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
36
37
38
39
            "message": "Missing or invalid parameters",
        }
        assert check_dict(resp.json, atleast)

40
    def test_analysis(self):
41
42
43
44
        """
        The dummy plugin returns an empty response,\
        it should contain the context
        """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
45
        resp = self.client.get("/api/?i=My aloha mohame")
46
        self.assert200(resp)
47
        logging.debug("Got response: %s", resp.json)
48
49
50
51
52
        assert "@context" in resp.json
        assert "entries" in resp.json

    def test_list(self):
        """ List the plugins """
Ignacio Corcuera's avatar
Ignacio Corcuera committed
53
        resp = self.client.get("/api/plugins/")
54
55
        self.assert200(resp)
        logging.debug(resp.json)
56
57
58
59
        assert 'plugins' in resp.json
        plugins = resp.json['plugins']
        assert len(plugins) > 1
        assert list(p for p in plugins if p['name'] == "Dummy")
60
61
62
        assert "@context" in resp.json

    def test_headers(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
        for i, j in product(["/api/plugins/?nothing=", "/api/?i=test&"],
64
                            ["inHeaders"]):
Ignacio Corcuera's avatar
Ignacio Corcuera committed
65
66
67
68
69
70
71
72
            resp = self.client.get("%s" % (i))
            assert "@context" in resp.json
            resp = self.client.get("%s&%s=0" % (i, j))
            assert "@context" in resp.json
            resp = self.client.get("%s&%s=1" % (i, j))
            assert "@context" not in resp.json
            resp = self.client.get("%s&%s=true" % (i, j))
            assert "@context" not in resp.json
73
74
75

    def test_detail(self):
        """ Show only one plugin"""
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
76
        resp = self.client.get("/api/plugins/Dummy/")
77
78
79
80
81
82
83
        self.assert200(resp)
        logging.debug(resp.json)
        assert "@id" in resp.json
        assert resp.json["@id"] == "Dummy_0.1"

    def test_activate(self):
        """ Activate and deactivate one plugin """
Ignacio Corcuera's avatar
Ignacio Corcuera committed
84
        resp = self.client.get("/api/plugins/Dummy/deactivate")
85
86
        self.assert200(resp)
        sleep(0.5)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
87
        resp = self.client.get("/api/plugins/Dummy/")
88
89
90
        self.assert200(resp)
        assert "is_activated" in resp.json
        assert resp.json["is_activated"] == False
Ignacio Corcuera's avatar
Ignacio Corcuera committed
91
        resp = self.client.get("/api/plugins/Dummy/activate")
92
93
        self.assert200(resp)
        sleep(0.5)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
94
        resp = self.client.get("/api/plugins/Dummy/")
95
96
97
        self.assert200(resp)
        assert "is_activated" in resp.json
        assert resp.json["is_activated"] == True
98
99
100

    def test_default(self):
        """ Show only one plugin"""
101
        resp = self.client.get("/api/plugins/default/")
102
103
104
105
        self.assert200(resp)
        logging.debug(resp.json)
        assert "@id" in resp.json
        assert resp.json["@id"] == "Dummy_0.1"
Ignacio Corcuera's avatar
Ignacio Corcuera committed
106
        resp = self.client.get("/api/plugins/Dummy/deactivate")
107
108
        self.assert200(resp)
        sleep(0.5)
109
        resp = self.client.get("/api/plugins/default/")
110
        self.assert404(resp)
111
112
113
114
115
116
117
118
119
120
121
122
123

    def test_context(self):
        resp = self.client.get("/api/contexts/context.jsonld")
        self.assert200(resp)
        assert "@context" in resp.json
        assert check_dict(
            resp.json["@context"],
            {"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})

    def test_schema(self):
        resp = self.client.get("/api/schemas/definitions.json")
        self.assert200(resp)
        assert "$schema" in resp.json