mirror of
https://github.com/gsi-upm/senpy
synced 2025-08-23 10:02:21 +00:00
PEP8+Better JSON-LD support
* The API has also changed, there are new parameters to send the context as part of the headers. * Improved tests * PEP8 compliance (despite the line about gevent)
This commit is contained in:
@@ -9,6 +9,7 @@ from senpy.extensions import Senpy
|
||||
from flask import Flask
|
||||
from flask.ext.testing import TestCase
|
||||
from gevent import sleep
|
||||
from itertools import product
|
||||
|
||||
|
||||
def check_dict(indic, template):
|
||||
@@ -16,6 +17,7 @@ def check_dict(indic, template):
|
||||
|
||||
|
||||
class BlueprintsTest(TestCase):
|
||||
|
||||
def create_app(self):
|
||||
self.app = Flask("test_extensions")
|
||||
self.senpy = Senpy()
|
||||
@@ -26,24 +28,31 @@ class BlueprintsTest(TestCase):
|
||||
return self.app
|
||||
|
||||
def test_home(self):
|
||||
""" Calling with no arguments should ask the user for more arguments """
|
||||
"""
|
||||
Calling with no arguments should ask the user for more arguments
|
||||
"""
|
||||
resp = self.client.get("/")
|
||||
self.assert200(resp)
|
||||
self.assert404(resp)
|
||||
logging.debug(resp.json)
|
||||
assert resp.json["status"] == "failed"
|
||||
assert resp.json["status"] == 404
|
||||
atleast = {
|
||||
"status": "failed",
|
||||
"status": 404,
|
||||
"message": "Missing or invalid parameters",
|
||||
}
|
||||
assert check_dict(resp.json, atleast)
|
||||
|
||||
def test_analysis(self):
|
||||
""" The dummy plugin returns an empty response, it should contain the context """
|
||||
"""
|
||||
The dummy plugin returns an empty response,\
|
||||
it should contain the context
|
||||
"""
|
||||
resp = self.client.get("/?i=My aloha mohame")
|
||||
self.assert200(resp)
|
||||
logging.debug(resp.json)
|
||||
logging.debug("Got response: %s", resp.json)
|
||||
assert "@context" in resp.json
|
||||
assert check_dict(resp.json["@context"], {"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})
|
||||
assert check_dict(
|
||||
resp.json["@context"],
|
||||
{"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})
|
||||
assert "entries" in resp.json
|
||||
|
||||
def test_list(self):
|
||||
@@ -52,6 +61,19 @@ class BlueprintsTest(TestCase):
|
||||
self.assert200(resp)
|
||||
logging.debug(resp.json)
|
||||
assert "Dummy" in resp.json
|
||||
assert "@context" in resp.json
|
||||
|
||||
def test_headers(self):
|
||||
for i, j in product(["/plugins/?nothing=", "/?i=test&"],
|
||||
["headers", "inHeaders"]):
|
||||
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
|
||||
|
||||
def test_detail(self):
|
||||
""" Show only one plugin"""
|
||||
@@ -77,3 +99,16 @@ class BlueprintsTest(TestCase):
|
||||
self.assert200(resp)
|
||||
assert "is_activated" in resp.json
|
||||
assert resp.json["is_activated"] == True
|
||||
|
||||
def test_default(self):
|
||||
""" Show only one plugin"""
|
||||
resp = self.client.get("/default")
|
||||
self.assert200(resp)
|
||||
logging.debug(resp.json)
|
||||
assert "@id" in resp.json
|
||||
assert resp.json["@id"] == "Dummy_0.1"
|
||||
resp = self.client.get("/plugins/Dummy/deactivate")
|
||||
self.assert200(resp)
|
||||
sleep(0.5)
|
||||
resp = self.client.get("/default")
|
||||
self.assert404(resp)
|
||||
|
@@ -1,6 +1,8 @@
|
||||
from senpy.plugins import SentimentPlugin
|
||||
from senpy.models import Response
|
||||
|
||||
|
||||
class DummyPlugin(SentimentPlugin):
|
||||
|
||||
def analyse(self, *args, **kwargs):
|
||||
return Response()
|
||||
return Response()
|
||||
|
@@ -11,6 +11,7 @@ from flask.ext.testing import TestCase
|
||||
|
||||
|
||||
class ExtensionsTest(TestCase):
|
||||
|
||||
def create_app(self):
|
||||
self.app = Flask("test_extensions")
|
||||
self.dir = os.path.join(os.path.dirname(__file__), "..")
|
||||
@@ -42,19 +43,30 @@ class ExtensionsTest(TestCase):
|
||||
def test_disabling(self):
|
||||
""" Disabling a plugin """
|
||||
self.senpy.deactivate_all(sync=True)
|
||||
assert self.senpy.plugins["Dummy"].is_activated == False
|
||||
assert self.senpy.plugins["Sleep"].is_activated == False
|
||||
assert not self.senpy.plugins["Dummy"].is_activated
|
||||
assert not self.senpy.plugins["Sleep"].is_activated
|
||||
|
||||
def test_default(self):
|
||||
""" Default plugin should be set """
|
||||
assert self.senpy.default_plugin
|
||||
assert self.senpy.default_plugin == "Dummy"
|
||||
assert self.senpy.default_plugin.name == "Dummy"
|
||||
self.senpy.deactivate_all(sync=True)
|
||||
logging.debug("Default: {}".format(self.senpy.default_plugin))
|
||||
assert self.senpy.default_plugin is None
|
||||
|
||||
def test_noplugin(self):
|
||||
""" Don't analyse if there isn't any plugin installed """
|
||||
self.senpy.deactivate_all(sync=True)
|
||||
resp = self.senpy.analyse(input="tupni")
|
||||
logging.debug("Response: {}".format(resp))
|
||||
assert resp["status"] == 404
|
||||
|
||||
def test_analyse(self):
|
||||
""" Using a plugin """
|
||||
# I was using mock until plugin started inheriting Leaf (defaultdict with
|
||||
# __setattr__ and __getattr__.
|
||||
r1 = self.senpy.analyse(algorithm="Dummy", input="tupni", output="tuptuo")
|
||||
# I was using mock until plugin started inheriting
|
||||
# Leaf (defaultdict with __setattr__ and __getattr__.
|
||||
r1 = self.senpy.analyse(
|
||||
algorithm="Dummy", input="tupni", output="tuptuo")
|
||||
r2 = self.senpy.analyse(input="tupni", output="tuptuo")
|
||||
assert r1.analysis[0].id[:5] == "Dummy"
|
||||
assert r2.analysis[0].id[:5] == "Dummy"
|
||||
@@ -62,7 +74,7 @@ class ExtensionsTest(TestCase):
|
||||
self.senpy.deactivate_plugin(plug, sync=True)
|
||||
resp = self.senpy.analyse(input="tupni")
|
||||
logging.debug("Response: {}".format(resp))
|
||||
assert resp["status"] == 400
|
||||
assert resp["status"] == 404
|
||||
|
||||
def test_filtering(self):
|
||||
""" Filtering plugins """
|
||||
@@ -70,4 +82,5 @@ class ExtensionsTest(TestCase):
|
||||
assert not len(self.senpy.filter_plugins(name="notdummy"))
|
||||
assert self.senpy.filter_plugins(name="Dummy", is_activated=True)
|
||||
self.senpy.deactivate_plugin("Dummy", sync=True)
|
||||
assert not len(self.senpy.filter_plugins(name="Dummy", is_activated=True))
|
||||
assert not len(
|
||||
self.senpy.filter_plugins(name="Dummy", is_activated=True))
|
||||
|
@@ -8,29 +8,74 @@ except ImportError:
|
||||
import json
|
||||
import os
|
||||
from unittest import TestCase
|
||||
from senpy.models import Response
|
||||
from senpy.models import Response, Entry
|
||||
from senpy.plugins import SenpyPlugin
|
||||
|
||||
|
||||
class ModelsTest(TestCase):
|
||||
|
||||
def test_response(self):
|
||||
r = Response(context=os.path.normpath(os.path.join(__file__, "..", "..", "context.jsonld")))
|
||||
r = Response(context=os.path.normpath(
|
||||
os.path.join(__file__, "..", "..", "context.jsonld")))
|
||||
assert("@context" in r)
|
||||
assert(r._frame)
|
||||
logging.debug("Default frame: %s", r._frame)
|
||||
assert("marl" in r.context)
|
||||
assert("entries" in r.context)
|
||||
|
||||
r2 = Response(context=json.loads('{"test": "roger"}'))
|
||||
assert("test" in r2.context)
|
||||
|
||||
r3 = Response(context=None)
|
||||
del r3.context
|
||||
assert("@context" not in r3)
|
||||
assert("entries" in r3)
|
||||
assert("analysis" in r3)
|
||||
|
||||
r4 = Response()
|
||||
assert("@context" in r4)
|
||||
assert("entries" in r4)
|
||||
assert("analysis" in r4)
|
||||
|
||||
dummy = SenpyPlugin({"name": "dummy", "version": 0})
|
||||
r5 = Response({"dummy": dummy}, context=None, frame=None)
|
||||
logging.debug("Response 5: %s", r5)
|
||||
assert("dummy" in r5)
|
||||
assert(r5["dummy"].name == "dummy")
|
||||
js = r5.jsonld(context={}, frame={})
|
||||
logging.debug("jsonld 5: %s", js)
|
||||
assert("dummy" in js)
|
||||
assert(js["dummy"].name == "dummy")
|
||||
|
||||
r6 = Response()
|
||||
r6.entries.append(Entry(text="Just testing"))
|
||||
logging.debug("Reponse 6: %s", r6)
|
||||
assert("@context" in r6)
|
||||
assert("marl" in r6.context)
|
||||
assert("entries" in r6.context)
|
||||
js = r6.jsonld()
|
||||
logging.debug("jsonld: %s", js)
|
||||
assert("entries" in js)
|
||||
assert("entries" in js)
|
||||
assert("analysis" in js)
|
||||
resp = r6.flask()
|
||||
received = json.loads(resp.data)
|
||||
logging.debug("Response: %s", js)
|
||||
assert(received["entries"])
|
||||
assert(received["entries"][0]["text"] == "Just testing")
|
||||
assert(received["entries"][0]["text"] != "Not testing")
|
||||
|
||||
def test_opinions(self):
|
||||
pass
|
||||
|
||||
def test_frame_plugin(self):
|
||||
def test_plugins(self):
|
||||
p = SenpyPlugin({"name": "dummy", "version": 0})
|
||||
c = p.frame()
|
||||
c = p.jsonld()
|
||||
assert "info" not in c
|
||||
assert "repo" not in c
|
||||
assert "params" not in c
|
||||
logging.debug("Framed: %s", c)
|
||||
assert "extra_params" in c
|
||||
|
||||
def test_frame_response(self):
|
||||
pass
|
||||
|
@@ -2,7 +2,9 @@ from senpy.plugins import SenpyPlugin
|
||||
from senpy.models import Response
|
||||
from time import sleep
|
||||
|
||||
|
||||
class SleepPlugin(SenpyPlugin):
|
||||
|
||||
def __init__(self, info, *args, **kwargs):
|
||||
super(SleepPlugin, self).__init__(info, *args, **kwargs)
|
||||
self.timeout = int(info["timeout"])
|
||||
|
Reference in New Issue
Block a user