1
0
mirror of https://github.com/gsi-upm/senpy synced 2025-08-23 18:12:20 +00:00

Macro commit

* Fixed Options for extra_params in UI
* Enhanced meta-programming for models
* Plugins can be imported from a python file if they're named
`senpy_<whatever>.py>` (no need for `.senpy` anymore!)
* Add docstings and tests to most plugins
* Read plugin description from the docstring
* Refactor code to get rid of unnecessary `.senpy`s
* Load models, plugins and utils into the main namespace (see __init__.py)
* Enhanced plugin development/experience with utils (easy_test, easy_serve)
* Fix bug in check_template that wouldn't check objects
* Make model defaults a private variable
* Add option to list loaded plugins in CLI
* Update docs
This commit is contained in:
J. Fernando Sánchez
2018-01-03 09:39:30 +01:00
parent abd401f863
commit 21a5a3f201
48 changed files with 1423 additions and 794 deletions

View File

@@ -19,11 +19,8 @@ class APITest(TestCase):
def test_basic(self):
a = {}
try:
parse_params(a, NIF_PARAMS)
raise AssertionError()
except Error:
pass
self.assertRaises(Error, parse_params, a)
self.assertRaises(Error, parse_params, a, NIF_PARAMS)
a = {'input': 'hello'}
p = parse_params(a, NIF_PARAMS)
assert 'input' in p
@@ -39,11 +36,7 @@ class APITest(TestCase):
'required': True
}
}
try:
parse_params(query, plug_params)
raise AssertionError()
except Error:
pass
self.assertRaises(Error, parse_params, plug_params)
query['hello'] = 'world'
p = parse_params(query, plug_params)
assert 'hello' in p
@@ -53,7 +46,6 @@ class APITest(TestCase):
query['hiya'] = 'dlrow'
p = parse_params(query, plug_params)
assert 'hello' in p
assert 'hiya' in p
assert p['hello'] == 'dlrow'
def test_default(self):

View File

