mirror of
https://github.com/gsi-upm/senpy
synced 2025-08-23 10:02:21 +00:00
Improved plugins, better tests, gevent
Moved from Yapsy again (it is not flexible enough), now we use a custom solution. The activation and deactivation of plugins is asynchronous, so that plugins that take a long time don't interfere with the rest.
This commit is contained in:
@@ -1 +0,0 @@
|
||||
|
||||
|
@@ -9,6 +9,7 @@ except ImportError:
|
||||
from senpy.extensions import Senpy
|
||||
from flask import Flask
|
||||
from flask.ext.testing import TestCase
|
||||
from gevent import sleep
|
||||
|
||||
|
||||
def check_dict(indic, template):
|
||||
@@ -22,6 +23,7 @@ class BlueprintsTest(TestCase):
|
||||
self.senpy.init_app(self.app)
|
||||
self.dir = os.path.join(os.path.dirname(__file__), "..")
|
||||
self.senpy.add_folder(self.dir)
|
||||
self.senpy.activate_plugin("Dummy", sync=True)
|
||||
return self.app
|
||||
|
||||
def test_home(self):
|
||||
@@ -36,3 +38,43 @@ class BlueprintsTest(TestCase):
|
||||
}
|
||||
assert check_dict(resp.json, atleast)
|
||||
|
||||
def test_analysis(self):
|
||||
""" 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)
|
||||
assert "@context" in resp.json
|
||||
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):
|
||||
""" List the plugins """
|
||||
resp = self.client.get("/plugins/")
|
||||
self.assert200(resp)
|
||||
logging.debug(resp.json)
|
||||
assert "Dummy" in resp.json
|
||||
|
||||
def test_detail(self):
|
||||
""" Show only one plugin"""
|
||||
resp = self.client.get("/plugins/Dummy")
|
||||
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 """
|
||||
resp = self.client.get("/plugins/Dummy/deactivate")
|
||||
self.assert200(resp)
|
||||
sleep(0.5)
|
||||
resp = self.client.get("/plugins/Dummy")
|
||||
self.assert200(resp)
|
||||
assert "is_activated" in resp.json
|
||||
assert resp.json["is_activated"] == False
|
||||
resp = self.client.get("/plugins/Dummy/activate")
|
||||
self.assert200(resp)
|
||||
sleep(0.5)
|
||||
resp = self.client.get("/plugins/Dummy")
|
||||
self.assert200(resp)
|
||||
assert "is_activated" in resp.json
|
||||
assert resp.json["is_activated"] == True
|
||||
|
@@ -1,3 +0,0 @@
|
||||
from senpy.plugins import SenpyPlugin
|
||||
|
||||
plugin = SenpyPlugin("dummy")
|
8
tests/dummy_plugin/dummy.py
Normal file
8
tests/dummy_plugin/dummy.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from senpy.plugins import SentimentPlugin
|
||||
from senpy.models import Response
|
||||
|
||||
class DummyPlugin(SentimentPlugin):
|
||||
pass
|
||||
|
||||
def analyse(self, *args, **kwargs):
|
||||
return Response()
|
7
tests/dummy_plugin/dummy.senpy
Normal file
7
tests/dummy_plugin/dummy.senpy
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "Dummy",
|
||||
"module": "dummy",
|
||||
"description": "I am dummy",
|
||||
"author": "@balkian",
|
||||
"version": "0.1"
|
||||
}
|
@@ -13,10 +13,10 @@ from flask.ext.testing import TestCase
|
||||
class ExtensionsTest(TestCase):
|
||||
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)
|
||||
self.senpy = Senpy(plugin_folder=self.dir)
|
||||
self.senpy.init_app(self.app)
|
||||
self.senpy.activate_plugin("Dummy", sync=True)
|
||||
return self.app
|
||||
|
||||
def test_init(self):
|
||||
@@ -31,41 +31,42 @@ class ExtensionsTest(TestCase):
|
||||
# noinspection PyProtectedMember
|
||||
assert self.dir in self.senpy._search_folders
|
||||
print self.senpy.plugins
|
||||
assert "dummy" in self.senpy.plugins
|
||||
assert "Dummy" in self.senpy.plugins
|
||||
|
||||
def test_enabling(self):
|
||||
""" Enabling a plugin """
|
||||
self.senpy.enable_plugin("dummy")
|
||||
assert self.senpy.plugins["dummy"].enabled
|
||||
self.senpy.activate_all(sync=True)
|
||||
assert len(self.senpy.plugins) == 2
|
||||
assert self.senpy.plugins["Sleep"].is_activated
|
||||
|
||||
def test_disabling(self):
|
||||
""" Disabling a plugin """
|
||||
self.senpy.enable_plugin("dummy")
|
||||
self.senpy.disable_plugin("dummy")
|
||||
assert not self.senpy.plugins["dummy"].enabled
|
||||
self.senpy.deactivate_all(sync=True)
|
||||
assert self.senpy.plugins["Dummy"].is_activated == False
|
||||
assert self.senpy.plugins["Sleep"].is_activated == False
|
||||
|
||||
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 == "Dummy"
|
||||
|
||||
def test_analyse(self):
|
||||
""" Using a plugin """
|
||||
with mock.patch.object(self.senpy.plugins["dummy"], "analyse") as mocked:
|
||||
self.senpy.analyse(algorithm="dummy", input="tupni", output="tuptuo")
|
||||
with mock.patch.object(self.senpy.plugins["Dummy"], "analyse") as mocked:
|
||||
self.senpy.analyse(algorithm="Dummy", input="tupni", output="tuptuo")
|
||||
self.senpy.analyse(input="tupni", output="tuptuo")
|
||||
mocked.assert_any_call(input="tupni", output="tuptuo", algorithm="dummy")
|
||||
mocked.assert_any_call(input="tupni", output="tuptuo", algorithm="Dummy")
|
||||
mocked.assert_any_call(input="tupni", output="tuptuo")
|
||||
for plug in self.senpy.plugins:
|
||||
self.senpy.disable_plugin(plug)
|
||||
self.senpy.deactivate_plugin(plug, sync=True)
|
||||
resp = self.senpy.analyse(input="tupni")
|
||||
logging.debug("Response: {}".format(resp))
|
||||
assert resp["status"] == 400
|
||||
|
||||
def test_filtering(self):
|
||||
""" Filtering plugins """
|
||||
assert len(self.senpy.filter_plugins(name="dummy")) > 0
|
||||
assert len(self.senpy.filter_plugins(name="Dummy")) > 0
|
||||
assert not len(self.senpy.filter_plugins(name="notdummy"))
|
||||
assert self.senpy.filter_plugins(name="dummy", enabled=True)
|
||||
self.senpy.disable_plugin("dummy")
|
||||
assert not len(self.senpy.filter_plugins(name="dummy", enabled=True))
|
||||
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))
|
||||
|
10
tests/sleep_plugin/sleep.py
Normal file
10
tests/sleep_plugin/sleep.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from senpy.plugins import SenpyPlugin
|
||||
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"])
|
||||
|
||||
def activate(self, *args, **kwargs):
|
||||
sleep(self.timeout)
|
8
tests/sleep_plugin/sleep.senpy
Normal file
8
tests/sleep_plugin/sleep.senpy
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "Sleep",
|
||||
"module": "sleep",
|
||||
"description": "I am dummy",
|
||||
"author": "@balkian",
|
||||
"version": "0.1",
|
||||
"timeout": "2"
|
||||
}
|
Reference in New Issue
Block a user