2014-11-07 18:12:21 +00:00
|
|
|
import os
|
|
|
|
import logging
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-11-07 18:12:21 +00:00
|
|
|
from senpy.extensions import Senpy
|
2017-02-17 00:53:02 +00:00
|
|
|
from senpy import models
|
2014-11-07 18:12:21 +00:00
|
|
|
from flask import Flask
|
2016-09-21 16:59:28 +00:00
|
|
|
from unittest import TestCase
|
2015-02-24 06:15:25 +00:00
|
|
|
from itertools import product
|
2014-11-07 18:12:21 +00:00
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-11-07 18:12:21 +00:00
|
|
|
def check_dict(indic, template):
|
2014-11-20 18:29:49 +00:00
|
|
|
return all(item in indic.items() for item in template.items())
|
2014-11-07 18:12:21 +00:00
|
|
|
|
2017-01-10 09:16:45 +00:00
|
|
|
|
2016-09-21 16:59:28 +00:00
|
|
|
def parse_resp(resp):
|
2017-02-17 00:53:02 +00:00
|
|
|
return models.from_json(resp.data.decode('utf-8'))
|
2016-09-21 16:59:28 +00:00
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
|
|
|
class BlueprintsTest(TestCase):
|
2017-08-19 21:53:17 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
"""Set up only once, and re-use in every individual test"""
|
|
|
|
cls.app = Flask("test_extensions")
|
|
|
|
cls.client = cls.app.test_client()
|
2018-01-03 08:39:30 +00:00
|
|
|
cls.senpy = Senpy(default_plugins=True)
|
2017-08-19 21:53:17 +00:00
|
|
|
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'
|
2014-11-07 18:12:21 +00:00
|
|
|
|
2018-07-04 14:14:09 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.app.config['TESTING'] = True # Tell Flask not to catch Exceptions
|
|
|
|
|
2016-09-21 16:59:28 +00:00
|
|
|
def assertCode(self, resp, code):
|
|
|
|
self.assertEqual(resp.status_code, code)
|
2017-01-10 09:16:45 +00:00
|
|
|
|
2018-01-03 08:39:30 +00:00
|
|
|
def test_playground(self):
|
|
|
|
resp = self.client.get("/")
|
2018-06-18 14:18:58 +00:00
|
|
|
assert "main.js" in resp.get_data(as_text=True)
|
2018-01-03 08:39:30 +00:00
|
|
|
|
2014-11-07 18:12:21 +00:00
|
|
|
def test_home(self):
|
2015-02-24 06:15:25 +00:00
|
|
|
"""
|
|
|
|
Calling with no arguments should ask the user for more arguments
|
|
|
|
"""
|
2018-07-04 14:14:09 +00:00
|
|
|
self.app.config['TESTING'] = False # Errors are expected in this case
|
2016-02-19 18:24:09 +00:00
|
|
|
resp = self.client.get("/api/")
|
2017-02-27 10:37:43 +00:00
|
|
|
self.assertCode(resp, 400)
|
2016-09-21 16:59:28 +00:00
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug(js)
|
2017-02-27 10:37:43 +00:00
|
|
|
assert js["status"] == 400
|
2014-11-07 18:12:21 +00:00
|
|
|
atleast = {
|
2017-02-27 10:37:43 +00:00
|
|
|
"status": 400,
|
2014-11-07 18:12:21 +00:00
|
|
|
"message": "Missing or invalid parameters",
|
|
|
|
}
|
2016-09-21 16:59:28 +00:00
|
|
|
assert check_dict(js, atleast)
|
2014-11-07 18:12:21 +00:00
|
|
|
|
2014-12-01 17:27:20 +00:00
|
|
|
def test_analysis(self):
|
2015-02-24 06:15:25 +00:00
|
|
|
"""
|
|
|
|
The dummy plugin returns an empty response,\
|
|
|
|
it should contain the context
|
|
|
|
"""
|
2017-06-21 17:58:18 +00:00
|
|
|
resp = self.client.get("/api/?i=My aloha mohame&with_parameters=True")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug("Got response: %s", js)
|
|
|
|
assert "@context" in js
|
2017-01-10 09:16:45 +00:00
|
|
|
assert "entries" in js
|
2018-10-30 14:15:37 +00:00
|
|
|
assert len(js['analysis']) == 1
|
|
|
|
|
|
|
|
def test_analysis_post(self):
|
|
|
|
"""
|
|
|
|
The results for a POST request should be the same as for a GET request.
|
|
|
|
"""
|
|
|
|
resp = self.client.post("/api/", data={'i': 'My aloha mohame',
|
|
|
|
'with_parameters': True})
|
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug("Got response: %s", js)
|
|
|
|
assert "@context" in js
|
|
|
|
assert "entries" in js
|
|
|
|
assert len(js['analysis']) == 1
|
2014-12-01 17:27:20 +00:00
|
|
|
|
2017-02-27 10:37:43 +00:00
|
|
|
def test_analysis_extra(self):
|
|
|
|
"""
|
2018-08-17 09:01:56 +00:00
|
|
|
Extra params that have a default should use it
|
2017-02-27 10:37:43 +00:00
|
|
|
"""
|
2017-08-27 16:43:40 +00:00
|
|
|
resp = self.client.get("/api/?i=My aloha mohame&algo=Dummy&with_parameters=true")
|
2017-02-27 10:37:43 +00:00
|
|
|
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.
|
|
|
|
"""
|
2018-07-04 14:14:09 +00:00
|
|
|
self.app.config['TESTING'] = False # Errors are expected in this case
|
2017-02-27 10:37:43 +00:00
|
|
|
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)
|
2018-01-03 08:39:30 +00:00
|
|
|
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)
|
2017-02-27 10:37:43 +00:00
|
|
|
|
2018-08-17 09:01:56 +00:00
|
|
|
def test_analysis_url(self):
|
|
|
|
"""
|
|
|
|
The algorithm can also be specified as part of the URL
|
|
|
|
"""
|
|
|
|
self.app.config['TESTING'] = False # Errors are expected in this case
|
|
|
|
resp = self.client.get("/api/DummyRequired?i=My aloha mohame")
|
|
|
|
self.assertCode(resp, 400)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug("Got response: %s", js)
|
|
|
|
assert isinstance(js, models.Error)
|
|
|
|
resp = self.client.get("/api/DummyRequired?i=My aloha mohame&example=notvalid")
|
|
|
|
self.assertCode(resp, 400)
|
|
|
|
resp = self.client.get("/api/DummyRequired?i=My aloha mohame&example=a")
|
|
|
|
self.assertCode(resp, 200)
|
|
|
|
|
|
|
|
def test_analysis_chain(self):
|
|
|
|
"""
|
|
|
|
More than one algorithm can be specified. Plugins will then be chained
|
|
|
|
"""
|
|
|
|
resp = self.client.get("/api/Dummy?i=My aloha mohame")
|
|
|
|
js = parse_resp(resp)
|
|
|
|
assert len(js['analysis']) == 1
|
|
|
|
assert js['entries'][0]['nif:isString'] == 'My aloha mohame'[::-1]
|
|
|
|
|
|
|
|
resp = self.client.get("/api/Dummy/Dummy?i=My aloha mohame")
|
|
|
|
# Calling dummy twice, should return the same string
|
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
assert len(js['analysis']) == 2
|
|
|
|
assert js['entries'][0]['nif:isString'] == 'My aloha mohame'
|
|
|
|
|
|
|
|
resp = self.client.get("/api/Dummy+Dummy?i=My aloha mohame")
|
|
|
|
# Same with pluses instead of slashes
|
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
assert len(js['analysis']) == 2
|
|
|
|
assert js['entries'][0]['nif:isString'] == 'My aloha mohame'
|
|
|
|
|
2017-02-17 00:53:02 +00:00
|
|
|
def test_error(self):
|
|
|
|
"""
|
|
|
|
The dummy plugin returns an empty response,\
|
|
|
|
it should contain the context
|
|
|
|
"""
|
2018-07-04 14:14:09 +00:00
|
|
|
self.app.config['TESTING'] = False # Errors are expected in this case
|
2017-02-17 00:53:02 +00:00
|
|
|
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)
|
|
|
|
|
2014-12-01 17:27:20 +00:00
|
|
|
def test_list(self):
|
|
|
|
""" List the plugins """
|
2016-02-02 10:34:30 +00:00
|
|
|
resp = self.client.get("/api/plugins/")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug(js)
|
|
|
|
assert 'plugins' in js
|
|
|
|
plugins = js['plugins']
|
2016-02-20 17:15:04 +00:00
|
|
|
assert len(plugins) > 1
|
|
|
|
assert list(p for p in plugins if p['name'] == "Dummy")
|
2016-09-21 16:59:28 +00:00
|
|
|
assert "@context" in js
|
2015-02-24 06:15:25 +00:00
|
|
|
|
|
|
|
def test_headers(self):
|
2016-02-19 18:24:09 +00:00
|
|
|
for i, j in product(["/api/plugins/?nothing=", "/api/?i=test&"],
|
2016-03-02 04:07:48 +00:00
|
|
|
["inHeaders"]):
|
2016-02-02 10:27:56 +00:00
|
|
|
resp = self.client.get("%s" % (i))
|
2016-09-21 16:59:28 +00:00
|
|
|
js = parse_resp(resp)
|
|
|
|
assert "@context" in js
|
2016-02-02 10:27:56 +00:00
|
|
|
resp = self.client.get("%s&%s=0" % (i, j))
|
2016-09-21 16:59:28 +00:00
|
|
|
js = parse_resp(resp)
|
2017-01-10 09:16:45 +00:00
|
|
|
assert "@context" in js
|
2016-02-02 10:27:56 +00:00
|
|
|
resp = self.client.get("%s&%s=1" % (i, j))
|
2016-09-21 16:59:28 +00:00
|
|
|
js = parse_resp(resp)
|
2017-01-10 09:16:45 +00:00
|
|
|
assert "@context" not in js
|
2016-02-02 10:27:56 +00:00
|
|
|
resp = self.client.get("%s&%s=true" % (i, j))
|
2016-09-21 16:59:28 +00:00
|
|
|
js = parse_resp(resp)
|
2017-01-10 09:16:45 +00:00
|
|
|
assert "@context" not in js
|
2014-12-01 17:27:20 +00:00
|
|
|
|
|
|
|
def test_detail(self):
|
|
|
|
""" Show only one plugin"""
|
2016-02-19 18:24:09 +00:00
|
|
|
resp = self.client.get("/api/plugins/Dummy/")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug(js)
|
|
|
|
assert "@id" in js
|
2018-06-28 16:24:18 +00:00
|
|
|
assert js["@id"] == "endpoint:plugins/Dummy_0.1"
|
2014-12-01 17:27:20 +00:00
|
|
|
|
2015-02-24 06:15:25 +00:00
|
|
|
def test_default(self):
|
|
|
|
""" Show only one plugin"""
|
2016-02-20 17:15:04 +00:00
|
|
|
resp = self.client.get("/api/plugins/default/")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug(js)
|
2017-01-10 09:16:45 +00:00
|
|
|
assert "@id" in js
|
2018-06-28 16:24:18 +00:00
|
|
|
assert js["@id"] == "endpoint:plugins/Dummy_0.1"
|
2016-02-21 18:36:24 +00:00
|
|
|
|
|
|
|
def test_context(self):
|
|
|
|
resp = self.client.get("/api/contexts/context.jsonld")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
assert "@context" in js
|
2016-02-21 18:36:24 +00:00
|
|
|
assert check_dict(
|
2016-09-21 16:59:28 +00:00
|
|
|
js["@context"],
|
2016-02-21 18:36:24 +00:00
|
|
|
{"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})
|
|
|
|
|
|
|
|
def test_schema(self):
|
|
|
|
resp = self.client.get("/api/schemas/definitions.json")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
2018-01-03 08:39:30 +00:00
|
|
|
assert "$schema" in resp.data.decode()
|
2017-06-21 17:58:18 +00:00
|
|
|
|
|
|
|
def test_help(self):
|
|
|
|
resp = self.client.get("/api/?help=true")
|
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
2018-01-01 12:13:17 +00:00
|
|
|
assert "valid_parameters" in js
|
|
|
|
assert "help" in js["valid_parameters"]
|
2018-01-03 08:39:30 +00:00
|
|
|
|
|
|
|
def test_conversion(self):
|
2018-07-04 14:14:09 +00:00
|
|
|
self.app.config['TESTING'] = False # Errors are expected in this case
|
2018-01-03 08:39:30 +00:00
|
|
|
resp = self.client.get("/api/?input=hello&algo=emoRand&emotionModel=DOES NOT EXIST")
|
|
|
|
self.assertCode(resp, 404)
|