test_blueprints.py 5.84 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
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 21 22 23 24 25
    @classmethod
    def setUpClass(cls):
        """Set up only once, and re-use in every individual test"""
        cls.app = Flask("test_extensions")
        cls.app.debug = False
        cls.client = cls.app.test_client()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
26
        cls.senpy = Senpy(default_plugins=True)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
27 28 29 30 31 32
        cls.senpy.init_app(cls.app)
        cls.dir = os.path.join(os.path.dirname(__file__), "..")
        cls.senpy.add_folder(cls.dir)
        cls.senpy.activate_plugin("Dummy", sync=True)
        cls.senpy.activate_plugin("DummyRequired", sync=True)
        cls.senpy.default_plugin = 'Dummy'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
33

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37 38
    def test_playground(self):
        resp = self.client.get("/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
39
        assert "main.js" in resp.get_data(as_text=True)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
40

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

56
    def test_analysis(self):
57 58 59 60
        """
        The dummy plugin returns an empty response,\
        it should contain the context
        """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
61
        resp = self.client.get("/api/?i=My aloha mohame&with_parameters=True")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
62 63 64 65
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug("Got response: %s", js)
        assert "@context" in js
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
66
        assert "entries" in js
67

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
68 69 70 71
    def test_analysis_extra(self):
        """
        Extra params that have a default should
        """
72
        resp = self.client.get("/api/?i=My aloha mohame&algo=Dummy&with_parameters=true")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
73 74 75 76 77 78 79 80 81 82 83
        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.
        """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
84
        self.app.debug = False
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
85 86 87 88 89
        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)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
90 91 92 93
        resp = self.client.get("/api/?i=My aloha mohame&algo=DummyRequired&example=notvalid")
        self.assertCode(resp, 400)
        resp = self.client.get("/api/?i=My aloha mohame&algo=DummyRequired&example=a")
        self.assertCode(resp, 200)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
94

95 96 97 98 99
    def test_error(self):
        """
        The dummy plugin returns an empty response,\
        it should contain the context
        """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
100
        self.app.debug = False
101 102 103 104 105 106
        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)

107 108
    def test_list(self):
        """ List the plugins """
Ignacio Corcuera's avatar
Ignacio Corcuera committed
109
        resp = self.client.get("/api/plugins/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
110 111 112 113 114
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
        assert 'plugins' in js
        plugins = js['plugins']
115 116
        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
117
        assert "@context" in js
118 119

    def test_headers(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
120
        for i, j in product(["/api/plugins/?nothing=", "/api/?i=test&"],
121
                            ["inHeaders"]):
Ignacio Corcuera's avatar
Ignacio Corcuera committed
122
            resp = self.client.get("%s" % (i))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
123 124
            js = parse_resp(resp)
            assert "@context" in js
Ignacio Corcuera's avatar
Ignacio Corcuera committed
125
            resp = self.client.get("%s&%s=0" % (i, j))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
126
            js = parse_resp(resp)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
127
            assert "@context" in js
Ignacio Corcuera's avatar
Ignacio Corcuera committed
128
            resp = self.client.get("%s&%s=1" % (i, j))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
129
            js = parse_resp(resp)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
130
            assert "@context" not in js
Ignacio Corcuera's avatar
Ignacio Corcuera committed
131
            resp = self.client.get("%s&%s=true" % (i, j))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
132
            js = parse_resp(resp)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
133
            assert "@context" not in js
134 135 136

    def test_detail(self):
        """ Show only one plugin"""
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
137
        resp = self.client.get("/api/plugins/Dummy/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
138 139 140 141
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
        assert "@id" in js
J. Fernando Sánchez's avatar
WIP  
J. Fernando Sánchez committed
142
        assert js["@id"] == "endpoint:plugins/Dummy_0.1"
143

144 145
    def test_default(self):
        """ Show only one plugin"""
146
        resp = self.client.get("/api/plugins/default/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
147 148 149
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
150
        assert "@id" in js
J. Fernando Sánchez's avatar
WIP  
J. Fernando Sánchez committed
151
        assert js["@id"] == "endpoint:plugins/Dummy_0.1"
152 153 154

    def test_context(self):
        resp = self.client.get("/api/contexts/context.jsonld")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
155 156 157
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        assert "@context" in js
158
        assert check_dict(
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
159
            js["@context"],
160 161 162 163
            {"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
164
        self.assertCode(resp, 200)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
165
        assert "$schema" in resp.data.decode()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
166 167 168 169 170

    def test_help(self):
        resp = self.client.get("/api/?help=true")
        self.assertCode(resp, 200)
        js = parse_resp(resp)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
171 172
        assert "valid_parameters" in js
        assert "help" in js["valid_parameters"]
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
173 174 175 176

    def test_conversion(self):
        resp = self.client.get("/api/?input=hello&algo=emoRand&emotionModel=DOES NOT EXIST")
        self.assertCode(resp, 404)