@@ -23,7 +23,7 @@ class BlueprintsTest(TestCase):
cls.app = Flask("test_extensions")
cls.app.debug = False
cls.client = cls.app.test_client()
cls.senpy = Senpy()
cls.senpy = Senpy(default_plugins=True)
cls.senpy.init_app(cls.app)
cls.dir = os.path.join(os.path.dirname(__file__), "..")
cls.senpy.add_folder(cls.dir)
@@ -34,11 +34,14 @@ class BlueprintsTest(TestCase):
def assertCode(self, resp, code):
self.assertEqual(resp.status_code, code)
def test_playground(self):
resp = self.client.get("/")
assert "main.js" in resp.data.decode()
def test_home(self):
"""
Calling with no arguments should ask the user for more arguments
"""
self.app.debug = False
resp = self.client.get("/api/")
self.assertCode(resp, 400)
js = parse_resp(resp)
@@ -84,6 +87,10 @@ class BlueprintsTest(TestCase):
js = parse_resp(resp)
logging.debug("Got response: %s", js)
assert isinstance(js, models.Error)
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)
def test_error(self):
"""
@@ -155,8 +162,7 @@ class BlueprintsTest(TestCase):
def test_schema(self):
resp = self.client.get("/api/schemas/definitions.json")
self.assertCode(resp, 200)
js = parse_resp(resp)
assert "$schema" in js
assert "$schema" in resp.data.decode()
def test_help(self):
resp = self.client.get("/api/?help=true")
@@ -164,3 +170,7 @@ class BlueprintsTest(TestCase):
js = parse_resp(resp)
assert "valid_parameters" in js
assert "help" in js["valid_parameters"]
def test_conversion(self):
resp = self.client.get("/api/?input=hello&algo=emoRand&emotionModel=DOES NOT EXIST")
self.assertCode(resp, 404)

View File

@@ -12,7 +12,8 @@ class CLITest(TestCase):
def test_basic(self):
self.assertRaises(Error, partial(main_function, []))
res = main_function(['--input', 'test', '--algo', 'rand', '--with-parameters'])
res = main_function(['--input', 'test', '--algo', 'rand',
'--with-parameters', '--default-plugins'])
assert res.parameters['input'] == 'test'
assert 'rand' in res.parameters['algorithm']
assert res.parameters['input'] == 'test'

View File

@@ -29,6 +29,7 @@ class ExtensionsTest(TestCase):
self.senpy = Senpy(plugin_folder=self.examples_dir,
app=self.app,
default_plugins=False)
self.senpy.deactivate_all()
self.senpy.activate_plugin("Dummy", sync=True)
def test_init(self):
@@ -41,21 +42,37 @@ class ExtensionsTest(TestCase):
def test_discovery(self):
""" Discovery of plugins in given folders. """
# noinspection PyProtectedMember
assert self.examples_dir in self.senpy._search_folders
print(self.senpy.plugins)
assert "Dummy" in self.senpy.plugins
print(self.senpy.plugins())
assert self.senpy.get_plugin("dummy")
def test_add_delete(self):
'''Should be able to add and delete new plugins. '''
new = plugins.Plugin(name='new', description='new', version=0)
self.senpy.add_plugin(new)
assert new in self.senpy.plugins()
self.senpy.delete_plugin(new)
assert new not in self.senpy.plugins()
def test_adding_folder(self):
""" It should be possible for senpy to look for plugins in more folders. """
senpy = Senpy(plugin_folder=None,
app=self.app,
default_plugins=False)
assert not senpy.analysis_plugins
senpy.add_folder(self.examples_dir)
assert senpy.analysis_plugins
self.assertRaises(AttributeError, senpy.add_folder, 'DOES NOT EXIST')
def test_installing(self):
""" Installing a plugin """
info = {
'name': 'TestPip',
'module': 'noop_plugin',
'module': 'mynoop',
'description': None,
'requirements': ['noop'],
'version': 0
}
root = os.path.join(self.examples_dir, 'noop')
module = plugins.load_plugin_from_info(info, root=root, install=True)
module = plugins.from_info(info, root=self.examples_dir, install=True)
assert module.name == 'TestPip'
assert module
import noop
@@ -64,8 +81,8 @@ class ExtensionsTest(TestCase):
def test_enabling(self):
""" Enabling a plugin """
self.senpy.activate_all(sync=True)
assert len(self.senpy.plugins) >= 3
assert self.senpy.plugins["Sleep"].is_activated
assert len(self.senpy.plugins()) >= 3
assert self.senpy.get_plugin("Sleep").is_activated
def test_installing_nonexistent(self):
""" Fail if the dependencies cannot be met """
@@ -82,8 +99,8 @@ class ExtensionsTest(TestCase):
def test_disabling(self):
""" Disabling a plugin """
self.senpy.deactivate_all(sync=True)
assert not self.senpy.plugins["Dummy"].is_activated
assert not self.senpy.plugins["Sleep"].is_activated
assert not self.senpy.get_plugin("dummy").is_activated
assert not self.senpy.get_plugin("sleep").is_activated
def test_default(self):
""" Default plugin should be set """
@@ -108,6 +125,17 @@ class ExtensionsTest(TestCase):
assert r2.analysis[0] == "plugins/Dummy_0.1"
assert r1.entries[0]['nif:isString'] == 'input'
def test_analyse_empty(self):
""" Trying to analyse when no plugins are installed should raise an error."""
senpy = Senpy(plugin_folder=None,
app=self.app,
default_plugins=False)
self.assertRaises(Error, senpy.analyse, Results())
def test_analyse_wrong(self):
""" Trying to analyse with a non-existent plugin should raise an error."""
self.assertRaises(Error, analyse, self.senpy, algorithm='DOES NOT EXIST', input='test')
def test_analyse_jsonld(self):
""" Using a plugin with JSON-LD input"""
js_input = '''{
@@ -135,9 +163,10 @@ class ExtensionsTest(TestCase):
def test_analyse_error(self):
mm = mock.MagicMock()
mm.id = 'magic_mock'
mm.name = 'mock'
mm.is_activated = True
mm.analyse_entries.side_effect = Error('error in analysis', status=500)
self.senpy.plugins['MOCK'] = mm
self.senpy.add_plugin(mm)
try:
analyse(self.senpy, input='nothing', algorithm='MOCK')
assert False
@@ -145,29 +174,29 @@ class ExtensionsTest(TestCase):
assert 'error in analysis' in ex['message']
assert ex['status'] == 500
mm.analyse.side_effect = Exception('generic exception on analysis')
mm.analyse_entries.side_effect = Exception(
'generic exception on analysis')
ex = Exception('generic exception on analysis')
mm.analyse.side_effect = ex
mm.analyse_entries.side_effect = ex
try:
analyse(self.senpy, input='nothing', algorithm='MOCK')
assert False
except Error as ex:
except Exception as ex:
assert 'generic exception on analysis' in ex['message']
assert ex['status'] == 500
def test_filtering(self):
""" Filtering plugins """
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", is_activated=True)
assert len(self.senpy.plugins(name="Dummy")) > 0
assert not len(self.senpy.plugins(name="NotDummy"))
assert self.senpy.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.plugins(name="Dummy",
is_activated=True))
def test_load_default_plugins(self):
senpy = Senpy(plugin_folder=self.examples_dir, default_plugins=True)
assert len(senpy.plugins) > 1
assert len(senpy.plugins()) > 1
def test_convert_emotions(self):
self.senpy.activate_all(sync=True)

View File

@@ -12,6 +12,7 @@ from senpy.plugins.conversion.emotion.centroids import CentroidConversion
class ShelfDummyPlugin(plugins.SentimentPlugin, plugins.ShelfMixin):
'''Dummy plugin for tests.'''
def activate(self, *args, **kwargs):
if 'counter' not in self.sh:
self.sh['counter'] = 0
@@ -65,7 +66,7 @@ class PluginsTest(TestCase):
('EmotionPlugin', 1)]
for name, num in cases:
res = plugins.pfilter(ps.plugins, plugin_type=name)
res = list(plugins.pfilter(ps.plugins, plugin_type=name))
assert len(res) == num
def test_shelf(self):
@@ -240,21 +241,20 @@ class PluginsTest(TestCase):
assert res["onyx:hasEmotionCategory"] == "c2"
def make_mini_test(plugin_info):
def make_mini_test(fpath):
def mini_test(self):
plugin = plugins.load_plugin_from_info(plugin_info, install=True)
plugin.test()
for plugin in plugins.from_path(fpath, install=True):
plugin.test()
return mini_test
def _add_tests():
root = os.path.join(os.path.dirname(__file__), '..')
print(root)
plugs = plugins.load_plugins([root, ], loader=plugins.parse_plugin_info)
for k, v in plugs.items():
for fpath in plugins.find_plugins([root, ]):
pass
t_method = make_mini_test(v)
t_method.__name__ = 'test_plugin_{}'.format(k)
t_method = make_mini_test(fpath)
t_method.__name__ = 'test_plugin_{}'.format(fpath)
setattr(PluginsTest, t_method.__name__, t_method)
del t_method